BondFairValueCalculator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
1
<?php
2
3
namespace FinanCalc\Calculators {
4
5
    use FinanCalc\Interfaces\Calculator\BondCalculatorAbstract;
6
    use FinanCalc\Utils\Lambdas;
7
    use FinanCalc\Utils\MathFuncs;
8
9
    /**
10
     * Class BondFairValueCalculator
11
     * @package FinanCalc\Calculators
12
     */
13
    class BondFairValueCalculator extends BondCalculatorAbstract
14
    {
15
16
        // valuation interest rate of the bond = 'i'
17
        protected $bondVIR;
18
19
        // INHERITED MEMBERS
20
        // face value of the bond = 'F'
21
        // $bondFaceValue;
22
23
        // coupon rate of the bond per annum = 'c'
24
        // $bondAnnualCouponRate;
25
26
        // number of years to the maturity of the bond
27
        // $bondYearsToMaturity;
28
29
        // frequency of bond payments (expressed in a divisor of 12 months ~ 1 year)
30
        // e.g.: divisor 2 means semi-annual payments
31
        // $bondPaymentFrequency;
32
33
        // props returned by the getResultAsArray method by default
34
        protected $propResultArray = [
35
            "bondFaceValue",
36
            "bondAnnualCouponRate",
37
            "bondVIR",
38
            "bondYearsToMaturity",
39
            "bondPaymentFrequency",
40
            "bondFairValue"
41
        ];
42
43
        /**
44
         * @param $bondFaceValue
45
         * @param $bondAnnualCouponRate
46
         * @param $bondVIR
47
         * @param $bondYearsToMaturity
48
         * @param $bondPaymentFrequency
49
         */
50
        public function __construct(
51
            $bondFaceValue,
52
            $bondAnnualCouponRate,
53
            $bondVIR,
54
            $bondYearsToMaturity,
55
            $bondPaymentFrequency = 1
56
        ) {
57
            $this->setBondFaceValue($bondFaceValue);
58
            $this->setBondAnnualCouponRate($bondAnnualCouponRate);
59
            $this->setBondVIR($bondVIR);
60
            $this->setBondYearsToMaturity($bondYearsToMaturity);
61
            $this->setBondPaymentFrequency($bondPaymentFrequency);
62
        }
63
64
        /**
65
         * @param $bondVIR
66
         */
67
        public function setBondVIR($bondVIR)
68
        {
69
            $this->setProperty("bondVIR", $bondVIR, Lambdas::checkIfPositive());
70
        }
71
72
        /**
73
         * @return mixed
74
         */
75
        public function getBondVIR()
76
        {
77
            return $this->bondVIR;
78
        }
79
80
        /**
81
         * @return string
82
         */
83
        public function getBondFairValue()
84
        {
85
            // we need to get the coupon rate per payment period = c/payment frequency
86
            $couponRateForPeriod = MathFuncs::div(
87
                $this->bondAnnualCouponRate,
88
                $this->bondPaymentFrequency
89
            );
90
91
            // similarly, we need to calculate the VIR per payment period = i/payment frequency
92
            $VIRForPeriod = MathFuncs::div(
93
                $this->bondVIR,
94
                $this->bondPaymentFrequency
95
            );
96
97
            // we also save the bond's number of payments to an auxiliary variable
98
            $bondNoOfPayments = $this->getBondNoOfPayments();
99
100
            // next, we also need the present value of the unit annuity pertaining to the bond, in arrears
101
            $PVofUnitBondAnnuity =
102
                MathFuncs::div(
103
                    MathFuncs::sub(
104
                        1,
105
                        Mathfuncs::pow(
106
                            MathFuncs::div(
107
                                1,
108
                                MathFuncs::add(
109
                                    1,
110
                                    $VIRForPeriod
111
                                )
112
                            ),
113
                            $bondNoOfPayments
114
                        )
115
                    ),
116
                    $VIRForPeriod
117
                );
118
119
            // now we can use the formula to calculate the real value of the bond (i.e., the present value of its payments)
120
            // PV = F*[c*PVofUnitBondAnnuity+1/((1+i)^n)]
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
122
            $fairValue =
123
                MathFuncs::mul(
124
                    $this->bondFaceValue,
125
                    MathFuncs::add(
126
                        MathFuncs::mul(
127
                            $couponRateForPeriod,
128
                            $PVofUnitBondAnnuity
129
                        ),
130
                        MathFuncs::div(
131
                            1,
132
                            MathFuncs::pow(
133
                                MathFuncs::add(
134
                                    1,
135
                                    $VIRForPeriod
136
                                ),
137
                                $bondNoOfPayments
138
                            )
139
                        )
140
                    )
141
                );
142
143
            return $fairValue;
144
        }
145
    }
146
}
147