01: /*
02: * Created on Mar 3, 2004
03: */
04: package uk.org.ponder.statutil;
05:
06: /**
07: * The class
08: *
09: * @author Bosmon
10: */
11: public class GaussCollector {
12: public double min;
13: public double max;
14: public double sx;
15: public double sx2;
16: public int n;
17:
18: public GaussCollector init() {
19: sx = sx2 = n = 0;
20: min = Double.POSITIVE_INFINITY;
21: max = Double.NEGATIVE_INFINITY;
22: return this ;
23: }
24:
25: public void collect(double x) {
26: sx += x;
27: sx2 += x * x;
28: if (x < min)
29: min = x;
30: if (x > max)
31: max = x;
32: ++n;
33: }
34:
35: public GaussCollector collect(GaussCollector other) {
36: sx += other.sx;
37: sx2 += other.sx2;
38: n += other.n;
39: if (other.min < min)
40: min = other.min;
41: if (other.max > max)
42: max = other.max;
43: return this ;
44: }
45:
46: public double mean() {
47: return n == 0 ? 0 : sx / n;
48: }
49:
50: public double variance() {
51: return (sx2 - sx * sx / n) / n;
52: }
53:
54: public String toString() {
55: return n + " data: mean " + mean() + " SD "
56: + Math.sqrt(variance()) + " min " + min + " max " + max;
57: }
58:
59: public int count() {
60: return n;
61: }
62: }
|