psteitz 2004/06/18 00:03:40 Modified: math/src/java/org/apache/commons/math/stat/univariate/moment GeometricMean.java Log: Improved javadoc, implementation. Revision Changes Path 1.19 +20 -15 jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java Index: GeometricMean.java =================================================================== RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- GeometricMean.java 27 Apr 2004 16:42:30 -0000 1.18 +++ GeometricMean.java 18 Jun 2004 07:03:40 -0000 1.19 @@ -21,7 +21,20 @@ /** * Returns the <a href="http://www.xycoon.com/geometric_mean.htm"> - * geometric mean </a> of the available values + * geometric mean </a> of the available values. + * <p> + * Uses [EMAIL PROTECTED] SumOfLogs} superclass to compute sum of logs and returns + * <code> exp( 1/n (sum of logs) ).</code> Therefore, + * <ul> + * <li>If any of values are < 0, the result is <code>NaN.</code></li> + * <li>If all values are non-negative and less than <code>Double.POSITIVE_INFINITY</code>, + * but at least one value is 0, the result is <code>0.</code></li> + * <li>If both <code>Double.POSITIVE_INFINITY</code> and + * <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is + * <code>NaN.</code></li> + * </ul> + * + * * @version $Revision$ $Date$ */ public class GeometricMean extends SumOfLogs implements Serializable{ @@ -29,15 +42,9 @@ /** Serializable version identifier */ static final long serialVersionUID = -8178734905303459453L; - /** */ + /**Number of values that have been added */ protected long n = 0; - /** */ - private double geoMean = Double.NaN; - - /** */ - private double lastSum = 0.0; - /** * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) */ @@ -50,11 +57,11 @@ * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult() */ public double getResult() { - if (lastSum != super.getResult() || n == 1) { - lastSum = super.getResult(); - geoMean = Math.exp(lastSum / (double) n); + if (n > 0) { + return Math.exp(super.getResult() / (double) n); + } else { + return Double.NaN; } - return geoMean; } /** @@ -62,8 +69,6 @@ */ public void clear() { super.clear(); - lastSum = 0.0; - geoMean = Double.NaN; n = 0; }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]