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