001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.stat.descriptive;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.commons.math.TestUtils;
023    
024    import junit.framework.Test;
025    import junit.framework.TestCase;
026    import junit.framework.TestSuite;
027    
028    /**
029     * Test cases for the {@link ListUnivariateImpl} class.
030     *
031     * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
032     */
033    
034    public final class ListUnivariateImplTest extends TestCase {
035        
036        private double one = 1;
037        private float two = 2;
038        private int three = 3;
039    
040        private double mean = 2;
041        private double sumSq = 18;
042        private double sum = 8;
043        private double var = 0.666666666666666666667;
044        private double std = Math.sqrt(var);
045        private double n = 4;
046        private double min = 1;
047        private double max = 3;
048        private double tolerance = 10E-15;
049        
050        public ListUnivariateImplTest(String name) {
051            super(name);
052        }
053        
054        public static Test suite() {
055            TestSuite suite = new TestSuite(ListUnivariateImplTest.class);
056            suite.setName("Frequency Tests");
057            return suite;
058        }
059        
060        /** test stats */
061        public void testStats() {
062            List<Object> externalList = new ArrayList<Object>();
063            
064            DescriptiveStatistics u = new ListUnivariateImpl( externalList ); 
065    
066            assertEquals("total count",0,u.getN(),tolerance);
067            u.addValue(one);
068            u.addValue(two);
069            u.addValue(two);
070            u.addValue(three);
071            assertEquals("N",n,u.getN(),tolerance);
072            assertEquals("sum",sum,u.getSum(),tolerance);
073            assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
074            assertEquals("var",var,u.getVariance(),tolerance);
075            assertEquals("std",std,u.getStandardDeviation(),tolerance);
076            assertEquals("mean",mean,u.getMean(),tolerance);
077            assertEquals("min",min,u.getMin(),tolerance);
078            assertEquals("max",max,u.getMax(),tolerance);
079            u.clear();
080            assertEquals("total count",0,u.getN(),tolerance);    
081        }     
082        
083        public void testN0andN1Conditions() throws Exception {
084            List<Object> list = new ArrayList<Object>();
085            
086            DescriptiveStatistics u = new ListUnivariateImpl( list );
087                    
088            assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
089            assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
090            assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
091    
092            list.add( Double.valueOf(one));
093    
094            assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
095            assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
096            assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);  
097        }
098        
099        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