1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.math.stat.descriptive;
17
18 import java.io.Serializable;
19 import org.apache.commons.math.stat.descriptive.moment.SecondMoment;
20 import org.apache.commons.math.stat.descriptive.moment.GeometricMean;
21 import org.apache.commons.math.stat.descriptive.moment.Mean;
22 import org.apache.commons.math.stat.descriptive.moment.Variance;
23 import org.apache.commons.math.stat.descriptive.rank.Max;
24 import org.apache.commons.math.stat.descriptive.rank.Min;
25 import org.apache.commons.math.stat.descriptive.summary.Sum;
26 import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
27 import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
28
29
30
31
32
33
34 public class SummaryStatisticsImpl extends SummaryStatistics implements Serializable {
35
36
37 private static final long serialVersionUID = 8787174276883311692L;
38
39
40 protected long n = 0;
41
42
43 protected SecondMoment secondMoment = null;
44
45
46 protected Sum sum = null;
47
48
49 protected SumOfSquares sumsq = null;
50
51
52 protected Min min = null;
53
54
55 protected Max max = null;
56
57
58 protected SumOfLogs sumLog = null;
59
60
61 protected GeometricMean geoMean = null;
62
63
64 protected Mean mean = null;
65
66
67 protected Variance variance = null;
68
69
70
71
72 public SummaryStatisticsImpl() {
73 sum = new Sum();
74 sumsq = new SumOfSquares();
75 min = new Min();
76 max = new Max();
77 sumLog = new SumOfLogs();
78 geoMean = new GeometricMean();
79 secondMoment = new SecondMoment();
80 }
81
82
83
84
85
86
87 public void addValue(double value) {
88 sum.increment(value);
89 sumsq.increment(value);
90 min.increment(value);
91 max.increment(value);
92 sumLog.increment(value);
93 geoMean.increment(value);
94 secondMoment.increment(value);
95 n++;
96 }
97
98
99
100
101
102 public long getN() {
103 return n;
104 }
105
106
107
108
109
110 public double getSum() {
111 return sum.getResult();
112 }
113
114
115
116
117
118
119
120
121 public double getSumsq() {
122 return sumsq.getResult();
123 }
124
125
126
127
128
129
130
131
132 public double getMean() {
133 return new Mean(secondMoment).getResult();
134 }
135
136
137
138
139
140
141
142
143 public double getStandardDeviation() {
144 double stdDev = Double.NaN;
145 if (getN() > 0) {
146 if (getN() > 1) {
147 stdDev = Math.sqrt(getVariance());
148 } else {
149 stdDev = 0.0;
150 }
151 }
152 return (stdDev);
153 }
154
155
156
157
158
159
160
161
162 public double getVariance() {
163 return new Variance(secondMoment).getResult();
164 }
165
166
167
168
169
170
171
172
173 public double getMax() {
174 return max.getResult();
175 }
176
177
178
179
180
181
182
183
184 public double getMin() {
185 return min.getResult();
186 }
187
188
189
190
191
192
193
194
195 public double getGeometricMean() {
196 return geoMean.getResult();
197 }
198
199
200
201
202
203
204
205 public String toString() {
206 StringBuffer outBuffer = new StringBuffer();
207 outBuffer.append("SummaryStatistics:\n");
208 outBuffer.append("n: " + getN() + "\n");
209 outBuffer.append("min: " + getMin() + "\n");
210 outBuffer.append("max: " + getMax() + "\n");
211 outBuffer.append("mean: " + getMean() + "\n");
212 outBuffer.append("geometric mean: " + getGeometricMean() + "\n");
213 outBuffer.append("variance: " + getVariance() + "\n");
214 outBuffer.append("sum of squares: " + getSumsq() + "\n");
215 outBuffer.append("standard deviation: " + getStandardDeviation() + "\n");
216 return outBuffer.toString();
217 }
218
219
220
221
222 public void clear() {
223 this.n = 0;
224 min.clear();
225 max.clear();
226 sum.clear();
227 sumLog.clear();
228 sumsq.clear();
229 geoMean.clear();
230 secondMoment.clear();
231 }
232
233 }