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.TestUtils;
22  import org.apache.commons.math.linear.RealMatrix;
23  
24  import junit.framework.Test;
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  public class VectorialCovarianceTest
29  extends TestCase {
30  
31      public VectorialCovarianceTest(String name) {
32          super(name);
33          points = null;
34      }
35  
36      public void testMismatch() {
37          try {
38              new VectorialCovariance(8, true).increment(new double[5]);
39              fail("an exception should have been thrown");
40          } catch (DimensionMismatchException dme) {
41              assertEquals(5, dme.getDimension1());
42              assertEquals(8, dme.getDimension2());
43          } catch (Exception e) {
44              fail("wrong exception type caught: " + e.getClass().getName());
45          }
46      }
47  
48      public void testSimplistic() throws DimensionMismatchException {
49          VectorialCovariance stat = new VectorialCovariance(2, true);
50          stat.increment(new double[] {-1.0,  1.0});
51          stat.increment(new double[] { 1.0, -1.0});
52          RealMatrix c = stat.getResult();
53          assertEquals( 2.0, c.getEntry(0, 0), 1.0e-12);
54          assertEquals(-2.0, c.getEntry(1, 0), 1.0e-12);
55          assertEquals( 2.0, c.getEntry(1, 1), 1.0e-12);
56      }
57  
58      public void testBasicStats() throws DimensionMismatchException {
59  
60          VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
61          for (int i = 0; i < points.length; ++i) {
62              stat.increment(points[i]);
63          }
64  
65          assertEquals(points.length, stat.getN());
66  
67          RealMatrix c = stat.getResult();
68          double[][] refC    = new double[][] {
69                  { 8.0470, -1.9195, -3.4445},
70                  {-1.9195,  1.0470,  3.2795},
71                  {-3.4445,  3.2795, 12.2070}
72          };
73  
74          for (int i = 0; i < c.getRowDimension(); ++i) {
75              for (int j = 0; j <= i; ++j) {
76                  assertEquals(refC[i][j], c.getEntry(i, j), 1.0e-12);
77              }
78          }
79  
80      }
81  
82      public void testSerial(){
83          VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
84          assertEquals(stat, TestUtils.serializeAndRecover(stat));
85      }
86      
87      @Override
88      public void setUp() {
89          points = new double[][] {
90                  { 1.2, 2.3,  4.5},
91                  {-0.7, 2.3,  5.0},
92                  { 3.1, 0.0, -3.1},
93                  { 6.0, 1.2,  4.2},
94                  {-0.7, 2.3,  5.0}
95          };
96      }
97  
98      @Override
99      public void tearDown() {
100         points = null;
101     }
102 
103     public static Test suite() {
104         return new TestSuite(VectorialCovarianceTest.class);
105     }
106 
107     private double [][] points;
108 
109 }