1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Values\Distribution; |
4
|
|
|
|
5
|
|
|
use Samsara\Exceptions\UsageError\IntegrityConstraint; |
6
|
|
|
use Samsara\Exceptions\UsageError\OptionalExit; |
7
|
|
|
use Samsara\Fermat\Numbers; |
8
|
|
|
use Samsara\Fermat\Types\Distribution; |
9
|
|
|
use Samsara\Fermat\Provider\PolyfillProvider; |
10
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface; |
11
|
|
|
use Samsara\Fermat\Values\ImmutableDecimal; |
12
|
|
|
|
13
|
|
|
class Exponential extends Distribution |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ImmutableDecimal |
18
|
|
|
*/ |
19
|
|
|
private $lambda; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Exponential constructor. |
23
|
|
|
* |
24
|
|
|
* @param int|float|DecimalInterface $lambda This is the *rate parameter* not the *scale parameter* |
25
|
|
|
* |
26
|
|
|
* @throws IntegrityConstraint |
27
|
|
|
*/ |
28
|
9 |
|
public function __construct($lambda) |
29
|
|
|
{ |
30
|
9 |
|
$lambda = Numbers::makeOrDont(Numbers::IMMUTABLE, $lambda); |
31
|
|
|
|
32
|
9 |
|
if (!$lambda->isPositive()) { |
|
|
|
|
33
|
1 |
|
throw new IntegrityConstraint( |
34
|
1 |
|
'Lambda must be positive', |
35
|
1 |
|
'Provide a positive lambda', |
36
|
1 |
|
'Exponential distributions work on time to occurrence; the mean time to occurrence (lambda) must be positive' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
$this->lambda = $lambda; |
|
|
|
|
41
|
9 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param int|float|DecimalInterface $x |
45
|
|
|
* |
46
|
|
|
* @return ImmutableDecimal |
47
|
|
|
* @throws IntegrityConstraint |
48
|
|
|
*/ |
49
|
2 |
|
public function cdf($x, int $scale = 10): ImmutableDecimal |
50
|
|
|
{ |
51
|
|
|
|
52
|
2 |
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x); |
53
|
|
|
/** @var ImmutableDecimal $one */ |
54
|
2 |
|
$one = Numbers::makeOne(); |
55
|
|
|
|
56
|
2 |
|
if (!$x->isPositive()) { |
57
|
1 |
|
throw new IntegrityConstraint( |
58
|
1 |
|
'X must be positive', |
59
|
1 |
|
'Provide a positive x', |
60
|
1 |
|
'Exponential distributions work on time to occurrence; the time to occurrence (x) must be positive' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$internalScale = $scale + 2; |
65
|
|
|
|
66
|
|
|
/** @var ImmutableDecimal $e */ |
67
|
1 |
|
$e = Numbers::makeE($internalScale); |
68
|
|
|
|
69
|
|
|
/** @var ImmutableDecimal $cdf */ |
70
|
1 |
|
$cdf = $one->subtract($e->pow($x->multiply($this->lambda)->multiply(-1)))->truncateToScale($scale); |
71
|
|
|
|
72
|
1 |
|
return $cdf; |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $x |
78
|
|
|
* |
79
|
|
|
* @return ImmutableDecimal |
80
|
|
|
* @throws IntegrityConstraint |
81
|
|
|
*/ |
82
|
3 |
|
public function pdf($x, int $scale = 10): ImmutableDecimal |
83
|
|
|
{ |
84
|
|
|
|
85
|
3 |
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x); |
86
|
|
|
|
87
|
3 |
|
if (!$x->isPositive()) { |
88
|
1 |
|
throw new IntegrityConstraint( |
89
|
1 |
|
'X must be positive', |
90
|
1 |
|
'Provide a positive x', |
91
|
1 |
|
'Exponential distributions work on time to occurrence; the time to occurrence (x) must be positive' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
$internalScale = $scale + 2; |
96
|
|
|
|
97
|
|
|
/** @var ImmutableDecimal $e */ |
98
|
2 |
|
$e = Numbers::makeE($internalScale); |
99
|
|
|
|
100
|
|
|
/** @var ImmutableDecimal $pdf */ |
101
|
2 |
|
$pdf = $this->lambda->multiply($e->pow($this->lambda->multiply(-1)->multiply($x)))->truncateToScale(10); |
102
|
|
|
|
103
|
2 |
|
return $pdf; |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $x1 |
109
|
|
|
* @param $x2 |
110
|
|
|
* |
111
|
|
|
* @return ImmutableDecimal |
112
|
|
|
* @throws IntegrityConstraint |
113
|
|
|
*/ |
114
|
4 |
|
public function rangePdf($x1, $x2, int $scale = 10): ImmutableDecimal |
115
|
|
|
{ |
116
|
4 |
|
$x1 = Numbers::makeOrDont(Numbers::IMMUTABLE, $x1); |
117
|
4 |
|
$x2 = Numbers::makeOrDont(Numbers::IMMUTABLE, $x2); |
118
|
|
|
|
119
|
4 |
|
if (!$x1->isPositive() || !$x2->isPositive()) { |
120
|
3 |
|
throw new IntegrityConstraint( |
121
|
3 |
|
'X must be positive', |
122
|
3 |
|
'Provide a positive x', |
123
|
3 |
|
'Exponential distributions work on time to occurrence; the time to occurrence (x) must be positive' |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$internalScale = $scale + 2; |
128
|
|
|
|
129
|
|
|
/** @var ImmutableDecimal $rangePdf */ |
130
|
1 |
|
$rangePdf = $this->pdf($x2, $internalScale)->subtract($this->pdf($x1, $internalScale))->abs()->truncateToScale($scale); |
131
|
|
|
|
132
|
1 |
|
return $rangePdf; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return ImmutableDecimal |
137
|
|
|
* |
138
|
|
|
* @codeCoverageIgnore |
139
|
|
|
*/ |
140
|
|
|
public function random(): ImmutableDecimal |
141
|
|
|
{ |
142
|
|
|
|
143
|
|
|
$randomInt = PolyfillProvider::randomInt(0, PHP_INT_MAX); |
144
|
|
|
$one = Numbers::makeOne(); |
145
|
|
|
$u = Numbers::make(Numbers::IMMUTABLE, $randomInt, 20); |
146
|
|
|
$u = $u->divide(PHP_INT_MAX); |
147
|
|
|
|
148
|
|
|
/** @var ImmutableDecimal $random */ |
149
|
|
|
$random = $one->subtract($u)->ln()->divide($this->lambda->multiply(-1)); |
150
|
|
|
|
151
|
|
|
return $random; |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param int|float|DecimalInterface $min |
157
|
|
|
* @param int|float|DecimalInterface $max |
158
|
|
|
* @param int $maxIterations |
159
|
|
|
* |
160
|
|
|
* @return ImmutableDecimal |
161
|
|
|
* @throws OptionalExit |
162
|
|
|
* |
163
|
|
|
* @codeCoverageIgnore |
164
|
|
|
*/ |
165
|
|
|
public function rangeRandom($min = 0, $max = PHP_INT_MAX, int $maxIterations = 20): ImmutableDecimal |
166
|
|
|
{ |
167
|
|
|
|
168
|
|
|
$i = 0; |
169
|
|
|
|
170
|
|
|
do { |
171
|
|
|
$randomNumber = $this->random(); |
172
|
|
|
$i++; |
173
|
|
|
} while (($randomNumber->isGreaterThan($max) || $randomNumber->isLessThan($min)) && $i < $maxIterations); |
174
|
|
|
|
175
|
|
|
if ($randomNumber->isGreaterThan($max) || $randomNumber->isLessThan($min)) { |
176
|
|
|
throw new OptionalExit( |
177
|
|
|
'All random numbers generated were outside of the requested range', |
178
|
|
|
'A suitable random number, restricted by the $max ('.$max.') and $min ('.$min.'), could not be found within '.$maxIterations.' iterations' |
|
|
|
|
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $randomNumber; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |