View Javadoc
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   //https://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.hipparchus.stat.descriptive.vector;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.fail;
22  
23  import org.hipparchus.UnitTestUtils;
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.stat.descriptive.moment.Mean;
26  import org.junit.Test;
27  
28  public class VectorialStorelessStatisticTest {
29      private double[][] points;
30  
31      public VectorialStorelessStatisticTest() {
32          points = new double[][] {
33              { 1.2, 2.3,  4.5},
34              {-0.7, 2.3,  5.0},
35              { 3.1, 0.0, -3.1},
36              { 6.0, 1.2,  4.2},
37              {-0.7, 2.3,  5.0}
38          };
39      }
40  
41      protected VectorialStorelessStatistic createStatistic(int dimension) {
42          return new VectorialStorelessStatistic(dimension, new Mean());
43      }
44  
45      @Test
46      public void testMismatch() {
47          try {
48              createStatistic(8).increment(new double[5]);
49              fail("an exception should have been thrown");
50          } catch (MathIllegalArgumentException dme) {
51              assertEquals(5, ((Integer) dme.getParts()[0]).intValue());
52              assertEquals(8, ((Integer) dme.getParts()[1]).intValue());
53          }
54      }
55  
56      @Test
57      public void testSimplistic() {
58          VectorialStorelessStatistic stat = createStatistic(2);
59          stat.increment(new double[] {-1.0,  1.0});
60          stat.increment(new double[] { 1.0, -1.0});
61          double[] mean = stat.getResult();
62          assertEquals(0.0, mean[0], 1.0e-12);
63          assertEquals(0.0, mean[1], 1.0e-12);
64      }
65  
66      @Test
67      public void testBasicStats() {
68  
69          VectorialStorelessStatistic stat = createStatistic(points[0].length);
70          for (int i = 0; i < points.length; ++i) {
71              stat.increment(points[i]);
72          }
73  
74          assertEquals(points.length, stat.getN());
75  
76          double[] mean = stat.getResult();
77          double[]   refMean = new double[] { 1.78, 1.62,  3.12};
78  
79          for (int i = 0; i < mean.length; ++i) {
80              assertEquals(refMean[i], mean[i], 1.0e-12);
81          }
82      }
83  
84      @Test
85      public void testSerial() {
86          VectorialStorelessStatistic stat = createStatistic(points[0].length);
87          for (int i = 0; i < points.length; ++i) {
88              stat.increment(points[i]);
89          }
90          assertEquals(stat, UnitTestUtils.serializeAndRecover(stat));
91      }
92  }