1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.stat.descriptive.moment;
18
19 import org.apache.commons.math4.legacy.stat.descriptive.StorelessUnivariateStatisticAbstractTest;
20 import org.apache.commons.math4.legacy.stat.descriptive.UnivariateStatistic;
21 import org.apache.commons.math4.legacy.core.MathArrays;
22 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.junit.jupiter.api.Assertions;
26
27
28
29
30
31 public class VarianceTest extends StorelessUnivariateStatisticAbstractTest{
32
33 protected Variance stat;
34
35
36
37
38 @Override
39 public UnivariateStatistic getUnivariateStatistic() {
40 return new Variance();
41 }
42
43
44
45
46 @Override
47 public double expectedValue() {
48 return this.var;
49 }
50
51
52 public double expectedWeightedValue() {
53 return this.weightedVar;
54 }
55
56
57
58
59
60 @Test
61 public void testNaN() {
62 StandardDeviation std = new StandardDeviation();
63 Assert.assertTrue(Double.isNaN(std.getResult()));
64 std.increment(1d);
65 Assert.assertEquals(0d, std.getResult(), 0);
66 }
67
68
69
70
71 @Test
72 public void testPopulation() {
73 double[] values = {-1.0d, 3.1d, 4.0d, -2.1d, 22d, 11.7d, 3d, 14d};
74 SecondMoment m = new SecondMoment();
75 m.incrementAll(values);
76 Variance v1 = new Variance();
77 v1.setBiasCorrected(false);
78 Assert.assertEquals(populationVariance(values), v1.evaluate(values), 1E-14);
79 v1.incrementAll(values);
80 Assert.assertEquals(populationVariance(values), v1.getResult(), 1E-14);
81 v1 = new Variance(false, m);
82 Assert.assertEquals(populationVariance(values), v1.getResult(), 1E-14);
83 v1 = new Variance(false);
84 Assert.assertEquals(populationVariance(values), v1.evaluate(values), 1E-14);
85 v1.incrementAll(values);
86 Assert.assertEquals(populationVariance(values), v1.getResult(), 1E-14);
87 }
88
89
90
91
92 protected double populationVariance(double[] v) {
93 double mean = new Mean().evaluate(v);
94 double sum = 0;
95 for (int i = 0; i < v.length; i++) {
96 sum += (v[i] - mean) * (v[i] - mean);
97 }
98 return sum / v.length;
99 }
100
101 @Test
102 public void testWeightedVariance() {
103 Variance variance = new Variance();
104 Assert.assertEquals(expectedWeightedValue(),
105 variance.evaluate(testArray, testWeightsArray, 0, testArray.length), getTolerance());
106
107
108 Assert.assertEquals(expectedValue(),
109 variance.evaluate(testArray, unitWeightsArray, 0, testArray.length), getTolerance());
110
111
112
113 Assert.assertEquals(expectedValue(),
114 variance.evaluate(testArray, MathArrays.normalizeArray(identicalWeightsArray, testArray.length),
115 0, testArray.length), getTolerance());
116 }
117
118 @Test
119 public void testZeroWeights() {
120 Variance variance = new Variance();
121 final double[] values = {1, 2, 3, 4};
122 final double[] weights = new double[values.length];
123
124
125 Assertions.assertThrows(MathIllegalArgumentException.class, () -> {
126 variance.evaluate(values, weights);
127 });
128
129
130 final int begin = 1;
131 final int zeroLength = 0;
132 Assertions.assertEquals(Double.NaN, variance.evaluate(values, weights, begin, zeroLength));
133
134
135 Assertions.assertThrows(MathIllegalArgumentException.class, () -> {
136 variance.evaluate(values, weights, begin, zeroLength + 1);
137 });
138
139 weights[begin] = Double.MIN_VALUE;
140 Assertions.assertEquals(0.0, variance.evaluate(values, weights, begin, zeroLength + 1));
141 }
142 }