1 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 34 public class SummaryStatisticsImpl extends SummaryStatistics implements Serializable { 35 36 37 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 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 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 102 public long getN() { 103 return n; 104 } 105 106 110 public double getSum() { 111 return sum.getResult(); 112 } 113 114 121 public double getSumsq() { 122 return sumsq.getResult(); 123 } 124 125 132 public double getMean() { 133 return new Mean(secondMoment).getResult(); 134 } 135 136 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 162 public double getVariance() { 163 return new Variance(secondMoment).getResult(); 164 } 165 166 173 public double getMax() { 174 return max.getResult(); 175 } 176 177 184 public double getMin() { 185 return min.getResult(); 186 } 187 188 195 public double getGeometricMean() { 196 return geoMean.getResult(); 197 } 198 199 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 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 } | Popular Tags |