Fix variance implementation and add tests to Math package.

refs #34
This commit is contained in:
Fabian Becker 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++) { for (int i = 0; i < vector.length; i++) {
result += Math.pow(vector[i] - mean, 2); 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) { public static double stdDev(double[] vector) {
double result = variance(vector); double result = variance(vector);
result = Math.sqrt(result); result = Math.sqrt(result);
@ -749,8 +755,8 @@ public final class Mathematics {
*/ */
public static double product(double[] vals) { public static double product(double[] vals) {
double prod = 1.; double prod = 1.;
for (int i = 0; i < vals.length; i++) { for (double val : vals) {
prod *= vals[i]; prod *= val;
} }
return prod; return prod;
} }

View File

@ -0,0 +1,111 @@
package eva2.tools.math;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class MathematicsTest {
@Test
public void testEuclidianDist() throws Exception {
}
@Test
public void testMax() throws Exception {
double[] vals = {1,2,3,4,5,6,4,3,2,2,12};
assertEquals(12.0, Mathematics.max(vals), 0.0);
}
@Test
public void testMean() throws Exception {
double[] vals = {2.0,3.05,4.9,7.8,12.7};
assertEquals(6.09, Mathematics.mean(vals), 0.0);
}
@Test
public void testZeroes() throws Exception {
double[] vals = Mathematics.zeroes(10);
for (final double val : vals) {
assertEquals(0.0, val, 0.0);
}
}
@Test
public void testContains() throws Exception {
assertTrue(Mathematics.contains(new int[]{1,2,3,4,5}, 4));
}
@Test
public void testMakeVector() throws Exception {
double[] vals = Mathematics.makeVector(42.23, 10);
assertEquals(10, vals.length);
for (final double val : vals) {
assertEquals(42.23, val, 0.0);
}
}
@Test
public void testMeanVect() throws Exception {
}
@Test
public void testMedian() throws Exception {
}
@Test
public void testMedian2() throws Exception {
}
@Test
public void testVariance() throws Exception {
double[] values = {9.8,9.2,12.3,15.7,3.14};
assertEquals(21.37892, Mathematics.variance(values), 0.000001);
}
@Test
public void testStdDev() throws Exception {
double[] values = {12.9,13.5,19.8,12.3,10.7};
// Mean
assertEquals(13.84, Mathematics.mean(values), 0.000001);
// Variance of sample
assertEquals(12.188, Mathematics.variance(values), 0.000001);
// Sample std.dev.
assertEquals(3.491131, Mathematics.stdDev(values), 0.000001);
}
@Test
public void testMin() throws Exception {
double[] values = {1.9,2.8,3.7,4.6,5.5};
assertEquals(1.9, Mathematics.min(values), 0.0);
}
@Test
public void testProjectToRange() throws Exception {
}
@Test
public void testScaleRange() throws Exception {
}
@Test
public void testSum() throws Exception {
double[] values = {1.9,2.8,3.7,4.6,5.5};
assertEquals(18.5, Mathematics.sum(values), 0.0);
}
@Test
public void testScale() throws Exception {
}
}