    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.util.NumberTransformer;
    22  import org.apache.commons.math.util.TransformerMap;
    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: 1.1 $ $Date: 2004/10/08 05:08:19 $
    31   */
    32
    33  public final class MixedListUnivariateImplTest extends TestCase {
    34      private double one = 1;
    35      private float two = 2;
    36      private int three = 3;
    37
    38      private double mean = 2;
    39      private double sumSq = 18;
    40      private double sum = 8;
    41      private double var = 0.666666666666666666667;
    42      private double std = Math.sqrt(var);
    43      private double n = 4;
    44      private double min = 1;
    45      private double max = 3;
    46      private double skewness = 0;
    47      private double kurtosis = 0.5;
    48      private double tolerance = 10E-15;
    49
    50      private TransformerMap transformers = new TransformerMap();
    51
    52      public MixedListUnivariateImplTest(String name) {
    53          super(name);
    54          transformers = new TransformerMap();
    55
    56          transformers.putTransformer(Foo.class, new NumberTransformer() {
    57              public double transform(Object o) {
    58                  return Double.parseDouble(((Foo) o).heresFoo());
    59              }
    60          });
    61
    62          transformers.putTransformer(Bar.class, new NumberTransformer() {
    63              public double transform(Object o) {
    64                  return Double.parseDouble(((Bar) o).heresBar());
    65              }
    66
    67          });
    68
    69      }
    70
    71      public void setUp() {
    72      }
    73
    74      public static Test suite() {
    75          TestSuite suite = new TestSuite(MixedListUnivariateImplTest.class);
    76          suite.setName("Mixed List Tests");
    77          return suite;
    78      }
    79
    80      /*** test stats */
    81      public void testStats() {
    82          List externalList = new ArrayList();
    83
    84          DescriptiveStatistics u = new ListUnivariateImpl(externalList,transformers);
    85
    86          assertEquals("total count", 0, u.getN(), tolerance);
    87          u.addValue(one);
    88          u.addValue(two);
    89          u.addValue(two);
    90          u.addValue(three);
    91          assertEquals("N", n, u.getN(), tolerance);
    92          assertEquals("sum", sum, u.getSum(), tolerance);
    93          assertEquals("sumsq", sumSq, u.getSumsq(), tolerance);
    94          assertEquals("var", var, u.getVariance(), tolerance);
    95          assertEquals("std", std, u.getStandardDeviation(), tolerance);
    96          assertEquals("mean", mean, u.getMean(), tolerance);
    97          assertEquals("min", min, u.getMin(), tolerance);
    98          assertEquals("max", max, u.getMax(), tolerance);
    99          u.clear();
    100         assertEquals("total count", 0, u.getN(), tolerance);
    101     }
    102
    103     public void testN0andN1Conditions() throws Exception {
    104         List list = new ArrayList();
    105
    106         DescriptiveStatistics u = new ListUnivariateImpl(new ArrayList(),transformers);
    107
    108         assertTrue(
    109             "Mean of n = 0 set should be NaN",
    110             Double.isNaN(u.getMean()));
    111         assertTrue(
    112             "Standard Deviation of n = 0 set should be NaN",
    113             Double.isNaN(u.getStandardDeviation()));
    114         assertTrue(
    115             "Variance of n = 0 set should be NaN",
    116             Double.isNaN(u.getVariance()));
    117
    118         u.addValue(one);
    119
    120         assertTrue(
    121             "Mean of n = 1 set should be value of single item n1, instead it is " + u.getMean() ,
    122             u.getMean() == one);
    123
    124         assertTrue(
    125             "StdDev of n = 1 set should be zero, instead it is: "
    126                 + u.getStandardDeviation(),
    127             u.getStandardDeviation() == 0);
    128         assertTrue(
    129             "Variance of n = 1 set should be zero",
    130             u.getVariance() == 0);
    131     }
    132
    133     public void testSkewAndKurtosis() {
    134         ListUnivariateImpl u =
    135             new ListUnivariateImpl(new ArrayList(), transformers);
    136
    137         u.addObject("12.5");
    138         u.addObject(new Integer(12));
    139         u.addObject("11.8");
    140         u.addObject("14.2");
    141         u.addObject(new Foo());
    142         u.addObject("14.5");
    143         u.addObject(new Long(21));
    144         u.addObject("8.2");
    145         u.addObject("10.3");
    146         u.addObject("11.3");
    147         u.addObject(new Float(14.1));
    148         u.addObject("9.9");
    149         u.addObject("12.2");
    150         u.addObject(new Bar());
    151         u.addObject("12.1");
    152         u.addObject("11");
    153         u.addObject(new Double(19.8));
    154         u.addObject("11");
    155         u.addObject("10");
    156         u.addObject("8.8");
    157         u.addObject("9");
    158         u.addObject("12.3");
    159
    160
    161         assertEquals("mean", 12.40455, u.getMean(), 0.0001);
    162         assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
    163         assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
    164         assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
    165     }
    166
    167     public void testProductAndGeometricMean() throws Exception {
    168         ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList(),transformers);
    169         u.setWindowSize(10);
    170
    171         u.addValue(1.0);
    172         u.addValue(2.0);
    173         u.addValue(3.0);
    174         u.addValue(4.0);
    175
    176         assertEquals(
    177             "Geometric mean not expected",
    178             2.213364,
    179             u.getGeometricMean(),
    180             0.00001);
    181
    182         // Now test rolling - StorelessDescriptiveStatistics should discount the contribution
    183         // of a discarded element
    184         for (int i = 0; i < 10; i++) {
    185             u.addValue(i + 2);
    186         }
    187         // Values should be (2,3,4,5,6,7,8,9,10,11)
    188         assertEquals(
    189             "Geometric mean not expected",
    190             5.755931,
    191             u.getGeometricMean(),
    192             0.00001);
    193
    194     }
    195
    196     public final class Foo {
    197         public String heresFoo() {
    198             return "14.9";
    199         }
    200     }
    201
    202     public final class Bar {
    203         public String heresBar() {
    204             return "12.0";
    205         }
    206     }
    207 }