Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
FDistribution
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
7 / 7
12
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
 getCdf
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMean
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getMode
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getVariance
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 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%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
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\Beta;
18
19/**
20 * F 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 FDistribution
28{
29    /**
30     * Get probability density function.
31     *
32     * @param float $x  Value x
33     * @param int   $d1 Degreegs of freedom
34     * @param int   $d2 Degreegs of freedom
35     *
36     * @return float
37     *
38     * @since 1.0.0
39     */
40    public static function getPdf(float $x, int $d1, int $d2) : float
41    {
42        return \sqrt((\pow($d1 * $x, $d1) * ($d2 ** $d2)) / \pow($d1 * $x + $d2, $d1 + $d2))
43            / ($x * Beta::beta($d1 / 2, $d2 / 2));
44    }
45
46    /**
47     * Get cumulative density function.
48     *
49     * @param float $x  Value x
50     * @param int   $d1 Degreegs of freedom
51     * @param int   $d2 Degreegs of freedom
52     *
53     * @return float
54     *
55     * @since 1.0.0
56     */
57    public static function getCdf(float $x, int $d1, int $d2) : float
58    {
59        return Beta::regularizedBeta($d1 * $x / ($d1 * $x + $d2), $d1 / 2, $d2 / 2);
60    }
61
62    /**
63     * Get expected value.
64     *
65     * @param int $d2 Degree of freedom
66     *
67     * @return float
68     *
69     * @since 1.0.0
70     */
71    public static function getMean(int $d2) : float
72    {
73        if ($d2 === 2) {
74            return 0.0;
75        }
76
77        return $d2 / ($d2 - 2);
78    }
79
80    /**
81     * Get mode.
82     *
83     * @param int $d1 Degree of freedom
84     * @param int $d2 Degree of freedom
85     *
86     * @return float
87     *
88     * @since 1.0.0
89     */
90    public static function getMode(int $d1, int $d2) : float
91    {
92        if ($d1 === 0) {
93            return 0.0;
94        }
95
96        return ($d1 - 2) / $d1 * $d2 / ($d2 + 2);
97    }
98
99    /**
100     * Get variance.
101     *
102     * @param int $d1 Degree of freedom
103     * @param int $d2 Degree of freedom
104     *
105     * @return float
106     *
107     * @since 1.0.0
108     */
109    public static function getVariance(int $d1, int $d2) : float
110    {
111        if ($d2 === 2 || $d2 === 4) {
112            return 0.0;
113        }
114
115        return 2 * $d2 ** 2 * ($d1 + $d2 - 2)
116            / ($d1 * ($d2 - 2) ** 2 * ($d2 - 4));
117    }
118
119    /**
120     * Get standard deviation.
121     *
122     * @param int $d1 Degree of freedom
123     * @param int $d2 Degree of freedom
124     *
125     * @return float
126     *
127     * @since 1.0.0
128     */
129    public static function getStandardDeviation(int $d1, int $d2) : float
130    {
131        return \sqrt(self::getVariance($d1, $d2));
132    }
133
134    /**
135     * Get skewness.
136     *
137     * @param int $d1 Degree of freedom
138     * @param int $d2 Degree of freedom
139     *
140     * @return float
141     *
142     * @since 1.0.0
143     */
144    public static function getSkewness(int $d1, int $d2) : float
145    {
146        if ($d2 < 7) {
147            return 0.0;
148        }
149
150        return (2 * $d1 + $d2 - 2) * \sqrt(8 * ($d2 - 4))
151            / (($d2 - 6) * \sqrt($d1 * ($d1 + $d2 - 2)));
152    }
153}