1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Provider; |
4
|
|
|
|
5
|
|
|
use Samsara\Exceptions\UsageError\IntegrityConstraint; |
6
|
|
|
use Samsara\Exceptions\SystemError\LogicalError\IncompatibleObjectState; |
7
|
|
|
use Samsara\Fermat\Numbers; |
8
|
|
|
use Samsara\Fermat\Types\Base\NumberInterface; |
9
|
|
|
use Samsara\Fermat\Types\Base\DecimalInterface; |
10
|
|
|
use Samsara\Fermat\Types\Base\FractionInterface; |
11
|
|
|
use Samsara\Fermat\Values\ImmutableNumber; |
12
|
|
|
|
13
|
|
|
class StatsProvider |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param $x |
18
|
|
|
* |
19
|
|
|
* @return NumberInterface |
20
|
|
|
* @throws IntegrityConstraint |
21
|
|
|
*/ |
22
|
|
|
public static function normalCDF($x) |
23
|
|
|
{ |
24
|
|
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x); |
25
|
|
|
|
26
|
|
|
$pi = Numbers::makePi(); |
27
|
|
|
$e = Numbers::makeE(); |
28
|
|
|
$one = Numbers::makeOne(); |
29
|
|
|
|
30
|
|
|
$eExponent = Numbers::make(Numbers::IMMUTABLE, $x->getValue()); |
31
|
|
|
$eExponent = $eExponent->pow(2)->divide(2)->multiply(-1); |
32
|
|
|
|
33
|
|
|
$answer = Numbers::make(Numbers::IMMUTABLE, 0.5); |
34
|
|
|
$answer = $answer->add( |
35
|
|
|
$one->divide($pi->multiply(2)->sqrt()) |
|
|
|
|
36
|
|
|
->multiply($e->pow($eExponent)) |
37
|
|
|
->multiply(SeriesProvider::maclaurinSeries( |
38
|
|
|
$x, |
39
|
|
|
function ($n) { |
|
|
|
|
40
|
|
|
return Numbers::makeOne(); |
41
|
|
|
}, |
42
|
|
|
function ($n) { |
43
|
|
|
return SequenceProvider::nthOddNumber($n); |
44
|
|
|
}, |
45
|
|
|
function ($n) { |
46
|
|
|
return SequenceProvider::nthOddNumber($n)->doubleFactorial(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
)) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
return $answer; |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $x |
57
|
|
|
* |
58
|
|
|
* @return DecimalInterface|NumberInterface |
59
|
|
|
* @throws IntegrityConstraint |
60
|
|
|
*/ |
61
|
|
|
public static function complementNormalCDF($x) |
62
|
|
|
{ |
63
|
|
|
$p = self::normalCDF($x); |
64
|
|
|
$one = Numbers::makeOne(); |
65
|
|
|
|
66
|
|
|
return $one->subtract($p); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $x |
71
|
|
|
* |
72
|
|
|
* @return DecimalInterface|FractionInterface|NumberInterface|ImmutableNumber |
73
|
|
|
* @throws IntegrityConstraint |
74
|
|
|
*/ |
75
|
3 |
|
public static function gaussErrorFunction($x) |
76
|
|
|
{ |
77
|
|
|
|
78
|
3 |
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x); |
79
|
3 |
|
$answer = Numbers::makeOne(); |
80
|
3 |
|
$pi = Numbers::makePi(); |
81
|
|
|
|
82
|
3 |
|
$answer = $answer->multiply(2)->divide($pi->sqrt()); |
|
|
|
|
83
|
|
|
|
84
|
3 |
|
$answer = $answer->multiply( |
85
|
3 |
|
SeriesProvider::maclaurinSeries( |
86
|
3 |
|
$x, |
87
|
|
|
function ($n) { |
88
|
3 |
|
$negOne = Numbers::make(Numbers::IMMUTABLE, -1); |
89
|
|
|
|
90
|
3 |
|
return $negOne->pow($n); |
|
|
|
|
91
|
3 |
|
}, |
92
|
|
|
function ($n) { |
93
|
3 |
|
return SequenceProvider::nthOddNumber($n); |
94
|
3 |
|
}, |
95
|
|
|
function ($n) { |
96
|
3 |
|
$n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n); |
97
|
|
|
|
98
|
3 |
|
return $n->factorial()->multiply(SequenceProvider::nthOddNumber($n)); |
|
|
|
|
99
|
3 |
|
} |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
|
103
|
3 |
|
return $answer; |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $p |
109
|
|
|
* @param int $precision |
110
|
|
|
* |
111
|
|
|
* @return DecimalInterface|NumberInterface|ImmutableNumber |
112
|
|
|
* @throws IntegrityConstraint |
113
|
|
|
*/ |
114
|
|
|
public static function inverseNormalCDF($p, int $precision = 10) |
115
|
|
|
{ |
116
|
|
|
$pi = Numbers::makePi(); |
117
|
|
|
$r2pi = $pi->multiply(2)->sqrt(); |
|
|
|
|
118
|
|
|
$e = Numbers::makeE(); |
119
|
|
|
$p = Numbers::makeOrDont(Numbers::IMMUTABLE, $p); |
120
|
|
|
|
121
|
|
|
$continue = true; |
122
|
|
|
|
123
|
|
|
$xCur = Numbers::make(Numbers::IMMUTABLE, $p); |
124
|
|
|
|
125
|
|
|
while ($continue) { |
126
|
|
|
|
127
|
|
|
$cumulative = self::normalCDF($xCur); |
128
|
|
|
$dx = $cumulative->subtract($p)->divide( |
129
|
|
|
$r2pi->multiply( |
|
|
|
|
130
|
|
|
$e->pow( |
131
|
|
|
$xCur->pow(2) |
132
|
|
|
)->divide(-2) |
|
|
|
|
133
|
|
|
) |
134
|
|
|
); |
135
|
|
|
$xCur = $xCur->subtract($dx); |
|
|
|
|
136
|
|
|
|
137
|
|
|
if ($dx->numberOfLeadingZeros() > $precision) { |
|
|
|
|
138
|
|
|
$continue = false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($p->isLessThan(0.5)) { |
144
|
|
|
return $xCur->multiply(-1); |
145
|
|
|
} else { |
146
|
|
|
return $xCur; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $n |
152
|
|
|
* @param $k |
153
|
|
|
* |
154
|
|
|
* @return DecimalInterface|NumberInterface|ImmutableNumber |
155
|
|
|
* @throws IntegrityConstraint |
156
|
|
|
* @throws IncompatibleObjectState |
157
|
|
|
*/ |
158
|
|
|
public static function binomialCoefficient($n, $k) |
159
|
|
|
{ |
160
|
|
|
|
161
|
|
|
$n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n); |
162
|
|
|
$k = Numbers::makeOrDont(Numbers::IMMUTABLE, $k); |
163
|
|
|
|
164
|
|
|
if ($k->isLessThan(0) || $n->isLessThan($k)) { |
165
|
|
|
throw new IntegrityConstraint( |
166
|
|
|
'$k must be larger or equal to 0 and less than or equal to $n', |
167
|
|
|
'Provide valid $n and $k values such that 0 <= $k <= $n', |
168
|
|
|
'For $n choose $k, the values of $n and $k must satisfy the inequality 0 <= $k <= $n' |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (!$n->isInt() || !$k->isInt()) { |
|
|
|
|
173
|
|
|
throw new IntegrityConstraint( |
174
|
|
|
'$k and $n must be whole numbers', |
175
|
|
|
'Provide whole numbers for $n and $k', |
176
|
|
|
'For $n choose $k, the values $n and $k must be whole numbers' |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $n->factorial()->divide($k->factorial()->multiply($n->subtract($k)->factorial())); |
|
|
|
|
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param $z |
186
|
|
|
* @param int $precision |
187
|
|
|
* |
188
|
|
|
* @return DecimalInterface|NumberInterface|ImmutableNumber |
189
|
|
|
* @throws IntegrityConstraint |
190
|
|
|
*/ |
191
|
|
|
public static function gammaFunction($z, int $precision = 10) |
192
|
|
|
{ |
193
|
|
|
$z = Numbers::makeOrDont(Numbers::IMMUTABLE, $z); |
194
|
|
|
|
195
|
|
|
if ($z->isInt()) { |
196
|
|
|
if ($z->isNegative() || $z->isEqual(0)) { |
197
|
|
|
throw new IntegrityConstraint( |
198
|
|
|
'Non-positive integers are not valid gamma function arguments', |
199
|
|
|
'Do not provide non-positive integers to this function', |
200
|
|
|
'The gamma function is not defined for zero or negative integers, but is continuous for all other values' |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
return $z->subtract(1)->factorial(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$x = Numbers::makeZero(); |
207
|
|
|
$e = Numbers::makeE(); |
208
|
|
|
$gamma = Numbers::makeZero(); |
209
|
|
|
|
210
|
|
|
$continue = true; |
211
|
|
|
|
212
|
|
|
while ($continue) { |
213
|
|
|
|
214
|
|
|
$adjustment = $x->pow( |
215
|
|
|
$z->subtract(1) |
216
|
|
|
)->multiply( |
217
|
|
|
$e->pow( |
218
|
|
|
$x->multiply(-1) |
219
|
|
|
) |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
$gamma = $gamma->add($adjustment); |
223
|
|
|
|
224
|
|
|
if ($adjustment->numberOfLeadingZeros() > $precision) { |
225
|
|
|
$continue = false; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $gamma; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
} |