Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| BetaDistribution | |
100.00% |
20 / 20 |
|
100.00% |
9 / 9 |
15 | |
100.00% |
1 / 1 |
| getMean | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMode | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
| getVariance | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStandardDeviation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSkewness | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getExKurtosis | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getMgf | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| getPdf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCdf | |
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\Beta; |
| 18 | use phpOMS\Math\Functions\Functions; |
| 19 | |
| 20 | /** |
| 21 | * Beta distribution. |
| 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 BetaDistribution |
| 29 | { |
| 30 | /** |
| 31 | * Get expected value. |
| 32 | * |
| 33 | * @param float $alpha Alpha |
| 34 | * @param float $beta Beta |
| 35 | * |
| 36 | * @return float |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | public static function getMean(float $alpha, float $beta) : float |
| 41 | { |
| 42 | return $alpha / ($alpha + $beta); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get mode. |
| 47 | * |
| 48 | * @param float $alpha Alpha |
| 49 | * @param float $beta Beta |
| 50 | * |
| 51 | * @return float |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | public static function getMode(float $alpha, float $beta) : float |
| 56 | { |
| 57 | if ($alpha > 1 && $beta > 1) { |
| 58 | return ($alpha - 1) / ($alpha + $beta - 2); |
| 59 | } |
| 60 | |
| 61 | if ($alpha <= 1.0 && $beta > 1) { |
| 62 | return 0.0; |
| 63 | } |
| 64 | |
| 65 | return 1.0; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get variance. |
| 70 | * |
| 71 | * @param float $alpha Alpha |
| 72 | * @param float $beta Beta |
| 73 | * |
| 74 | * @return float |
| 75 | * |
| 76 | * @since 1.0.0 |
| 77 | */ |
| 78 | public static function getVariance(float $alpha, float $beta) : float |
| 79 | { |
| 80 | return $alpha * $beta / (($alpha + $beta) ** 2 * ($alpha + $beta + 1)); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get standard deviation. |
| 85 | * |
| 86 | * @param float $alpha Alpha |
| 87 | * @param float $beta Beta |
| 88 | * |
| 89 | * @return float |
| 90 | * |
| 91 | * @since 1.0.0 |
| 92 | */ |
| 93 | public static function getStandardDeviation(float $alpha, float $beta) : float |
| 94 | { |
| 95 | return \sqrt(self::getVariance($alpha, $beta)); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get skewness. |
| 100 | * |
| 101 | * @param float $alpha Alpha |
| 102 | * @param float $beta Beta |
| 103 | * |
| 104 | * @return float |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | */ |
| 108 | public static function getSkewness(float $alpha, float $beta) : float |
| 109 | { |
| 110 | return 2 * ($beta - $alpha) * \sqrt($alpha + $beta + 1) / (($alpha + $beta + 2) * \sqrt($alpha * $beta)); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get Ex. kurtosis. |
| 115 | * |
| 116 | * @param float $alpha Alpha |
| 117 | * @param float $beta Beta |
| 118 | * |
| 119 | * @return float |
| 120 | * |
| 121 | * @since 1.0.0 |
| 122 | */ |
| 123 | public static function getExKurtosis(float $alpha, float $beta) : float |
| 124 | { |
| 125 | return 6 * (($alpha - $beta) ** 2 * ($alpha + $beta + 1) - $alpha * $beta * ($alpha + $beta + 2)) |
| 126 | / ($alpha * $beta * ($alpha + $beta + 2) * ($alpha + $beta + 3)); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get moment generating function. |
| 131 | * |
| 132 | * @param float $t Value t |
| 133 | * @param float $alpha Alpha |
| 134 | * @param float $beta Beta |
| 135 | * |
| 136 | * @return float |
| 137 | * |
| 138 | * @since 1.0.0 |
| 139 | */ |
| 140 | public static function getMgf(float $t, float $alpha, float $beta) : float |
| 141 | { |
| 142 | $sum = 0; |
| 143 | for ($k = 1; $k < 15; ++$k) { |
| 144 | $product = 1; |
| 145 | for ($r = 0; $r < $k; ++$r) { |
| 146 | $product *= ($alpha + $r) / ($alpha + $beta + $r); |
| 147 | } |
| 148 | |
| 149 | $sum += $product * $t ** $k / Functions::fact($k); |
| 150 | } |
| 151 | |
| 152 | return 1 + $sum; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get prdobability distribution function. |
| 157 | * |
| 158 | * @param float $x Value |
| 159 | * @param float $alpha Alpha |
| 160 | * @param float $beta Beta |
| 161 | * |
| 162 | * @return float |
| 163 | * |
| 164 | * @since 1.0.0 |
| 165 | */ |
| 166 | public static function getPdf(float $x, float $alpha, float $beta) : float |
| 167 | { |
| 168 | return \pow($x, $alpha - 1) * \pow(1 - $x, $beta - 1) / Beta::beta($alpha, $beta); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get cumulative distribution function. |
| 173 | * |
| 174 | * @param float $x Value |
| 175 | * @param float $alpha Alpha |
| 176 | * @param float $beta Beta |
| 177 | * |
| 178 | * @return float |
| 179 | * |
| 180 | * @since 1.0.0 |
| 181 | */ |
| 182 | public static function getCdf(float $x, float $alpha, float $beta) : float |
| 183 | { |
| 184 | return Beta::regularizedBeta($x, $alpha, $beta); |
| 185 | } |
| 186 | } |