Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
BernoulliDistribution
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
12 / 12
20
100.00% covered (success)
100.00%
1 / 1
 getPmf
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getCdf
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getMode
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 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%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 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
 getMgf
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
 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%
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
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
17/**
18 * Bernulli 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 */
25final class BernoulliDistribution
26{
27    /**
28     * Get probability mass function.
29     *
30     * @param float $p Value p
31     * @param int   $k Value k
32     *
33     * @return float
34     *
35     * @throws \InvalidArgumentException
36     *
37     * @since 1.0.0
38     */
39    public static function getPmf(float $p, int $k) : float
40    {
41        if ($k === 0) {
42            return 1 - $p;
43        } elseif ($k === 1) {
44            return $p;
45        }
46
47        throw new \InvalidArgumentException('k needs to be 0 or 1');
48    }
49
50    /**
51     * Get cumulative distribution function.
52     *
53     * @param float $p Value p
54     * @param float $k Value k
55     *
56     * @return float
57     *
58     * @since 1.0.0
59     */
60    public static function getCdf(float $p, float $k) : float
61    {
62        if ($k < 0) {
63            return 0;
64        } elseif ($k >= 1) {
65            return 1;
66        }
67
68        return 1 - $p;
69    }
70
71    /**
72     * Get mode.
73     *
74     * @param float $p Value p
75     *
76     * @return int
77     *
78     * @since 1.0.0
79     */
80    public static function getMode(float $p) : int
81    {
82        if ($p === 0.5) {
83            return 0;
84        } elseif ($p > 0.5) {
85            return 1;
86        }
87
88        return 0;
89    }
90
91    /**
92     * Get expected value.
93     *
94     * @param float $p Value p
95     *
96     * @return float
97     *
98     * @since 1.0.0
99     */
100    public static function getMean(float $p) : float
101    {
102        return $p;
103    }
104
105    /**
106     * Get median.
107     *
108     * @param float $p Value p
109     *
110     * @return float
111     *
112     * @since 1.0.0
113     */
114    public static function getMedian(float $p) : float
115    {
116        if ($p === 0.5) {
117            return 0.5;
118        } elseif ($p > 0.5) {
119            return 1;
120        }
121
122        return 0.0;
123    }
124
125    /**
126     * Get variance.
127     *
128     * @param float $p Value p
129     *
130     * @return float
131     *
132     * @since 1.0.0
133     */
134    public static function getVariance(float $p) : float
135    {
136        return $p * (1 - $p);
137    }
138
139    /**
140     * Get standard deviation.
141     *
142     * @param float $p Value p
143     *
144     * @return float
145     *
146     * @since 1.0.0
147     */
148    public static function getStandardDeviation(float $p) : float
149    {
150        return \sqrt(self::getVariance($p));
151    }
152
153    /**
154     * Get moment generating function.
155     *
156     * @param float $p Value p
157     * @param float $t Value t
158     *
159     * @return float
160     *
161     * @since 1.0.0
162     */
163    public static function getMgf(float $p, float $t) : float
164    {
165        return (1 - $p) + $p * \exp($t);
166    }
167
168    /**
169     * Get skewness.
170     *
171     * @param float $p Value p
172     *
173     * @return float
174     *
175     * @since 1.0.0
176     */
177    public static function getSkewness(float $p) : float
178    {
179        return (1 - 2 * $p) / \sqrt($p * (1 - $p));
180    }
181
182    /**
183     * Get entropy.
184     *
185     * @param float $p Value p
186     *
187     * @return float
188     *
189     * @since 1.0.0
190     */
191    public static function getEntropy(float $p) : float
192    {
193        return -(1 - $p) * \log(1 - $p) - $p * \log($p);
194    }
195
196    /**
197     * Get Fisher information.
198     *
199     * @param float $p Value p
200     *
201     * @return float
202     *
203     * @since 1.0.0
204     */
205    public static function getFisherInformation(float $p) : float
206    {
207        return 1 / ($p * (1 - $p));
208    }
209
210    /**
211     * Get Ex. kurtosis.
212     *
213     * @param float $p Value p
214     *
215     * @return float
216     *
217     * @since 1.0.0
218     */
219    public static function getExKurtosis(float $p) : float
220    {
221        return (1 - 6 * $p * (1 - $p)) / ($p * (1 - $p));
222    }
223}