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: 618202 $ $Date: 2008-02-04 01:44:36 -0700 (Mon, 04 Feb 2008) $
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 void setUp() {  
55      }
56      
57      public static Test suite() {
58          TestSuite suite = new TestSuite(ListUnivariateImplTest.class);
59          suite.setName("Frequency Tests");
60          return suite;
61      }
62      
63      /** test stats */
64      public void testStats() {
65          List externalList = new ArrayList();
66          
67          DescriptiveStatistics u = new ListUnivariateImpl( externalList ); 
68  
69          assertEquals("total count",0,u.getN(),tolerance);
70          u.addValue(one);
71          u.addValue(two);
72          u.addValue(two);
73          u.addValue(three);
74          assertEquals("N",n,u.getN(),tolerance);
75          assertEquals("sum",sum,u.getSum(),tolerance);
76          assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
77          assertEquals("var",var,u.getVariance(),tolerance);
78          assertEquals("std",std,u.getStandardDeviation(),tolerance);
79          assertEquals("mean",mean,u.getMean(),tolerance);
80          assertEquals("min",min,u.getMin(),tolerance);
81          assertEquals("max",max,u.getMax(),tolerance);
82          u.clear();
83          assertEquals("total count",0,u.getN(),tolerance);    
84      }     
85      
86      public void testN0andN1Conditions() throws Exception {
87          List list = new ArrayList();
88          
89          DescriptiveStatistics u = new ListUnivariateImpl( list );
90                  
91          assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
92          assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
93          assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
94  
95          list.add( new Double(one));
96  
97          assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
98          assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
99          assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);  
100     }
101     
102     public void testSkewAndKurtosis() {
103         DescriptiveStatistics u = new DescriptiveStatistics();
104         
105         double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
106                                              9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
107         for( int i = 0; i < testArray.length; i++) {
108             u.addValue( testArray[i]);
109         }
110         
111         assertEquals("mean", 12.40455, u.getMean(), 0.0001);
112         assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
113         assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
114         assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
115     }
116 
117     public void testProductAndGeometricMean() throws Exception {
118         ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList());
119         u.setWindowSize(10);
120                 
121         u.addValue( 1.0 );
122         u.addValue( 2.0 );
123         u.addValue( 3.0 );
124         u.addValue( 4.0 );
125 
126         assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 );
127 
128         // Now test rolling - StorelessDescriptiveStatistics should discount the contribution
129         // of a discarded element
130         for( int i = 0; i < 10; i++ ) {
131             u.addValue( i + 2 );
132         }
133         // Values should be (2,3,4,5,6,7,8,9,10,11)
134         
135         assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 );
136 
137 
138     }
139     
140     /** test stats */
141     public void testSerialization() {
142         
143         DescriptiveStatistics u = new ListUnivariateImpl();
144         
145         assertEquals("total count",0,u.getN(),tolerance);
146         u.addValue(one);
147         u.addValue(two);
148         
149         DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); 
150  
151         u2.addValue(two);
152         u2.addValue(three);
153         
154         assertEquals("N",n,u2.getN(),tolerance);
155         assertEquals("sum",sum,u2.getSum(),tolerance);
156         assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
157         assertEquals("var",var,u2.getVariance(),tolerance);
158         assertEquals("std",std,u2.getStandardDeviation(),tolerance);
159         assertEquals("mean",mean,u2.getMean(),tolerance);
160         assertEquals("min",min,u2.getMin(),tolerance);
161         assertEquals("max",max,u2.getMax(),tolerance);
162 
163         u2.clear();
164         assertEquals("total count",0,u2.getN(),tolerance);    
165     }       
166 }
167