1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.hipparchus.stat.descriptive.vector;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
22
23 import org.hipparchus.UnitTestUtils;
24 import org.hipparchus.exception.MathIllegalArgumentException;
25 import org.hipparchus.linear.RealMatrix;
26 import org.junit.Test;
27
28 public class VectorialCovarianceTest {
29 private double[][] points;
30
31 public VectorialCovarianceTest() {
32 points = new double[][] {
33 { 1.2, 2.3, 4.5},
34 {-0.7, 2.3, 5.0},
35 { 3.1, 0.0, -3.1},
36 { 6.0, 1.2, 4.2},
37 {-0.7, 2.3, 5.0}
38 };
39 }
40
41 @Test
42 public void testMismatch() {
43 try {
44 new VectorialCovariance(8, true).increment(new double[5]);
45 fail("an exception should have been thrown");
46 } catch (MathIllegalArgumentException dme) {
47 assertEquals(5, ((Integer) dme.getParts()[0]).intValue());
48 assertEquals(8, ((Integer) dme.getParts()[1]).intValue());
49 }
50 }
51
52 @Test
53 public void testSimplistic() {
54 VectorialCovariance stat = new VectorialCovariance(2, true);
55 stat.increment(new double[] {-1.0, 1.0});
56 stat.increment(new double[] { 1.0, -1.0});
57 RealMatrix c = stat.getResult();
58 assertEquals( 2.0, c.getEntry(0, 0), 1.0e-12);
59 assertEquals(-2.0, c.getEntry(1, 0), 1.0e-12);
60 assertEquals( 2.0, c.getEntry(1, 1), 1.0e-12);
61 }
62
63 @Test
64 public void testBasicStats() {
65
66 VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
67 for (int i = 0; i < points.length; ++i) {
68 stat.increment(points[i]);
69 }
70
71 assertEquals(points.length, stat.getN());
72
73 RealMatrix c = stat.getResult();
74 double[][] refC = new double[][] {
75 { 8.0470, -1.9195, -3.4445},
76 {-1.9195, 1.0470, 3.2795},
77 {-3.4445, 3.2795, 12.2070}
78 };
79
80 for (int i = 0; i < c.getRowDimension(); ++i) {
81 for (int j = 0; j <= i; ++j) {
82 assertEquals(refC[i][j], c.getEntry(i, j), 1.0e-12);
83 }
84 }
85
86 }
87
88 @Test
89 public void testSerial(){
90 VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
91 assertEquals(stat, UnitTestUtils.serializeAndRecover(stat));
92 }
93 }