1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.stat.descriptive.moment;
19
20 import org.apache.commons.math.DimensionMismatchException;
21 import org.apache.commons.math.TestUtils;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 public class VectorialMeanTest
28 extends TestCase {
29
30 public VectorialMeanTest(String name) {
31 super(name);
32 points = null;
33 }
34
35 public void testMismatch() {
36 try {
37 new VectorialMean(8).increment(new double[5]);
38 fail("an exception should have been thrown");
39 } catch (DimensionMismatchException dme) {
40 assertEquals(5, dme.getDimension1());
41 assertEquals(8, dme.getDimension2());
42 } catch (Exception e) {
43 fail("wrong exception type caught: " + e.getClass().getName());
44 }
45 }
46
47 public void testSimplistic() throws DimensionMismatchException {
48 VectorialMean stat = new VectorialMean(2);
49 stat.increment(new double[] {-1.0, 1.0});
50 stat.increment(new double[] { 1.0, -1.0});
51 double[] mean = stat.getResult();
52 assertEquals(0.0, mean[0], 1.0e-12);
53 assertEquals(0.0, mean[1], 1.0e-12);
54 }
55
56 public void testBasicStats() throws DimensionMismatchException {
57
58 VectorialMean stat = new VectorialMean(points[0].length);
59 for (int i = 0; i < points.length; ++i) {
60 stat.increment(points[i]);
61 }
62
63 assertEquals(points.length, stat.getN());
64
65 double[] mean = stat.getResult();
66 double[] refMean = new double[] { 1.78, 1.62, 3.12};
67
68 for (int i = 0; i < mean.length; ++i) {
69 assertEquals(refMean[i], mean[i], 1.0e-12);
70 }
71
72 }
73
74 public void testSerial() throws DimensionMismatchException {
75 VectorialMean stat = new VectorialMean(points[0].length);
76 for (int i = 0; i < points.length; ++i) {
77 stat.increment(points[i]);
78 }
79 assertEquals(stat, TestUtils.serializeAndRecover(stat));
80 }
81 @Override
82 public void setUp() {
83 points = new double[][] {
84 { 1.2, 2.3, 4.5},
85 {-0.7, 2.3, 5.0},
86 { 3.1, 0.0, -3.1},
87 { 6.0, 1.2, 4.2},
88 {-0.7, 2.3, 5.0}
89 };
90 }
91
92 @Override
93 public void tearDown() {
94 points = null;
95 }
96
97 public static Test suite() {
98 return new TestSuite(VectorialMeanTest.class);
99 }
100
101 private double [][] points;
102
103 }