1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.stat.descriptive;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22
23 import org.apache.commons.math4.legacy.TestUtils;
24 import org.apache.commons.statistics.distribution.DiscreteDistribution;
25 import org.apache.commons.statistics.distribution.ContinuousDistribution;
26 import org.apache.commons.math4.legacy.distribution.AbstractRealDistribution;
27 import org.apache.commons.statistics.distribution.UniformDiscreteDistribution;
28 import org.apache.commons.statistics.distribution.UniformContinuousDistribution;
29 import org.apache.commons.numbers.core.Precision;
30 import org.apache.commons.rng.simple.RandomSource;
31 import org.junit.Assert;
32 import org.junit.Test;
33
34
35
36
37
38 public class AggregateSummaryStatisticsTest {
39
40
41
42
43 @Test
44 public void testAggregation() {
45 AggregateSummaryStatistics aggregate = new AggregateSummaryStatistics();
46 SummaryStatistics setOneStats = aggregate.createContributingStatistics();
47 SummaryStatistics setTwoStats = aggregate.createContributingStatistics();
48
49 Assert.assertNotNull("The set one contributing stats are null", setOneStats);
50 Assert.assertNotNull("The set two contributing stats are null", setTwoStats);
51 Assert.assertNotSame("Contributing stats objects are the same", setOneStats, setTwoStats);
52
53 setOneStats.addValue(2);
54 setOneStats.addValue(3);
55 setOneStats.addValue(5);
56 setOneStats.addValue(7);
57 setOneStats.addValue(11);
58 Assert.assertEquals("Wrong number of set one values", 5, setOneStats.getN());
59 Assert.assertTrue("Wrong sum of set one values", Precision.equals(28.0, setOneStats.getSum(), 1));
60
61 setTwoStats.addValue(2);
62 setTwoStats.addValue(4);
63 setTwoStats.addValue(8);
64 Assert.assertEquals("Wrong number of set two values", 3, setTwoStats.getN());
65 Assert.assertTrue("Wrong sum of set two values", Precision.equals(14.0, setTwoStats.getSum(), 1));
66
67 Assert.assertEquals("Wrong number of aggregate values", 8, aggregate.getN());
68 Assert.assertTrue("Wrong aggregate sum", Precision.equals(42.0, aggregate.getSum(), 1));
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 @Test
85 public void testAggregationConsistency() {
86
87
88 double[] totalSample = generateSample();
89 double[][] subSamples = generatePartition(totalSample);
90 int nSamples = subSamples.length;
91
92
93 AggregateSummaryStatistics aggregate = new AggregateSummaryStatistics();
94 SummaryStatistics totalStats = new SummaryStatistics();
95
96
97 SummaryStatistics componentStats[] = new SummaryStatistics[nSamples];
98
99 for (int i = 0; i < nSamples; i++) {
100
101
102 componentStats[i] = aggregate.createContributingStatistics();
103
104
105 for (int j = 0; j < subSamples[i].length; j++) {
106 componentStats[i].addValue(subSamples[i][j]);
107 }
108 }
109
110
111 for (int i = 0; i < totalSample.length; i++) {
112 totalStats.addValue(totalSample[i]);
113 }
114
115
116
117
118
119
120
121
122 Assert.assertEquals(totalStats.getSummary(), aggregate.getSummary());
123 }
124
125
126
127
128
129
130
131
132 @Test
133 public void testAggregate() {
134
135
136 double[] totalSample = generateSample();
137 double[][] subSamples = generatePartition(totalSample);
138 int nSamples = subSamples.length;
139
140
141 SummaryStatistics totalStats = new SummaryStatistics();
142 for (int i = 0; i < totalSample.length; i++) {
143 totalStats.addValue(totalSample[i]);
144 }
145
146
147 SummaryStatistics[] subSampleStats = new SummaryStatistics[nSamples];
148 for (int i = 0; i < nSamples; i++) {
149 subSampleStats[i] = new SummaryStatistics();
150 }
151 Collection<SummaryStatistics> aggregate = new ArrayList<>();
152 for (int i = 0; i < nSamples; i++) {
153 for (int j = 0; j < subSamples[i].length; j++) {
154 subSampleStats[i].addValue(subSamples[i][j]);
155 }
156 aggregate.add(subSampleStats[i]);
157 }
158
159
160 StatisticalSummary aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate);
161 assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12);
162 }
163
164
165
166
167
168 @Test
169 public void testAggregateStatisticalSummary() {
170
171
172 double[] totalSample = generateSample();
173 double[][] subSamples = generatePartition(totalSample);
174 int nSamples = subSamples.length;
175
176
177 SummaryStatistics totalStats = new SummaryStatistics();
178 for (int i = 0; i < totalSample.length; i++) {
179 totalStats.addValue(totalSample[i]);
180 }
181
182
183 SummaryStatistics[] subSampleStats = new SummaryStatistics[nSamples];
184 for (int i = 0; i < nSamples; i++) {
185 subSampleStats[i] = new SummaryStatistics();
186 }
187 Collection<StatisticalSummary> aggregate = new ArrayList<>();
188 for (int i = 0; i < nSamples; i++) {
189 for (int j = 0; j < subSamples[i].length; j++) {
190 subSampleStats[i].addValue(subSamples[i][j]);
191 }
192 aggregate.add(subSampleStats[i].getSummary());
193 }
194
195
196 StatisticalSummary aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate);
197 assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12);
198 }
199
200
201 @Test
202 public void testAggregateDegenerate() {
203 double[] totalSample = {1, 2, 3, 4, 5};
204 double[][] subSamples = {{1}, {2}, {3}, {4}, {5}};
205
206
207 SummaryStatistics totalStats = new SummaryStatistics();
208 for (int i = 0; i < totalSample.length; i++) {
209 totalStats.addValue(totalSample[i]);
210 }
211
212
213 SummaryStatistics[] subSampleStats = new SummaryStatistics[5];
214 for (int i = 0; i < 5; i++) {
215 subSampleStats[i] = new SummaryStatistics();
216 }
217 Collection<SummaryStatistics> aggregate = new ArrayList<>();
218 for (int i = 0; i < 5; i++) {
219 for (int j = 0; j < subSamples[i].length; j++) {
220 subSampleStats[i].addValue(subSamples[i][j]);
221 }
222 aggregate.add(subSampleStats[i]);
223 }
224
225
226 StatisticalSummaryValues aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate);
227 assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12);
228 }
229
230 @Test
231 public void testAggregateSpecialValues() {
232 double[] totalSample = {Double.POSITIVE_INFINITY, 2, 3, Double.NaN, 5};
233 double[][] subSamples = {{Double.POSITIVE_INFINITY, 2}, {3}, {Double.NaN}, {5}};
234
235
236 SummaryStatistics totalStats = new SummaryStatistics();
237 for (int i = 0; i < totalSample.length; i++) {
238 totalStats.addValue(totalSample[i]);
239 }
240
241
242 SummaryStatistics[] subSampleStats = new SummaryStatistics[5];
243 for (int i = 0; i < 4; i++) {
244 subSampleStats[i] = new SummaryStatistics();
245 }
246 Collection<SummaryStatistics> aggregate = new ArrayList<>();
247 for (int i = 0; i < 4; i++) {
248 for (int j = 0; j < subSamples[i].length; j++) {
249 subSampleStats[i].addValue(subSamples[i][j]);
250 }
251 aggregate.add(subSampleStats[i]);
252 }
253
254
255 StatisticalSummaryValues aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate);
256 assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12);
257 }
258
259
260
261
262
263
264 protected static void assertEquals(StatisticalSummary expected, StatisticalSummary observed, double delta) {
265 TestUtils.assertEquals(expected.getMax(), observed.getMax(), 0);
266 TestUtils.assertEquals(expected.getMin(), observed.getMin(), 0);
267 Assert.assertEquals(expected.getN(), observed.getN());
268 TestUtils.assertEquals(expected.getSum(), observed.getSum(), delta);
269 TestUtils.assertEquals(expected.getMean(), observed.getMean(), delta);
270 TestUtils.assertEquals(expected.getStandardDeviation(), observed.getStandardDeviation(), delta);
271 TestUtils.assertEquals(expected.getVariance(), observed.getVariance(), delta);
272 }
273
274
275
276
277
278
279
280
281
282 private double[] generateSample() {
283 final DiscreteDistribution.Sampler size =
284 UniformDiscreteDistribution.of(10, 100).createSampler(RandomSource.WELL_512_A.create(327652));
285 final ContinuousDistribution.Sampler randomData
286 = UniformContinuousDistribution.of(-100, 100).createSampler(RandomSource.WELL_512_A.create(64925784252L));
287 final int sampleSize = size.sample();
288 final double[] out = AbstractRealDistribution.sample(sampleSize, randomData);
289 return out;
290 }
291
292
293
294
295
296
297
298
299 private double[][] generatePartition(double[] sample) {
300 final int length = sample.length;
301 final double[][] out = new double[5][];
302 int cur = 0;
303 int offset = 0;
304 int sampleCount = 0;
305 for (int i = 0; i < 5; i++) {
306 if (cur == length || offset == length) {
307 break;
308 }
309 final int next;
310 if (i == 4 || cur == length - 1) {
311 next = length - 1;
312 } else {
313 final DiscreteDistribution.Sampler sampler =
314 UniformDiscreteDistribution.of(cur, length - 1).createSampler(RandomSource.WELL_512_A.create());
315 next = sampler.sample();
316 }
317 final int subLength = next - cur + 1;
318 out[i] = new double[subLength];
319 System.arraycopy(sample, offset, out[i], 0, subLength);
320 cur = next + 1;
321 sampleCount++;
322 offset += subLength;
323 }
324 if (sampleCount < 5) {
325 double[][] out2 = new double[sampleCount][];
326 for (int j = 0; j < sampleCount; j++) {
327 final int curSize = out[j].length;
328 out2[j] = new double[curSize];
329 System.arraycopy(out[j], 0, out2[j], 0, curSize);
330 }
331 return out2;
332 } else {
333 return out;
334 }
335 }
336 }