~ubuntu-branches/ubuntu/maverick/commons-math/maverick

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/stat/correlation/SpearmansRankCorrelationTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-08-22 01:13:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090822011325-hi4peq1ua5weguwn
Tags: 2.0-1
* New upstream release.
* Set Maintainer field to Debian Java Team
* Add myself as Uploaders
* Switch to Quilt patch system:
  - Refresh all patchs
  - Remove B-D on dpatch, Add B-D on quilt
  - Include patchsys-quilt.mk in debian/rules
* Bump Standards-Version to 3.8.3:
  - Add a README.source to describe patch system
* Maven POMs:
  - Add a Build-Depends-Indep dependency on maven-repo-helper
  - Use mh_installpom and mh_installjar to install the POM and the jar to the
    Maven repository
* Use default-jdk/jre:
  - Depends on java5-runtime-headless
  - Build-Depends on default-jdk
  - Use /usr/lib/jvm/default-java as JAVA_HOME
* Move api documentation to /usr/share/doc/libcommons-math-java/api
* Build-Depends on junit4 instead of junit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.math.stat.correlation;
 
18
 
 
19
import org.apache.commons.math.TestUtils;
 
20
import org.apache.commons.math.linear.BlockRealMatrix;
 
21
import org.apache.commons.math.linear.RealMatrix;
 
22
 
 
23
/**
 
24
 * Test cases for Spearman's rank correlation
 
25
 * 
 
26
 * @since 2.0
 
27
 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
 
28
 */
 
29
public class SpearmansRankCorrelationTest extends PearsonsCorrelationTest {
 
30
 
 
31
    @Override
 
32
    protected void setUp() throws Exception {
 
33
        super.setUp();
 
34
    }
 
35
 
 
36
    @Override
 
37
    protected void tearDown() throws Exception {
 
38
        super.tearDown();
 
39
    }
 
40
    
 
41
    /**
 
42
     * Test Longley dataset against R.
 
43
     */
 
44
    @Override
 
45
    public void testLongly() throws Exception {  
 
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
        TestUtils.assertEquals("Spearman's correlation matrix", createRealMatrix(rData, 7, 7), correlationMatrix, 10E-15);
 
60
    }
 
61
    
 
62
    /**
 
63
     * Test R swiss fertility dataset.
 
64
     */
 
65
    public void testSwiss() throws Exception {  
 
66
        RealMatrix matrix = createRealMatrix(swissData, 47, 5);
 
67
        SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix); 
 
68
        RealMatrix correlationMatrix = corrInstance.getCorrelationMatrix();
 
69
        double[] rData = new double[] {
 
70
                1, 0.2426642769364176, -0.660902996352354, -0.443257690360988, 0.4136455623012432,
 
71
                0.2426642769364176, 1, -0.598859938748963, -0.650463814145816, 0.2886878090882852,
 
72
               -0.660902996352354, -0.598859938748963, 1, 0.674603831406147, -0.4750575257171745,
 
73
               -0.443257690360988, -0.650463814145816, 0.674603831406147, 1, -0.1444163088302244,
 
74
                0.4136455623012432, 0.2886878090882852, -0.4750575257171745, -0.1444163088302244, 1
 
75
        }; 
 
76
        TestUtils.assertEquals("Spearman's correlation matrix", createRealMatrix(rData, 5, 5), correlationMatrix, 10E-15);
 
77
    }
 
78
    
 
79
    /**
 
80
     * Constant column
 
81
     */
 
82
    @Override
 
83
    public void testConstant() {
 
84
        double[] noVariance = new double[] {1, 1, 1, 1};
 
85
        double[] values = new double[] {1, 2, 3, 4};
 
86
        assertTrue(Double.isNaN(new SpearmansCorrelation().correlation(noVariance, values)));
 
87
    }
 
88
    
 
89
    /**
 
90
     * Insufficient data
 
91
     */ 
 
92
    @Override
 
93
    public void testInsufficientData() {
 
94
        double[] one = new double[] {1};
 
95
        double[] two = new double[] {2};
 
96
        try {
 
97
            new SpearmansCorrelation().correlation(one, two);
 
98
            fail("Expecting IllegalArgumentException");
 
99
        } 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
}