View Javadoc
1   /*
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
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
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.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat.descriptive.moment;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  
27  import org.hipparchus.stat.StatUtils;
28  import org.hipparchus.stat.descriptive.StorelessUnivariateStatisticAbstractTest;
29  import org.hipparchus.util.FastMath;
30  import org.junit.Test;
31  
32  /**
33   * Test cases for the {@link StandardDeviation} class.
34   */
35  public class StandardDeviationTest extends StorelessUnivariateStatisticAbstractTest{
36  
37      @Override
38      public StandardDeviation getUnivariateStatistic() {
39          return new StandardDeviation();
40      }
41  
42      @Override
43      public double expectedValue() {
44          return this.std;
45      }
46  
47      /**
48       * Make sure Double.NaN is returned iff n = 0
49       */
50      @Test
51      public void testNaN() {
52          StandardDeviation std = getUnivariateStatistic();
53          assertTrue(Double.isNaN(std.getResult()));
54          std.increment(1d);
55          assertEquals(0d, std.getResult(), 0);
56      }
57  
58      /**
59       * Test population version of variance
60       */
61      @Test
62      public void testPopulation() {
63          double[] values = { -1.0d, 3.1d, 4.0d, -2.1d, 22d, 11.7d, 3d, 14d };
64          double sigma = populationStandardDeviation(values);
65          SecondMoment m = new SecondMoment();
66          m.incrementAll(values);  // side effect is to add values
67  
68          StandardDeviation s1 = getUnivariateStatistic();
69          s1 = s1.withBiasCorrection(false);
70          assertEquals(sigma, s1.evaluate(values), 1E-14);
71          s1.incrementAll(values);
72          assertEquals(sigma, s1.getResult(), 1E-14);
73          s1 = new StandardDeviation(false, m);
74          assertEquals(sigma, s1.getResult(), 1E-14);
75          s1 = new StandardDeviation(false);
76          assertEquals(sigma, s1.evaluate(values), 1E-14);
77          s1.incrementAll(values);
78          assertEquals(sigma, s1.getResult(), 1E-14);
79      }
80  
81      /**
82       * Definitional formula for population standard deviation
83       */
84      protected double populationStandardDeviation(double[] v) {
85          double mean = StatUtils.mean(v);
86          double sum = 0;
87          for (double val : v) {
88              sum += (val - mean) * (val - mean);
89          }
90          return FastMath.sqrt(sum / v.length);
91      }
92  
93  }