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