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.MathArrays;
30  import org.junit.Test;
31  
32  /**
33   * Test cases for the {@link Variance} class.
34   */
35  public class VarianceTest extends StorelessUnivariateStatisticAbstractTest{
36  
37      @Override
38      public Variance getUnivariateStatistic() {
39          return new Variance();
40      }
41  
42      @Override
43      public double expectedValue() {
44          return this.var;
45      }
46  
47      /** Expected value for  the testArray defined in UnivariateStatisticAbstractTest */
48      public double expectedWeightedValue() {
49          return this.weightedVar;
50      }
51  
52      /**
53       * Make sure Double.NaN is returned iff n = 0
54       */
55      @Test
56      public void testNaN() {
57          Variance var = getUnivariateStatistic();
58          assertTrue(Double.isNaN(var.getResult()));
59          var.increment(1d);
60          assertEquals(0d, var.getResult(), 0);
61      }
62  
63      /**
64       * Test population version of variance
65       */
66      @Test
67      public void testPopulation() {
68          double[] values = { -1.0d, 3.1d, 4.0d, -2.1d, 22d, 11.7d, 3d, 14d };
69          SecondMoment m = new SecondMoment();
70          m.incrementAll(values);  // side effect is to add values
71          Variance v1 = new Variance();
72          v1 = v1.withBiasCorrection(false);
73          assertEquals(populationVariance(values), v1.evaluate(values), 1E-14);
74          v1.incrementAll(values);
75          assertEquals(populationVariance(values), v1.getResult(), 1E-14);
76          v1 = new Variance(false, m);
77          assertEquals(populationVariance(values), v1.getResult(), 1E-14);
78          v1 = new Variance(false);
79          assertEquals(populationVariance(values), v1.evaluate(values), 1E-14);
80          v1.incrementAll(values);
81          assertEquals(populationVariance(values), v1.getResult(), 1E-14);
82      }
83  
84      /**
85       * Definitional formula for population variance
86       */
87      protected double populationVariance(double[] v) {
88          double mean = StatUtils.mean(v);
89          double sum = 0;
90          for (double val : v) {
91             sum += (val - mean) * (val - mean);
92          }
93          return sum / v.length;
94      }
95  
96      @Test
97      public void testWeightedVariance() {
98          Variance variance = getUnivariateStatistic();
99          assertEquals(expectedWeightedValue(),
100                      variance.evaluate(testArray, testWeightsArray, 0, testArray.length),
101                      getTolerance());
102 
103         // All weights = 1 -> weighted variance = unweighted variance
104         assertEquals(expectedValue(),
105                      variance.evaluate(testArray, unitWeightsArray, 0, testArray.length),
106                      getTolerance());
107 
108         // All weights the same -> when weights are normalized to sum to the length of the values array,
109         // weighted variance = unweighted value
110         assertEquals(expectedValue(),
111                      variance.evaluate(testArray,
112                                        MathArrays.normalizeArray(identicalWeightsArray, testArray.length),
113                                        0, testArray.length),
114                      getTolerance());
115 
116     }
117 
118 }