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  package org.apache.commons.math4.legacy.stat.descriptive;
18  
19  import org.apache.commons.math4.legacy.TestUtils;
20  import org.apache.commons.math4.legacy.stat.descriptive.moment.SecondMoment;
21  import org.apache.commons.math4.core.jdkmath.JdkMath;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  /**
26   * Test cases for {@link StorelessUnivariateStatistic} classes.
27   */
28  public abstract class StorelessUnivariateStatisticAbstractTest
29      extends UnivariateStatisticAbstractTest {
30  
31      /** Small sample arrays */
32      protected double[][] smallSamples = {{}, {1}, {1,2}, {1,2,3}, {1,2,3,4}};
33  
34      /** Return a new instance of the statistic */
35      @Override
36      public abstract UnivariateStatistic getUnivariateStatistic();
37  
38      /**Expected value for  the testArray defined in UnivariateStatisticAbstractTest */
39      @Override
40      public abstract double expectedValue();
41  
42      /**
43       *  Verifies that increment() and incrementAll work properly.
44       */
45      @Test
46      public void testIncrementation() {
47  
48          StorelessUnivariateStatistic statistic =
49              (StorelessUnivariateStatistic) getUnivariateStatistic();
50  
51          // Add testArray one value at a time and check result
52          for (int i = 0; i < testArray.length; i++) {
53              statistic.increment(testArray[i]);
54          }
55  
56          Assert.assertEquals(expectedValue(), statistic.getResult(), getTolerance());
57          Assert.assertEquals(testArray.length, statistic.getN());
58  
59          statistic.clear();
60  
61          // Add testArray all at once and check again
62          statistic.incrementAll(testArray);
63          Assert.assertEquals(expectedValue(), statistic.getResult(), getTolerance());
64          Assert.assertEquals(testArray.length, statistic.getN());
65  
66          statistic.clear();
67  
68          // Cleared
69          checkClearValue(statistic);
70          Assert.assertEquals(0, statistic.getN());
71      }
72  
73      protected void checkClearValue(StorelessUnivariateStatistic statistic){
74          Assert.assertTrue(Double.isNaN(statistic.getResult()));
75      }
76  
77      @Test
78      public void testEqualsAndHashCode() {
79          StorelessUnivariateStatistic statistic =
80              (StorelessUnivariateStatistic) getUnivariateStatistic();
81          StorelessUnivariateStatistic statistic2 = null;
82  
83          Assert.assertFalse("non-null, compared to null", statistic.equals(statistic2));
84          Assert.assertEquals("reflexive, non-null", statistic, statistic);
85  
86          int emptyHash = statistic.hashCode();
87          statistic2 = (StorelessUnivariateStatistic) getUnivariateStatistic();
88          Assert.assertEquals("empty stats should be equal", statistic, statistic2);
89          Assert.assertEquals("empty stats should have the same hash code",
90                  emptyHash, statistic2.hashCode());
91  
92          statistic.increment(1d);
93          Assert.assertEquals("reflexive, non-empty", statistic, statistic);
94          Assert.assertNotEquals("non-empty, compared to empty", statistic, statistic2);
95          Assert.assertNotEquals("non-empty, compared to empty", statistic2, statistic);
96          Assert.assertTrue("non-empty stat should have different hash code from empty stat",
97                  statistic.hashCode() != emptyHash);
98  
99          statistic2.increment(1d);
100         Assert.assertEquals("stats with same data should be equal", statistic, statistic2);
101         Assert.assertEquals("stats with same data should have the same hash code",
102                 statistic.hashCode(), statistic2.hashCode());
103 
104         statistic.increment(Double.POSITIVE_INFINITY);
105         Assert.assertNotEquals("stats with different n's should not be equal", statistic2, statistic);
106         Assert.assertTrue("stats with different n's should have different hash codes",
107                 statistic.hashCode() != statistic2.hashCode());
108 
109         statistic2.increment(Double.POSITIVE_INFINITY);
110         Assert.assertEquals("stats with same data should be equal", statistic, statistic2);
111         Assert.assertEquals("stats with same data should have the same hash code",
112                 statistic.hashCode(), statistic2.hashCode());
113 
114         statistic.clear();
115         statistic2.clear();
116         Assert.assertEquals("cleared stats should be equal", statistic, statistic2);
117         Assert.assertEquals("cleared stats should have thash code of empty stat",
118                 emptyHash, statistic2.hashCode());
119         Assert.assertEquals("cleared stats should have thash code of empty stat",
120                 emptyHash, statistic.hashCode());
121     }
122 
123     @Test
124     public void testMomentSmallSamples() {
125         UnivariateStatistic stat = getUnivariateStatistic();
126         if (stat instanceof SecondMoment) {
127             SecondMoment moment = (SecondMoment) getUnivariateStatistic();
128             Assert.assertTrue(Double.isNaN(moment.getResult()));
129             moment.increment(1d);
130             Assert.assertEquals(0d, moment.getResult(), 0);
131         }
132     }
133 
134     /**
135      * Make sure that evaluate(double[]) and inrementAll(double[]),
136      * getResult() give same results.
137      */
138     @Test
139     public void testConsistency() {
140         StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) getUnivariateStatistic();
141         stat.incrementAll(testArray);
142         Assert.assertEquals(stat.getResult(), stat.evaluate(testArray), getTolerance());
143         for (int i = 0; i < smallSamples.length; i++) {
144             stat.clear();
145             for (int j =0; j < smallSamples[i].length; j++) {
146                 stat.increment(smallSamples[i][j]);
147             }
148             TestUtils.assertEquals(stat.getResult(), stat.evaluate(smallSamples[i]), getTolerance());
149         }
150     }
151 
152     /**
153      * Verifies that copied statistics remain equal to originals when
154      * incremented the same way.
155      */
156     @Test
157     public void testCopyConsistency() {
158 
159         StorelessUnivariateStatistic master =
160             (StorelessUnivariateStatistic) getUnivariateStatistic();
161 
162         StorelessUnivariateStatistic replica = null;
163 
164         // Randomly select a portion of testArray to load first
165         long index = JdkMath.round((JdkMath.random()) * testArray.length);
166 
167         // Put first half in master and copy master to replica
168         master.incrementAll(testArray, 0, (int) index);
169         replica = master.copy();
170 
171         // Check same
172         Assert.assertEquals(replica, master);
173         Assert.assertEquals(master, replica);
174 
175         // Now add second part to both and check again
176         master.incrementAll(testArray,
177                 (int) index, (int) (testArray.length - index));
178         replica.incrementAll(testArray,
179                 (int) index, (int) (testArray.length - index));
180         Assert.assertEquals(replica, master);
181         Assert.assertEquals(master, replica);
182     }
183 
184     /**
185      * Make sure that evaluate(double[]) does not alter the internal state.
186      */
187     @Test
188     public void testEvaluateInternalState() {
189         StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) getUnivariateStatistic();
190         stat.evaluate(testArray);
191         Assert.assertEquals(0, stat.getN());
192 
193         stat.incrementAll(testArray);
194 
195         StorelessUnivariateStatistic savedStatistic = stat.copy();
196 
197         Assert.assertNotEquals(stat.getResult(), stat.evaluate(testArray, 0, 5), getTolerance());
198 
199         Assert.assertEquals(savedStatistic.getResult(), stat.getResult(), 0.0);
200         Assert.assertEquals(savedStatistic.getN(), stat.getN());
201     }
202 }