Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
UniformDistributionDiscrete
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
9 / 9
11
100.00% covered (success)
100.00%
1 / 1
 getPmf
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCdf
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getMgf
100.00% covered (success)
100.00%
2 / 2
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%
2 / 2
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
 getMean
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
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 * Uniform (discrete) 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 UniformDistributionDiscrete
26{
27    /**
28     * Get probability mass function.
29     *
30     * @param float $a Value a
31     * @param float $b Value b
32     *
33     * @return float
34     *
35     * @since 1.0.0
36     */
37    public static function getPmf(float $a, float $b) : float
38    {
39        return 1 / ($b - $a + 1);
40    }
41
42    /**
43     * Get cumulative distribution function.
44     *
45     * @param float $k Value k element of [a, b]
46     * @param float $a Value a
47     * @param float $b Value b
48     *
49     * @return float
50     *
51     * @throws \OutOfBoundsException
52     *
53     * @since 1.0.0
54     */
55    public static function getCdf(float $k, float $a, float $b) : float
56    {
57        if ($k > $b || $k < $a) {
58            throw new \OutOfBoundsException('Out of bounds');
59        }
60
61        return (\floor($k) - $a + 1) / ($b - $a + 1);
62    }
63
64    /**
65     * Get moment generating function.
66     *
67     * @param int   $t Value t
68     * @param float $a Value a
69     * @param float $b Value b
70     *
71     * @return float
72     *
73     * @since 1.0.0
74     */
75    public static function getMgf(int $t, float $a, float $b) : float
76    {
77        return (\exp($a * $t) - \exp(($b + 1) * $t))
78            / (($b - $a + 1) * (1 - \exp($t)));
79    }
80
81    /**
82     * Get skewness.
83     *
84     * @return float
85     *
86     * @since 1.0.0
87     */
88    public static function getSkewness() : float
89    {
90        return 0.0;
91    }
92
93    /**
94     * Get Ex. kurtosis.
95     *
96     * @param float $a Value a
97     * @param float $b Value b
98     *
99     * @return float
100     *
101     * @since 1.0.0
102     */
103    public static function getExKurtosis(float $a, float $b) : float
104    {
105        $n = ($b - $a + 1);
106
107        return -6 / 5 * ($n ** 2 + 1) / ($n ** 2 - 1);
108    }
109
110    /**
111     * Get median.
112     *
113     * @param float $a Value a
114     * @param float $b Value b
115     *
116     * @return float
117     *
118     * @since 1.0.0
119     */
120    public static function getMedian(float $a, float $b) : float
121    {
122        return ($a + $b) / 2;
123    }
124
125    /**
126     * Get expected value.
127     *
128     * @param float $a Value a
129     * @param float $b Value b
130     *
131     * @return float
132     *
133     * @since 1.0.0
134     */
135    public static function getMean(float $a, float $b) : float
136    {
137        return ($a + $b) / 2;
138    }
139
140    /**
141     * Get variance.
142     *
143     * @param float $a Value a
144     * @param float $b Value b
145     *
146     * @return float
147     *
148     * @since 1.0.0
149     */
150    public static function getVariance(float $a, float $b) : float
151    {
152        return (($b - $a + 1) ** 2 - 1) / 12;
153    }
154
155    /**
156     * Get standard deviation.
157     *
158     * @param float $a Value a
159     * @param float $b Value b
160     *
161     * @return float
162     *
163     * @since 1.0.0
164     */
165    public static function getStandardDeviation(float $a, float $b) : float
166    {
167        return \sqrt(self::getVariance($a, $b));
168    }
169}