1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.stat.correlation;
23
24 import org.hipparchus.UnitTestUtils;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.linear.Array2DRowRealMatrix;
27 import org.hipparchus.linear.RealMatrix;
28 import org.hipparchus.stat.descriptive.moment.Variance;
29 import org.junit.jupiter.api.Test;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.fail;
33
34
35 class CovarianceTest {
36
37 protected final double[] longleyData = new double[] {
38 60323,83.0,234289,2356,1590,107608,1947,
39 61122,88.5,259426,2325,1456,108632,1948,
40 60171,88.2,258054,3682,1616,109773,1949,
41 61187,89.5,284599,3351,1650,110929,1950,
42 63221,96.2,328975,2099,3099,112075,1951,
43 63639,98.1,346999,1932,3594,113270,1952,
44 64989,99.0,365385,1870,3547,115094,1953,
45 63761,100.0,363112,3578,3350,116219,1954,
46 66019,101.2,397469,2904,3048,117388,1955,
47 67857,104.6,419180,2822,2857,118734,1956,
48 68169,108.4,442769,2936,2798,120445,1957,
49 66513,110.8,444546,4681,2637,121950,1958,
50 68655,112.6,482704,3813,2552,123366,1959,
51 69564,114.2,502601,3931,2514,125368,1960,
52 69331,115.7,518173,4806,2572,127852,1961,
53 70551,116.9,554894,4007,2827,130081,1962
54 };
55
56 protected final double[] swissData = new double[] {
57 80.2,17.0,15,12,9.96,
58 83.1,45.1,6,9,84.84,
59 92.5,39.7,5,5,93.40,
60 85.8,36.5,12,7,33.77,
61 76.9,43.5,17,15,5.16,
62 76.1,35.3,9,7,90.57,
63 83.8,70.2,16,7,92.85,
64 92.4,67.8,14,8,97.16,
65 82.4,53.3,12,7,97.67,
66 82.9,45.2,16,13,91.38,
67 87.1,64.5,14,6,98.61,
68 64.1,62.0,21,12,8.52,
69 66.9,67.5,14,7,2.27,
70 68.9,60.7,19,12,4.43,
71 61.7,69.3,22,5,2.82,
72 68.3,72.6,18,2,24.20,
73 71.7,34.0,17,8,3.30,
74 55.7,19.4,26,28,12.11,
75 54.3,15.2,31,20,2.15,
76 65.1,73.0,19,9,2.84,
77 65.5,59.8,22,10,5.23,
78 65.0,55.1,14,3,4.52,
79 56.6,50.9,22,12,15.14,
80 57.4,54.1,20,6,4.20,
81 72.5,71.2,12,1,2.40,
82 74.2,58.1,14,8,5.23,
83 72.0,63.5,6,3,2.56,
84 60.5,60.8,16,10,7.72,
85 58.3,26.8,25,19,18.46,
86 65.4,49.5,15,8,6.10,
87 75.5,85.9,3,2,99.71,
88 69.3,84.9,7,6,99.68,
89 77.3,89.7,5,2,100.00,
90 70.5,78.2,12,6,98.96,
91 79.4,64.9,7,3,98.22,
92 65.0,75.9,9,9,99.06,
93 92.2,84.6,3,3,99.46,
94 79.3,63.1,13,13,96.83,
95 70.4,38.4,26,12,5.62,
96 65.7,7.7,29,11,13.79,
97 72.7,16.7,22,13,11.22,
98 64.4,17.6,35,32,16.92,
99 77.6,37.6,15,7,4.97,
100 67.6,18.7,25,7,8.65,
101 35.0,1.2,37,53,42.34,
102 44.7,46.6,16,29,50.43,
103 42.8,27.7,22,29,58.33
104 };
105
106
107
108
109
110
111
112
113
114
115
116
117 @Test
118 void testLongly() {
119 RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
120 RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();
121 double[] rData = new double[] {
122 12333921.73333333246, 3.679666000000000e+04, 343330206.333333313,
123 1649102.666666666744, 1117681.066666666651, 23461965.733333334, 16240.93333333333248,
124 36796.66000000000, 1.164576250000000e+02, 1063604.115416667,
125 6258.666250000000, 3490.253750000000, 73503.000000000, 50.92333333333334,
126 343330206.33333331347, 1.063604115416667e+06, 9879353659.329166412,
127 56124369.854166664183, 30880428.345833335072, 685240944.600000024, 470977.90000000002328,
128 1649102.66666666674, 6.258666250000000e+03, 56124369.854166664,
129 873223.429166666698, -115378.762499999997, 4462741.533333333, 2973.03333333333330,
130 1117681.06666666665, 3.490253750000000e+03, 30880428.345833335,
131 -115378.762499999997, 484304.095833333326, 1764098.133333333, 1382.43333333333339,
132 23461965.73333333433, 7.350300000000000e+04, 685240944.600000024,
133 4462741.533333333209, 1764098.133333333302, 48387348.933333330, 32917.40000000000146,
134 16240.93333333333, 5.092333333333334e+01, 470977.900000000,
135 2973.033333333333, 1382.433333333333, 32917.40000000, 22.66666666666667
136 };
137
138 UnitTestUtils.customAssertEquals("covariance matrix", createRealMatrix(rData, 7, 7), covarianceMatrix, 10E-9);
139
140 }
141
142
143
144
145
146 @Test
147 void testSwissFertility() {
148 RealMatrix matrix = createRealMatrix(swissData, 47, 5);
149 RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();
150 double[] rData = new double[] {
151 156.0424976873265, 100.1691489361702, -64.36692876965772, -79.7295097132285, 241.5632030527289,
152 100.169148936170251, 515.7994172062905, -124.39283071230344, -139.6574005550416, 379.9043755781684,
153 -64.3669287696577, -124.3928307123034, 63.64662349676226, 53.5758556891767, -190.5606105457909,
154 -79.7295097132285, -139.6574005550416, 53.57585568917669, 92.4560592044403, -61.6988297872340,
155 241.5632030527289, 379.9043755781684, -190.56061054579092, -61.6988297872340, 1739.2945371877890
156 };
157
158 UnitTestUtils.customAssertEquals("covariance matrix", createRealMatrix(rData, 5, 5), covarianceMatrix, 10E-13);
159 }
160
161
162
163
164 @Test
165 void testConstant() {
166 double[] noVariance = new double[] {1, 1, 1, 1};
167 double[] values = new double[] {1, 2, 3, 4};
168 assertEquals(0d, new Covariance().covariance(noVariance, values, true), Double.MIN_VALUE);
169 assertEquals(0d, new Covariance().covariance(noVariance, noVariance, true), Double.MIN_VALUE);
170 }
171
172
173
174
175 @Test
176 void testOneColumn() {
177 RealMatrix cov = new Covariance(new double[][] {{1}, {2}}, false).getCovarianceMatrix();
178 assertEquals(1, cov.getRowDimension());
179 assertEquals(1, cov.getColumnDimension());
180 assertEquals(0.25, cov.getEntry(0, 0), 1.0e-15);
181 }
182
183
184
185
186 @Test
187 void testInsufficientData() {
188 double[] one = new double[] {1};
189 double[] two = new double[] {2};
190 try {
191 new Covariance().covariance(one, two, false);
192 fail("Expecting MathIllegalArgumentException");
193 } catch (MathIllegalArgumentException ex) {
194
195 }
196 try {
197 new Covariance(new double[][] {{},{}});
198 fail("Expecting MathIllegalArgumentException");
199 } catch (MathIllegalArgumentException ex) {
200
201 }
202 }
203
204
205
206
207
208 @Test
209 void testConsistency() {
210 final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
211 final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();
212
213
214 Variance variance = new Variance();
215 for (int i = 0; i < 5; i++) {
216 assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
217 }
218
219
220 assertEquals(covarianceMatrix.getEntry(2, 3),
221 new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
222 assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);
223
224
225 RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
226 for (int i = 0; i < 3; i++) {
227 repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
228 }
229 RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
230 double columnVariance = variance.evaluate(matrix.getColumn(0));
231 for (int i = 0; i < 3; i++) {
232 for (int j = 0; j < 3; j++) {
233 assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
234 }
235 }
236
237
238 double[][] data = matrix.getData();
239 UnitTestUtils.customAssertEquals("Covariances",
240 covarianceMatrix, new Covariance().computeCovarianceMatrix(data), Double.MIN_VALUE);
241 UnitTestUtils.customAssertEquals("Covariances",
242 covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true), Double.MIN_VALUE);
243
244 double[] x = data[0];
245 double[] y = data[1];
246 assertEquals(new Covariance().covariance(x, y),
247 new Covariance().covariance(x, y, true), Double.MIN_VALUE);
248 }
249
250 protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) {
251 double[][] matrixData = new double[nRows][nCols];
252 int ptr = 0;
253 for (int i = 0; i < nRows; i++) {
254 System.arraycopy(data, ptr, matrixData[i], 0, nCols);
255 ptr += nCols;
256 }
257 return new Array2DRowRealMatrix(matrixData);
258 }
259 }