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   //http://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.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 }