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  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  public class VectorialMeanTest
28  extends TestCase {
29  
30      public VectorialMeanTest(String name) {
31          super(name);
32          points = null;
33      }
34  
35      public void testMismatch() {
36          try {
37              new VectorialMean(8).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());
44          }
45      }
46  
47      public void testSimplistic() throws DimensionMismatchException {
48          VectorialMean stat = new VectorialMean(2);
49          stat.increment(new double[] {-1.0,  1.0});
50          stat.increment(new double[] { 1.0, -1.0});
51          double[] mean = stat.getResult();
52          assertEquals(0.0, mean[0], 1.0e-12);
53          assertEquals(0.0, mean[1], 1.0e-12);
54      }
55  
56      public void testBasicStats() throws DimensionMismatchException {
57  
58          VectorialMean stat = new VectorialMean(points[0].length);
59          for (int i = 0; i < points.length; ++i) {
60              stat.increment(points[i]);
61          }
62  
63          assertEquals(points.length, stat.getN());
64  
65          double[] mean = stat.getResult();
66          double[]   refMean = new double[] { 1.78, 1.62,  3.12};
67  
68          for (int i = 0; i < mean.length; ++i) {
69              assertEquals(refMean[i], mean[i], 1.0e-12);
70          }
71  
72      }
73  
74      public void testSerial() throws DimensionMismatchException {
75          VectorialMean stat = new VectorialMean(points[0].length);
76          for (int i = 0; i < points.length; ++i) {
77              stat.increment(points[i]);
78          }
79          assertEquals(stat, TestUtils.serializeAndRecover(stat));
80      }
81      @Override
82      public void setUp() {
83          points = new double[][] {
84                  { 1.2, 2.3,  4.5},
85                  {-0.7, 2.3,  5.0},
86                  { 3.1, 0.0, -3.1},
87                  { 6.0, 1.2,  4.2},
88                  {-0.7, 2.3,  5.0}
89          };
90      }
91  
92      @Override
93      public void tearDown() {
94          points = null;
95      }
96  
97      public static Test suite() {
98          return new TestSuite(VectorialMeanTest.class);
99      }
100 
101     private double [][] points;
102 
103 }