001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math.stat.correlation;
018
019 import org.apache.commons.math.TestUtils;
020 import org.apache.commons.math.linear.BlockRealMatrix;
021 import org.apache.commons.math.linear.RealMatrix;
022
023 /**
024 * Test cases for Spearman's rank correlation
025 *
026 * @since 2.0
027 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
028 */
029 public class SpearmansRankCorrelationTest extends PearsonsCorrelationTest {
030
031 @Override
032 protected void setUp() throws Exception {
033 super.setUp();
034 }
035
036 @Override
037 protected void tearDown() throws Exception {
038 super.tearDown();
039 }
040
041 /**
042 * Test Longley dataset against R.
043 */
044 @Override
045 public void testLongly() throws Exception {
046 RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
047 SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
048 RealMatrix correlationMatrix = corrInstance.getCorrelationMatrix();
049 double[] rData = new double[] {
050 1, 0.982352941176471, 0.985294117647059, 0.564705882352941, 0.2264705882352941, 0.976470588235294,
051 0.976470588235294, 0.982352941176471, 1, 0.997058823529412, 0.664705882352941, 0.2205882352941176,
052 0.997058823529412, 0.997058823529412, 0.985294117647059, 0.997058823529412, 1, 0.638235294117647,
053 0.2235294117647059, 0.9941176470588236, 0.9941176470588236, 0.564705882352941, 0.664705882352941,
054 0.638235294117647, 1, -0.3411764705882353, 0.685294117647059, 0.685294117647059, 0.2264705882352941,
055 0.2205882352941176, 0.2235294117647059, -0.3411764705882353, 1, 0.2264705882352941, 0.2264705882352941,
056 0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1,
057 0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1
058 };
059 TestUtils.assertEquals("Spearman's correlation matrix", createRealMatrix(rData, 7, 7), correlationMatrix, 10E-15);
060 }
061
062 /**
063 * Test R swiss fertility dataset.
064 */
065 public void testSwiss() throws Exception {
066 RealMatrix matrix = createRealMatrix(swissData, 47, 5);
067 SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
068 RealMatrix correlationMatrix = corrInstance.getCorrelationMatrix();
069 double[] rData = new double[] {
070 1, 0.2426642769364176, -0.660902996352354, -0.443257690360988, 0.4136455623012432,
071 0.2426642769364176, 1, -0.598859938748963, -0.650463814145816, 0.2886878090882852,
072 -0.660902996352354, -0.598859938748963, 1, 0.674603831406147, -0.4750575257171745,
073 -0.443257690360988, -0.650463814145816, 0.674603831406147, 1, -0.1444163088302244,
074 0.4136455623012432, 0.2886878090882852, -0.4750575257171745, -0.1444163088302244, 1
075 };
076 TestUtils.assertEquals("Spearman's correlation matrix", createRealMatrix(rData, 5, 5), correlationMatrix, 10E-15);
077 }
078
079 /**
080 * Constant column
081 */
082 @Override
083 public void testConstant() {
084 double[] noVariance = new double[] {1, 1, 1, 1};
085 double[] values = new double[] {1, 2, 3, 4};
086 assertTrue(Double.isNaN(new SpearmansCorrelation().correlation(noVariance, values)));
087 }
088
089 /**
090 * Insufficient data
091 */
092 @Override
093 public void testInsufficientData() {
094 double[] one = new double[] {1};
095 double[] two = new double[] {2};
096 try {
097 new SpearmansCorrelation().correlation(one, two);
098 fail("Expecting IllegalArgumentException");
099 } catch (IllegalArgumentException ex) {
100 // Expected
101 }
102 RealMatrix matrix = new BlockRealMatrix(new double[][] {{0},{1}});
103 try {
104 new SpearmansCorrelation(matrix);
105 fail("Expecting IllegalArgumentException");
106 } catch (IllegalArgumentException ex) {
107 // Expected
108 }
109 }
110
111 @Override
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 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 // Not relevant here
125 @Override
126 public void testStdErrorConsistency() throws Exception {}
127 @Override
128 public void testCovarianceConsistency() throws Exception {}
129
130 }