|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Ds\Vector; |
|
6
|
|
|
use Samsara\Exceptions\UsageError\IntegrityConstraint; |
|
7
|
|
|
use Samsara\Exceptions\SystemError\LogicalError\IncompatibleObjectState; |
|
8
|
|
|
use Samsara\Exceptions\UsageError\OptionalExit; |
|
9
|
|
|
use Samsara\Fermat\Numbers; |
|
10
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Numbers\NumberInterface; |
|
11
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface; |
|
12
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Numbers\FractionInterface; |
|
13
|
|
|
use Samsara\Fermat\Values\ImmutableDecimal; |
|
14
|
|
|
use Samsara\Fermat\Values\ImmutableFraction; |
|
15
|
|
|
|
|
16
|
|
|
class StatsProvider |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Vector |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $inverseErrorCoefs; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param $x |
|
26
|
|
|
* |
|
27
|
|
|
* @return NumberInterface |
|
28
|
|
|
* @throws IntegrityConstraint |
|
29
|
|
|
* @throws OptionalExit|\ReflectionException |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public static function normalCDF($x): ImmutableDecimal |
|
32
|
|
|
{ |
|
33
|
2 |
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x); |
|
34
|
|
|
|
|
35
|
2 |
|
$precision = $x->getPrecision(); |
|
|
|
|
|
|
36
|
2 |
|
$internalPrecision = $precision+2; |
|
37
|
|
|
|
|
38
|
2 |
|
$pi = Numbers::makePi($internalPrecision); |
|
39
|
2 |
|
$e = Numbers::makeE($internalPrecision); |
|
40
|
2 |
|
$one = Numbers::makeOne($internalPrecision); |
|
41
|
|
|
|
|
42
|
2 |
|
$eExponent = Numbers::make(Numbers::IMMUTABLE, $x->getValue()); |
|
|
|
|
|
|
43
|
2 |
|
$eExponent = $eExponent->pow(2)->divide(2)->multiply(-1); |
|
44
|
|
|
|
|
45
|
2 |
|
$answer = Numbers::make(Numbers::IMMUTABLE, 0.5); |
|
46
|
2 |
|
$answer = $answer->add( |
|
47
|
2 |
|
$one->divide($pi->multiply(2)->sqrt()) |
|
48
|
2 |
|
->multiply($e->pow($eExponent)) |
|
49
|
2 |
|
->multiply(SeriesProvider::maclaurinSeries( |
|
50
|
2 |
|
$x, |
|
|
|
|
|
|
51
|
|
|
function ($n) { |
|
|
|
|
|
|
52
|
2 |
|
return Numbers::makeOne(); |
|
53
|
2 |
|
}, |
|
54
|
|
|
function ($n) { |
|
55
|
2 |
|
return SequenceProvider::nthOddNumber($n); |
|
56
|
2 |
|
}, |
|
57
|
|
|
function ($n) { |
|
58
|
2 |
|
return SequenceProvider::nthOddNumber($n)->doubleFactorial(); |
|
|
|
|
|
|
59
|
2 |
|
}, |
|
60
|
2 |
|
0, |
|
61
|
|
|
$internalPrecision |
|
62
|
|
|
)) |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
2 |
|
return $answer->truncateToPrecision($precision); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $x |
|
71
|
|
|
* |
|
72
|
|
|
* @return DecimalInterface|NumberInterface |
|
73
|
|
|
* @throws IntegrityConstraint |
|
74
|
|
|
* @throws OptionalExit |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public static function complementNormalCDF($x): ImmutableDecimal |
|
77
|
|
|
{ |
|
78
|
1 |
|
$p = self::normalCDF($x); |
|
79
|
1 |
|
$one = Numbers::makeOne(); |
|
80
|
|
|
|
|
81
|
1 |
|
return $one->subtract($p); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $x |
|
86
|
|
|
* |
|
87
|
|
|
* @return DecimalInterface|FractionInterface|NumberInterface|ImmutableDecimal |
|
88
|
|
|
* @throws IntegrityConstraint |
|
89
|
|
|
* @throws OptionalExit |
|
90
|
|
|
*/ |
|
91
|
5 |
|
public static function gaussErrorFunction($x): ImmutableDecimal |
|
92
|
|
|
{ |
|
93
|
|
|
|
|
94
|
5 |
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x); |
|
95
|
|
|
|
|
96
|
5 |
|
$precision = $x->getPrecision(); |
|
97
|
5 |
|
$internalPrecision = $precision + 2; |
|
98
|
|
|
|
|
99
|
5 |
|
$answer = Numbers::makeOne($internalPrecision); |
|
100
|
5 |
|
$pi = Numbers::makePi($internalPrecision); |
|
101
|
|
|
|
|
102
|
5 |
|
$answer = $answer->multiply(2)->divide($pi->sqrt()); |
|
103
|
|
|
|
|
104
|
5 |
|
$answer = $answer->multiply( |
|
105
|
5 |
|
SeriesProvider::maclaurinSeries( |
|
106
|
5 |
|
$x, |
|
|
|
|
|
|
107
|
|
|
function ($n) { |
|
108
|
5 |
|
$negOne = Numbers::make(Numbers::IMMUTABLE, -1); |
|
109
|
|
|
|
|
110
|
5 |
|
return $negOne->pow($n); |
|
111
|
5 |
|
}, |
|
112
|
|
|
function ($n) { |
|
113
|
5 |
|
return SequenceProvider::nthOddNumber($n); |
|
114
|
5 |
|
}, |
|
115
|
|
|
function ($n) { |
|
116
|
5 |
|
$n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n); |
|
117
|
|
|
|
|
118
|
5 |
|
return $n->factorial()->multiply(SequenceProvider::nthOddNumber($n->asInt())); |
|
|
|
|
|
|
119
|
5 |
|
}, |
|
120
|
5 |
|
0, |
|
121
|
|
|
$internalPrecision |
|
122
|
|
|
) |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
5 |
|
return $answer->truncateToPrecision($precision); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param $p |
|
131
|
|
|
* @param int $precision |
|
132
|
|
|
* |
|
133
|
|
|
* @return DecimalInterface|NumberInterface|ImmutableDecimal |
|
134
|
|
|
* @throws IntegrityConstraint |
|
135
|
|
|
* @throws OptionalExit |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public static function inverseNormalCDF($p, int $precision = 10): ImmutableDecimal |
|
138
|
|
|
{ |
|
139
|
1 |
|
$p = Numbers::makeOrDont(Numbers::IMMUTABLE, $p); |
|
140
|
|
|
|
|
141
|
1 |
|
$precision = $precision ?? $p->getPrecision(); |
|
142
|
1 |
|
$internalPrecision = $precision + 2; |
|
143
|
|
|
|
|
144
|
1 |
|
$two = Numbers::make(Numbers::IMMUTABLE, 2, $internalPrecision); |
|
145
|
1 |
|
$invErfArg = $two->multiply($p)->subtract(1); |
|
146
|
|
|
|
|
147
|
1 |
|
return StatsProvider::inverseGaussErrorFunction($invErfArg, $internalPrecision)->multiply($two->sqrt($internalPrecision))->roundToPrecision($precision); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param $n |
|
152
|
|
|
* @param $k |
|
153
|
|
|
* |
|
154
|
|
|
* @return DecimalInterface|NumberInterface|ImmutableDecimal |
|
155
|
|
|
* @throws IntegrityConstraint |
|
156
|
|
|
* @throws IncompatibleObjectState |
|
157
|
|
|
*/ |
|
158
|
4 |
|
public static function binomialCoefficient($n, $k): ImmutableDecimal |
|
159
|
|
|
{ |
|
160
|
|
|
|
|
161
|
4 |
|
$n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n); |
|
162
|
4 |
|
$k = Numbers::makeOrDont(Numbers::IMMUTABLE, $k); |
|
163
|
|
|
|
|
164
|
4 |
|
if ($k->isLessThan(0) || $n->isLessThan($k)) { |
|
|
|
|
|
|
165
|
2 |
|
throw new IntegrityConstraint( |
|
166
|
2 |
|
'$k must be larger or equal to 0 and less than or equal to $n', |
|
167
|
2 |
|
'Provide valid $n and $k values such that 0 <= $k <= $n', |
|
168
|
2 |
|
'For $n choose $k, the values of $n and $k must satisfy the inequality 0 <= $k <= $n' |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
2 |
|
if (!$n->isInt() || !$k->isInt()) { |
|
|
|
|
|
|
173
|
1 |
|
throw new IntegrityConstraint( |
|
174
|
1 |
|
'$k and $n must be whole numbers', |
|
175
|
1 |
|
'Provide whole numbers for $n and $k', |
|
176
|
1 |
|
'For $n choose $k, the values $n and $k must be whole numbers' |
|
177
|
|
|
); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
return $n->factorial()->divide($k->factorial()->multiply($n->subtract($k)->factorial())); |
|
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
3 |
|
public static function inverseErrorCoefficients(int $termIndex): ImmutableFraction |
|
185
|
|
|
{ |
|
186
|
|
|
|
|
187
|
3 |
|
$terms =& static::$inverseErrorCoefs; |
|
188
|
|
|
|
|
189
|
3 |
|
if (is_null(static::$inverseErrorCoefs)) { |
|
190
|
1 |
|
$terms = new Vector(); |
|
191
|
1 |
|
$terms->push(new ImmutableFraction(Numbers::makeOne(), Numbers::makeOne())); |
|
192
|
1 |
|
$terms->push(new ImmutableFraction(Numbers::makeOne(), Numbers::makeOne())); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
3 |
|
if ($terms->offsetExists($termIndex)) { |
|
196
|
3 |
|
return $terms->get($termIndex); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
2 |
|
$nextTerm = $terms->count(); |
|
200
|
|
|
|
|
201
|
2 |
|
for ($k = $nextTerm;$k <= $termIndex;$k++) { |
|
202
|
2 |
|
$termValue = new ImmutableFraction(new ImmutableDecimal('0'), new ImmutableDecimal('1')); |
|
203
|
2 |
|
for ($m = 0;$m <= ($k - 1);$m++) { |
|
204
|
2 |
|
$part1 = $terms->get($m); |
|
205
|
2 |
|
$part2 = $terms->get($k - 1 - $m); |
|
206
|
2 |
|
$part3 = $part1->multiply($part2); |
|
207
|
2 |
|
$part4 = ($m + 1)*($m*2 + 1); |
|
208
|
2 |
|
$part5 = $part3->divide($part4); |
|
209
|
2 |
|
$termValue = $termValue->add($part5); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
2 |
|
$termValue = $termValue->simplify(); |
|
213
|
|
|
|
|
214
|
2 |
|
$terms->push($termValue); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
2 |
|
return $terms->get($termIndex); |
|
218
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param $z |
|
223
|
|
|
* @param int $precision |
|
224
|
|
|
* |
|
225
|
|
|
* @return ImmutableDecimal |
|
226
|
|
|
* @throws IntegrityConstraint |
|
227
|
|
|
* @throws OptionalExit |
|
228
|
|
|
* @throws \ReflectionException |
|
229
|
|
|
*/ |
|
230
|
2 |
|
public static function inverseGaussErrorFunction($z, int $precision = 10): ImmutableDecimal |
|
231
|
|
|
{ |
|
232
|
|
|
|
|
233
|
2 |
|
$z = Numbers::makeOrDont(Numbers::IMMUTABLE, $z); |
|
234
|
|
|
|
|
235
|
2 |
|
$precision = $precision ?? $z->getPrecision(); |
|
236
|
2 |
|
$internalPrecision = $precision + 1; |
|
237
|
|
|
|
|
238
|
2 |
|
$pi = Numbers::makePi($internalPrecision); |
|
239
|
|
|
|
|
240
|
2 |
|
$answer = SeriesProvider::maclaurinSeries( |
|
241
|
2 |
|
$z, |
|
|
|
|
|
|
242
|
|
|
function ($n) use ($pi) { |
|
243
|
2 |
|
if ($n > 0) { |
|
244
|
2 |
|
return $pi->pow($n)->multiply(StatsProvider::inverseErrorCoefficients($n)); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
2 |
|
return Numbers::makeOne(); |
|
248
|
2 |
|
}, |
|
249
|
|
|
function ($n) { |
|
250
|
2 |
|
return SequenceProvider::nthOddNumber($n); |
|
251
|
2 |
|
}, |
|
252
|
|
|
function ($n) { |
|
253
|
2 |
|
if ($n > 0) { |
|
254
|
2 |
|
$extra = Numbers::make(Numbers::IMMUTABLE, 2)->pow(SequenceProvider::nthEvenNumber($n)); |
|
255
|
|
|
} else { |
|
256
|
2 |
|
$extra = Numbers::makeOne(); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
2 |
|
return SequenceProvider::nthOddNumber($n)->multiply($extra); |
|
260
|
2 |
|
}, |
|
261
|
2 |
|
0, |
|
262
|
|
|
$internalPrecision |
|
263
|
|
|
); |
|
264
|
|
|
|
|
265
|
2 |
|
$answer = $answer->multiply($pi->sqrt($internalPrecision)->divide(2, $internalPrecision)); |
|
|
|
|
|
|
266
|
|
|
|
|
267
|
2 |
|
return $answer->roundToPrecision($precision); |
|
268
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
} |