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 StandardDeviationTest extends StorelessUnivariateStatisticAbstractTest{
031    
032        protected StandardDeviation stat;
033        
034        /**
035         * @param name
036         */
037        public StandardDeviationTest(String name) {
038            super(name);
039        }
040    
041        /**
042         * {@inheritDoc}
043         */
044        @Override
045        public UnivariateStatistic getUnivariateStatistic() {
046            return new StandardDeviation();
047        }
048    
049        public static Test suite() {
050            TestSuite suite = new TestSuite(StandardDeviationTest.class);
051            suite.setName("StandardDeviation Tests");
052            return suite;
053        }
054        
055        /**
056         * {@inheritDoc}
057         */
058        @Override
059        public double expectedValue() {
060            return this.std;
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            double sigma = populationStandardDeviation(values);
080            SecondMoment m = new SecondMoment();
081            m.evaluate(values);  // side effect is to add values
082            StandardDeviation s1 = new StandardDeviation();
083            s1.setBiasCorrected(false);
084            assertEquals(sigma, s1.evaluate(values), 1E-14);
085            s1.incrementAll(values);
086            assertEquals(sigma, s1.getResult(), 1E-14);
087            s1 = new StandardDeviation(false, m);
088            assertEquals(sigma, s1.getResult(), 1E-14);     
089            s1 = new StandardDeviation(false);
090            assertEquals(sigma, s1.evaluate(values), 1E-14);
091            s1.incrementAll(values);
092            assertEquals(sigma, s1.getResult(), 1E-14);     
093        }
094        
095        /**
096         * Definitional formula for population standard deviation
097         */
098        protected double populationStandardDeviation(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 Math.sqrt(sum / v.length);
105        }
106    
107    }