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