1 16 package org.apache.commons.math.stat.descriptive; 17 18 import java.io.Serializable ; 19 import java.util.Arrays ; 20 21 import org.apache.commons.discovery.tools.DiscoverClass; 22 import org.apache.commons.math.stat.descriptive.moment.GeometricMean; 23 import org.apache.commons.math.stat.descriptive.moment.Kurtosis; 24 import org.apache.commons.math.stat.descriptive.moment.Mean; 25 import org.apache.commons.math.stat.descriptive.moment.Skewness; 26 import org.apache.commons.math.stat.descriptive.moment.Variance; 27 import org.apache.commons.math.stat.descriptive.rank.Max; 28 import org.apache.commons.math.stat.descriptive.rank.Min; 29 import org.apache.commons.math.stat.descriptive.rank.Percentile; 30 import org.apache.commons.math.stat.descriptive.summary.Sum; 31 import org.apache.commons.math.stat.descriptive.summary.SumOfSquares; 32 33 34 39 public abstract class DescriptiveStatistics implements StatisticalSummary, Serializable { 40 41 42 static final long serialVersionUID = 5188298269533339922L; 43 44 54 public static DescriptiveStatistics newInstance(Class cls) throws InstantiationException , IllegalAccessException { 55 return (DescriptiveStatistics)cls.newInstance(); 56 } 57 58 62 public static DescriptiveStatistics newInstance() { 63 DescriptiveStatistics factory = null; 64 try { 65 DiscoverClass dc = new DiscoverClass(); 66 factory = (DescriptiveStatistics) dc.newInstance( 67 DescriptiveStatistics.class, 68 "org.apache.commons.math.stat.descriptive.DescriptiveStatisticsImpl"); 69 } catch(Throwable t) { 70 return new DescriptiveStatisticsImpl(); 71 } 72 return factory; 73 } 74 75 81 public static final int INFINITE_WINDOW = -1; 82 83 87 public abstract void addValue(double v); 88 89 94 public double getMean() { 95 return apply(new Mean()); 96 } 97 98 104 public double getGeometricMean() { 105 return apply(new GeometricMean()); 106 } 107 108 113 public double getVariance() { 114 return apply(new Variance()); 115 } 116 117 122 public double getStandardDeviation() { 123 double stdDev = Double.NaN; 124 if (getN() > 0) { 125 if (getN() > 1) { 126 stdDev = Math.sqrt(getVariance()); 127 } else { 128 stdDev = 0.0; 129 } 130 } 131 return (stdDev); 132 } 133 134 140 public double getSkewness() { 141 return apply(new Skewness()); 142 } 143 144 150 public double getKurtosis() { 151 return apply(new Kurtosis()); 152 } 153 154 158 public double getMax() { 159 return apply(new Max()); 160 } 161 162 166 public double getMin() { 167 return apply(new Min()); 168 } 169 170 174 public abstract long getN(); 175 176 180 public double getSum() { 181 return apply(new Sum()); 182 } 183 184 189 public double getSumsq() { 190 return apply(new SumOfSquares()); 191 } 192 193 196 public abstract void clear(); 197 198 203 204 public abstract int getWindowSize(); 205 206 215 public abstract void setWindowSize(int windowSize); 216 217 226 public abstract double[] getValues(); 227 228 236 public double[] getSortedValues() { 237 double[] sort = getValues(); 238 Arrays.sort(sort); 239 return sort; 240 } 241 242 247 public abstract double getElement(int index); 248 249 266 public double getPercentile(double p) { 267 return apply(new Percentile(p)); 268 } 269 270 277 public String toString() { 278 StringBuffer outBuffer = new StringBuffer (); 279 outBuffer.append("DescriptiveStatistics:\n"); 280 outBuffer.append("n: " + getN() + "\n"); 281 outBuffer.append("min: " + getMin() + "\n"); 282 outBuffer.append("max: " + getMax() + "\n"); 283 outBuffer.append("mean: " + getMean() + "\n"); 284 outBuffer.append("std dev: " + getStandardDeviation() + "\n"); 285 outBuffer.append("median: " + getPercentile(50) + "\n"); 286 outBuffer.append("skewness: " + getSkewness() + "\n"); 287 outBuffer.append("kurtosis: " + getKurtosis() + "\n"); 288 return outBuffer.toString(); 289 } 290 291 296 public abstract double apply(UnivariateStatistic stat); 297 298 } 299 | Popular Tags |