Line | |
---|
1 | package geniusweb.blingbling;
|
---|
2 |
|
---|
3 | import java.util.Map;
|
---|
4 | import java.util.LinkedHashMap;
|
---|
5 |
|
---|
6 | //record the distribution. Not used yet.
|
---|
7 | public class Distribution {
|
---|
8 | private double mu = 0.0;
|
---|
9 | private double var = 0.0;
|
---|
10 | private int n = 0;
|
---|
11 |
|
---|
12 | private Double min = null;
|
---|
13 | private Double max = null;
|
---|
14 |
|
---|
15 | public void addSample(double sample) {
|
---|
16 | double mu_new = ((double) n * mu + sample) / ((double) n + 1.0);
|
---|
17 |
|
---|
18 | var = ((double) n * var + (double) n * Math.pow(mu - mu_new, 2.0) + Math.pow(sample - mu_new, 2.0)) / ((double) n + 1.0);
|
---|
19 | mu = mu_new;
|
---|
20 |
|
---|
21 | n++;
|
---|
22 |
|
---|
23 | if ((min == null) || (sample < min))
|
---|
24 | min = sample;
|
---|
25 | if ((max == null) || (sample > max))
|
---|
26 | max = sample;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public double getStdDev() {
|
---|
30 | return Math.sqrt(var);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public double getMean() {
|
---|
34 | return mu;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public Map<String, String> toMap(String name) {
|
---|
38 | Map<String, String> map = new LinkedHashMap<String, String>();
|
---|
39 |
|
---|
40 | map.put(name + "_mean", String.valueOf(mu));
|
---|
41 | map.put(name + "_stdDev", String.valueOf(Math.sqrt(var)));
|
---|
42 | map.put(name + "_min", String.valueOf(min));
|
---|
43 | map.put(name + "_max", String.valueOf(max));
|
---|
44 |
|
---|
45 | return map;
|
---|
46 | }
|
---|
47 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.