psteitz 2004/07/17 21:37:08
Modified: math/src/java/org/apache/commons/math/stat/univariate/moment
GeometricMean.java
Log:
Changed implementation to wrap, rather than extend SumOfLogs.
Revision Changes Path
1.22 +30 -20
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.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- GeometricMean.java 4 Jul 2004 09:02:36 -0000 1.21
+++ GeometricMean.java 18 Jul 2004 04:37:08 -0000 1.22
@@ -15,20 +15,20 @@
*/
package org.apache.commons.math.stat.univariate.moment;
-import java.io.Serializable;
-
+import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
/**
* Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
* geometric mean </a> of the available values.
* <p>
- * Uses [EMAIL PROTECTED] SumOfLogs} superclass to compute sum of logs and returns
+ * Uses a [EMAIL PROTECTED] SumOfLogs} instance 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 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>
@@ -42,28 +42,34 @@
*
* @version $Revision$ $Date$
*/
-public class GeometricMean extends SumOfLogs implements Serializable{
+public class GeometricMean extends AbstractStorelessUnivariateStatistic {
/** Serializable version identifier */
static final long serialVersionUID = -8178734905303459453L;
-
- /**Number of values that have been added */
- protected long n = 0;
+
+ /** Wrapped SumOfLogs instance */
+ private SumOfLogs sumOfLogs;
/**
+ * Create a GeometricMean instance
+ */
+ public GeometricMean() {
+ sumOfLogs = new SumOfLogs();
+ }
+
+ /**
* @see
org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(final double d) {
- n++;
- super.increment(d);
+ sumOfLogs.increment(d);
}
/**
* @see
org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
- if (n > 0) {
- return Math.exp(super.getResult() / (double) n);
+ if (sumOfLogs.getN() > 0) {
+ return Math.exp(sumOfLogs.getResult() / (double) sumOfLogs.getN());
} else {
return Double.NaN;
}
@@ -73,8 +79,7 @@
* @see
org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public void clear() {
- super.clear();
- n = 0;
+ sumOfLogs.clear();
}
/**
@@ -94,11 +99,16 @@
* index parameters are not valid
*/
public double evaluate(
- final double[] values,
- final int begin,
- final int length) {
+ final double[] values, final int begin, final int length) {
return Math.exp(
- super.evaluate(values, begin, length) / (double) length);
+ sumOfLogs.evaluate(values, begin, length) / (double) length);
+ }
+
+ /**
+ * @see
org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getN()
+ */
+ public long getN() {
+ return sumOfLogs.getN();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]