Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Correlation
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 bravaisPersonCorrelationCoefficientPopulation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 bravaisPersonCorrelationCoefficientSample
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 autocorrelationCoefficient
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 boxPierceTest
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 ljungBoxTest
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\Statistic
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\Statistic;
16
17/**
18 * Correlation.
19 *
20 * @package phpOMS\Math\Statistic
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 */
25final class Correlation
26{
27    /**
28     * Constructor.
29     *
30     * @since 1.0.0
31     * @codeCoverageIgnore
32     */
33    private function __construct()
34    {
35    }
36
37    /**
38     * Calculage bravais person correlation coefficient.
39     *
40     * Example: ([4, 5, 9, 1, 3], [4, 5, 9, 1, 3])
41     *
42     * @latex \rho_{XY} = \frac{cov(X, Y)}{\sigma_X \sigma_Y}
43     *
44     * @param array<int|float> $x Values
45     * @param array<int|float> $y Values
46     *
47     * @return float
48     *
49     * @since 1.0.0
50     */
51    public static function bravaisPersonCorrelationCoefficientPopulation(array $x, array $y) : float
52    {
53        return MeasureOfDispersion::empiricalCovariance($x, $y) / (MeasureOfDispersion::standardDeviationPopulation($x) * MeasureOfDispersion::standardDeviationPopulation($y));
54    }
55
56    /**
57     * Calculage bravais person correlation coefficient.
58     *
59     * Example: ([4, 5, 9, 1, 3], [4, 5, 9, 1, 3])
60     *
61     * @latex \rho_{XY} = \frac{cov(X, Y)}{\sigma_X \sigma_Y}
62     *
63     * @param array<int|float> $x Values
64     * @param array<int|float> $y Values
65     *
66     * @return float
67     *
68     * @since 1.0.0
69     */
70    public static function bravaisPersonCorrelationCoefficientSample(array $x, array $y) : float
71    {
72        return MeasureOfDispersion::sampleCovariance($x, $y) / (MeasureOfDispersion::standardDeviationSample($x) * MeasureOfDispersion::standardDeviationSample($y));
73    }
74
75    /**
76     * Get the autocorrelation coefficient (ACF).
77     *
78     * @param array<int|float> $x Dataset
79     * @param int              $k k-th coefficient
80     *
81     * @return float
82     *
83     * @since 1.0.0
84     */
85    public static function autocorrelationCoefficient(array $x, int $k = 0) : float
86    {
87        $squaredMeanDeviation = MeasureOfDispersion::squaredMeanDeviation($x);
88        $mean                 = Average::arithmeticMean($x);
89        $count                = \count($x);
90        $sum                  = 0.0;
91
92        for ($i = $k; $i < $count; ++$i) {
93            $sum += ($x[$i] - $mean) * ($x[$i - $k] - $mean);
94        }
95
96        return $sum / ($squaredMeanDeviation * $count);
97    }
98
99    /**
100     * Box Pierce test (portmanteau test).
101     *
102     * @param float[] $autocorrelations Autocorrelations
103     * @param int     $h                Maximum leg considered
104     * @param int     $n                Amount of observations
105     *
106     * @return float
107     *
108     * @since 1.0.0
109     */
110    public static function boxPierceTest(array $autocorrelations, int $h, int $n) : float
111    {
112        $sum = 0;
113        for ($i = 0; $i < $h; ++$i) {
114            $sum += $autocorrelations[$i] ** 2;
115        }
116
117        return $n * $sum;
118    }
119
120    /**
121     * Ljung Box test (portmanteau test).
122     *
123     * @param float[] $autocorrelations Autocorrelations
124     * @param int     $h                Maximum leg considered
125     * @param int     $n                Amount of observations
126     *
127     * @return float
128     *
129     * @since 1.0.0
130     */
131    public static function ljungBoxTest(array $autocorrelations, int $h, int $n) : float
132    {
133        $sum = 0;
134
135        for ($i = 0; $i < $h; ++$i) {
136            $sum += 1 / ($n - ($i + 1)) * $autocorrelations[$i] ** 2;
137        }
138
139        return $n * ($n + 2) * $sum;
140    }
141}