001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive.moment;
017:
018: import junit.framework.Test;
019: import junit.framework.TestSuite;
020:
021: import org.apache.commons.math.stat.descriptive.StorelessUnivariateStatisticAbstractTest;
022: import org.apache.commons.math.stat.descriptive.UnivariateStatistic;
023:
024: /**
025: * Test cases for the {@link UnivariateStatistic} class.
026: *
027: * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
028: */
029: public class VarianceTest extends
030: StorelessUnivariateStatisticAbstractTest {
031:
032: protected Variance stat;
033:
034: /**
035: * @param name
036: */
037: public VarianceTest(String name) {
038: super (name);
039: }
040:
041: /* (non-Javadoc)
042: * @see org.apache.commons.math.stat.descriptive.UnivariateStatisticAbstractTest#getUnivariateStatistic()
043: */
044: public UnivariateStatistic getUnivariateStatistic() {
045: return new Variance();
046: }
047:
048: public static Test suite() {
049: TestSuite suite = new TestSuite(VarianceTest.class);
050: suite.setName("Variance Tests");
051: return suite;
052: }
053:
054: /* (non-Javadoc)
055: * @see org.apache.commons.math.stat.descriptive.UnivariateStatisticAbstractTest#expectedValue()
056: */
057: public double expectedValue() {
058: return this .var;
059: }
060:
061: /**
062: * Make sure Double.NaN is returned iff n = 0
063: *
064: */
065: public void testNaN() {
066: StandardDeviation std = new StandardDeviation();
067: assertTrue(Double.isNaN(std.getResult()));
068: std.increment(1d);
069: assertEquals(0d, std.getResult(), 0);
070: }
071:
072: /**
073: * Test population version of variance
074: */
075: public void testPopulation() {
076: double[] values = { -1.0d, 3.1d, 4.0d, -2.1d, 22d, 11.7d, 3d,
077: 14d };
078: SecondMoment m = new SecondMoment();
079: m.evaluate(values); // side effect is to add values
080: Variance v1 = new Variance();
081: v1.setBiasCorrected(false);
082: assertEquals(populationVariance(values), v1.evaluate(values),
083: 1E-14);
084: v1.incrementAll(values);
085: assertEquals(populationVariance(values), v1.getResult(), 1E-14);
086: v1 = new Variance(false, m);
087: assertEquals(populationVariance(values), v1.getResult(), 1E-14);
088: v1 = new Variance(false);
089: assertEquals(populationVariance(values), v1.evaluate(values),
090: 1E-14);
091: v1.incrementAll(values);
092: assertEquals(populationVariance(values), v1.getResult(), 1E-14);
093: }
094:
095: /**
096: * Definitional formula for population variance
097: */
098: protected double populationVariance(double[] v) {
099: double mean = new Mean().evaluate(v);
100: double sum = 0;
101: for (int i = 0; i < v.length; i++) {
102: sum += (v[i] - mean) * (v[i] - mean);
103: }
104: return sum / (double) v.length;
105: }
106:
107: }
|