Fix variance implementation and add tests to Math package.

refs #34
This commit is contained in:
2014-11-10 17:46:34 +01:00
parent 6386c03f43
commit 1af305ae3d
2 changed files with 120 additions and 3 deletions

View File

@@ -631,9 +631,15 @@ public final class Mathematics {
for (int i = 0; i < vector.length; i++) {
result += Math.pow(vector[i] - mean, 2);
}
return result;
return result / (vector.length - 1);
}
/**
* Computes the sample standard deviation.
*
* @param vector
* @return
*/
public static double stdDev(double[] vector) {
double result = variance(vector);
result = Math.sqrt(result);
@@ -749,8 +755,8 @@ public final class Mathematics {
*/
public static double product(double[] vals) {
double prod = 1.;
for (int i = 0; i < vals.length; i++) {
prod *= vals[i];
for (double val : vals) {
prod *= val;
}
return prod;
}