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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat.correlation;
23  
24  import org.hipparchus.UnitTestUtils;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.linear.BlockRealMatrix;
27  import org.hipparchus.linear.MatrixUtils;
28  import org.hipparchus.linear.RealMatrix;
29  import org.hipparchus.stat.ranking.NaNStrategy;
30  import org.hipparchus.stat.ranking.NaturalRanking;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  /**
35   * Test cases for Spearman's rank correlation
36   *
37   */
38  public class SpearmansRankCorrelationTest extends PearsonsCorrelationTest {
39  
40      /**
41       * Test Longley dataset against R.
42       */
43      @Override
44      @Test
45      public void testLongly() {
46          RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
47          SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
48          RealMatrix correlationMatrix = corrInstance.getCorrelationMatrix();
49          double[] rData = new double[] {
50                  1, 0.982352941176471, 0.985294117647059, 0.564705882352941, 0.2264705882352941, 0.976470588235294,
51                  0.976470588235294, 0.982352941176471, 1, 0.997058823529412, 0.664705882352941, 0.2205882352941176,
52                  0.997058823529412, 0.997058823529412, 0.985294117647059, 0.997058823529412, 1, 0.638235294117647,
53                  0.2235294117647059, 0.9941176470588236, 0.9941176470588236, 0.564705882352941, 0.664705882352941,
54                  0.638235294117647, 1, -0.3411764705882353, 0.685294117647059, 0.685294117647059, 0.2264705882352941,
55                  0.2205882352941176, 0.2235294117647059, -0.3411764705882353, 1, 0.2264705882352941, 0.2264705882352941,
56                  0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1,
57                  0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1
58          };
59          UnitTestUtils.assertEquals("Spearman's correlation matrix", createRealMatrix(rData, 7, 7), correlationMatrix, 10E-15);
60      }
61  
62      /**
63       * Test R swiss fertility dataset.
64       */
65      @Test
66      public void testSwiss() {
67          RealMatrix matrix = createRealMatrix(swissData, 47, 5);
68          SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
69          RealMatrix correlationMatrix = corrInstance.getCorrelationMatrix();
70          double[] rData = new double[] {
71                  1, 0.2426642769364176, -0.660902996352354, -0.443257690360988, 0.4136455623012432,
72                  0.2426642769364176, 1, -0.598859938748963, -0.650463814145816, 0.2886878090882852,
73                 -0.660902996352354, -0.598859938748963, 1, 0.674603831406147, -0.4750575257171745,
74                 -0.443257690360988, -0.650463814145816, 0.674603831406147, 1, -0.1444163088302244,
75                  0.4136455623012432, 0.2886878090882852, -0.4750575257171745, -0.1444163088302244, 1
76          };
77          UnitTestUtils.assertEquals("Spearman's correlation matrix", createRealMatrix(rData, 5, 5), correlationMatrix, 10E-15);
78      }
79  
80      /**
81       * Constant column
82       */
83      @Override
84      @Test
85      public void testConstant() {
86          double[] noVariance = new double[] {1, 1, 1, 1};
87          double[] values = new double[] {1, 2, 3, 4};
88          Assert.assertTrue(Double.isNaN(new SpearmansCorrelation().correlation(noVariance, values)));
89      }
90  
91      /**
92       * Insufficient data
93       */
94      @Override
95      @Test
96      public void testInsufficientData() {
97          double[] one = new double[] {1};
98          double[] two = new double[] {2};
99          try {
100             new SpearmansCorrelation().correlation(one, two);
101             Assert.fail("Expecting MathIllegalArgumentException");
102         } catch (MathIllegalArgumentException ex) {
103             // Expected
104         }
105         RealMatrix matrix = new BlockRealMatrix(new double[][] {{0},{1}});
106         try {
107             new SpearmansCorrelation(matrix);
108             Assert.fail("Expecting MathIllegalArgumentException");
109         } catch (MathIllegalArgumentException ex) {
110             // Expected
111         }
112     }
113 
114     @Override
115     @Test
116     public void testConsistency() {
117         RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
118         SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
119         double[][] data = matrix.getData();
120         double[] x = matrix.getColumn(0);
121         double[] y = matrix.getColumn(1);
122         Assert.assertEquals(new SpearmansCorrelation().correlation(x, y),
123                 corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
124         UnitTestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
125                 new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
126     }
127 
128     @Test(expected = MathIllegalArgumentException.class)
129     public void testMath891Array() {
130         // NaNStrategy.REMOVED is not supported since 4.0
131         final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
132         final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };
133 
134         NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
135         SpearmansCorrelation spearman = new SpearmansCorrelation(ranking);
136 
137         Assert.assertEquals(0.5, spearman.correlation(xArray, yArray), Double.MIN_VALUE);
138     }
139 
140     @Test(expected = MathIllegalArgumentException.class)
141     public void testMath891Matrix() {
142         // NaNStrategy.REMOVED is not supported since 4.0
143         final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
144         final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };
145 
146         RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
147         for (int i = 0; i < xArray.length; i++) {
148             matrix.addToEntry(i, 0, xArray[i]);
149             matrix.addToEntry(i, 1, yArray[i]);
150         }
151 
152         // compute correlation
153         NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
154         SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
155 
156         Assert.assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
157     }
158 
159     // Not relevant here
160     @Override
161     @Test
162     public void testStdErrorConsistency() {}
163     @Override
164     @Test
165     public void testCovarianceConsistency() {}
166 
167 }