-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStockDividendDiscountModelCalculator.php
185 lines (166 loc) · 6.2 KB
/
StockDividendDiscountModelCalculator.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
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace FinanCalc\Calculators {
use Exception;
use FinanCalc\Constants\StockDDMTypes;
use FinanCalc\Interfaces\Calculator\CalculatorAbstract;
use FinanCalc\Utils\Helpers;
use FinanCalc\Utils\Lambdas;
use FinanCalc\Utils\MathFuncs;
use FinanCalc\Utils\Strings;
use InvalidArgumentException;
/**
* Class StockDividendDiscountModelCalculator
* @package FinanCalc\Calculators
*/
class StockDividendDiscountModelCalculator extends CalculatorAbstract
{
/** @var StockDDMTypes */
// the type of the dividend discount model according to which the result will be calculated
protected $dividendDiscountModelType;
// stock's valuation interest rate
protected $stockVIR;
// absolute value of the stock's annual dividends
protected $stockAnnualDividendsValue;
// the rate by which the stock's annual dividends grow annually (i.e., a decimal number between 0 and 1)
// this value applies only to the multiple growth dividend discount model
protected $stockAnnualDividendsGrowth;
// props returned by the getResultAsArray method by default
protected $propResultArray = [
"dividendDiscountModelType",
"stockVIR",
"stockAnnualDividendsValue",
"stockAnnualDividendsGrowth",
"stockPresentValue"
];
/**
* @param StockDDMTypes $dividendDiscountModelType
* @param $stockVIR
* @param $stockAnnualDividendValue
* @param null $stockAnnualDividendsGrowth
*/
public function __construct(
StockDDMTypes $dividendDiscountModelType,
$stockVIR,
$stockAnnualDividendValue,
$stockAnnualDividendsGrowth = null
) {
$this->setDividendDiscountModelType($dividendDiscountModelType);
$this->setStockVIR($stockVIR);
$this->setStockAnnualDividendsValue($stockAnnualDividendValue);
$this->setStockAnnualDividendsGrowth($stockAnnualDividendsGrowth);
}
/**
* @param StockDDMTypes $dividendDiscountModelType
*/
public function setDividendDiscountModelType(StockDDMTypes $dividendDiscountModelType)
{
if (
$dividendDiscountModelType->getValue() == StockDDMTypes::ZERO_GROWTH
&&
$this->stockAnnualDividendsGrowth !== null
) {
$this->stockAnnualDividendsGrowth = null;
}
$this->setProperty("dividendDiscountModelType", $dividendDiscountModelType);
}
/**
* @param $stockVIR
*/
public function setStockVIR($stockVIR)
{
$this->setProperty("stockVIR", $stockVIR, Lambdas::checkIfPositive());
}
/**
* @param $stockAnnualDividendsValue
*/
public function setStockAnnualDividendsValue($stockAnnualDividendsValue)
{
$this->setProperty("stockAnnualDividendsValue", $stockAnnualDividendsValue, Lambdas::checkIfPositive());
}
/**
* @param $stockAnnualDividendsGrowth
*/
public function setStockAnnualDividendsGrowth($stockAnnualDividendsGrowth)
{
$dividendDiscountModelType = $this->dividendDiscountModelType->getValue();
if ($dividendDiscountModelType == StockDDMTypes::ZERO_GROWTH
&&
$stockAnnualDividendsGrowth !== null
) {
throw new InvalidArgumentException(Strings::getString('message_cannot_set_growth_value_on_zero_growth_type'));
}
if ($dividendDiscountModelType == StockDDMTypes::MULTIPLE_GROWTH) {
if (Helpers::checkIfPositiveNumberOrThrowAnException($stockAnnualDividendsGrowth)) {
Helpers::checkIfLeftOperandGreaterOrThrowAnException(
$this->stockVIR,
$stockAnnualDividendsGrowth,
Strings::getString('message_vir_must_be_higher_than_growth_value')
);
}
}
$this->setProperty("stockAnnualDividendsGrowth", $stockAnnualDividendsGrowth);
}
/**
* @return mixed
*/
public function getDividendDiscountModelType()
{
return $this->dividendDiscountModelType;
}
/**
* @return mixed
*/
public function getStockVIR()
{
return $this->stockVIR;
}
/**
* @return mixed
*/
public function getStockAnnualDividendsValue()
{
return $this->stockAnnualDividendsValue;
}
/**
* @return mixed
*/
public function getStockAnnualDividendsGrowth()
{
return $this->stockAnnualDividendsGrowth;
}
/**
* @return string
* @throws Exception
*/
public function getStockPresentValue()
{
switch ($this->dividendDiscountModelType->getValue()) {
case StockDDMTypes::ZERO_GROWTH:
// PV = D/i
return MathFuncs::div(
$this->stockAnnualDividendsValue,
$this->stockVIR
);
case StockDDMTypes::MULTIPLE_GROWTH:
if ($this->stockAnnualDividendsGrowth === null) {
throw new Exception(Strings::getString('message_must_set_growth_value'));
}
// PV = (D*(1+g))/(i-g)
return MathFuncs::mul(
$this->stockAnnualDividendsValue,
MathFuncs::div(
MathFuncs::add(
1,
$this->stockAnnualDividendsGrowth
),
MathFuncs::sub(
$this->stockVIR,
$this->stockAnnualDividendsGrowth
)
)
);
}
return null;
}
}
}