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
023 import junit.framework.Test;
024 import junit.framework.TestCase;
025 import junit.framework.TestSuite;
026
027 public class VectorialMeanTest
028 extends TestCase {
029
030 public VectorialMeanTest(String name) {
031 super(name);
032 points = null;
033 }
034
035 public void testMismatch() {
036 try {
037 new VectorialMean(8).increment(new double[5]);
038 fail("an exception should have been thrown");
039 } catch (DimensionMismatchException dme) {
040 assertEquals(5, dme.getDimension1());
041 assertEquals(8, dme.getDimension2());
042 } catch (Exception e) {
043 fail("wrong exception type caught: " + e.getClass().getName());
044 }
045 }
046
047 public void testSimplistic() throws DimensionMismatchException {
048 VectorialMean stat = new VectorialMean(2);
049 stat.increment(new double[] {-1.0, 1.0});
050 stat.increment(new double[] { 1.0, -1.0});
051 double[] mean = stat.getResult();
052 assertEquals(0.0, mean[0], 1.0e-12);
053 assertEquals(0.0, mean[1], 1.0e-12);
054 }
055
056 public void testBasicStats() throws DimensionMismatchException {
057
058 VectorialMean stat = new VectorialMean(points[0].length);
059 for (int i = 0; i < points.length; ++i) {
060 stat.increment(points[i]);
061 }
062
063 assertEquals(points.length, stat.getN());
064
065 double[] mean = stat.getResult();
066 double[] refMean = new double[] { 1.78, 1.62, 3.12};
067
068 for (int i = 0; i < mean.length; ++i) {
069 assertEquals(refMean[i], mean[i], 1.0e-12);
070 }
071
072 }
073
074 public void testSerial() throws DimensionMismatchException {
075 VectorialMean stat = new VectorialMean(points[0].length);
076 for (int i = 0; i < points.length; ++i) {
077 stat.increment(points[i]);
078 }
079 assertEquals(stat, TestUtils.serializeAndRecover(stat));
080 }
081 @Override
082 public void setUp() {
083 points = new double[][] {
084 { 1.2, 2.3, 4.5},
085 {-0.7, 2.3, 5.0},
086 { 3.1, 0.0, -3.1},
087 { 6.0, 1.2, 4.2},
088 {-0.7, 2.3, 5.0}
089 };
090 }
091
092 @Override
093 public void tearDown() {
094 points = null;
095 }
096
097 public static Test suite() {
098 return new TestSuite(VectorialMeanTest.class);
099 }
100
101 private double [][] points;
102
103 }