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.exception.MathIllegalStateException;
20 import org.apache.commons.math4.legacy.exception.NullArgumentException;
21 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
22 import org.apache.commons.math4.legacy.stat.descriptive.moment.GeometricMean;
23 import org.apache.commons.math4.legacy.stat.descriptive.moment.Mean;
24 import org.apache.commons.math4.legacy.stat.descriptive.moment.SecondMoment;
25 import org.apache.commons.math4.legacy.stat.descriptive.moment.Variance;
26 import org.apache.commons.math4.legacy.stat.descriptive.rank.Max;
27 import org.apache.commons.math4.legacy.stat.descriptive.rank.Min;
28 import org.apache.commons.math4.legacy.stat.descriptive.summary.Sum;
29 import org.apache.commons.math4.legacy.stat.descriptive.summary.SumOfLogs;
30 import org.apache.commons.math4.legacy.stat.descriptive.summary.SumOfSquares;
31 import org.apache.commons.math4.core.jdkmath.JdkMath;
32 import org.apache.commons.numbers.core.Precision;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class SummaryStatistics implements StatisticalSummary {
58
59 private long n;
60
61
62 private SecondMoment secondMoment = new SecondMoment();
63
64
65 private Sum sum = new Sum();
66
67
68 private SumOfSquares sumsq = new SumOfSquares();
69
70
71 private Min min = new Min();
72
73
74 private Max max = new Max();
75
76
77 private SumOfLogs sumLog = new SumOfLogs();
78
79
80 private GeometricMean geoMean = new GeometricMean(sumLog);
81
82
83 private Mean mean = new Mean(secondMoment);
84
85
86 private Variance variance = new Variance(secondMoment);
87
88
89 private StorelessUnivariateStatistic sumImpl = sum;
90
91
92 private StorelessUnivariateStatistic sumsqImpl = sumsq;
93
94
95 private StorelessUnivariateStatistic minImpl = min;
96
97
98 private StorelessUnivariateStatistic maxImpl = max;
99
100
101 private StorelessUnivariateStatistic sumLogImpl = sumLog;
102
103
104 private StorelessUnivariateStatistic geoMeanImpl = geoMean;
105
106
107 private StorelessUnivariateStatistic meanImpl = mean;
108
109
110 private StorelessUnivariateStatistic varianceImpl = variance;
111
112
113
114
115 public SummaryStatistics() {
116 }
117
118
119
120
121
122
123
124 public SummaryStatistics(SummaryStatistics original) throws NullArgumentException {
125 copy(original, this);
126 }
127
128
129
130
131
132
133 public StatisticalSummary getSummary() {
134 return new StatisticalSummaryValues(getMean(), getVariance(), getN(),
135 getMax(), getMin(), getSum());
136 }
137
138
139
140
141
142 public void addValue(double value) {
143 sumImpl.increment(value);
144 sumsqImpl.increment(value);
145 minImpl.increment(value);
146 maxImpl.increment(value);
147 sumLogImpl.increment(value);
148 secondMoment.increment(value);
149
150
151 if (meanImpl != mean) {
152 meanImpl.increment(value);
153 }
154 if (varianceImpl != variance) {
155 varianceImpl.increment(value);
156 }
157 if (geoMeanImpl != geoMean) {
158 geoMeanImpl.increment(value);
159 }
160 n++;
161 }
162
163
164
165
166
167 @Override
168 public long getN() {
169 return n;
170 }
171
172
173
174
175
176 @Override
177 public double getSum() {
178 return sumImpl.getResult();
179 }
180
181
182
183
184
185
186
187
188 public double getSumsq() {
189 return sumsqImpl.getResult();
190 }
191
192
193
194
195
196
197
198
199 @Override
200 public double getMean() {
201 return meanImpl.getResult();
202 }
203
204
205
206
207
208
209
210
211 @Override
212 public double getStandardDeviation() {
213 double stdDev = Double.NaN;
214 if (getN() > 0) {
215 if (getN() > 1) {
216 stdDev = JdkMath.sqrt(getVariance());
217 } else {
218 stdDev = 0.0;
219 }
220 }
221 return stdDev;
222 }
223
224
225
226
227
228
229
230
231 public double getQuadraticMean() {
232 final long size = getN();
233 return size > 0 ? JdkMath.sqrt(getSumsq() / size) : Double.NaN;
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247 @Override
248 public double getVariance() {
249 return varianceImpl.getResult();
250 }
251
252
253
254
255
256
257
258
259
260 public double getPopulationVariance() {
261 Variance populationVariance = new Variance(secondMoment);
262 populationVariance.setBiasCorrected(false);
263 return populationVariance.getResult();
264 }
265
266
267
268
269
270
271
272
273 @Override
274 public double getMax() {
275 return maxImpl.getResult();
276 }
277
278
279
280
281
282
283
284
285 @Override
286 public double getMin() {
287 return minImpl.getResult();
288 }
289
290
291
292
293
294
295
296
297 public double getGeometricMean() {
298 return geoMeanImpl.getResult();
299 }
300
301
302
303
304
305
306
307
308
309 public double getSumOfLogs() {
310 return sumLogImpl.getResult();
311 }
312
313
314
315
316
317
318
319
320
321
322
323
324 public double getSecondMoment() {
325 return secondMoment.getResult();
326 }
327
328
329
330
331
332
333
334 @Override
335 public String toString() {
336 StringBuilder outBuffer = new StringBuilder();
337 String endl = "\n";
338 outBuffer.append("SummaryStatistics:").append(endl);
339 outBuffer.append("n: ").append(getN()).append(endl);
340 outBuffer.append("min: ").append(getMin()).append(endl);
341 outBuffer.append("max: ").append(getMax()).append(endl);
342 outBuffer.append("sum: ").append(getSum()).append(endl);
343 outBuffer.append("mean: ").append(getMean()).append(endl);
344 outBuffer.append("geometric mean: ").append(getGeometricMean())
345 .append(endl);
346 outBuffer.append("variance: ").append(getVariance()).append(endl);
347 outBuffer.append("population variance: ").append(getPopulationVariance()).append(endl);
348 outBuffer.append("second moment: ").append(getSecondMoment()).append(endl);
349 outBuffer.append("sum of squares: ").append(getSumsq()).append(endl);
350 outBuffer.append("standard deviation: ").append(getStandardDeviation())
351 .append(endl);
352 outBuffer.append("sum of logs: ").append(getSumOfLogs()).append(endl);
353 return outBuffer.toString();
354 }
355
356
357
358
359 public void clear() {
360 this.n = 0;
361 minImpl.clear();
362 maxImpl.clear();
363 sumImpl.clear();
364 sumLogImpl.clear();
365 sumsqImpl.clear();
366 geoMeanImpl.clear();
367 secondMoment.clear();
368 if (meanImpl != mean) {
369 meanImpl.clear();
370 }
371 if (varianceImpl != variance) {
372 varianceImpl.clear();
373 }
374 }
375
376
377
378
379
380
381
382
383 @Override
384 public boolean equals(Object object) {
385 if (object == this) {
386 return true;
387 }
388 if (!(object instanceof SummaryStatistics)) {
389 return false;
390 }
391 SummaryStatistics stat = (SummaryStatistics)object;
392 return Precision.equalsIncludingNaN(stat.getGeometricMean(), getGeometricMean()) &&
393 Precision.equalsIncludingNaN(stat.getMax(), getMax()) &&
394 Precision.equalsIncludingNaN(stat.getMean(), getMean()) &&
395 Precision.equalsIncludingNaN(stat.getMin(), getMin()) &&
396 Precision.equalsIncludingNaN(stat.getN(), getN()) &&
397 Precision.equalsIncludingNaN(stat.getSum(), getSum()) &&
398 Precision.equalsIncludingNaN(stat.getSumsq(), getSumsq()) &&
399 Precision.equalsIncludingNaN(stat.getVariance(), getVariance());
400 }
401
402
403
404
405
406 @Override
407 public int hashCode() {
408 int result = 31 + Double.hashCode(getGeometricMean());
409 result = result * 31 + Double.hashCode(getGeometricMean());
410 result = result * 31 + Double.hashCode(getMax());
411 result = result * 31 + Double.hashCode(getMean());
412 result = result * 31 + Double.hashCode(getMin());
413 result = result * 31 + Double.hashCode(getN());
414 result = result * 31 + Double.hashCode(getSum());
415 result = result * 31 + Double.hashCode(getSumsq());
416 result = result * 31 + Double.hashCode(getVariance());
417 return result;
418 }
419
420
421
422
423
424
425
426 public StorelessUnivariateStatistic getSumImpl() {
427 return sumImpl;
428 }
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445 public void setSumImpl(StorelessUnivariateStatistic sumImpl)
446 throws MathIllegalStateException {
447 checkEmpty();
448 this.sumImpl = sumImpl;
449 }
450
451
452
453
454
455
456 public StorelessUnivariateStatistic getSumsqImpl() {
457 return sumsqImpl;
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475 public void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl)
476 throws MathIllegalStateException {
477 checkEmpty();
478 this.sumsqImpl = sumsqImpl;
479 }
480
481
482
483
484
485
486 public StorelessUnivariateStatistic getMinImpl() {
487 return minImpl;
488 }
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505 public void setMinImpl(StorelessUnivariateStatistic minImpl)
506 throws MathIllegalStateException {
507 checkEmpty();
508 this.minImpl = minImpl;
509 }
510
511
512
513
514
515
516 public StorelessUnivariateStatistic getMaxImpl() {
517 return maxImpl;
518 }
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535 public void setMaxImpl(StorelessUnivariateStatistic maxImpl)
536 throws MathIllegalStateException {
537 checkEmpty();
538 this.maxImpl = maxImpl;
539 }
540
541
542
543
544
545
546 public StorelessUnivariateStatistic getSumLogImpl() {
547 return sumLogImpl;
548 }
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565 public void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl)
566 throws MathIllegalStateException {
567 checkEmpty();
568 this.sumLogImpl = sumLogImpl;
569 geoMean.setSumLogImpl(sumLogImpl);
570 }
571
572
573
574
575
576
577 public StorelessUnivariateStatistic getGeoMeanImpl() {
578 return geoMeanImpl;
579 }
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596 public void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl)
597 throws MathIllegalStateException {
598 checkEmpty();
599 this.geoMeanImpl = geoMeanImpl;
600 }
601
602
603
604
605
606
607 public StorelessUnivariateStatistic getMeanImpl() {
608 return meanImpl;
609 }
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626 public void setMeanImpl(StorelessUnivariateStatistic meanImpl)
627 throws MathIllegalStateException {
628 checkEmpty();
629 this.meanImpl = meanImpl;
630 }
631
632
633
634
635
636
637 public StorelessUnivariateStatistic getVarianceImpl() {
638 return varianceImpl;
639 }
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656 public void setVarianceImpl(StorelessUnivariateStatistic varianceImpl)
657 throws MathIllegalStateException {
658 checkEmpty();
659 this.varianceImpl = varianceImpl;
660 }
661
662
663
664
665
666 private void checkEmpty() throws MathIllegalStateException {
667 if (n > 0) {
668 throw new MathIllegalStateException(
669 LocalizedFormats.VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC, n);
670 }
671 }
672
673
674
675
676
677
678 public SummaryStatistics copy() {
679 SummaryStatistics result = new SummaryStatistics();
680
681 copy(this, result);
682 return result;
683 }
684
685
686
687
688
689
690
691
692
693 public static void copy(SummaryStatistics source, SummaryStatistics dest)
694 throws NullArgumentException {
695 NullArgumentException.check(source);
696 NullArgumentException.check(dest);
697 dest.maxImpl = source.maxImpl.copy();
698 dest.minImpl = source.minImpl.copy();
699 dest.sumImpl = source.sumImpl.copy();
700 dest.sumLogImpl = source.sumLogImpl.copy();
701 dest.sumsqImpl = source.sumsqImpl.copy();
702 dest.secondMoment = source.secondMoment.copy();
703 dest.n = source.n;
704
705
706 if (source.getVarianceImpl() instanceof Variance) {
707 dest.varianceImpl = new Variance(dest.secondMoment);
708 } else {
709 dest.varianceImpl = source.varianceImpl.copy();
710 }
711 if (source.meanImpl instanceof Mean) {
712 dest.meanImpl = new Mean(dest.secondMoment);
713 } else {
714 dest.meanImpl = source.meanImpl.copy();
715 }
716 if (source.getGeoMeanImpl() instanceof GeometricMean) {
717 dest.geoMeanImpl = new GeometricMean((SumOfLogs) dest.sumLogImpl);
718 } else {
719 dest.geoMeanImpl = source.geoMeanImpl.copy();
720 }
721
722
723
724 if (source.geoMean == source.geoMeanImpl) {
725 dest.geoMean = (GeometricMean) dest.geoMeanImpl;
726 } else {
727 GeometricMean.copy(source.geoMean, dest.geoMean);
728 }
729 if (source.max == source.maxImpl) {
730 dest.max = (Max) dest.maxImpl;
731 } else {
732 Max.copy(source.max, dest.max);
733 }
734 if (source.mean == source.meanImpl) {
735 dest.mean = (Mean) dest.meanImpl;
736 } else {
737 Mean.copy(source.mean, dest.mean);
738 }
739 if (source.min == source.minImpl) {
740 dest.min = (Min) dest.minImpl;
741 } else {
742 Min.copy(source.min, dest.min);
743 }
744 if (source.sum == source.sumImpl) {
745 dest.sum = (Sum) dest.sumImpl;
746 } else {
747 Sum.copy(source.sum, dest.sum);
748 }
749 if (source.variance == source.varianceImpl) {
750 dest.variance = (Variance) dest.varianceImpl;
751 } else {
752 Variance.copy(source.variance, dest.variance);
753 }
754 if (source.sumLog == source.sumLogImpl) {
755 dest.sumLog = (SumOfLogs) dest.sumLogImpl;
756 } else {
757 SumOfLogs.copy(source.sumLog, dest.sumLog);
758 }
759 if (source.sumsq == source.sumsqImpl) {
760 dest.sumsq = (SumOfSquares) dest.sumsqImpl;
761 } else {
762 SumOfSquares.copy(source.sumsq, dest.sumsq);
763 }
764 }
765 }