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.linear.RealMatrix;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 public class VectorialCovarianceTest
28 extends TestCase {
29
30 public VectorialCovarianceTest(String name) {
31 super(name);
32 points = null;
33 }
34
35 public void testMismatch() {
36 try {
37 new VectorialCovariance(8, true).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 VectorialCovariance stat = new VectorialCovariance(2, true);
49 stat.increment(new double[] {-1.0, 1.0});
50 stat.increment(new double[] { 1.0, -1.0});
51 RealMatrix c = stat.getResult();
52 assertEquals( 2.0, c.getEntry(0, 0), 1.0e-12);
53 assertEquals(-2.0, c.getEntry(1, 0), 1.0e-12);
54 assertEquals( 2.0, c.getEntry(1, 1), 1.0e-12);
55 }
56
57 public void testBasicStats() throws DimensionMismatchException {
58
59 VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
60 for (int i = 0; i < points.length; ++i) {
61 stat.increment(points[i]);
62 }
63
64 assertEquals(points.length, stat.getN());
65
66 RealMatrix c = stat.getResult();
67 double[][] refC = new double[][] {
68 { 8.0470, -1.9195, -3.4445},
69 {-1.9195, 1.0470, 3.2795},
70 {-3.4445, 3.2795, 12.2070}
71 };
72
73 for (int i = 0; i < c.getRowDimension(); ++i) {
74 for (int j = 0; j <= i; ++j) {
75 assertEquals(refC[i][j], c.getEntry(i, j), 1.0e-12);
76 }
77 }
78
79 }
80
81 public void setUp() {
82 points = new double[][] {
83 { 1.2, 2.3, 4.5},
84 {-0.7, 2.3, 5.0},
85 { 3.1, 0.0, -3.1},
86 { 6.0, 1.2, 4.2},
87 {-0.7, 2.3, 5.0}
88 };
89 }
90
91 public void tearDown() {
92 points = null;
93 }
94
95 public static Test suite() {
96 return new TestSuite(VectorialCovarianceTest.class);
97 }
98
99 private double [][] points;
100
101 }