~ubuntu-branches/ubuntu/oneiric/commons-math/oneiric

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/stat/ranking/NaturalRankingTest.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.ranking;
 
18
 
 
19
import org.apache.commons.math.TestUtils;
 
20
import org.apache.commons.math.random.JDKRandomGenerator;
 
21
import org.apache.commons.math.random.RandomGenerator;
 
22
 
 
23
import junit.framework.TestCase;
 
24
 
 
25
/**
 
26
 * Test cases for NaturalRanking class
 
27
 * 
 
28
 * @since 2.0
 
29
 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
 
30
 */
 
31
public class NaturalRankingTest extends TestCase {
 
32
 
 
33
    private final double[] exampleData = { 20, 17, 30, 42.3, 17, 50,
 
34
            Double.NaN, Double.NEGATIVE_INFINITY, 17 };
 
35
    private final double[] tiesFirst = { 0, 0, 2, 1, 4 };
 
36
    private final double[] tiesLast = { 4, 4, 1, 0 };
 
37
    private final double[] multipleNaNs = { 0, 1, Double.NaN, Double.NaN };
 
38
    private final double[] multipleTies = { 3, 2, 5, 5, 6, 6, 1 };
 
39
    private final double[] allSame = { 0, 0, 0, 0 };
 
40
 
 
41
    public NaturalRankingTest(String arg0) {
 
42
        super(arg0);
 
43
    }
 
44
 
 
45
    @Override
 
46
    protected void setUp() throws Exception {
 
47
        super.setUp();
 
48
    }
 
49
 
 
50
    @Override
 
51
    protected void tearDown() throws Exception {
 
52
        super.tearDown();
 
53
    }
 
54
 
 
55
    public void testDefault() { // Ties averaged, NaNs maximal
 
56
        NaturalRanking ranking = new NaturalRanking();
 
57
        double[] ranks = ranking.rank(exampleData);
 
58
        double[] correctRanks = { 5, 3, 6, 7, 3, 8, 9, 1, 3 };
 
59
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
60
        ranks = ranking.rank(tiesFirst);
 
61
        correctRanks = new double[] { 1.5, 1.5, 4, 3, 5 };
 
62
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
63
        ranks = ranking.rank(tiesLast);
 
64
        correctRanks = new double[] { 3.5, 3.5, 2, 1 };
 
65
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
66
        ranks = ranking.rank(multipleNaNs);
 
67
        correctRanks = new double[] { 1, 2, 3.5, 3.5 };
 
68
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
69
        ranks = ranking.rank(multipleTies);
 
70
        correctRanks = new double[] { 3, 2, 4.5, 4.5, 6.5, 6.5, 1 };
 
71
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
72
        ranks = ranking.rank(allSame);
 
73
        correctRanks = new double[] { 2.5, 2.5, 2.5, 2.5 };
 
74
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
75
    }
 
76
 
 
77
    public void testNaNsMaximalTiesMinimum() {
 
78
        NaturalRanking ranking = new NaturalRanking(TiesStrategy.MINIMUM);
 
79
        double[] ranks = ranking.rank(exampleData);
 
80
        double[] correctRanks = { 5, 2, 6, 7, 2, 8, 9, 1, 2 };
 
81
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
82
        ranks = ranking.rank(tiesFirst);
 
83
        correctRanks = new double[] { 1, 1, 4, 3, 5 };
 
84
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
85
        ranks = ranking.rank(tiesLast);
 
86
        correctRanks = new double[] { 3, 3, 2, 1 };
 
87
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
88
        ranks = ranking.rank(multipleNaNs);
 
89
        correctRanks = new double[] { 1, 2, 3, 3 };
 
90
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
91
        ranks = ranking.rank(multipleTies);
 
92
        correctRanks = new double[] { 3, 2, 4, 4, 6, 6, 1 };
 
93
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
94
        ranks = ranking.rank(allSame);
 
95
        correctRanks = new double[] { 1, 1, 1, 1 };
 
96
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
97
    }
 
98
 
 
99
    public void testNaNsRemovedTiesSequential() {
 
100
        NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED,
 
101
                TiesStrategy.SEQUENTIAL);
 
102
        double[] ranks = ranking.rank(exampleData);
 
103
        double[] correctRanks = { 5, 2, 6, 7, 3, 8, 1, 4 };
 
104
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
105
        ranks = ranking.rank(tiesFirst);
 
106
        correctRanks = new double[] { 1, 2, 4, 3, 5 };
 
107
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
108
        ranks = ranking.rank(tiesLast);
 
109
        correctRanks = new double[] { 3, 4, 2, 1 };
 
110
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
111
        ranks = ranking.rank(multipleNaNs);
 
112
        correctRanks = new double[] { 1, 2 };
 
113
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
114
        ranks = ranking.rank(multipleTies);
 
115
        correctRanks = new double[] { 3, 2, 4, 5, 6, 7, 1 };
 
116
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
117
        ranks = ranking.rank(allSame);
 
118
        correctRanks = new double[] { 1, 2, 3, 4 };
 
119
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
120
    }
 
