mdiggory 2003/06/17 18:56:03
Modified: math/src/java/org/apache/commons/math/stat StatUtils.java
Log:
Addition of sumLog method (natural) to get the sum of the Logs. Altered
GeometricMean to calculate the geometricMean from the sum of logs.
Revision Changes Path
1.4 +20 -4
jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StatUtils.java
Index: StatUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StatUtils.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- StatUtils.java 17 Jun 2003 23:23:07 -0000 1.3
+++ StatUtils.java 18 Jun 2003 01:56:03 -0000 1.4
@@ -92,7 +92,7 @@
* Returns the product for this collection of values
* @param values Is a double[] containing the values
* @return the product values or Double.NaN if the array is empty
- */
+ */
public static double product(double[] values) {
double product = Double.NaN;
if( values.length > 0 ) {
@@ -105,13 +105,29 @@
}
/**
+ * Returns the sum of the natural logs for this collection of values
+ * @param values Is a double[] containing the values
+ * @return the sumLog value or Double.NaN if the array is empty
+ */
+ public static double sumLog(double[] values) {
+ double sumLog = Double.NaN;
+ if( values.length > 0 ) {
+ sumLog = 0.0;
+ for( int i = 0; i < values.length; i++) {
+ sumLog += Math.log(values[i]);
+ }
+ }
+ return sumLog;
+ }
+
+ /**
* Returns the geometric mean for this collection of values
* @param values Is a double[] containing the values
* @return the geometric mean or Double.NaN if the array is empty or
* any of the values are <= 0.
*/
public static double geometricMean(double[] values) {
- return Math.pow(product(values),(1.0/values.length));
+ return Math.exp(sumLog(values) / (double)values.length);
}
/**
@@ -121,7 +137,7 @@
* @return the mean of the values or Double.NaN if the array is empty
*/
public static double mean(double[] values) {
- return sum(values) / values.length;
+ return sum(values) / (double)values.length;
}
/**
@@ -155,7 +171,7 @@
for (int i = 0; i < values.length; i++) {
accum += Math.pow((values[i] - mean), 2.0);
}
- variance = accum / (values.length - 1);
+ variance = accum / (double)(values.length - 1);
}
return variance;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]