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  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  public class VectorialMeanTest
27  extends TestCase {
28  
29      public VectorialMeanTest(String name) {
30          super(name);
31          points = null;
32      }
33  
34      public void testMismatch() {
35          try {
36              new VectorialMean(8).increment(new double[5]);
37              fail("an exception should have been thrown");
38          } catch (DimensionMismatchException dme) {
39              assertEquals(5, dme.getDimension1());
40              assertEquals(8, dme.getDimension2());
41          } catch (Exception e) {
42              fail("wrong exception type caught: " + e.getClass().getName());
43          }
44      }
45  
46      public void testSimplistic() throws DimensionMismatchException {
47          VectorialMean stat = new VectorialMean(2);
48          stat.increment(new double[] {-1.0,  1.0});
49          stat.increment(new double[] { 1.0, -1.0});
50          double[] mean = stat.getResult();
51          assertEquals(0.0, mean[0], 1.0e-12);
52          assertEquals(0.0, mean[1], 1.0e-12);
53      }
54  
55      public void testBasicStats() throws DimensionMismatchException {
56  
57          VectorialMean stat = new VectorialMean(points[0].length);
58          for (int i = 0; i < points.length; ++i) {
59              stat.increment(points[i]);
60          }
61  
62          assertEquals(points.length, stat.getN());
63  
64          double[] mean = stat.getResult();
65          double[]   refMean = new double[] { 1.78, 1.62,  3.12};
66  
67          for (int i = 0; i < mean.length; ++i) {
68              assertEquals(refMean[i], mean[i], 1.0e-12);
69          }
70  
71      }
72  
73      public void setUp() {
74          points = new double[][] {
75                  { 1.2, 2.3,  4.5},
76                  {-0.7, 2.3,  5.0},
77                  { 3.1, 0.0, -3.1},
78                  { 6.0, 1.2,  4.2},
79                  {-0.7, 2.3,  5.0}
80          };
81      }
82  
83      public void tearDown() {
84          points = null;
85      }
86  
87      public static Test suite() {
88          return new TestSuite(VectorialMeanTest.class);
89      }
90  
91      private double [][] points;
92  
93  }