Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| BinomialDistribution | |
100.00% |
14 / 14 |
|
100.00% |
11 / 11 |
12 | |
100.00% |
1 / 1 |
| getMode | |
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% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getExKurtosis | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCdf | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getPmf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMedian | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMean | |
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 | |||
| 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 | * Binomial 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 BinomialDistribution |
| 28 | { |
| 29 | /** |
| 30 | * Get mode. |
| 31 | * |
| 32 | * @param int $n Value n |
| 33 | * @param float $p Value p |
| 34 | * |
| 35 | * @return float |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public static function getMode(int $n, float $p) : float |
| 40 | { |
| 41 | return \floor(($n + 1) * $p); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get moment generating function. |
| 46 | * |
| 47 | * @param int $n Value n |
| 48 | * @param float $t Value t |
| 49 | * @param float $p Value p |
| 50 | * |
| 51 | * @return float |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | public static function getMgf(int $n, float $t, float $p) : float |
| 56 | { |
| 57 | return \pow(1 - $p + $p * \exp($t), $n); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get skewness. |
| 62 | * |
| 63 | * @param int $n Value n |
| 64 | * @param float $p Value p |
| 65 | * |
| 66 | * @return float |
| 67 | * |
| 68 | * @since 1.0.0 |
| 69 | */ |
| 70 | public static function getSkewness(int $n, float $p) : float |
| 71 | { |
| 72 | return (1 - 2 * $p) / \sqrt($n * $p * (1 - $p)); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get Fisher information. |
| 77 | * |
| 78 | * @param int $n Value n |
| 79 | * @param float $p Value p |
| 80 | * |
| 81 | * @return float |
| 82 | * |
| 83 | * @since 1.0.0 |
| 84 | */ |
| 85 | public static function getFisherInformation(int $n, float $p) : float |
| 86 | { |
| 87 | return $n / ($p * (1 - $p)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get Ex. kurtosis. |
| 92 | * |
| 93 | * @param int $n Value n |
| 94 | * @param float $p Value p |
| 95 | * |
| 96 | * @return float |
| 97 | * |
| 98 | * @since 1.0.0 |
| 99 | */ |
| 100 | public static function getExKurtosis(int $n, float $p) : float |
| 101 | { |
| 102 | return (1 - 6 * $p * (1 - $p)) / ($n * $p * (1 - $p)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get cumulative distribution function. |
| 107 | * |
| 108 | * @param int $n Value n |
| 109 | * @param int $x Value x |
| 110 | * @param float $p Value p |
| 111 | * |
| 112 | * @return float |
| 113 | * |
| 114 | * @since 1.0.0 |
| 115 | */ |
| 116 | public static function getCdf(int $n, int $x, float $p) : float |
| 117 | { |
| 118 | $sum = 0.0; |
| 119 | |
| 120 | for ($i = 1; $i < $x; ++$i) { |
| 121 | $sum += self::getPmf($n, $i, $p); |
| 122 | } |
| 123 | |
| 124 | return $sum; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get probability mass function. |
| 129 | * |
| 130 | * Formula: C(n, k) * p^k * (1-p)^(n-k) |
| 131 | * |
| 132 | * @param int $n Value n |
| 133 | * @param int $k Value k |
| 134 | * @param float $p Value p |
| 135 | * |
| 136 | * @return float |
| 137 | * |
| 138 | * @since 1.0.0 |
| 139 | */ |
| 140 | public static function getPmf(int $n, int $k, float $p) : float |
| 141 | { |
| 142 | return Functions::binomialCoefficient($n, $k) * \pow($p, $k) * \pow(1 - $p, $n - $k); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get median. |
| 147 | * |
| 148 | * @param int $n Value n |
| 149 | * @param float $p Value p |
| 150 | * |
| 151 | * @return float |
| 152 | * |
| 153 | * @since 1.0.0 |
| 154 | */ |
| 155 | public static function getMedian(int $n, float $p) : float |
| 156 | { |
| 157 | return \floor($n * $p); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get expected value. |
| 162 | * |
| 163 | * @param int $n Value n |
| 164 | * @param float $p Value p |
| 165 | * |
| 166 | * @return float |
| 167 | * |
| 168 | * @since 1.0.0 |
| 169 | */ |
| 170 | public static function getMean(int $n, float $p) : float |
| 171 | { |
| 172 | return $n * $p; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get variance. |
| 177 | * |
| 178 | * @param int $n Value n |
| 179 | * @param float $p Value p |
| 180 | * |
| 181 | * @return float |
| 182 | * |
| 183 | * @since 1.0.0 |
| 184 | */ |
| 185 | public static function getVariance(int $n, float $p) : float |
| 186 | { |
| 187 | return $n * $p * (1 - $p); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get standard deviation. |
| 192 | * |
| 193 | * @param int $n Value n |
| 194 | * @param float $p Value p |
| 195 | * |
| 196 | * @return float |
| 197 | * |
| 198 | * @since 1.0.0 |
| 199 | */ |
| 200 | public static function getStandardDeviation(int $n, float $p) : float |
| 201 | { |
| 202 | return \sqrt(self::getVariance($n, $p)); |
| 203 | } |
| 204 | } |