KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > statistics > SampleStatistics


1 package JSci.maths.statistics;
2
3 /**
4 * This class calculates commonly used sample statistics in an incremental fashion.
5 * @version 1.0
6 * @author Mark Hale
7 */

8 public class SampleStatistics {
9         private int n = 0;
10         private double sum = 0.0;
11         private double sumSqr = 0.0;
12         private double min = Double.POSITIVE_INFINITY;
13         private double max = Double.NEGATIVE_INFINITY;
14
15         public SampleStatistics() {}
16         public void update(double x) {
17                 n++;
18                 sum += x;
19                 sumSqr += x*x;
20                 min = Math.min(x, min);
21                 max = Math.max(x, max);
22         }
23         public int getCount() {
24                 return n;
25         }
26         public double getMean() {
27                 return sum/n;
28         }
29         public double getVariance() {
30                 return (sumSqr - sum*sum/n)/(n-1);
31         }
32         public double getMinimum() {
33                 return min;
34         }
35         public double getMaximum() {
36                 return max;
37         }
38 }
39
40
Popular Tags