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