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:
parent
d4f6a5c818
commit
374b62d120
|
@ -92,7 +92,7 @@ public class StatUtils {
|
||||||
* Returns the product for this collection of values
|
* Returns the product for this collection of values
|
||||||
* @param values Is a double[] containing the values
|
* @param values Is a double[] containing the values
|
||||||
* @return the product values or Double.NaN if the array is empty
|
* @return the product values or Double.NaN if the array is empty
|
||||||
*/
|
*/
|
||||||
public static double product(double[] values) {
|
public static double product(double[] values) {
|
||||||
double product = Double.NaN;
|
double product = Double.NaN;
|
||||||
if( values.length > 0 ) {
|
if( values.length > 0 ) {
|
||||||
|
@ -104,6 +104,22 @@ public class StatUtils {
|
||||||
return product;
|
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
|
* Returns the geometric mean for this collection of values
|
||||||
* @param values Is a double[] containing the values
|
* @param values Is a double[] containing the values
|
||||||
|
@ -111,7 +127,7 @@ public class StatUtils {
|
||||||
* any of the values are <= 0.
|
* any of the values are <= 0.
|
||||||
*/
|
*/
|
||||||
public static double geometricMean(double[] values) {
|
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
|
* @return the mean of the values or Double.NaN if the array is empty
|
||||||
*/
|
*/
|
||||||
public static double mean(double[] values) {
|
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++) {
|
for (int i = 0; i < values.length; i++) {
|
||||||
accum += Math.pow((values[i] - mean), 2.0);
|
accum += Math.pow((values[i] - mean), 2.0);
|
||||||
}
|
}
|
||||||
variance = accum / (values.length - 1);
|
variance = accum / (double)(values.length - 1);
|
||||||
}
|
}
|
||||||
return variance;
|
return variance;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue