Addition of sumLog method (natural) to get the sum of the Logs. Altered GeometricMean to calculate the geometricMean from the sum of logs.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140920 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-06-18 01:56:03 +00:00
parent d4f6a5c818
commit 374b62d120
1 changed files with 20 additions and 4 deletions

View File

@ -104,6 +104,22 @@ public class StatUtils {
return product;
}
/**
* 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
@ -111,7 +127,7 @@ public class StatUtils {
* any of the values are &lt;= 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 @@ public class StatUtils {
* @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 @@ public class StatUtils {
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;
}