1 16 package org.apache.commons.math.stat.descriptive.moment; 17 18 import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic; 19 import org.apache.commons.math.stat.descriptive.summary.SumOfLogs; 20 21 45 public class GeometricMean extends AbstractStorelessUnivariateStatistic { 46 47 48 static final long serialVersionUID = -8178734905303459453L; 49 50 51 private SumOfLogs sumOfLogs; 52 53 56 public GeometricMean() { 57 sumOfLogs = new SumOfLogs(); 58 } 59 60 63 public void increment(final double d) { 64 sumOfLogs.increment(d); 65 } 66 67 70 public double getResult() { 71 if (sumOfLogs.getN() > 0) { 72 return Math.exp(sumOfLogs.getResult() / (double) sumOfLogs.getN()); 73 } else { 74 return Double.NaN; 75 } 76 } 77 78 81 public void clear() { 82 sumOfLogs.clear(); 83 } 84 85 101 public double evaluate( 102 final double[] values, final int begin, final int length) { 103 return Math.exp( 104 sumOfLogs.evaluate(values, begin, length) / (double) length); 105 } 106 107 110 public long getN() { 111 return sumOfLogs.getN(); 112 } 113 114 } | Popular Tags |