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.apache.commons.math4.legacy.linear.RealMatrix;
22 import org.junit.Test;
23 import org.junit.Assert;
24
25 public class VectorialCovarianceTest {
26 private double[][] points;
27
28 public VectorialCovarianceTest() {
29 points = new double[][] {
30 { 1.2, 2.3, 4.5},
31 {-0.7, 2.3, 5.0},
32 { 3.1, 0.0, -3.1},
33 { 6.0, 1.2, 4.2},
34 {-0.7, 2.3, 5.0}
35 };
36 }
37
38 @Test
39 public void testMismatch() {
40 try {
41 new VectorialCovariance(8, true).increment(new double[5]);
42 Assert.fail("an exception should have been thrown");
43 } catch (DimensionMismatchException dme) {
44 Assert.assertEquals(5, dme.getArgument());
45 Assert.assertEquals(8, dme.getDimension());
46 }
47 }
48
49 @Test
50 public void testSimplistic() {
51 VectorialCovariance stat = new VectorialCovariance(2, true);
52 stat.increment(new double[] {-1.0, 1.0});
53 stat.increment(new double[] { 1.0, -1.0});
54 RealMatrix c = stat.getResult();
55 Assert.assertEquals( 2.0, c.getEntry(0, 0), 1.0e-12);
56 Assert.assertEquals(-2.0, c.getEntry(1, 0), 1.0e-12);
57 Assert.assertEquals( 2.0, c.getEntry(1, 1), 1.0e-12);
58 }
59
60 @Test
61 public void testBasicStats() {
62
63 VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
64 for (int i = 0; i < points.length; ++i) {
65 stat.increment(points[i]);
66 }
67
68 Assert.assertEquals(points.length, stat.getN());
69
70 RealMatrix c = stat.getResult();
71 double[][] refC = new double[][] {
72 { 8.0470, -1.9195, -3.4445},
73 {-1.9195, 1.0470, 3.2795},
74 {-3.4445, 3.2795, 12.2070}
75 };
76
77 for (int i = 0; i < c.getRowDimension(); ++i) {
78 for (int j = 0; j <= i; ++j) {
79 Assert.assertEquals(refC[i][j], c.getEntry(i, j), 1.0e-12);
80 }
81 }
82 }
83 }