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