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.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
36
37
38 public class SpearmansRankCorrelationTest extends PearsonsCorrelationTest {
39
40
41
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
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
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
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
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
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
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
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
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
160 @Override
161 @Test
162 public void testStdErrorConsistency() {}
163 @Override
164 @Test
165 public void testCovarianceConsistency() {}
166
167 }