~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/descriptive/rank/PercentileTest.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.descriptive.rank;
 
18
 
 
19
import junit.framework.Test;
 
20
import junit.framework.TestSuite;
 
21
 
 
22
import org.apache.commons.math.stat.descriptive.UnivariateStatistic;
 
23
import org.apache.commons.math.stat.descriptive.UnivariateStatisticAbstractTest;
 
24
 
 
25
/**
 
26
 * Test cases for the {@link UnivariateStatistic} class.
 
27
 * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
 
28
 */
 
29
public class PercentileTest extends UnivariateStatisticAbstractTest{
 
30
 
 
31
    protected Percentile stat;
 
32
    
 
33
    /**
 
34
     * @param name
 
35
     */
 
36
    public PercentileTest(String name) {
 
37
        super(name);
 
38
    }
 
39
 
 
40
    public static Test suite() {
 
41
        TestSuite suite = new TestSuite(PercentileTest.class);
 
42
        suite.setName("Percentile Tests");
 
43
        return suite;
 
44
    }
 
45
    
 
46
    /**
 
47
     * {@inheritDoc}
 
48
     */
 
49
    @Override
 
50
    public UnivariateStatistic getUnivariateStatistic() {   
 
51
        return new Percentile(95.0);
 
52
    }
 
53
 
 
54
    /**
 
55
     * {@inheritDoc}
 
56
     */
 
57
    @Override
 
58
    public double expectedValue() {
 
59
        return this.percentile95;
 
60
    }
 
61
 
 
62
    public void testHighPercentile(){
 
63
        double[] d = new double[]{1, 2, 3};
 
64
        Percentile p = new Percentile(75);
 
65
        assertEquals(3.0, p.evaluate(d), 1.0e-5);
 
66
    }
 
67
    
 
68
    public void testPercentile() {
 
69
        double[] d = new double[] {1, 3, 2, 4};
 
70
        Percentile p = new Percentile(30);
 
71
        assertEquals(1.5, p.evaluate(d), 1.0e-5);
 
72
        p.setQuantile(25);
 
73
        assertEquals(1.25, p.evaluate(d), 1.0e-5);
 
74
        p.setQuantile(75);
 
75
        assertEquals(3.75, p.evaluate(d), 1.0e-5);
 
76
        p.setQuantile(50);
 
77
        assertEquals(2.5, p.evaluate(d), 1.0e-5);
 
78
        
 
79
        // invalid percentiles
 
80
        try {
 
81
            p.evaluate(d, 0, d.length, -1.0);
 
82
            fail();
 
83
        } catch (IllegalArgumentException ex) {
 
84
            // success
 
85
        }
 
86
        try {
 
87
            p.evaluate(d, 0, d.length, 101.0);
 
88
            fail();
 
89
        } catch (IllegalArgumentException ex) {
 
90
            // success
 
91
        }
 
92
    }
 
93
    
 
94
    public void testNISTExample() {
 
95
        double[] d = new double[] {95.1772, 95.1567, 95.1937, 95.1959, 
 
96
                95.1442, 95.0610,  95.1591, 95.1195, 95.1772, 95.0925, 95.1990, 95.1682
 
97
        };
 
98
        Percentile p = new Percentile(90); 
 
99
        assertEquals(95.1981, p.evaluate(d), 1.0e-4);
 
100
        assertEquals(95.1990, p.evaluate(d,0,d.length, 100d), 0);
 
101
    }
 
102
    
 
103
    public void test5() {
 
104
        Percentile percentile = new Percentile(5);
 
105
        assertEquals(this.percentile5, percentile.evaluate(testArray), getTolerance());
 
106
    }
 
107
    
 
108
    public void testNullEmpty() {
 
109
        Percentile percentile = new Percentile(50);
 
110
        double[] nullArray = null;
 
111
        double[] emptyArray = new double[] {};
 
112
        try {
 
113
            percentile.evaluate(nullArray);
 
114
            fail("Expecting IllegalArgumentException for null array");
 
115
        } catch (IllegalArgumentException ex) {
 
116
            // expected
 
117
        }  
 
118
        assertTrue(Double.isNaN(percentile.evaluate(emptyArray)));        
 
119
    }
 
120
    
 
121
    public void testSingleton() {
 
122
        Percentile percentile = new Percentile(50);
 
123
        double[] singletonArray = new double[] {1d};
 
124
        assertEquals(1d, percentile.evaluate(singletonArray), 0);
 
125
        assertEquals(1d, percentile.evaluate(singletonArray, 0, 1), 0);
 
126
        assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 5), 0);
 
127
        assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 100), 0); 
 
128
        assertTrue(Double.isNaN(percentile.evaluate(singletonArray, 0, 0)));     
 
129
    }
 
130
    
 
131
    public void testSpecialValues() {
 
132
        Percentile percentile = new Percentile(50);
 
133
        double[] specialValues = new double[] {0d, 1d, 2d, 3d, 4d,  Double.NaN};
 
134
        assertEquals(2.5d, percentile.evaluate(specialValues), 0);
 
135
        specialValues =  new double[] {Double.NEGATIVE_INFINITY, 1d, 2d, 3d,
 
136
                Double.NaN, Double.POSITIVE_INFINITY};
 
137
        assertEquals(2.5d, percentile.evaluate(specialValues), 0);
 
138
        specialValues = new double[] {1d, 1d, Double.POSITIVE_INFINITY, 
 
139
                Double.POSITIVE_INFINITY};
 
140
        assertTrue(Double.isInfinite(percentile.evaluate(specialValues)));
 
141
        specialValues = new double[] {1d, 1d, Double.NaN, 
 
142
                Double.NaN};
 
143
        assertTrue(Double.isNaN(percentile.evaluate(specialValues)));
 
144
        specialValues = new double[] {1d, 1d, Double.NEGATIVE_INFINITY, 
 
145
                Double.NEGATIVE_INFINITY};
 
146
        // Interpolation results in NEGATIVE_INFINITY + POSITIVE_INFINITY
 
147
        assertTrue(Double.isNaN(percentile.evaluate(specialValues)));   
 
148
    }
 
149
    
 
150
    public void testSetQuantile() {
 
151
        Percentile percentile = new Percentile(10);
 
152
        percentile.setQuantile(100); // OK
 
153
        assertEquals(100, percentile.getQuantile(), 0);      
 
154
        try {
 
155
            percentile.setQuantile(0);
 
156
            fail("Expecting IllegalArgumentException");
 
157
        } catch (IllegalArgumentException ex) {
 
158
            // expected
 
159
        }
 
160
        try {
 
161
            new Percentile(0);
 
162
            fail("Expecting IllegalArgumentException");
 
163
        } catch (IllegalArgumentException ex) {
 
164
            // expected
 
165
        }        
 
166
    }
 
167
    
 
168
}