001 //Licensed to the Apache Software Foundation (ASF) under one 002 //or more contributor license agreements. See the NOTICE file 003 //distributed with this work for additional information 004 //regarding copyright ownership. The ASF licenses this file 005 //to you under the Apache License, Version 2.0 (the 006 //"License"); you may not use this file except in compliance 007 //with the License. You may obtain a copy of the License at 008 009 //http://www.apache.org/licenses/LICENSE-2.0 010 011 //Unless required by applicable law or agreed to in writing, 012 //software distributed under the License is distributed on an 013 //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 014 //KIND, either express or implied. See the License for the 015 //specific language governing permissions and limitations 016 //under the License. 017 018 package org.apache.commons.math.stat.descriptive.moment; 019 020 import org.apache.commons.math.DimensionMismatchException; 021 import org.apache.commons.math.TestUtils; 022 import org.apache.commons.math.linear.RealMatrix; 023 024 import junit.framework.Test; 025 import junit.framework.TestCase; 026 import junit.framework.TestSuite; 027 028 public class VectorialCovarianceTest 029 extends TestCase { 030 031 public VectorialCovarianceTest(String name) { 032 super(name); 033 points = null; 034 } 035 036 public void testMismatch() { 037 try { 038 new VectorialCovariance(8, true).increment(new double[5]); 039 fail("an exception should have been thrown"); 040 } catch (DimensionMismatchException dme) { 041 assertEquals(5, dme.getDimension1()); 042 assertEquals(8, dme.getDimension2()); 043 } catch (Exception e) { 044 fail("wrong exception type caught: " + e.getClass().getName()); 045 } 046 } 047 048 public void testSimplistic() throws DimensionMismatchException { 049 VectorialCovariance stat = new VectorialCovariance(2, true); 050 stat.increment(new double[] {-1.0, 1.0}); 051 stat.increment(new double[] { 1.0, -1.0}); 052 RealMatrix c = stat.getResult(); 053 assertEquals( 2.0, c.getEntry(0, 0), 1.0e-12); 054 assertEquals(-2.0, c.getEntry(1, 0), 1.0e-12); 055 assertEquals( 2.0, c.getEntry(1, 1), 1.0e-12); 056 } 057 058 public void testBasicStats() throws DimensionMismatchException { 059 060 VectorialCovariance stat = new VectorialCovariance(points[0].length, true); 061 for (int i = 0; i < points.length; ++i) { 062 stat.increment(points[i]); 063 } 064 065 assertEquals(points.length, stat.getN()); 066 067 RealMatrix c = stat.getResult(); 068 double[][] refC = new double[][] { 069 { 8.0470, -1.9195, -3.4445}, 070 {-1.9195, 1.0470, 3.2795}, 071 {-3.4445, 3.2795, 12.2070} 072 }; 073 074 for (int i = 0; i < c.getRowDimension(); ++i) { 075 for (int j = 0; j <= i; ++j) { 076 assertEquals(refC[i][j], c.getEntry(i, j), 1.0e-12); 077 } 078 } 079 080 } 081 082 public void testSerial(){ 083 VectorialCovariance stat = new VectorialCovariance(points[0].length, true); 084 assertEquals(stat, TestUtils.serializeAndRecover(stat)); 085 } 086 087 @Override 088 public void setUp() { 089 points = new double[][] { 090 { 1.2, 2.3, 4.5}, 091 {-0.7, 2.3, 5.0}, 092 { 3.1, 0.0, -3.1}, 093 { 6.0, 1.2, 4.2}, 094 {-0.7, 2.3, 5.0} 095 }; 096 } 097 098 @Override 099 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 }