1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Provider\Stats\Distribution;
|
4
|
|
|
|
5
|
|
|
use RandomLib\Factory;
|
6
|
|
|
use Samsara\Fermat\Numbers;
|
7
|
|
|
use Samsara\Fermat\Provider\Stats\Stats;
|
8
|
|
|
use Samsara\Fermat\Types\Base\NumberInterface;
|
9
|
|
|
use Samsara\Fermat\Values\ImmutableNumber;
|
10
|
|
|
|
11
|
|
|
class Normal
|
12
|
|
|
{
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* @var NumberInterface
|
16
|
|
|
*/
|
17
|
|
|
private $mean;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @var NumberInterface
|
21
|
|
|
*/
|
22
|
|
|
private $sd;
|
23
|
|
|
|
24
|
|
|
public function __construct($mean, $sd)
|
25
|
|
|
{
|
26
|
|
|
$mean = Numbers::makeOrDont(Numbers::IMMUTABLE, $mean);
|
27
|
|
|
$sd = Numbers::makeOrDont(Numbers::IMMUTABLE, $sd);
|
28
|
|
|
|
29
|
|
|
$this->mean = $mean;
|
|
|
|
|
30
|
|
|
$this->sd = $sd;
|
|
|
|
|
31
|
|
|
}
|
32
|
|
|
|
33
|
|
View Code Duplication |
public static function makeFromMean($p, $x, $mean)
|
|
|
|
|
34
|
|
|
{
|
35
|
|
|
$one = Numbers::makeOne();
|
36
|
|
|
$p = Numbers::makeOrDont(Numbers::IMMUTABLE, $p);
|
37
|
|
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x);
|
38
|
|
|
$mean = Numbers::makeOrDont(Numbers::IMMUTABLE, $mean);
|
39
|
|
|
|
40
|
|
|
$z = Stats::inverseNormalCDF($one->subtract($p));
|
41
|
|
|
|
42
|
|
|
$sd = $x->subtract($mean)->divide($z);
|
43
|
|
|
|
44
|
|
|
return new Normal($mean, $sd);
|
45
|
|
|
}
|
46
|
|
|
|
47
|
|
View Code Duplication |
public static function makeFromSd($p, $x, $sd)
|
|
|
|
|
48
|
|
|
{
|
49
|
|
|
$one = Numbers::makeOne();
|
50
|
|
|
$p = Numbers::makeOrDont(Numbers::IMMUTABLE, $p);
|
51
|
|
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x);
|
52
|
|
|
$sd = Numbers::makeOrDont(Numbers::IMMUTABLE, $sd);
|
53
|
|
|
|
54
|
|
|
$z = Stats::inverseNormalCDF($one->subtract($p));
|
55
|
|
|
|
56
|
|
|
$mean = $x->subtract($z->multiply($sd));
|
57
|
|
|
|
58
|
|
|
return new Normal($mean, $sd);
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
public function cdf($x)
|
62
|
|
|
{
|
63
|
|
|
$x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x);
|
64
|
|
|
|
65
|
|
|
if (function_exists('stats_cdf_normal') &&
|
66
|
|
|
$x->isLessThanOrEqualTo(PHP_INT_MAX) &&
|
67
|
|
|
$x->isGreaterThanOrEqualTo(PHP_INT_MIN) &&
|
68
|
|
|
$this->mean->isLessThanOrEqualTo(PHP_INT_MAX) &&
|
69
|
|
|
$this->mean->isGreaterThanOrEqualTo(PHP_INT_MIN) &&
|
70
|
|
|
$this->sd->isLessThanOrEqualTo(PHP_INT_MAX) &&
|
71
|
|
|
$this->sd->isGreaterThanOrEqualTo(PHP_INT_MIN)) {
|
72
|
|
|
return Numbers::make(Numbers::IMMUTABLE, stats_cdf_normal($x->getValue(), $this->mean, $this->sd));
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
$oneHalf = Numbers::make(Numbers::IMMUTABLE, '0.5');
|
76
|
|
|
$one = Numbers::makeOne();
|
77
|
|
|
$sqrtTwo = Numbers::make(Numbers::IMMUTABLE, 2)->sqrt();
|
78
|
|
|
|
79
|
|
|
return $oneHalf->multiply($one->add(Stats::gaussErrorFunction(
|
80
|
|
|
$x->subtract($this->mean)->divide($this->sd->multiply($sqrtTwo))
|
81
|
|
|
)));
|
82
|
|
|
}
|
83
|
|
|
|
84
|
|
|
public function pdf($x1, $x2)
|
85
|
|
|
{
|
86
|
|
|
return $this->cdf($x1)->subtract($this->cdf($x2))->abs();
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
/**
|
90
|
|
|
* @return ImmutableNumber
|
91
|
|
|
*/
|
92
|
|
|
public function random()
|
93
|
|
|
{
|
94
|
|
|
if (function_exists('stats_rand_gen_normal')) {
|
95
|
|
|
return Numbers::make(Numbers::IMMUTABLE, stats_rand_gen_normal($this->mean, $this->sd), 20);
|
96
|
|
|
} else {
|
97
|
|
|
$randFactory = new Factory();
|
98
|
|
|
$generator = $randFactory->getMediumStrengthGenerator();
|
99
|
|
|
|
100
|
|
|
$rand1 = Numbers::make(Numbers::IMMUTABLE, $generator->generateInt(), 20);
|
101
|
|
|
$rand1 = $rand1->divide(PHP_INT_MAX);
|
102
|
|
|
$rand2 = Numbers::make(Numbers::IMMUTABLE, $generator->generateInt(), 20);
|
103
|
|
|
$rand2 = $rand2->divide(PHP_INT_MAX);
|
104
|
|
|
|
105
|
|
|
$randomNumber = $rand1->ln()->multiply(-2)->sqrt()->multiply($rand2->multiply(Numbers::TAU)->cos(1, 2, 20));
|
|
|
|
|
106
|
|
|
$randomNumber = $randomNumber->multiply($this->sd)->add($this->mean);
|
107
|
|
|
|
108
|
|
|
return $randomNumber;
|
109
|
|
|
}
|
110
|
|
|
}
|
111
|
|
|
|
112
|
|
|
public function rangeRandom($min = 0, $max = PHP_INT_MAX, $maxIterations = 20)
|
113
|
|
|
{
|
114
|
|
|
$i = 0;
|
115
|
|
|
|
116
|
|
|
do {
|
117
|
|
|
$randomNumber = $this->random();
|
118
|
|
|
$i++;
|
119
|
|
|
} while ($randomNumber->isLessThanOrEqualTo($max) && $randomNumber->isGreaterThanOrEqualTo($min) && $i < $maxIterations);
|
120
|
|
|
|
121
|
|
|
if ($randomNumber->isGreaterThan($max) || $randomNumber->isLessThan($min)) {
|
122
|
|
|
throw new \Exception();
|
123
|
|
|
} else {
|
124
|
|
|
return $randomNumber;
|
125
|
|
|
}
|
126
|
|
|
}
|
127
|
|
|
|
128
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.