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