View Javadoc
1   //Licensed to the Apache Software Foundation (ASF) under one
2   //or more contributor license agreements.  See the NOTICE file
3   //distributed with this work for additional information
4   //regarding copyright ownership.  The ASF licenses this file
5   //to you under the Apache License, Version 2.0 (the
6   //"License"); you may not use this file except in compliance
7   //with the License.  You may obtain a copy of the License at
8   
9   //https://www.apache.org/licenses/LICENSE-2.0
10  
11  //Unless required by applicable law or agreed to in writing,
12  //software distributed under the License is distributed on an
13  //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  //KIND, either express or implied.  See the License for the
15  //specific language governing permissions and limitations
16  //under the License.
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  }