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