1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Provider\Distribution;
|
4
|
|
|
|
5
|
|
|
use RandomLib\Factory;
|
6
|
|
|
use Samsara\Exceptions\UsageError\IntegrityConstraint;
|
7
|
|
|
use Samsara\Exceptions\UsageError\OptionalExit;
|
8
|
|
|
use Samsara\Fermat\Numbers;
|
9
|
|
|
use Samsara\Fermat\Provider\Distribution\Base\Distribution;
|
10
|
|
|
use Samsara\Fermat\Provider\StatsProvider;
|
11
|
|
|
use Samsara\Fermat\Types\Base\DecimalInterface;
|
12
|
|
|
use Samsara\Fermat\Types\Base\NumberCollectionInterface;
|
13
|
|
|
use Samsara\Fermat\Types\Base\NumberInterface;
|
14
|
|
|
use Samsara\Fermat\Types\NumberCollection;
|
15
|
|
|
use Samsara\Fermat\Values\ImmutableNumber;
|
16
|
|
|
|
17
|
|
|
class Normal extends Distribution
|
18
|
|
|
{
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* @var ImmutableNumber
|
22
|
|
|
*/
|
23
|
|
|
private $mean;
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* @var ImmutableNumber
|
27
|
|
|
*/
|
28
|
|
|
private $sd;
|
29
|
|
|
|
30
|
|
|
/**
|
31
|
|
|
* Normal constructor.
|
32
|
|
|
*
|
33
|
|
|
* @param int|float|DecimalInterface $mean
|
34
|
|
|
* @param int|float|DecimalInterface $sd
|
35
|
|
|
* @throws IntegrityConstraint
|
36
|
|
|
*/
|
37
|
|
|
public function __construct($mean, $sd)
|
38
|
|
|
{
|
39
|
|
|
$mean = Numbers::makeOrDont(Numbers::IMMUTABLE, $mean);
|
40
|
|
|
$sd = Numbers::makeOrDont(Numbers::IMMUTABLE, $sd);
|
41
|
|
|
|
42
|
|
|
$this->mean = $mean;
|
|
|
|
|
43
|
|
|
$this->sd = $sd;
|
|
|
|
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* @param int|float|DecimalInterface $p
|
48
|
|
|
* @param int|float|DecimalInterface $x
|
49
|
|
|
* @param int|float|DecimalInterface $mean
|
50
|
|
|
*
|
51
|
|
|
* @return Normal
|
52
|
|
|
* @throws IntegrityConstraint
|
53
|
|
|
*/
|
54
|
|
View Code Duplication |
public static function makeFromMean($p, $x, $mean): Normal
|
|
|
|
|
55
|
|
|
{
|
56
|
|
|
$one = Numbers::makeOne();
|
57
|
|
|
$p = Numbers::makeOrDont(Numbers::IMMUTABLE, $p);
|
58
|
|
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x);
|
59
|
|
|
$mean = Numbers::makeOrDont(Numbers::IMMUTABLE, $mean);
|
60
|
|
|
|
61
|
|
|
$z = StatsProvider::inverseNormalCDF($one->subtract($p));
|
62
|
|
|
|
63
|
|
|
$sd = $x->subtract($mean)->divide($z);
|
|
|
|
|
64
|
|
|
|
65
|
|
|
return new Normal($mean, $sd);
|
|
|
|
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* @param int|float|DecimalInterface $p
|
70
|
|
|
* @param int|float|DecimalInterface $x
|
71
|
|
|
* @param int|float|DecimalInterface $sd
|
72
|
|
|
*
|
73
|
|
|
* @return Normal
|
74
|
|
|
* @throws IntegrityConstraint
|
75
|
|
|
*/
|
76
|
|
View Code Duplication |
public static function makeFromSd($p, $x, $sd): Normal
|
|
|
|
|
77
|
|
|
{
|
78
|
|
|
$one = Numbers::makeOne();
|
79
|
|
|
$p = Numbers::makeOrDont(Numbers::IMMUTABLE, $p);
|
80
|
|
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x);
|
81
|
|
|
$sd = Numbers::makeOrDont(Numbers::IMMUTABLE, $sd);
|
82
|
|
|
|
83
|
|
|
$z = StatsProvider::inverseNormalCDF($one->subtract($p));
|
84
|
|
|
|
85
|
|
|
$mean = $x->subtract($z->multiply($sd));
|
|
|
|
|
86
|
|
|
|
87
|
|
|
return new Normal($mean, $sd);
|
|
|
|
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
/**
|
91
|
|
|
* @param int|float|DecimalInterface $x
|
92
|
|
|
*
|
93
|
|
|
* @return ImmutableNumber
|
94
|
|
|
* @throws IntegrityConstraint
|
95
|
|
|
*/
|
96
|
|
|
public function cdf($x): ImmutableNumber
|
97
|
|
|
{
|
98
|
|
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x);
|
99
|
|
|
|
100
|
|
|
$oneHalf = Numbers::make(Numbers::IMMUTABLE, '0.5');
|
101
|
|
|
$one = Numbers::makeOne();
|
102
|
|
|
$sqrtTwo = Numbers::make(Numbers::IMMUTABLE, 2)->sqrt();
|
|
|
|
|
103
|
|
|
|
104
|
|
|
/** @var ImmutableNumber $cdf */
|
105
|
|
|
$cdf = $oneHalf->multiply($one->add(StatsProvider::gaussErrorFunction(
|
|
|
|
|
106
|
|
|
$x->subtract($this->mean)->divide($this->sd->multiply($sqrtTwo))
|
|
|
|
|
107
|
|
|
)));
|
108
|
|
|
|
109
|
|
|
return $cdf;
|
110
|
|
|
}
|
111
|
|
|
|
112
|
|
|
/**
|
113
|
|
|
* @param int|float|DecimalInterface $x1
|
114
|
|
|
* @param null|int|float|DecimalInterface $x2
|
115
|
|
|
*
|
116
|
|
|
* @return ImmutableNumber
|
117
|
|
|
* @throws IntegrityConstraint
|
118
|
|
|
*/
|
119
|
|
|
public function pdf($x1, $x2 = null): ImmutableNumber
|
120
|
|
|
{
|
121
|
|
|
|
122
|
|
|
$x1 = Numbers::makeOrDont(Numbers::IMMUTABLE, $x1);
|
123
|
|
|
|
124
|
|
|
if (is_null($x2)) {
|
125
|
|
|
$separation = $x1->subtract($this->mean)->multiply(2)->abs();
|
|
|
|
|
126
|
|
|
|
127
|
|
|
if ($this->mean->isLessThan($x1)) {
|
128
|
|
|
$x2 = $x1->subtract($separation);
|
129
|
|
|
} else {
|
130
|
|
|
$x2 = $x1->add($separation);
|
|
|
|
|
131
|
|
|
}
|
132
|
|
|
}
|
133
|
|
|
|
134
|
|
|
/** @var ImmutableNumber $pdf */
|
135
|
|
|
$pdf = $this->cdf($x1)->subtract($this->cdf($x2))->abs();
|
|
|
|
|
136
|
|
|
|
137
|
|
|
return $pdf;
|
138
|
|
|
}
|
139
|
|
|
|
140
|
|
|
/**
|
141
|
|
|
* @param int|float|DecimalInterface $x
|
142
|
|
|
*
|
143
|
|
|
* @return ImmutableNumber
|
144
|
|
|
* @throws IntegrityConstraint
|
145
|
|
|
*/
|
146
|
|
|
public function percentBelowX($x): ImmutableNumber
|
147
|
|
|
{
|
148
|
|
|
return $this->cdf($x);
|
149
|
|
|
}
|
150
|
|
|
|
151
|
|
|
/**
|
152
|
|
|
* @param int|float|DecimalInterface $x
|
153
|
|
|
*
|
154
|
|
|
* @return ImmutableNumber
|
155
|
|
|
* @throws IntegrityConstraint
|
156
|
|
|
*/
|
157
|
|
|
public function percentAboveX($x): ImmutableNumber
|
158
|
|
|
{
|
159
|
|
|
$one = Numbers::makeOne();
|
160
|
|
|
|
161
|
|
|
return $one->subtract($this->cdf($x));
|
162
|
|
|
}
|
163
|
|
|
|
164
|
|
|
/**
|
165
|
|
|
* @param int|float|DecimalInterface $x
|
166
|
|
|
*
|
167
|
|
|
* @return ImmutableNumber
|
168
|
|
|
* @throws IntegrityConstraint
|
169
|
|
|
*/
|
170
|
|
|
public function zScoreOfX($x): ImmutableNumber
|
171
|
|
|
{
|
172
|
|
|
/** @var ImmutableNumber $x */
|
173
|
|
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x);
|
174
|
|
|
|
175
|
|
|
/** @var ImmutableNumber $z */
|
176
|
|
|
$z = $x->subtract($this->mean)->divide($this->sd);
|
177
|
|
|
|
178
|
|
|
return $z;
|
179
|
|
|
}
|
180
|
|
|
|
181
|
|
|
/**
|
182
|
|
|
* @param int|float|DecimalInterface $z
|
183
|
|
|
*
|
184
|
|
|
* @return ImmutableNumber
|
185
|
|
|
* @throws IntegrityConstraint
|
186
|
|
|
*/
|
187
|
|
|
public function xFromZScore($z): ImmutableNumber
|
188
|
|
|
{
|
189
|
|
|
$z = Numbers::makeOrDont(Numbers::IMMUTABLE, $z);
|
190
|
|
|
|
191
|
|
|
/** @var ImmutableNumber $x */
|
192
|
|
|
$x = $z->multiply($this->sd)->add($this->mean);
|
|
|
|
|
193
|
|
|
|
194
|
|
|
return $x;
|
195
|
|
|
}
|
196
|
|
|
|
197
|
|
|
/**
|
198
|
|
|
* @return ImmutableNumber
|
199
|
|
|
* @throws IntegrityConstraint
|
200
|
|
|
*/
|
201
|
|
|
public function random(): ImmutableNumber
|
202
|
|
|
{
|
203
|
|
|
$randFactory = new Factory();
|
204
|
|
|
$generator = $randFactory->getMediumStrengthGenerator();
|
205
|
|
|
|
206
|
|
|
$rand1 = Numbers::make(Numbers::IMMUTABLE, $generator->generateInt(), 20);
|
207
|
|
|
$rand1 = $rand1->divide(PHP_INT_MAX);
|
|
|
|
|
208
|
|
|
$rand2 = Numbers::make(Numbers::IMMUTABLE, $generator->generateInt(), 20);
|
209
|
|
|
$rand2 = $rand2->divide(PHP_INT_MAX);
|
210
|
|
|
|
211
|
|
|
$randomNumber = $rand1->ln()->multiply(-2)->sqrt()->multiply($rand2->multiply(Numbers::TAU)->cos(1, false));
|
212
|
|
|
/** @var ImmutableNumber $randomNumber */
|
213
|
|
|
$randomNumber = $randomNumber->multiply($this->sd)->add($this->mean);
|
|
|
|
|
214
|
|
|
|
215
|
|
|
return $randomNumber;
|
216
|
|
|
}
|
217
|
|
|
|
218
|
|
|
/**
|
219
|
|
|
* @param int|float|NumberInterface $min
|
220
|
|
|
* @param int|float|NumberInterface $max
|
221
|
|
|
* @param int $maxIterations
|
222
|
|
|
*
|
223
|
|
|
* @return ImmutableNumber
|
224
|
|
|
* @throws OptionalExit
|
225
|
|
|
* @throws IntegrityConstraint
|
226
|
|
|
*/
|
227
|
|
View Code Duplication |
public function rangeRandom($min = 0, $max = PHP_INT_MAX, int $maxIterations = 20): ImmutableNumber
|
|
|
|
|
228
|
|
|
{
|
229
|
|
|
$i = 0;
|
230
|
|
|
|
231
|
|
|
do {
|
232
|
|
|
$randomNumber = $this->random();
|
233
|
|
|
$i++;
|
234
|
|
|
} while (($randomNumber->isGreaterThan($max) || $randomNumber->isLessThan($min)) && $i < $maxIterations);
|
235
|
|
|
|
236
|
|
|
if ($randomNumber->isGreaterThan($max) || $randomNumber->isLessThan($min)) {
|
237
|
|
|
throw new OptionalExit(
|
238
|
|
|
'All random numbers generated were outside of the requested range',
|
239
|
|
|
'A suitable random number, restricted by the $max ('.$max.') and $min ('.$min.'), could not be found within '.$maxIterations.' iterations'
|
240
|
|
|
);
|
241
|
|
|
}
|
242
|
|
|
|
243
|
|
|
return $randomNumber;
|
244
|
|
|
}
|
245
|
|
|
|
246
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..