    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.math.stat.descriptive;
    17
    18  import java.util.ArrayList;
    19  import java.util.List;
    20
    21  import org.apache.commons.math.TestUtils;
    22
    23  import junit.framework.Test;
    24  import junit.framework.TestCase;
    25  import junit.framework.TestSuite;
    26
    27  /***
    28   * Test cases for the {@link Univariate} class.
    29   *
    30   * @version $Revision: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
    31   */
    32
    33  public final class ListUnivariateImplTest extends TestCase {
    34
    35      private double one = 1;
    36      private float two = 2;
    37      private int three = 3;
    38
    39      private double mean = 2;
    40      private double sumSq = 18;
    41      private double sum = 8;
    42      private double var = 0.666666666666666666667;
    43      private double std = Math.sqrt(var);
    44      private double n = 4;
    45      private double min = 1;
    46      private double max = 3;
    47      private double skewness = 0;
    48      private double kurtosis = 0.5;
    49      private double tolerance = 10E-15;
    50
    51      public ListUnivariateImplTest(String name) {
    52          super(name);
    53      }
    54
    55      public void setUp() {
    56      }
    57
    58      public static Test suite() {
    59          TestSuite suite = new TestSuite(ListUnivariateImplTest.class);
    60          suite.setName("Frequency Tests");
    61          return suite;
    62      }
    63
    64      /*** test stats */
    65      public void testStats() {
    66          List externalList = new ArrayList();
    67
    68          DescriptiveStatistics u = new ListUnivariateImpl( externalList );
    69
    70          assertEquals("total count",0,u.getN(),tolerance);
    71          u.addValue(one);
    72          u.addValue(two);
    73          u.addValue(two);
    74          u.addValue(three);
    75          assertEquals("N",n,u.getN(),tolerance);
    76          assertEquals("sum",sum,u.getSum(),tolerance);
    77          assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
    78          assertEquals("var",var,u.getVariance(),tolerance);
    79          assertEquals("std",std,u.getStandardDeviation(),tolerance);
    80          assertEquals("mean",mean,u.getMean(),tolerance);
    81          assertEquals("min",min,u.getMin(),tolerance);
    82          assertEquals("max",max,u.getMax(),tolerance);
    83          u.clear();
    84          assertEquals("total count",0,u.getN(),tolerance);
    85      }
    86
    87      public void testN0andN1Conditions() throws Exception {
    88          List list = new ArrayList();
    89
    90          DescriptiveStatistics u = new ListUnivariateImpl( list );
    91
    92          assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
    93          assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
    94          assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
    95
    96          list.add( new Double(one));
    97
    98          assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
    99          assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
    100         assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);
    101     }
    102
    103     public void testSkewAndKurtosis() {
    104         DescriptiveStatistics u = DescriptiveStatistics.newInstance();
    105
    106         double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
    107                                              9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
    108         for( int i = 0; i < testArray.length; i++) {
    109             u.addValue( testArray[i]);
    110         }
    111
    112         assertEquals("mean", 12.40455, u.getMean(), 0.0001);
    113         assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
    114         assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
    115         assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
    116     }
    117
    118     public void testProductAndGeometricMean() throws Exception {
    119         ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList());
    120         u.setWindowSize(10);
    121
    122         u.addValue( 1.0 );
    123         u.addValue( 2.0 );
    124         u.addValue( 3.0 );
    125         u.addValue( 4.0 );
    126
    127         assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 );
    128
    129         // Now test rolling - StorelessDescriptiveStatistics should discount the contribution
    130         // of a discarded element
    131         for( int i = 0; i < 10; i++ ) {
    132             u.addValue( i + 2 );
    133         }
    134         // Values should be (2,3,4,5,6,7,8,9,10,11)
    135
    136         assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 );
    137
    138
    139     }
    140
    141     /*** test stats */
    142     public void testSerialization() {
    143
    144         DescriptiveStatistics u = null;
    145
    146         try {
    147             u = DescriptiveStatistics.newInstance(ListUnivariateImpl.class);
    148         } catch (InstantiationException e) {
    149             fail(e.getMessage());
    150         } catch (IllegalAccessException e) {
    151             fail(e.getMessage());
    152         }
    153
    154         assertEquals("total count",0,u.getN(),tolerance);
    155         u.addValue(one);
    156         u.addValue(two);
    157
    158         DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
    159
    160         u2.addValue(two);
    161         u2.addValue(three);
    162
    163         assertEquals("N",n,u2.getN(),tolerance);
    164         assertEquals("sum",sum,u2.getSum(),tolerance);
    165         assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
    166         assertEquals("var",var,u2.getVariance(),tolerance);
    167         assertEquals("std",std,u2.getStandardDeviation(),tolerance);
    168         assertEquals("mean",mean,u2.getMean(),tolerance);
    169         assertEquals("min",min,u2.getMin(),tolerance);
    170         assertEquals("max",max,u2.getMax(),tolerance);
    171
    172         u2.clear();
    173         assertEquals("total count",0,u2.getN(),tolerance);
    174     }
    175 }
    176