diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java b/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java
index ad5b440ec..a59e95ced 100644
--- a/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java
+++ b/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java
@@ -21,23 +21,30 @@ import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
/**
* Returns the
- * geometric mean of the available values
- * @version $Revision: 1.18 $ $Date: 2004/04/27 16:42:30 $
+ * geometric mean of the available values.
+ *
+ * Uses {@link SumOfLogs} superclass to compute sum of logs and returns
+ * exp( 1/n (sum of logs) ).
Therefore,
+ *
+ * - If any of values are < 0, the result is
NaN.
+ * - If all values are non-negative and less than
Double.POSITIVE_INFINITY
,
+ * but at least one value is 0, the result is 0.
+ * - If both
Double.POSITIVE_INFINITY
and
+ * Double.NEGATIVE_INFINITY
are among the values, the result is
+ * NaN.
+ *
+ *
+ *
+ * @version $Revision: 1.19 $ $Date: 2004/06/18 07:03:40 $
*/
public class GeometricMean extends SumOfLogs implements Serializable{
/** 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 @@ public class GeometricMean extends SumOfLogs implements Serializable{
* @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 class GeometricMean extends SumOfLogs implements Serializable{
*/
public void clear() {
super.clear();
- lastSum = 0.0;
- geoMean = Double.NaN;
n = 0;
}