-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBondYTMCalculator.php
119 lines (103 loc) · 3.61 KB
/
BondYTMCalculator.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
<?php
namespace FinanCalc\Calculators {
use FinanCalc\Interfaces\Calculator\BondCalculatorAbstract;
use FinanCalc\Utils\Lambdas;
use FinanCalc\Utils\MathFuncs;
/**
* Class BondYTMCalculator
* @package FinanCalc\Calculators
*/
class BondYTMCalculator extends BondCalculatorAbstract
{
// market value of the bond = 'P'
protected $bondMarketValue;
// 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",
"bondMarketValue",
"bondAnnualCouponRate",
"bondYearsToMaturity",
"bondPaymentFrequency",
"bondApproxYTM" => "approxBondYTM"
];
/**
* @param $bondFaceValue
* @param $bondMarketValue
* @param $bondAnnualCouponRate
* @param $bondYearsToMaturity
* @param $bondPaymentFrequency
*/
public function __construct(
$bondFaceValue,
$bondMarketValue,
$bondAnnualCouponRate,
$bondYearsToMaturity,
$bondPaymentFrequency = 1
) {
$this->setBondFaceValue($bondFaceValue);
$this->setBondMarketValue($bondMarketValue);
$this->setBondAnnualCouponRate($bondAnnualCouponRate);
$this->setBondYearsToMaturity($bondYearsToMaturity);
$this->setBondPaymentFrequency($bondPaymentFrequency);
}
/**
* @param $bondMarketValue
*/
public function setBondMarketValue($bondMarketValue)
{
$this->setProperty("bondMarketValue", $bondMarketValue, Lambdas::checkIfPositive());
}
/**
* @return mixed
*/
public function getBondMarketValue()
{
return $this->bondMarketValue;
}
/**
* @return string
*/
public function getApproxBondYTM()
{
// we need to calculate the coupon payment C = F*(c/payment frequency)
$couponPayment =
MathFuncs::mul(
$this->bondFaceValue,
MathFuncs::div(
$this->bondAnnualCouponRate,
$this->bondPaymentFrequency
)
);
// we use a formula to approximate the YTM = (C+(F-P)/n)/((F+P)/2)
$approxYTM =
MathFuncs::div(
MathFuncs::add(
$couponPayment,
MathFuncs::div(
MathFuncs::sub(
$this->bondFaceValue,
$this->bondMarketValue),
$this->getBondNoOfPayments()
)
),
MathFuncs::div(
MathFuncs::add(
$this->bondFaceValue,
$this->bondMarketValue
),
2));
return $approxYTM;
}
// TODO – add a method for precise bond YTM calculation by means of a polynomial equation
}
}