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;
18
19 import org.apache.commons.math4.legacy.TestUtils;
20 import org.apache.commons.math4.legacy.stat.descriptive.moment.SecondMoment;
21 import org.apache.commons.math4.core.jdkmath.JdkMath;
22 import org.junit.Assert;
23 import org.junit.Test;
24
25
26
27
28 public abstract class StorelessUnivariateStatisticAbstractTest
29 extends UnivariateStatisticAbstractTest {
30
31
32 protected double[][] smallSamples = {{}, {1}, {1,2}, {1,2,3}, {1,2,3,4}};
33
34
35 @Override
36 public abstract UnivariateStatistic getUnivariateStatistic();
37
38
39 @Override
40 public abstract double expectedValue();
41
42
43
44
45 @Test
46 public void testIncrementation() {
47
48 StorelessUnivariateStatistic statistic =
49 (StorelessUnivariateStatistic) getUnivariateStatistic();
50
51
52 for (int i = 0; i < testArray.length; i++) {
53 statistic.increment(testArray[i]);
54 }
55
56 Assert.assertEquals(expectedValue(), statistic.getResult(), getTolerance());
57 Assert.assertEquals(testArray.length, statistic.getN());
58
59 statistic.clear();
60
61
62 statistic.incrementAll(testArray);
63 Assert.assertEquals(expectedValue(), statistic.getResult(), getTolerance());
64 Assert.assertEquals(testArray.length, statistic.getN());
65
66 statistic.clear();
67
68
69 checkClearValue(statistic);
70 Assert.assertEquals(0, statistic.getN());
71 }
72
73 protected void checkClearValue(StorelessUnivariateStatistic statistic){
74 Assert.assertTrue(Double.isNaN(statistic.getResult()));
75 }
76
77 @Test
78 public void testEqualsAndHashCode() {
79 StorelessUnivariateStatistic statistic =
80 (StorelessUnivariateStatistic) getUnivariateStatistic();
81 StorelessUnivariateStatistic statistic2 = null;
82
83 Assert.assertFalse("non-null, compared to null", statistic.equals(statistic2));
84 Assert.assertEquals("reflexive, non-null", statistic, statistic);
85
86 int emptyHash = statistic.hashCode();
87 statistic2 = (StorelessUnivariateStatistic) getUnivariateStatistic();
88 Assert.assertEquals("empty stats should be equal", statistic, statistic2);
89 Assert.assertEquals("empty stats should have the same hash code",
90 emptyHash, statistic2.hashCode());
91
92 statistic.increment(1d);
93 Assert.assertEquals("reflexive, non-empty", statistic, statistic);
94 Assert.assertNotEquals("non-empty, compared to empty", statistic, statistic2);
95 Assert.assertNotEquals("non-empty, compared to empty", statistic2, statistic);
96 Assert.assertTrue("non-empty stat should have different hash code from empty stat",
97 statistic.hashCode() != emptyHash);
98
99 statistic2.increment(1d);
100 Assert.assertEquals("stats with same data should be equal", statistic, statistic2);
101 Assert.assertEquals("stats with same data should have the same hash code",
102 statistic.hashCode(), statistic2.hashCode());
103
104 statistic.increment(Double.POSITIVE_INFINITY);
105 Assert.assertNotEquals("stats with different n's should not be equal", statistic2, statistic);
106 Assert.assertTrue("stats with different n's should have different hash codes",
107 statistic.hashCode() != statistic2.hashCode());
108
109 statistic2.increment(Double.POSITIVE_INFINITY);
110 Assert.assertEquals("stats with same data should be equal", statistic, statistic2);
111 Assert.assertEquals("stats with same data should have the same hash code",
112 statistic.hashCode(), statistic2.hashCode());
113
114 statistic.clear();
115 statistic2.clear();
116 Assert.assertEquals("cleared stats should be equal", statistic, statistic2);
117 Assert.assertEquals("cleared stats should have thash code of empty stat",
118 emptyHash, statistic2.hashCode());
119 Assert.assertEquals("cleared stats should have thash code of empty stat",
120 emptyHash, statistic.hashCode());
121 }
122
123 @Test
124 public void testMomentSmallSamples() {
125 UnivariateStatistic stat = getUnivariateStatistic();
126 if (stat instanceof SecondMoment) {
127 SecondMoment moment = (SecondMoment) getUnivariateStatistic();
128 Assert.assertTrue(Double.isNaN(moment.getResult()));
129 moment.increment(1d);
130 Assert.assertEquals(0d, moment.getResult(), 0);
131 }
132 }
133
134
135
136
137
138 @Test
139 public void testConsistency() {
140 StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) getUnivariateStatistic();
141 stat.incrementAll(testArray);
142 Assert.assertEquals(stat.getResult(), stat.evaluate(testArray), getTolerance());
143 for (int i = 0; i < smallSamples.length; i++) {
144 stat.clear();
145 for (int j =0; j < smallSamples[i].length; j++) {
146 stat.increment(smallSamples[i][j]);
147 }
148 TestUtils.assertEquals(stat.getResult(), stat.evaluate(smallSamples[i]), getTolerance());
149 }
150 }
151
152
153
154
155
156 @Test
157 public void testCopyConsistency() {
158
159 StorelessUnivariateStatistic master =
160 (StorelessUnivariateStatistic) getUnivariateStatistic();
161
162 StorelessUnivariateStatistic replica = null;
163
164
165 long index = JdkMath.round((JdkMath.random()) * testArray.length);
166
167
168 master.incrementAll(testArray, 0, (int) index);
169 replica = master.copy();
170
171
172 Assert.assertEquals(replica, master);
173 Assert.assertEquals(master, replica);
174
175
176 master.incrementAll(testArray,
177 (int) index, (int) (testArray.length - index));
178 replica.incrementAll(testArray,
179 (int) index, (int) (testArray.length - index));
180 Assert.assertEquals(replica, master);
181 Assert.assertEquals(master, replica);
182 }
183
184
185
186
187 @Test
188 public void testEvaluateInternalState() {
189 StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) getUnivariateStatistic();
190 stat.evaluate(testArray);
191 Assert.assertEquals(0, stat.getN());
192
193 stat.incrementAll(testArray);
194
195 StorelessUnivariateStatistic savedStatistic = stat.copy();
196
197 Assert.assertNotEquals(stat.getResult(), stat.evaluate(testArray, 0, 5), getTolerance());
198
199 Assert.assertEquals(savedStatistic.getResult(), stat.getResult(), 0.0);
200 Assert.assertEquals(savedStatistic.getN(), stat.getN());
201 }
202 }