~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/ListUnivariateImplTest.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;
 
18
 
 
19
import java.util.ArrayList;
 
20
import java.util.List;
 
21
 
 
22
import org.apache.commons.math.TestUtils;
 
23
 
 
24
import junit.framework.Test;
 
25
import junit.framework.TestCase;
 
26
import junit.framework.TestSuite;
 
27
 
 
28
/**
 
29
 * Test cases for the {@link ListUnivariateImpl} class.
 
30
 *
 
31
 * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
 
32
 */
 
33
 
 
34
public final class ListUnivariateImplTest extends TestCase {
 
35
    
 
36
    private double one = 1;
 
37
    private float two = 2;
 
38
    private int three = 3;
 
39
 
 
40
    private double mean = 2;
 
41
    private double sumSq = 18;
 
42
    private double sum = 8;
 
43
    private double var = 0.666666666666666666667;
 
44
    private double std = Math.sqrt(var);
 
45
    private double n = 4;
 
46
    private double min = 1;
 
47
    private double max = 3;
 
48
    private double tolerance = 10E-15;
 
49
    
 
50
    public ListUnivariateImplTest(String name) {
 
51
        super(name);
 
52
    }
 
53
    
 
54
    public static Test suite() {
 
55
        TestSuite suite = new TestSuite(ListUnivariateImplTest.class);
 
56
        suite.setName("Frequency Tests");
 
57
        return suite;
 
58
    }
 
59
    
 
60
    /** test stats */
 
61
    public void testStats() {
 
62
        List<Object> externalList = new ArrayList<Object>();
 
63
        
 
64
        DescriptiveStatistics u = new ListUnivariateImpl( externalList ); 
 
65
 
 
66
        assertEquals("total count",0,u.getN(),tolerance);
 
67
        u.addValue(one);
 
68
        u.addValue(two);
 
69
        u.addValue(two);
 
70
        u.addValue(three);
 
71
        assertEquals("N",n,u.getN(),tolerance);
 
72
        assertEquals("sum",sum,u.getSum(),tolerance);
 
73
        assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
 
74
        assertEquals("var",var,u.getVariance(),tolerance);
 
75
        assertEquals("std",std,u.getStandardDeviation(),tolerance);
 
76
        assertEquals("mean",mean,u.getMean(),tolerance);
 
77
        assertEquals("min",min,u.getMin(),tolerance);
 
78
        assertEquals("max",max,u.getMax(),tolerance);
 
79
        u.clear();
 
80
        assertEquals("total count",0,u.getN(),tolerance);    
 
81
    }     
 
82
    
 
83
    public void testN0andN1Conditions() throws Exception {
 
84
        List<Object> list = new ArrayList<Object>();
 
85
        
 
86
        DescriptiveStatistics u = new ListUnivariateImpl( list );
 
87
                
 
88
        assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
 
89
        assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
 
90
        assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
 
91
 
 
92
        list.add( Double.valueOf(one));
 
93
 
 
94
        assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
 
95
        assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
 
96
        assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);  
 
97
    }
 
98
    
 
99
    public void testSkewAndKurtosis() {
 
100
        DescriptiveStatistics u = new DescriptiveStatistics();
 
101
        
 
102
        double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
 
103
                                             9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
 
104
        for( int i = 0; i < testArray.length; i++) {
 
105
            u.addValue( testArray[i]);
 
106
        }
 
107
        
 
108
        assertEquals("mean", 12.40455, u.getMean(), 0.0001);
 
109
        assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
 
110
        assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
 
111
        assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
 
112
    }
 
113
 
 
114
    public void testProductAndGeometricMean() throws Exception {
 
115
        ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList<Object>());
 
116
        u.setWindowSize(10);
 
117
                
 
118
        u.addValue( 1.0 );
 
119
        u.addValue( 2.0 );
 
120
        u.addValue( 3.0 );
 
121
        u.addValue( 4.0 );
 
122
 
 
123
        assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 );
 
124
 
 
125
        // Now test rolling - StorelessDescriptiveStatistics should discount the contribution
 
126
        // of a discarded element
 
127
        for( int i = 0; i < 10; i++ ) {
 
128
            u.addValue( i + 2 );
 
129
        }
 
130
        // Values should be (2,3,4,5,6,7,8,9,10,11)
 
131
        
 
132
        assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 );
 
133
 
 
134
 
 
135
    }
 
136
    
 
137
    /** test stats */
 
138
    public void testSerialization() {
 
139
        
 
140
        DescriptiveStatistics u = new ListUnivariateImpl();
 
141
        
 
142
        assertEquals("total count",0,u.getN(),tolerance);
 
143
        u.addValue(one);
 
144
        u.addValue(two);
 
145
        
 
146
        DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); 
 
147
 
 
148
        u2.addValue(two);
 
149
        u2.addValue(three);
 
150
        
 
151
        assertEquals("N",n,u2.getN(),tolerance);
 
152
        assertEquals("sum",sum,u2.getSum(),tolerance);
 
153
        assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
 
154
        assertEquals("var",var,u2.getVariance(),tolerance);
 
155
        assertEquals("std",std,u2.getStandardDeviation(),tolerance);
 
156
        assertEquals("mean",mean,u2.getMean(),tolerance);
 
157
        assertEquals("min",min,u2.getMin(),tolerance);
 
158
        assertEquals("max",max,u2.getMax(),tolerance);
 
159
 
 
160
        u2.clear();
 
161
        assertEquals("total count",0,u2.getN(),tolerance);    
 
162
    }       
 
163
}
 
164