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