1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
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
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);
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
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 }