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.jupiter.api.Test;
32
33 import static org.junit.jupiter.api.Assertions.assertEquals;
34 import static org.junit.jupiter.api.Assertions.assertThrows;
35 import static org.junit.jupiter.api.Assertions.assertTrue;
36 import static org.junit.jupiter.api.Assertions.fail;
37
38
39
40
41
42 class SpearmansRankCorrelationTest extends PearsonsCorrelationTest {
43
44
45
46
47 @Override
48 @Test
49 public void testLongly() {
50 RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
51 SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
52 RealMatrix correlationMatrix = corrInstance.getCorrelationMatrix();
53 double[] rData = new double[] {
54 1, 0.982352941176471, 0.985294117647059, 0.564705882352941, 0.2264705882352941, 0.976470588235294,
55 0.976470588235294, 0.982352941176471, 1, 0.997058823529412, 0.664705882352941, 0.2205882352941176,
56 0.997058823529412, 0.997058823529412, 0.985294117647059, 0.997058823529412, 1, 0.638235294117647,
57 0.2235294117647059, 0.9941176470588236, 0.9941176470588236, 0.564705882352941, 0.664705882352941,
58 0.638235294117647, 1, -0.3411764705882353, 0.685294117647059, 0.685294117647059, 0.2264705882352941,
59 0.2205882352941176, 0.2235294117647059, -0.3411764705882353, 1, 0.2264705882352941, 0.2264705882352941,
60 0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1,
61 0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1
62 };
63 UnitTestUtils.customAssertEquals("Spearman's correlation matrix", createRealMatrix(rData, 7, 7), correlationMatrix, 10E-15);
64 }
65
66
67
68
69 @Test
70 void testSwiss() {
71 RealMatrix matrix = createRealMatrix(swissData, 47, 5);
72 SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
73 RealMatrix correlationMatrix = corrInstance.getCorrelationMatrix();
74 double[] rData = new double[] {
75 1, 0.2426642769364176, -0.660902996352354, -0.443257690360988, 0.4136455623012432,
76 0.2426642769364176, 1, -0.598859938748963, -0.650463814145816, 0.2886878090882852,
77 -0.660902996352354, -0.598859938748963, 1, 0.674603831406147, -0.4750575257171745,
78 -0.443257690360988, -0.650463814145816, 0.674603831406147, 1, -0.1444163088302244,
79 0.4136455623012432, 0.2886878090882852, -0.4750575257171745, -0.1444163088302244, 1
80 };
81 UnitTestUtils.customAssertEquals("Spearman's correlation matrix", createRealMatrix(rData, 5, 5), correlationMatrix, 10E-15);
82 }
83
84
85
86
87 @Override
88 @Test
89 public void testConstant() {
90 double[] noVariance = new double[] {1, 1, 1, 1};
91 double[] values = new double[] {1, 2, 3, 4};
92 assertTrue(Double.isNaN(new SpearmansCorrelation().correlation(noVariance, values)));
93 }
94
95
96
97
98 @Override
99 @Test
100 public void testInsufficientData() {
101 double[] one = new double[] {1};
102 double[] two = new double[] {2};
103 try {
104 new SpearmansCorrelation().correlation(one, two);
105 fail("Expecting MathIllegalArgumentException");
106 } catch (MathIllegalArgumentException ex) {
107
108 }
109 RealMatrix matrix = new BlockRealMatrix(new double[][] {{0},{1}});
110 try {
111 new SpearmansCorrelation(matrix);
112 fail("Expecting MathIllegalArgumentException");
113 } catch (MathIllegalArgumentException ex) {
114
115 }
116 }
117
118 @Override
119 @Test
120 public void testConsistency() {
121 RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
122 SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
123 double[][] data = matrix.getData();
124 double[] x = matrix.getColumn(0);
125 double[] y = matrix.getColumn(1);
126 assertEquals(new SpearmansCorrelation().correlation(x, y),
127 corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
128 UnitTestUtils.customAssertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
129 new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
130 }
131
132 @Test
133 void testMath891Array() {
134 assertThrows(MathIllegalArgumentException.class, () -> {
135
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 NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
140 SpearmansCorrelation spearman = new SpearmansCorrelation(ranking);
141
142 assertEquals(0.5, spearman.correlation(xArray, yArray), Double.MIN_VALUE);
143 });
144 }
145
146 @Test
147 void testMath891Matrix() {
148 assertThrows(MathIllegalArgumentException.class, () -> {
149
150 final double[] xArray = new double[]{Double.NaN, 1.9, 2, 100, 3};
151 final double[] yArray = new double[]{10, 2, 10, Double.NaN, 4};
152
153 RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
154 for (int i = 0; i < xArray.length; i++) {
155 matrix.addToEntry(i, 0, xArray[i]);
156 matrix.addToEntry(i, 1, yArray[i]);
157 }
158
159
160 NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
161 SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
162
163 assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
164 });
165 }
166
167
168 @Override
169 @Test
170 public void testStdErrorConsistency() {}
171 @Override
172 @Test
173 public void testCovarianceConsistency() {}
174
175 }