121
 
 
122
    public void testNaNsMinimalTiesMaximum() {
 
123
        NaturalRanking ranking = new NaturalRanking(NaNStrategy.MINIMAL,
 
124
                TiesStrategy.MAXIMUM);
 
125
        double[] ranks = ranking.rank(exampleData);
 
126
        double[] correctRanks = { 6, 5, 7, 8, 5, 9, 2, 2, 5 };
 
127
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
128
        ranks = ranking.rank(tiesFirst);
 
129
        correctRanks = new double[] { 2, 2, 4, 3, 5 };
 
130
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
131
        ranks = ranking.rank(tiesLast);
 
132
        correctRanks = new double[] { 4, 4, 2, 1 };
 
133
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
134
        ranks = ranking.rank(multipleNaNs);
 
135
        correctRanks = new double[] { 3, 4, 2, 2 };
 
136
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
137
        ranks = ranking.rank(multipleTies);
 
138
        correctRanks = new double[] { 3, 2, 5, 5, 7, 7, 1 };
 
139
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
140
        ranks = ranking.rank(allSame);
 
141
        correctRanks = new double[] { 4, 4, 4, 4 };
 
142
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
143
    }
 
144
 
 
145
    public void testNaNsMinimalTiesAverage() {
 
146
        NaturalRanking ranking = new NaturalRanking(NaNStrategy.MINIMAL);
 
147
        double[] ranks = ranking.rank(exampleData);
 
148
        double[] correctRanks = { 6, 4, 7, 8, 4, 9, 1.5, 1.5, 4 };
 
149
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
150
        ranks = ranking.rank(tiesFirst);
 
151
        correctRanks = new double[] { 1.5, 1.5, 4, 3, 5 };
 
152
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
153
        ranks = ranking.rank(tiesLast);
 
154
        correctRanks = new double[] { 3.5, 3.5, 2, 1 };
 
155
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
156
        ranks = ranking.rank(multipleNaNs);
 
157
        correctRanks = new double[] { 3, 4, 1.5, 1.5 };
 
158
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
159
        ranks = ranking.rank(multipleTies);
 
160
        correctRanks = new double[] { 3, 2, 4.5, 4.5, 6.5, 6.5, 1 };
 
161
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
162
        ranks = ranking.rank(allSame);
 
163
        correctRanks = new double[] { 2.5, 2.5, 2.5, 2.5 };
 
164
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
165
    }
 
166
 
 
167
    public void testNaNsFixedTiesRandom() {
 
168
        RandomGenerator randomGenerator = new JDKRandomGenerator();
 
169
        randomGenerator.setSeed(1000);
 
170
        NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
 
171
                randomGenerator);
 
172
        double[] ranks = ranking.rank(exampleData);
 
173
        double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
 
174
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
175
        ranks = ranking.rank(tiesFirst);
 
176
        correctRanks = new double[] { 1, 1, 4, 3, 5 };
 
177
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
178
        ranks = ranking.rank(tiesLast);
 
179
        correctRanks = new double[] { 3, 4, 2, 1 };
 
180
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
181
        ranks = ranking.rank(multipleNaNs);
 
182
        correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
 
183
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
184
        ranks = ranking.rank(multipleTies);
 
185
        correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
 
186
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
187
        ranks = ranking.rank(allSame);
 
188
        correctRanks = new double[] { 1, 3, 4, 4 };
 
189
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
190
    }
 
191
 
 
192
    public void testNaNsAndInfs() {
 
193
        double[] data = { 0, Double.POSITIVE_INFINITY, Double.NaN,
 
194
                Double.NEGATIVE_INFINITY };
 
195
        NaturalRanking ranking = new NaturalRanking(NaNStrategy.MAXIMAL);
 
196
        double[] ranks = ranking.rank(data);
 
197
        double[] correctRanks = new double[] { 2, 3.5, 3.5, 1 };
 
198
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
199
        ranking = new NaturalRanking(NaNStrategy.MINIMAL);
 
200
        ranks = ranking.rank(data);
 
201
        correctRanks = new double[] { 3, 4, 1.5, 1.5 };
 
202
        TestUtils.assertEquals(correctRanks, ranks, 0d);
 
203
    }
 
204
}