View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.legacy.stat.correlation;
18  
19  import org.apache.commons.math4.legacy.TestUtils;
20  import org.apache.commons.math4.legacy.linear.Array2DRowRealMatrix;
21  import org.apache.commons.math4.legacy.linear.RealMatrix;
22  import org.apache.commons.rng.UniformRandomProvider;
23  import org.apache.commons.rng.simple.RandomSource;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  public class StorelessCovarianceTest {
28  
29      protected final double[] longleyData = new double[] {
30              60323,83.0,234289,2356,1590,107608,1947,
31              61122,88.5,259426,2325,1456,108632,1948,
32              60171,88.2,258054,3682,1616,109773,1949,
33              61187,89.5,284599,3351,1650,110929,1950,
34              63221,96.2,328975,2099,3099,112075,1951,
35              63639,98.1,346999,1932,3594,113270,1952,
36              64989,99.0,365385,1870,3547,115094,1953,
37              63761,100.0,363112,3578,3350,116219,1954,
38              66019,101.2,397469,2904,3048,117388,1955,
39              67857,104.6,419180,2822,2857,118734,1956,
40              68169,108.4,442769,2936,2798,120445,1957,
41              66513,110.8,444546,4681,2637,121950,1958,
42              68655,112.6,482704,3813,2552,123366,1959,
43              69564,114.2,502601,3931,2514,125368,1960,
44              69331,115.7,518173,4806,2572,127852,1961,
45              70551,116.9,554894,4007,2827,130081,1962
46          };
47  
48      protected final double[] swissData = new double[] {
49              80.2,17.0,15,12,9.96,
50              83.1,45.1,6,9,84.84,
51              92.5,39.7,5,5,93.40,
52              85.8,36.5,12,7,33.77,
53              76.9,43.5,17,15,5.16,
54              76.1,35.3,9,7,90.57,
55              83.8,70.2,16,7,92.85,
56              92.4,67.8,14,8,97.16,
57              82.4,53.3,12,7,97.67,
58              82.9,45.2,16,13,91.38,
59              87.1,64.5,14,6,98.61,
60              64.1,62.0,21,12,8.52,
61              66.9,67.5,14,7,2.27,
62              68.9,60.7,19,12,4.43,
63              61.7,69.3,22,5,2.82,
64              68.3,72.6,18,2,24.20,
65              71.7,34.0,17,8,3.30,
66              55.7,19.4,26,28,12.11,
67              54.3,15.2,31,20,2.15,
68              65.1,73.0,19,9,2.84,
69              65.5,59.8,22,10,5.23,
70              65.0,55.1,14,3,4.52,
71              56.6,50.9,22,12,15.14,
72              57.4,54.1,20,6,4.20,
73              72.5,71.2,12,1,2.40,
74              74.2,58.1,14,8,5.23,
75              72.0,63.5,6,3,2.56,
76              60.5,60.8,16,10,7.72,
77              58.3,26.8,25,19,18.46,
78              65.4,49.5,15,8,6.10,
79              75.5,85.9,3,2,99.71,
80              69.3,84.9,7,6,99.68,
81              77.3,89.7,5,2,100.00,
82              70.5,78.2,12,6,98.96,
83              79.4,64.9,7,3,98.22,
84              65.0,75.9,9,9,99.06,
85              92.2,84.6,3,3,99.46,
86              79.3,63.1,13,13,96.83,
87              70.4,38.4,26,12,5.62,
88              65.7,7.7,29,11,13.79,
89              72.7,16.7,22,13,11.22,
90              64.4,17.6,35,32,16.92,
91              77.6,37.6,15,7,4.97,
92              67.6,18.7,25,7,8.65,
93              35.0,1.2,37,53,42.34,
94              44.7,46.6,16,29,50.43,
95              42.8,27.7,22,29,58.33
96          };
97  
98      protected final double[][] longleyDataSimple = {
99          {60323, 83.0},
100         {61122,88.5},
101         {60171, 88.2},
102         {61187, 89.5},
103         {63221, 96.2},
104         {63639, 98.1},
105         {64989, 99.0},
106         {63761, 100.0},
107         {66019, 101.2},
108         {67857, 104.6},
109         {68169, 108.4},
110         {66513, 110.8},
111         {68655, 112.6},
112         {69564, 114.2},
113         {69331, 115.7},
114         {70551, 116.9}
115     };
116 
117     @Test
118     public void testLongleySimpleVar(){
119         double rCov = 12333921.73333333246;
120         StorelessBivariateCovariance cov = new StorelessBivariateCovariance();
121         for(int i=0;i<longleyDataSimple.length;i++){
122             cov.increment(longleyDataSimple[i][0],longleyDataSimple[i][0]);
123         }
124         TestUtils.assertEquals("simple covariance test", rCov, cov.getResult(), 10E-7);
125     }
126 
127     @Test
128     public void testLongleySimpleCov(){
129         double rCov = 36796.660000;
130         StorelessBivariateCovariance cov = new StorelessBivariateCovariance();
131         for(int i=0;i<longleyDataSimple.length;i++){
132             cov.increment(longleyDataSimple[i][0], longleyDataSimple[i][1]);
133         }
134         TestUtils.assertEquals("simple covariance test", rCov, cov.getResult(), 10E-7);
135     }
136 
137     /**
138      * Test Longley dataset against R.
139      * Data Source: J. Longley (1967) "An Appraisal of Least Squares
140      * Programs for the Electronic Computer from the Point of View of the User"
141      * Journal of the American Statistical Association, vol. 62. September,
142      * pp. 819-841.
143      *
144      * Data are from NIST:
145      * http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Longley.dat
146      */
147     @Test
148     public void testLongleyByRow() {
149         RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
150 
151         double[] rData = new double[] {
152          12333921.73333333246, 3.679666000000000e+04, 343330206.333333313,
153          1649102.666666666744, 1117681.066666666651, 23461965.733333334, 16240.93333333333248,
154          36796.66000000000, 1.164576250000000e+02, 1063604.115416667,
155          6258.666250000000, 3490.253750000000, 73503.000000000, 50.92333333333334,
156          343330206.33333331347, 1.063604115416667e+06, 9879353659.329166412,
157          56124369.854166664183, 30880428.345833335072, 685240944.600000024, 470977.90000000002328,
158          1649102.66666666674, 6.258666250000000e+03, 56124369.854166664,
159          873223.429166666698, -115378.762499999997, 4462741.533333333, 2973.03333333333330,
160          1117681.06666666665, 3.490253750000000e+03, 30880428.345833335,
161          -115378.762499999997, 484304.095833333326, 1764098.133333333, 1382.43333333333339,
162          23461965.73333333433, 7.350300000000000e+04, 685240944.600000024,
163          4462741.533333333209, 1764098.133333333302, 48387348.933333330, 32917.40000000000146,
164          16240.93333333333, 5.092333333333334e+01, 470977.900000000,
165          2973.033333333333, 1382.433333333333, 32917.40000000, 22.66666666666667
166         };
167 
168         StorelessCovariance covMatrix = new StorelessCovariance(7);
169         for(int i=0;i<matrix.getRowDimension();i++){
170             covMatrix.increment(matrix.getRow(i));
171         }
172 
173         RealMatrix covarianceMatrix = covMatrix.getCovarianceMatrix();
174 
175         TestUtils.assertEquals("covariance matrix", createRealMatrix(rData, 7, 7), covarianceMatrix, 10E-7);
176     }
177 
178     /**
179      * Test R Swiss fertility dataset against R.
180      * Data Source: R datasets package
181      */
182     @Test
183     public void testSwissFertilityByRow() {
184          RealMatrix matrix = createRealMatrix(swissData, 47, 5);
185 
186          double[] rData = new double[] {
187            156.0424976873265, 100.1691489361702, -64.36692876965772, -79.7295097132285, 241.5632030527289,
188            100.169148936170251, 515.7994172062905, -124.39283071230344, -139.6574005550416, 379.9043755781684,
189            -64.3669287696577, -124.3928307123034, 63.64662349676226, 53.5758556891767, -190.5606105457909,
190            -79.7295097132285, -139.6574005550416, 53.57585568917669, 92.4560592044403, -61.6988297872340,
191             241.5632030527289, 379.9043755781684, -190.56061054579092, -61.6988297872340, 1739.2945371877890
192          };
193 
194         StorelessCovariance covMatrix = new StorelessCovariance(5);
195         for(int i=0;i<matrix.getRowDimension();i++){
196             covMatrix.increment(matrix.getRow(i));
197         }
198 
199         RealMatrix covarianceMatrix = covMatrix.getCovarianceMatrix();
200 
201         TestUtils.assertEquals("covariance matrix", createRealMatrix(rData, 5, 5), covarianceMatrix, 10E-13);
202     }
203 
204     /**
205      * Test symmetry of the covariance matrix
206      */
207     @Test
208     public void testSymmetry() {
209         RealMatrix matrix = createRealMatrix(swissData, 47, 5);
210 
211         final int dimension = 5;
212         StorelessCovariance storelessCov = new StorelessCovariance(dimension);
213         for(int i=0;i<matrix.getRowDimension();i++){
214             storelessCov.increment(matrix.getRow(i));
215         }
216 
217         double[][] covMatrix = storelessCov.getData();
218         for (int i = 0; i < dimension; i++) {
219             for (int j = i; j < dimension; j++) {
220                 Assert.assertEquals(covMatrix[i][j], covMatrix[j][i], 10e-9);
221             }
222         }
223     }
224 
225     /**
226      * Test equality of covariance. chk: covariance of two
227      * samples separately and adds them together. cov: computes
228      * covariance of the combined sample showing both are equal.
229      */
230     @Test
231     public void testEquivalence() {
232         int num_sets = 2;
233         StorelessBivariateCovariance cov = new StorelessBivariateCovariance();// covariance of the superset
234         StorelessBivariateCovariance chk = new StorelessBivariateCovariance();// check covariance made by appending covariance of subsets
235 
236         final UniformRandomProvider rand = RandomSource.ISAAC.create(10L);// Seed can be changed
237         for (int s = 0; s < num_sets; s++) {// loop through sets of samlpes
238             StorelessBivariateCovariance covs = new StorelessBivariateCovariance();
239             for (int i = 0; i < 5; i++) { // loop through individual samlpes.
240                 double x = rand.nextDouble();
241                 double y = rand.nextDouble();
242                 covs.increment(x, y);// add sample to the subset
243                 cov.increment(x, y);// add sample to the superset
244             }
245            chk.append(covs);
246         }
247 
248         TestUtils.assertEquals("covariance subset test", chk.getResult(), cov.getResult(), 10E-7);
249     }
250 
251     protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) {
252         double[][] matrixData = new double[nRows][nCols];
253         int ptr = 0;
254         for (int i = 0; i < nRows; i++) {
255             System.arraycopy(data, ptr, matrixData[i], 0, nCols);
256             ptr += nCols;
257         }
258         return new Array2DRowRealMatrix(matrixData);
259     }
260 
261 }
262