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
9
//http://www.apache.org/licenses/LICENSE-2.0
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
18
package org.apache.commons.math.stat.descriptive.moment;
20
import org.apache.commons.math.DimensionMismatchException;
21
import org.apache.commons.math.linear.RealMatrix;
23
import junit.framework.Test;
24
import junit.framework.TestCase;
25
import junit.framework.TestSuite;
27
public class VectorialCovarianceTest
30
public VectorialCovarianceTest(String name) {
35
public void testMismatch() {
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());
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);
57
public void testBasicStats() throws DimensionMismatchException {
59
VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
60
for (int i = 0; i < points.length; ++i) {
61
stat.increment(points[i]);
64
assertEquals(points.length, stat.getN());
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}
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);
82
points = new double[][] {
91
public void tearDown() {
95
public static Test suite() {
96
return new TestSuite(VectorialCovarianceTest.class);
99
private double [][] points;