View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * 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, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math4.legacy.stat.descriptive.moment;
19  
20  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
21  import org.junit.Test;
22  import org.junit.Assert;
23  
24  public class VectorialMeanTest {
25      private double[][] points;
26  
27      public VectorialMeanTest() {
28          points = new double[][] {
29              { 1.2, 2.3,  4.5},
30              {-0.7, 2.3,  5.0},
31              { 3.1, 0.0, -3.1},
32              { 6.0, 1.2,  4.2},
33              {-0.7, 2.3,  5.0}
34          };
35      }
36  
37      @Test
38      public void testMismatch() {
39          try {
40              new VectorialMean(8).increment(new double[5]);
41              Assert.fail("an exception should have been thrown");
42          } catch (DimensionMismatchException dme) {
43              Assert.assertEquals(5, dme.getArgument());
44              Assert.assertEquals(8, dme.getDimension());
45          }
46      }
47  
48      @Test
49      public void testSimplistic() {
50          VectorialMean stat = new VectorialMean(2);
51          stat.increment(new double[] {-1.0,  1.0});
52          stat.increment(new double[] { 1.0, -1.0});
53          double[] mean = stat.getResult();
54          Assert.assertEquals(0.0, mean[0], 1.0e-12);
55          Assert.assertEquals(0.0, mean[1], 1.0e-12);
56      }
57  
58      @Test
59      public void testBasicStats() {
60  
61          VectorialMean stat = new VectorialMean(points[0].length);
62          for (int i = 0; i < points.length; ++i) {
63              stat.increment(points[i]);
64          }
65  
66          Assert.assertEquals(points.length, stat.getN());
67  
68          double[] mean = stat.getResult();
69          double[]   refMean = new double[] { 1.78, 1.62,  3.12};
70  
71          for (int i = 0; i < mean.length; ++i) {
72              Assert.assertEquals(refMean[i], mean[i], 1.0e-12);
73          }
74      }
75  }