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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.stat.descriptive;
19
import java.util.ArrayList;
20
import java.util.List;
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;
29
* Test cases for the {@link ListUnivariateImpl} class.
31
* @version $Revision: 618202 $ $Date: 2008-02-04 01:44:36 -0700 (Mon, 04 Feb 2008) $
34
public final class MixedListUnivariateImplTest extends TestCase {
35
private double one = 1;
36
private float two = 2;
37
private int three = 3;
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);
45
private double min = 1;
46
private double max = 3;
47
private double tolerance = 10E-15;
49
private TransformerMap transformers = new TransformerMap();
51
public MixedListUnivariateImplTest(String name) {
53
transformers = new TransformerMap();
55
transformers.putTransformer(Foo.class, new FooTransformer());
57
transformers.putTransformer(Bar.class, new BarTransformer());
64
public static Test suite() {
65
TestSuite suite = new TestSuite(MixedListUnivariateImplTest.class);
66
suite.setName("Mixed List Tests");
71
public void testStats() {
72
List externalList = new ArrayList();
74
DescriptiveStatistics u = new ListUnivariateImpl(externalList,transformers);
76
assertEquals("total count", 0, u.getN(), tolerance);
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);
90
assertEquals("total count", 0, u.getN(), tolerance);
93
public void testN0andN1Conditions() throws Exception {
94
DescriptiveStatistics u = new ListUnivariateImpl(new ArrayList(),transformers);
97
"Mean of n = 0 set should be NaN",
98
Double.isNaN(u.getMean()));
100
"Standard Deviation of n = 0 set should be NaN",
101
Double.isNaN(u.getStandardDeviation()));
103
"Variance of n = 0 set should be NaN",
104
Double.isNaN(u.getVariance()));
109
"Mean of n = 1 set should be value of single item n1, instead it is " + u.getMean() ,
113
"StdDev of n = 1 set should be zero, instead it is: "
114
+ u.getStandardDeviation(),
115
u.getStandardDeviation() == 0);
117
"Variance of n = 1 set should be zero",
118
u.getVariance() == 0);
121
public void testSkewAndKurtosis() {
122
ListUnivariateImpl u =
123
new ListUnivariateImpl(new ArrayList(), transformers);
126
u.addObject(new Integer(12));
129
u.addObject(new Foo());
131
u.addObject(new Long(21));
135
u.addObject(new Float(14.1));
138
u.addObject(new Bar());
141
u.addObject(new Double(19.8));
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);
155
public void testProductAndGeometricMean() throws Exception {
156
ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList(),transformers);
165
"Geometric mean not expected",
167
u.getGeometricMean(),
170
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
171
// of a discarded element
172
for (int i = 0; i < 10; i++) {
175
// Values should be (2,3,4,5,6,7,8,9,10,11)
177
"Geometric mean not expected",
179
u.getGeometricMean(),
184
public static final class Foo {
185
public String heresFoo() {
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());
197
public static final class Bar {
198
public String heresBar() {
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());