Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| HypergeometricDistribution | |
100.00% |
17 / 17 |
|
100.00% |
8 / 8 |
8 | |
100.00% |
1 / 1 |
| getPmf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMean | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMode | |
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 | |||
| getSkewness | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getExKurtosis | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getCdf | |
100.00% |
8 / 8 |
|
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 | * Hypergeometric 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 HypergeometricDistribution |
| 28 | { |
| 29 | /** |
| 30 | * Get probability mass function. |
| 31 | * |
| 32 | * @param int $K Successful states in the population |
| 33 | * @param int $N Population size |
| 34 | * @param int $k Observed successes |
| 35 | * @param int $n Number of draws |
| 36 | * |
| 37 | * @return float |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | public static function getPmf(int $K, int $N, int $k, int $n) : float |
| 42 | { |
| 43 | return Functions::binomialCoefficient($K, $k) * Functions::binomialCoefficient($N - $K, $n - $k) / Functions::binomialCoefficient($N, $n); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get expected value. |
| 48 | * |
| 49 | * @param int $K Successful states in the population |
| 50 | * @param int $N Population size |
| 51 | * @param int $n Number of draws |
| 52 | * |
| 53 | * @return float |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | public static function getMean(int $K, int $N, int $n) : float |
| 58 | { |
| 59 | return $n * $K / $N; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get mode. |
| 64 | * |
| 65 | * @param int $K Successful states in the population |
| 66 | * @param int $N Population size |
| 67 | * @param int $n Number of draws |
| 68 | * |
| 69 | * @return int |
| 70 | * |
| 71 | * @since 1.0.0 |
| 72 | */ |
| 73 | public static function getMode(int $K, int $N, int $n) : int |
| 74 | { |
| 75 | return (int) (($n + 1) * ($K + 1) / ($N + 2)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get variance. |
| 80 | * |
| 81 | * @param int $K Successful states in the population |
| 82 | * @param int $N Population size |
| 83 | * @param int $n Number of draws |
| 84 | * |
| 85 | * @return float |
| 86 | * |
| 87 | * @since 1.0.0 |
| 88 | */ |
| 89 | public static function getVariance(int $K, int $N, int $n) : float |
| 90 | { |
| 91 | return $n * $K / $N * ($N - $K) / $N * ($N - $n) / ($N - 1); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get standard deviation. |
| 96 | * |
| 97 | * @param int $K Successful states in the population |
| 98 | * @param int $N Population size |
| 99 | * @param int $n Number of draws |
| 100 | * |
| 101 | * @return float |
| 102 | * |
| 103 | * @since 1.0.0 |
| 104 | */ |
| 105 | public static function getStandardDeviation(int $K, int $N, int $n) : float |
| 106 | { |
| 107 | return \sqrt($n * $K / $N * ($N - $K) / $N * ($N - $n) / ($N - 1)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get skewness. |
| 112 | * |
| 113 | * @param int $K Successful states in the population |
| 114 | * @param int $N Population size |
| 115 | * @param int $n Number of draws |
| 116 | * |
| 117 | * @return float |
| 118 | * |
| 119 | * @since 1.0.0 |
| 120 | */ |
| 121 | public static function getSkewness(int $K, int $N, int $n) : float |
| 122 | { |
| 123 | return ($N - 2 * $K) * \sqrt($N - 1) * ($N - 2 * $n) |
| 124 | / (\sqrt($n * $K * ($N - $K) * ($N - $n)) * ($N - 2)); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get Ex. kurtosis. |
| 129 | * |
| 130 | * @param int $K Successful states in the population |
| 131 | * @param int $N Population size |
| 132 | * @param int $n Number of draws |
| 133 | * |
| 134 | * @return float |
| 135 | * |
| 136 | * @since 1.0.0 |
| 137 | */ |
| 138 | public static function getExKurtosis(int $K, int $N, int $n) : float |
| 139 | { |
| 140 | return (($N - 1) * $N ** 2 * ($N * ($N + 1) - 6 * $K * ($N - $K) - 6 * $n * ($N - $n)) + 6 * $n * $K * ($N - $K) * ($N - $n) * (5 * $N - 6)) |
| 141 | / ($n * $K * ($N - $K) * ($N - $n) * ($N - 2) * ($N - 3)); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get cumulative distribution function. |
| 146 | * |
| 147 | * @param int $K Successful states in the population |
| 148 | * @param int $N Population size |
| 149 | * @param int $k Observed successes |
| 150 | * @param int $n Number of draws |
| 151 | * |
| 152 | * @return float |
| 153 | * |
| 154 | * @since 1.0.0 |
| 155 | */ |
| 156 | public static function getCdf(int $K, int $N, int $k, int $n) : float |
| 157 | { |
| 158 | return 1 - Functions::binomialCoefficient($n, $k + 1) |
| 159 | * Functions::binomialCoefficient($N - $n, $K - $k - 1) |
| 160 | / Functions::binomialCoefficient($N, $K) |
| 161 | * Functions::generalizedHypergeometricFunction( |
| 162 | [1, $k + 1 - $K, $k + 1 - $n], |
| 163 | [$k + 2, $N + $k + 2 - $K - $n], |
| 164 | 1 |
| 165 | ); |
| 166 | } |
| 167 | } |