View Javadoc
1   package com.github.davidmoten.rx;
2   
3   public final class Statistics {
4   
5       private final long count;
6       private final double sumX;
7       private final double sumX2;
8   
9       private Statistics(long count, double sumX, double sumX2) {
10          this.count = count;
11          this.sumX = sumX;
12          this.sumX2 = sumX2;
13      }
14  
15      public static Statistics create() {
16          return new Statistics(0, 0, 0);
17      }
18  
19      public Statistics add(Number number) {
20          double x = number.doubleValue();
21          return new Statistics(count + 1, sumX + x, sumX2 + x * x);
22      }
23  
24      public long count() {
25          return count;
26      }
27  
28      public double sum() {
29          return sumX;
30      }
31  
32      public double sumSquares() {
33          return sumX2;
34      }
35  
36      public double mean() {
37          return sumX / count;
38      }
39  
40      public double sd() {
41          double m = mean();
42          return Math.sqrt(sumX2 / count - m * m);
43      }
44  
45      @Override
46      public String toString() {
47          StringBuilder builder = new StringBuilder();
48          builder.append("Statistics [count=");
49          builder.append(count);
50          builder.append(", sum=");
51          builder.append(sum());
52          builder.append(", mean=");
53          builder.append(mean());
54          builder.append(", sd=");
55          builder.append(sd());
56          builder.append("]");
57          return builder.toString();
58      }
59  }