Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.12% |
16 / 17 |
|
92.86% |
13 / 14 |
CRAP | |
0.00% |
0 / 1 |
NormalDistribution | |
94.12% |
16 / 17 |
|
92.86% |
13 / 14 |
14.04 | |
0.00% |
0 / 1 |
getSampleSizeFromPopulation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSampleSizeFromInfinitePopulation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPdf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCdf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getICdf | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMean | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMedian | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getVariance | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStandardDeviation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMgf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSkewness | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFisherInformation | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getExKurtosis | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Math\Stochastic\Distribution |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Math\Stochastic\Distribution; |
16 | |
17 | use phpOMS\Math\Functions\Functions; |
18 | |
19 | /** |
20 | * Normal distribution. |
21 | * |
22 | * @package phpOMS\Math\Stochastic\Distribution |
23 | * @license OMS License 2.0 |
24 | * @link https://jingga.app |
25 | * @since 1.0.0 |
26 | */ |
27 | final class NormalDistribution |
28 | { |
29 | /** |
30 | * Normal table. |
31 | * |
32 | * Z-Score. |
33 | * |
34 | * @var array<string, float> |
35 | * @since 1.0.0 |
36 | */ |
37 | public const TABLE = [ |
38 | '0.50' => 0.67, '0.55' => 0.76, '0.60' => 0.84, '0.65' => 0.93, '0.70' => 1.04, '0.75' => 1.15, '0.80' => 1.28, |
39 | '0.85' => 1.44, '0.90' => 1.64, '0.95' => 1.96, '0.96' => 2.05, '0.97' => 2.17, '0.98' => 2.33, '0.99' => 2.58, |
40 | ]; |
41 | |
42 | /** |
43 | * Calculate the sample size |
44 | * |
45 | * @param float $zScore Z-Score |
46 | * @param float $errorMargin Error margin |
47 | * @param int $populationSize Population size |
48 | * @param float $populationProportion Proportion of the population (percentage) |
49 | * |
50 | * @return float |
51 | * |
52 | * @since 1.0.0 |
53 | */ |
54 | public static function getSampleSizeFromPopulation(float $zScore, float $errorMargin, int $populationSize, float $populationProportion) : float |
55 | { |
56 | return self::getSampleSizeFromInfinitePopulation($zScore, $errorMargin, $populationProportion) / (1 + $zScore ** 2 * $populationProportion * (1 - $populationProportion) / ($errorMargin ** 2 * $populationSize)); |
57 | } |
58 | |
59 | /** |
60 | * Calculate the sample size |
61 | * |
62 | * @param float $zScore Z-Score |
63 | * @param float $errorMargin Error margin |
64 | * @param float $populationProportion Proportion of the population (percentage) |
65 | * |
66 | * @return float |
67 | * |
68 | * @since 1.0.0 |
69 | */ |
70 | public static function getSampleSizeFromInfinitePopulation(float $zScore, float $errorMargin, float $populationProportion) : float |
71 | { |
72 | return ($zScore ** 2) * $populationProportion * (1 - $populationProportion) / ($errorMargin ** 2); |
73 | } |
74 | |
75 | /** |
76 | * Get probability density function. |
77 | * |
78 | * @param float $x Value x |
79 | * @param float $mu Mean |
80 | * @param float $sig Sigma |
81 | * |
82 | * @return float |
83 | * |
84 | * @since 1.0.0 |
85 | */ |
86 | public static function getPdf(float $x, float $mu, float $sig) : float |
87 | { |
88 | return 1 / ($sig * \sqrt(2 * \M_PI)) * \exp(-($x - $mu) ** 2 / (2 * $sig ** 2)); |
89 | } |
90 | |
91 | /** |
92 | * Get cumulative distribution function. |
93 | * |
94 | * @param float $x Value x |
95 | * @param float $mu Mean |
96 | * @param float $sig Sigma |
97 | * |
98 | * @return float |
99 | * |
100 | * @since 1.0.0 |
101 | */ |
102 | public static function getCdf(float $x, float $mu, float $sig) : float |
103 | { |
104 | return 0.5 * (1 + Functions::getErf(($x - $mu) / ($sig * \sqrt(2)))); |
105 | } |
106 | |
107 | /** |
108 | * Inverse cumulative distribution function / AKA Quantile function / PPF |
109 | * |
110 | * @param float $x Value x |
111 | * @param float $mu Mean |
112 | * @param float $sig Sigma |
113 | * |
114 | * @return float |
115 | * |
116 | * @since 1.0.0 |
117 | */ |
118 | public static function getICdf(float $x, float $mu, float $sig) : float |
119 | { |
120 | return $mu - $sig * \sqrt(2) * Functions::getInvErfc(2 * $x); |
121 | } |
122 | |
123 | /** |
124 | * Get mode. |
125 | * |
126 | * @param float $mu Mean |
127 | * |
128 | * @return float |
129 | * |
130 | * @since 1.0.0 |
131 | */ |
132 | public static function getMode(float $mu) : float |
133 | { |
134 | return $mu; |
135 | } |
136 | |
137 | /** |
138 | * Get expected value. |
139 | * |
140 | * @param float $mu Mean |
141 | * |
142 | * @return float |
143 | * |
144 | * @since 1.0.0 |
145 | */ |
146 | public static function getMean(float $mu) : float |
147 | { |
148 | return $mu; |
149 | } |
150 | |
151 | /** |
152 | * Get median. |
153 | * |
154 | * @param float $mu Mean |
155 | * |
156 | * @return float |
157 | * |
158 | * @since 1.0.0 |
159 | */ |
160 | public static function getMedian(float $mu) : float |
161 | { |
162 | return $mu; |
163 | } |
164 | |
165 | /** |
166 | * Get variance. |
167 | * |
168 | * @param float $sig Sigma |
169 | * |
170 | * @return float |
171 | * |
172 | * @since 1.0.0 |
173 | */ |
174 | public static function getVariance(float $sig) : float |
175 | { |
176 | return $sig ** 2; |
177 | } |
178 | |
179 | /** |
180 | * Get standard deviation. |
181 | * |
182 | * @param float $sig Sigma |
183 | * |
184 | * @return float |
185 | * |
186 | * @since 1.0.0 |
187 | */ |
188 | public static function getStandardDeviation(float $sig) : float |
189 | { |
190 | return $sig; |
191 | } |
192 | |
193 | /** |
194 | * Get moment generating function. |
195 | * |
196 | * @param float $t Value t |
197 | * @param float $mu Mean |
198 | * @param float $sig Sigma |
199 | * |
200 | * @return float |
201 | * |
202 | * @since 1.0.0 |
203 | */ |
204 | public static function getMgf(float $t, float $mu, float $sig) : float |
205 | { |
206 | return \exp($mu * $t + ($sig ** 2 * $t ** 2) / 2); |
207 | } |
208 | |
209 | /** |
210 | * Get skewness. |
211 | * |
212 | * @return float |
213 | * |
214 | * @since 1.0.0 |
215 | */ |
216 | public static function getSkewness() : float |
217 | { |
218 | return 0; |
219 | } |
220 | |
221 | /** |
222 | * Get Fisher information. |
223 | * |
224 | * @param float $sig Sigma |
225 | * |
226 | * @return array |
227 | * |
228 | * @since 1.0.0 |
229 | */ |
230 | public static function getFisherInformation(float $sig) : array |
231 | { |
232 | return [ |
233 | [1 / $sig ** 2, 0], |
234 | [0, 1 / (2 * $sig ** 4)], |
235 | ]; |
236 | } |
237 | |
238 | /** |
239 | * Get Ex. kurtosis. |
240 | * |
241 | * @return float |
242 | * |
243 | * @since 1.0.0 |
244 | */ |
245 | public static function getExKurtosis() : float |
246 | { |
247 | return 0; |
248 | } |
249 | } |