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