Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| WeibullDistribution | |
100.00% |
17 / 17 |
|
100.00% |
9 / 9 |
11 | |
100.00% |
1 / 1 |
| getPdf | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getCdf | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | |||
| getMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSkewness | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getEntropy | |
100.00% |
2 / 2 |
|
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\Gamma; |
| 18 | |
| 19 | /** |
| 20 | * Weibull 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 WeibullDistribution |
| 28 | { |
| 29 | /** |
| 30 | * Get probability density function. |
| 31 | * |
| 32 | * @param float $x Value x |
| 33 | * @param float $lambda Scale lambda |
| 34 | * @param float $k Shape k |
| 35 | * |
| 36 | * @return float |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | public static function getPdf(float $x, float $lambda, float $k) : float |
| 41 | { |
| 42 | return $x < 0.0 |
| 43 | ? 0.0 |
| 44 | : $k / $lambda * \pow($x / $lambda, $k - 1) * \exp(-($x / $lambda) ** $k); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get cumulative distribution function. |
| 49 | * |
| 50 | * @param float $x Value x |
| 51 | * @param float $lambda Scale lambda |
| 52 | * @param float $k Shape k |
| 53 | * |
| 54 | * @return float |
| 55 | * |
| 56 | * @since 1.0.0 |
| 57 | */ |
| 58 | public static function getCdf(float $x, float $lambda, float $k) : float |
| 59 | { |
| 60 | return $x < 0.0 |
| 61 | ? 0.0 |
| 62 | : 1 - \exp(-($x / $lambda) ** $k); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get mean. |
| 67 | * |
| 68 | * @param float $lambda Scale lambda |
| 69 | * @param float $k Shape k |
| 70 | * |
| 71 | * @return float |
| 72 | * |
| 73 | * @since 1.0.0 |
| 74 | */ |
| 75 | public static function getMean(float $lambda, float $k) : float |
| 76 | { |
| 77 | return $lambda * Gamma::gamma(1 + 1 / $k); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get median. |
| 82 | * |
| 83 | * @param float $lambda Scale lambda |
| 84 | * @param float $k Shape k |
| 85 | * |
| 86 | * @return float |
| 87 | * |
| 88 | * @since 1.0.0 |
| 89 | */ |
| 90 | public static function getMedian(float $lambda, float $k) : float |
| 91 | { |
| 92 | return $lambda * \pow(\log(2), 1 / $k); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the variance. |
| 97 | * |
| 98 | * @param float $lambda Scale lambda |
| 99 | * @param float $k Shape k |
| 100 | * |
| 101 | * @return float |
| 102 | * |
| 103 | * @since 1.0.0 |
| 104 | */ |
| 105 | public static function getVariance(float $lambda, float $k) : float |
| 106 | { |
| 107 | return $lambda ** 2 * (Gamma::gamma(1 + 2 / $k) - Gamma::gamma(1 + 1 / $k) ** 2); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the standard deviation. |
| 112 | * |
| 113 | * @param float $lambda Scale lambda |
| 114 | * @param float $k Shape k |
| 115 | * |
| 116 | * @return float |
| 117 | * |
| 118 | * @since 1.0.0 |
| 119 | */ |
| 120 | public static function getStandardDeviation(float $lambda, float $k) : float |
| 121 | { |
| 122 | return \sqrt(self::getVariance($lambda, $k)); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get mode. |
| 127 | * |
| 128 | * @param float $lambda Scale lambda |
| 129 | * @param float $k Shape k |
| 130 | * |
| 131 | * @return float |
| 132 | * |
| 133 | * @since 1.0.0 |
| 134 | */ |
| 135 | public static function getMode(float $lambda, float $k) : float |
| 136 | { |
| 137 | return $lambda * \pow(($k - 1) / $k, 1 / $k); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get skewness. |
| 142 | * |
| 143 | * @param float $lambda Scale lambda |
| 144 | * @param float $k Shape k |
| 145 | * |
| 146 | * @return float |
| 147 | * |
| 148 | * @since 1.0.0 |
| 149 | */ |
| 150 | public static function getSkewness(float $lambda, float $k) : float |
| 151 | { |
| 152 | $mu = self::getMean($lambda, $k); |
| 153 | $sig = self::getStandardDeviation($lambda, $k); |
| 154 | |
| 155 | return (Gamma::gamma(1 + 3 / $k) * $lambda ** 3 - 3 * $mu * $sig ** 2 - $mu ** 3) |
| 156 | / ($sig ** 3); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get entropy. |
| 161 | * |
| 162 | * @param float $lambda Scale lambda |
| 163 | * @param float $k Shape k |
| 164 | * |
| 165 | * @return float |
| 166 | * |
| 167 | * @since 1.0.0 |
| 168 | */ |
| 169 | public static function getEntropy(float $lambda, float $k) : float |
| 170 | { |
| 171 | $gamma = 0.57721566490153286060651209008240243104215933593992; |
| 172 | |
| 173 | return $gamma * (1 - 1 / $k) + \log($lambda / $k) + 1; |
| 174 | } |
| 175 | } |