1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.stat.descriptive.moment;
19
20 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
21 import org.junit.Test;
22 import org.junit.Assert;
23
24 public class VectorialMeanTest {
25 private double[][] points;
26
27 public VectorialMeanTest() {
28 points = new double[][] {
29 { 1.2, 2.3, 4.5},
30 {-0.7, 2.3, 5.0},
31 { 3.1, 0.0, -3.1},
32 { 6.0, 1.2, 4.2},
33 {-0.7, 2.3, 5.0}
34 };
35 }
36
37 @Test
38 public void testMismatch() {
39 try {
40 new VectorialMean(8).increment(new double[5]);
41 Assert.fail("an exception should have been thrown");
42 } catch (DimensionMismatchException dme) {
43 Assert.assertEquals(5, dme.getArgument());
44 Assert.assertEquals(8, dme.getDimension());
45 }
46 }
47
48 @Test
49 public void testSimplistic() {
50 VectorialMean stat = new VectorialMean(2);
51 stat.increment(new double[] {-1.0, 1.0});
52 stat.increment(new double[] { 1.0, -1.0});
53 double[] mean = stat.getResult();
54 Assert.assertEquals(0.0, mean[0], 1.0e-12);
55 Assert.assertEquals(0.0, mean[1], 1.0e-12);
56 }
57
58 @Test
59 public void testBasicStats() {
60
61 VectorialMean stat = new VectorialMean(points[0].length);
62 for (int i = 0; i < points.length; ++i) {
63 stat.increment(points[i]);
64 }
65
66 Assert.assertEquals(points.length, stat.getN());
67
68 double[] mean = stat.getResult();
69 double[] refMean = new double[] { 1.78, 1.62, 3.12};
70
71 for (int i = 0; i < mean.length; ++i) {
72 Assert.assertEquals(refMean[i], mean[i], 1.0e-12);
73 }
74 }
75 }