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.TestUtils;
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: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
34
public final class ListUnivariateImplTest extends TestCase {
36
private double one = 1;
37
private float two = 2;
38
private int three = 3;
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);
46
private double min = 1;
47
private double max = 3;
48
private double tolerance = 10E-15;
50
public ListUnivariateImplTest(String name) {
54
public static Test suite() {
55
TestSuite suite = new TestSuite(ListUnivariateImplTest.class);
56
suite.setName("Frequency Tests");
61
public void testStats() {
62
List<Object> externalList = new ArrayList<Object>();
64
DescriptiveStatistics u = new ListUnivariateImpl( externalList );
66
assertEquals("total count",0,u.getN(),tolerance);
71
assertEquals("N",n,u.getN(),tolerance);
72
assertEquals("sum",sum,u.getSum(),tolerance);
73
assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
74
assertEquals("var",var,u.getVariance(),tolerance);
75
assertEquals("std",std,u.getStandardDeviation(),tolerance);
76
assertEquals("mean",mean,u.getMean(),tolerance);
77
assertEquals("min",min,u.getMin(),tolerance);
78
assertEquals("max",max,u.getMax(),tolerance);
80
assertEquals("total count",0,u.getN(),tolerance);
83
public void testN0andN1Conditions() throws Exception {
84
List<Object> list = new ArrayList<Object>();
86
DescriptiveStatistics u = new ListUnivariateImpl( list );
88
assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
89
assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
90
assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
92
list.add( Double.valueOf(one));
94
assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
95
assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
96
assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);
99
public void testSkewAndKurtosis() {
100
DescriptiveStatistics u = new DescriptiveStatistics();
102
double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
103
9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
104
for( int i = 0; i < testArray.length; i++) {
105
u.addValue( testArray[i]);
108
assertEquals("mean", 12.40455, u.getMean(), 0.0001);
109
assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
110
assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
111
assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
114
public void testProductAndGeometricMean() throws Exception {
115
ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList<Object>());
123
assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 );
125
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
126
// of a discarded element
127
for( int i = 0; i < 10; i++ ) {
130
// Values should be (2,3,4,5,6,7,8,9,10,11)
132
assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 );
138
public void testSerialization() {
140
DescriptiveStatistics u = new ListUnivariateImpl();
142
assertEquals("total count",0,u.getN(),tolerance);
146
DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
151
assertEquals("N",n,u2.getN(),tolerance);
152
assertEquals("sum",sum,u2.getSum(),tolerance);
153
assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
154
assertEquals("var",var,u2.getVariance(),tolerance);
155
assertEquals("std",std,u2.getStandardDeviation(),tolerance);
156
assertEquals("mean",mean,u2.getMean(),tolerance);
157
assertEquals("min",min,u2.getMin(),tolerance);
158
assertEquals("max",max,u2.getMax(),tolerance);
161
assertEquals("total count",0,u2.getN(),tolerance);