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.TestUtils;
22
import org.apache.commons.math.linear.RealMatrix;
24
import junit.framework.Test;
25
import junit.framework.TestCase;
26
import junit.framework.TestSuite;
28
public class VectorialCovarianceTest
31
public VectorialCovarianceTest(String name) {
36
public void testMismatch() {
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());
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);
58
public void testBasicStats() throws DimensionMismatchException {
60
VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
61
for (int i = 0; i < points.length; ++i) {
62
stat.increment(points[i]);
65
assertEquals(points.length, stat.getN());
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}
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);
82
public void testSerial(){
83
VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
84
assertEquals(stat, TestUtils.serializeAndRecover(stat));
89
points = new double[][] {
99
public void tearDown() {
103
public static Test suite() {
104
return new TestSuite(VectorialCovarianceTest.class);
107
private double [][] points;