-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBondDurationCalculator.php
174 lines (149 loc) · 5.36 KB
/
BondDurationCalculator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace FinanCalc\Calculators {
use FinanCalc\Interfaces\Calculator\BondCalculatorAbstract;
use FinanCalc\Utils\Lambdas;
use FinanCalc\Utils\MathFuncs;
/**
* Class BondDurationCalculator
* @package FinanCalc\Calculators
*/
class BondDurationCalculator extends BondCalculatorAbstract
{
// annual yield of the bond
protected $bondAnnualYield;
// INHERITED MEMBERS
// face value of the bond = 'F'
// $bondFaceValue;
// coupon rate of the bond per annum = 'c'
// $bondAnnualCouponRate;
// number of years to the maturity of the bond
// $bondYearsToMaturity;
// frequency of bond payments (expressed in a divisor of 12 months ~ 1 year)
// e.g.: divisor 2 means semi-annual payments
// $bondPaymentFrequency;
// props returned by the getResultAsArray method by default
protected $propResultArray = [
"bondFaceValue",
"bondAnnualCouponRate",
"bondAnnualYield",
"bondYearsToMaturity",
"bondPaymentFrequency",
"bondYieldPerPaymentPeriod",
"bondNominalCashFlows",
"bondDiscountedCashFlows",
"bondPresentValue",
"bondDuration"
];
/**
* @param $bondFaceValue
* @param $bondAnnualCouponRate
* @param $bondAnnualYield
* @param $bondYearsToMaturity
* @param $bondPaymentFrequency
*/
public function __construct(
$bondFaceValue,
$bondAnnualCouponRate,
$bondAnnualYield,
$bondYearsToMaturity,
$bondPaymentFrequency = 1
) {
$this->setBondFaceValue($bondFaceValue);
$this->setBondAnnualCouponRate($bondAnnualCouponRate);
$this->setBondAnnualYield($bondAnnualYield);
$this->setBondYearsToMaturity($bondYearsToMaturity);
$this->setBondPaymentFrequency($bondPaymentFrequency);
}
/**
* @param $bondAnnualYield
*/
public function setBondAnnualYield($bondAnnualYield)
{
$this->setProperty("bondAnnualYield", $bondAnnualYield, Lambdas::checkIfPositive());
}
/**
* @return mixed
*/
public function getBondAnnualYield()
{
return $this->bondAnnualYield;
}
/**
* @return string
*/
public function getBondYieldPerPaymentPeriod()
{
return MathFuncs::div($this->bondAnnualYield, $this->bondPaymentFrequency);
}
/**
* @return array
*/
public function getBondNominalCashFlows()
{
// nominal cash flows = coupons each period + face value in the last period
$numberOfPayments = $this->getBondNoOfPayments();
$couponPayment = $this->getCouponPayment();
$nominalCashFlows = array();
for ($i = 1; $i <= $numberOfPayments; $i++) {
if ($i == $numberOfPayments) {
$nominalCashFlows[$i] = MathFuncs::add($couponPayment, $this->bondFaceValue);
} else {
$nominalCashFlows[$i] = $couponPayment;
}
}
return $nominalCashFlows;
}
/**
* @return array
*/
public function getBondDiscountedCashFlows()
{
// discounted cash flows = nominal cash flows discounted by the means of yield
$discountFactor = MathFuncs::div(1, MathFuncs::add(1, $this->getBondYieldPerPaymentPeriod()));
$nominalCashFlows = $this->getBondNominalCashFlows();
$discountedCashFlows = array();
$i = 1;
foreach ($nominalCashFlows as $nominalCashFlowEntry) {
$discountedCashFlows[] = MathFuncs::mul(
$nominalCashFlowEntry,
MathFuncs::pow(
$discountFactor,
$i++)
);
}
return $discountedCashFlows;
}
/**
* @return float
*/
public function getBondPresentValue()
{
// bond present value = sum of all discounted cash flows during the life of the bond
$presentValue = 0;
foreach ($this->getBondDiscountedCashFlows() as $discountedCashFlowEntry) {
$presentValue = MathFuncs::add($presentValue, $discountedCashFlowEntry);
}
return $presentValue;
}
/**
* @return string
*/
public function getBondDuration()
{
// duration of the bond = sum of auxiliary values of all periods / bond present value
// auxiliary value for a period = discounted cash flow in the period * number of the period
$auxiliaryValue = 0;
$i = 1;
foreach ($this->getBondDiscountedCashFlows() as $discountedCashFlowEntry) {
$auxiliaryValue = MathFuncs::add(
$auxiliaryValue,
MathFuncs::mul(
$discountedCashFlowEntry,
$i++)
);
}
$duration = MathFuncs::div($auxiliaryValue, $this->getBondPresentValue());
return $duration;
}
}
}