Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| LogDistribution | |
100.00% |
8 / 8 |
|
100.00% |
7 / 7 |
7 | |
100.00% |
1 / 1 |
| getPmf | |
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 |
1 | |||
| getMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getVariance | |
100.00% |
2 / 2 |
|
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 | |||
| 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 | |
| 19 | /** |
| 20 | * Log 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 LogDistribution |
| 28 | { |
| 29 | /** |
| 30 | * Epsilon for float comparison. |
| 31 | * |
| 32 | * @var float |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | public const EPSILON = 4.88e-04; |
| 36 | |
| 37 | /** |
| 38 | * Get probability mass function. |
| 39 | * |
| 40 | * @latex -\frac{1}{\log(1-p)} \cdot \frac{p^k}{k} |
| 41 | * |
| 42 | * @param float $p Value p |
| 43 | * @param int $k Value k |
| 44 | * |
| 45 | * @return float |
| 46 | * |
| 47 | * @since 1.0.0 |
| 48 | */ |
| 49 | public static function getPmf(float $p, int $k) : float |
| 50 | { |
| 51 | return -1 / \log(1 - $p) * $p ** $k / $k; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get cumulative distribution function. |
| 56 | * |
| 57 | * @param float $p Value p |
| 58 | * @param int $k Value k |
| 59 | * |
| 60 | * @return float |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | */ |
| 64 | public static function getCdf(float $p, int $k) : float |
| 65 | { |
| 66 | // This is a workaround! |
| 67 | // Actually 0 should be used instead of self::EPSILON. |
| 68 | // This is only used because the incomplete beta function doesn't work for p or q = 0 |
| 69 | return 1 + Beta::incompleteBeta($p, $k + 1, self::EPSILON) / \log(1 - $p); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get expected value. |
| 74 | * |
| 75 | * @latex -\frac{1}{\log(1-p)} \cdot \frac{p}{1-p} |
| 76 | * |
| 77 | * @param float $p Value p |
| 78 | * |
| 79 | * @return float |
| 80 | * |
| 81 | * @since 1.0.0 |
| 82 | */ |
| 83 | public static function getMean(float $p) : float |
| 84 | { |
| 85 | return -1 / \log(1 - $p) * $p / (1 - $p); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get mode. |
| 90 | * |
| 91 | * @return int |
| 92 | * |
| 93 | * @since 1.0.0 |
| 94 | */ |
| 95 | public static function getMode() : int |
| 96 | { |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get variance. |
| 102 | * |
| 103 | * @latex -\frac{p^2 + p\log(1-p)}{(1-p)^2\log(1-p)^2} |
| 104 | * |
| 105 | * @param float $p Value p |
| 106 | * |
| 107 | * @return float |
| 108 | * |
| 109 | * @since 1.0.0 |
| 110 | */ |
| 111 | public static function getVariance(float $p) : float |
| 112 | { |
| 113 | return -($p ** 2 + $p * \log(1 - $p)) |
| 114 | / ((1 - $p) ** 2 * \log(1 - $p) ** 2); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get standard deviation. |
| 119 | * |
| 120 | * @latex \sqrt{-\frac{p^2 + p\log(1-p)}{(1-p)^2\log(1-p)^2}} |
| 121 | * |
| 122 | * @param float $p Value p |
| 123 | * |
| 124 | * @return float |
| 125 | * |
| 126 | * @since 1.0.0 |
| 127 | */ |
| 128 | public static function getStandardDeviation(float $p) : float |
| 129 | { |
| 130 | return \sqrt(self::getVariance($p)); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get moment generating function. |
| 135 | * |
| 136 | * @latex \frac{\log(1-p\exp(t))}{\log(1-p)} |
| 137 | * |
| 138 | * @param float $p Value p |
| 139 | * @param float $t Value t |
| 140 | * |
| 141 | * @return float |
| 142 | * |
| 143 | * @since 1.0.0 |
| 144 | */ |
| 145 | public static function getMgf(float $p, float $t) : float |
| 146 | { |
| 147 | return \log(1 - $p * \exp($t)) / \log(1 - $p); |
| 148 | } |
| 149 | } |