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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat.descriptive;
23  
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertFalse;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  import java.util.Locale;
31  
32  import org.hipparchus.UnitTestUtils;
33  import org.hipparchus.exception.MathIllegalArgumentException;
34  import org.hipparchus.util.FastMath;
35  import org.junit.Test;
36  
37  /**
38   * Test cases for the {@link MultivariateSummaryStatistics} class.
39   */
40  public class MultivariateSummaryStatisticsTest {
41  
42      protected MultivariateSummaryStatistics createMultivariateSummaryStatistics(int k, boolean covarianceBiasCorrected) {
43          return new MultivariateSummaryStatistics(k, covarianceBiasCorrected);
44      }
45  
46      @Test
47      public void testToString() {
48          MultivariateSummaryStatistics stats = createMultivariateSummaryStatistics(2, true);
49          stats.addValue(new double[] {1, 3});
50          stats.addValue(new double[] {2, 2});
51          stats.addValue(new double[] {3, 1});
52          Locale d = Locale.getDefault();
53          Locale.setDefault(Locale.US);
54          final String suffix = System.getProperty("line.separator");
55          assertEquals("MultivariateSummaryStatistics:" + suffix+
56                       "n: 3" +suffix+
57                       "min: 1.0, 1.0" +suffix+
58                       "max: 3.0, 3.0" +suffix+
59                       "mean: 2.0, 2.0" +suffix+
60                       "geometric mean: 1.817..., 1.817..." +suffix+
61                       "sum of squares: 14.0, 14.0" +suffix+
62                       "sum of logarithms: 1.791..., 1.791..." +suffix+
63                       "standard deviation: 1.0, 1.0" +suffix+
64                       "covariance: Array2DRowRealMatrix{{1.0,-1.0},{-1.0,1.0}}" +suffix,
65                       stats.toString().replaceAll("([0-9]+\\.[0-9][0-9][0-9])[0-9]+", "$1..."));
66          Locale.setDefault(d);
67      }
68  
69      @Test
70      public void testDimension() {
71          try {
72              createMultivariateSummaryStatistics(2, true).addValue(new double[3]);
73              fail("Expecting MathIllegalArgumentException");
74          } catch (MathIllegalArgumentException dme) {
75              // expected behavior
76          }
77      }
78  
79      /** test stats */
80      @Test
81      public void testStats() {
82          MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true);
83          assertEquals(0, u.getN());
84          u.addValue(new double[] { 1, 2 });
85          u.addValue(new double[] { 2, 3 });
86          u.addValue(new double[] { 2, 3 });
87          u.addValue(new double[] { 3, 4 });
88          assertEquals( 4, u.getN());
89          assertEquals( 8, u.getSum()[0], 1.0e-10);
90          assertEquals(12, u.getSum()[1], 1.0e-10);
91          assertEquals(18, u.getSumSq()[0], 1.0e-10);
92          assertEquals(38, u.getSumSq()[1], 1.0e-10);
93          assertEquals( 1, u.getMin()[0], 1.0e-10);
94          assertEquals( 2, u.getMin()[1], 1.0e-10);
95          assertEquals( 3, u.getMax()[0], 1.0e-10);
96          assertEquals( 4, u.getMax()[1], 1.0e-10);
97          assertEquals(2.4849066497880003102, u.getSumLog()[0], 1.0e-10);
98          assertEquals( 4.276666119016055311, u.getSumLog()[1], 1.0e-10);
99          assertEquals( 1.8612097182041991979, u.getGeometricMean()[0], 1.0e-10);
100         assertEquals( 2.9129506302439405217, u.getGeometricMean()[1], 1.0e-10);
101         assertEquals( 2, u.getMean()[0], 1.0e-10);
102         assertEquals( 3, u.getMean()[1], 1.0e-10);
103         assertEquals(FastMath.sqrt(2.0 / 3.0), u.getStandardDeviation()[0], 1.0e-10);
104         assertEquals(FastMath.sqrt(2.0 / 3.0), u.getStandardDeviation()[1], 1.0e-10);
105         assertEquals(2.0 / 3.0, u.getCovariance().getEntry(0, 0), 1.0e-10);
106         assertEquals(2.0 / 3.0, u.getCovariance().getEntry(0, 1), 1.0e-10);
107         assertEquals(2.0 / 3.0, u.getCovariance().getEntry(1, 0), 1.0e-10);
108         assertEquals(2.0 / 3.0, u.getCovariance().getEntry(1, 1), 1.0e-10);
109         u.clear();
110         assertEquals(0, u.getN());
111     }
112 
113     @Test
114     public void testN0andN1Conditions() {
115         MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(1, true);
116         assertTrue(Double.isNaN(u.getMean()[0]));
117         assertTrue(Double.isNaN(u.getStandardDeviation()[0]));
118 
119         /* n=1 */
120         u.addValue(new double[] { 1 });
121         assertEquals(1.0, u.getMean()[0], 1.0e-10);
122         assertEquals(1.0, u.getGeometricMean()[0], 1.0e-10);
123         assertEquals(0.0, u.getStandardDeviation()[0], 1.0e-10);
124 
125         /* n=2 */
126         u.addValue(new double[] { 2 });
127         assertTrue(u.getStandardDeviation()[0] > 0);
128 
129     }
130 
131     @Test
132     public void testNaNContracts() {
133         MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(1, true);
134         assertTrue(Double.isNaN(u.getMean()[0]));
135         assertTrue(Double.isNaN(u.getMin()[0]));
136         assertTrue(Double.isNaN(u.getStandardDeviation()[0]));
137         assertTrue(Double.isNaN(u.getGeometricMean()[0]));
138 
139         u.addValue(new double[] { 1.0 });
140         assertFalse(Double.isNaN(u.getMean()[0]));
141         assertFalse(Double.isNaN(u.getMin()[0]));
142         assertFalse(Double.isNaN(u.getStandardDeviation()[0]));
143         assertFalse(Double.isNaN(u.getGeometricMean()[0]));
144 
145     }
146 
147     @Test
148     public void testSerialization() {
149         MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true);
150         // Empty test
151         UnitTestUtils.checkSerializedEquality(u);
152         MultivariateSummaryStatistics s = (MultivariateSummaryStatistics) UnitTestUtils.serializeAndRecover(u);
153         assertEquals(u, s);
154 
155         // Add some data
156         u.addValue(new double[] { 2d, 1d });
157         u.addValue(new double[] { 1d, 1d });
158         u.addValue(new double[] { 3d, 1d });
159         u.addValue(new double[] { 4d, 1d });
160         u.addValue(new double[] { 5d, 1d });
161 
162         // Test again
163         UnitTestUtils.checkSerializedEquality(u);
164         s = (MultivariateSummaryStatistics) UnitTestUtils.serializeAndRecover(u);
165         assertEquals(u, s);
166 
167     }
168 
169     @SuppressWarnings("unlikely-arg-type")
170     @Test
171     public void testEqualsAndHashCode() {
172         MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true);
173         MultivariateSummaryStatistics t = null;
174         int emptyHash = u.hashCode();
175         assertTrue(u.equals(u));
176         assertFalse(u.equals(t));
177         assertFalse(u.equals(Double.valueOf(0)));
178         t = createMultivariateSummaryStatistics(2, true);
179         assertTrue(t.equals(u));
180         assertTrue(u.equals(t));
181         assertEquals(emptyHash, t.hashCode());
182 
183         // Add some data to u
184         u.addValue(new double[] { 2d, 1d });
185         u.addValue(new double[] { 1d, 1d });
186         u.addValue(new double[] { 3d, 1d });
187         u.addValue(new double[] { 4d, 1d });
188         u.addValue(new double[] { 5d, 1d });
189         assertFalse(t.equals(u));
190         assertFalse(u.equals(t));
191         assertTrue(u.hashCode() != t.hashCode());
192 
193         //Add data in same order to t
194         t.addValue(new double[] { 2d, 1d });
195         t.addValue(new double[] { 1d, 1d });
196         t.addValue(new double[] { 3d, 1d });
197         t.addValue(new double[] { 4d, 1d });
198         t.addValue(new double[] { 5d, 1d });
199         assertTrue(t.equals(u));
200         assertTrue(u.equals(t));
201         assertEquals(u.hashCode(), t.hashCode());
202 
203         // Clear and make sure summaries are indistinguishable from empty summary
204         u.clear();
205         t.clear();
206         assertTrue(t.equals(u));
207         assertTrue(u.equals(t));
208         assertEquals(emptyHash, t.hashCode());
209         assertEquals(emptyHash, u.hashCode());
210     }
211 }