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  s * 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  import org.hipparchus.UnitTestUtils;
25  import org.hipparchus.random.RandomDataGenerator;
26  import org.hipparchus.util.FastMath;
27  import org.junit.jupiter.api.Test;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertNotSame;
35  
36  /**
37   * Test cases for the {@link UnivariateStatistic} class.
38   */
39  public abstract class UnivariateStatisticAbstractTest {
40  
41      protected double mean = 12.404545454545455d;
42      protected double geoMean = 12.070589161633011d;
43  
44      protected double var = 10.00235930735931d;
45      protected double std = FastMath.sqrt(var);
46      protected double skew = 1.437423729196190d;
47      protected double kurt = 2.377191264804700d;
48  
49      protected double min = 8.2d;
50      protected double max = 21d;
51      protected double median = 12d;
52      protected double percentile5 = 8.29d;
53      protected double percentile95 = 20.82d;
54  
55      protected double product = 628096400563833396009676.9200400128d;
56      protected double sumLog = 54.7969806116451507d;
57      protected double sumSq = 3595.250d;
58      protected double sum = 272.90d;
59      protected double secondMoment = 210.04954545454547d;
60      protected double thirdMoment = 868.0906859504136;
61      protected double fourthMoment = 9244.080993773481;
62  
63  
64      protected double weightedMean = 12.366995073891626d;
65      protected double weightedVar =   9.974760968886391d;
66      protected double weightedStd = FastMath.sqrt(weightedVar);
67      protected double weightedProduct = 8517647448765288000000d;
68      protected double weightedSum = 251.05d;
69  
70      protected double tolerance = 10E-12;
71  
72      protected double[] testArray =
73          { 12.5, 12.0, 11.8, 14.2, 14.9, 14.5, 21.0,  8.2, 10.3, 11.3,
74            14.1,  9.9, 12.2, 12.0, 12.1, 11.0, 19.8, 11.0, 10.0,  8.8,
75             9.0, 12.3 };
76  
77      protected double[] testWeightsArray =
78          {  1.5,  0.8,  1.2,  0.4,  0.8,  1.8,  1.2,  1.1,  1.0,  0.7,
79             1.3,  0.6,  0.7,  1.3,  0.7,  1.0,  0.4,  0.1,  1.4,  0.9,
80             1.1,  0.3 };
81  
82      protected double[] identicalWeightsArray =
83          {  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
84             0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
85             0.5,  0.5 };
86  
87      protected double[] unitWeightsArray =
88          {  1.0,  1.0,  1.0,  1.0,  1.0,  1.0,  1.0,  1.0,  1.0,  1.0,
89             1.0,  1.0,  1.0,  1.0,  1.0,  1.0,  1.0,  1.0,  1.0,  1.0,
90             1.0,  1.0 };
91  
92      public abstract UnivariateStatistic getUnivariateStatistic();
93  
94      public abstract double expectedValue();
95  
96      public double getTolerance() {
97          return tolerance;
98      }
99  
100     @Test
101     public void testEvaluation() {
102         assertEquals(expectedValue(), getUnivariateStatistic().evaluate(testArray), getTolerance());
103     }
104 
105     @Test
106     public void testEvaluateArraySegment() {
107         final UnivariateStatistic stat = getUnivariateStatistic();
108         final double[] arrayZero = new double[5];
109         System.arraycopy(testArray, 0, arrayZero, 0, 5);
110         assertEquals(stat.evaluate(arrayZero), stat.evaluate(testArray, 0, 5), 0);
111         final double[] arrayOne = new double[5];
112         System.arraycopy(testArray, 5, arrayOne, 0, 5);
113         assertEquals(stat.evaluate(arrayOne), stat.evaluate(testArray, 5, 5), 0);
114         final double[] arrayEnd = new double[5];
115         System.arraycopy(testArray, testArray.length - 5, arrayEnd, 0, 5);
116         assertEquals(stat.evaluate(arrayEnd), stat.evaluate(testArray, testArray.length - 5, 5), 0);
117     }
118 
119     @Test
120     public void testEvaluateArraySegmentWeighted() {
121         // See if this statistic computes weighted statistics
122         // If not, skip this test
123         UnivariateStatistic statistic = getUnivariateStatistic();
124         if (!(statistic instanceof WeightedEvaluation)) {
125             return;
126         }
127         final WeightedEvaluation stat = (WeightedEvaluation) getUnivariateStatistic();
128         final double[] arrayZero = new double[5];
129         final double[] weightZero = new double[5];
130         System.arraycopy(testArray, 0, arrayZero, 0, 5);
131         System.arraycopy(testWeightsArray, 0, weightZero, 0, 5);
132         assertEquals(stat.evaluate(arrayZero, weightZero),
133                             stat.evaluate(testArray, testWeightsArray, 0, 5), 0);
134         final double[] arrayOne = new double[5];
135         final double[] weightOne = new double[5];
136         System.arraycopy(testArray, 5, arrayOne, 0, 5);
137         System.arraycopy(testWeightsArray, 5, weightOne, 0, 5);
138         assertEquals(stat.evaluate(arrayOne, weightOne),
139                             stat.evaluate(testArray, testWeightsArray, 5, 5), 0);
140         final double[] arrayEnd = new double[5];
141         final double[] weightEnd = new double[5];
142         System.arraycopy(testArray, testArray.length - 5, arrayEnd, 0, 5);
143         System.arraycopy(testWeightsArray, testArray.length - 5, weightEnd, 0, 5);
144         assertEquals(stat.evaluate(arrayEnd, weightEnd),
145                             stat.evaluate(testArray, testWeightsArray, testArray.length - 5, 5), 0);
146     }
147 
148     @Test
149     public void testCopy() {
150         UnivariateStatistic original = getUnivariateStatistic();
151         UnivariateStatistic copy = original.copy();
152         assertEquals(expectedValue(), copy.evaluate(testArray), getTolerance());
153     }
154 
155     @Test
156     public void testCopyData() {
157         UnivariateStatistic stat = getUnivariateStatistic();
158 
159         if (stat instanceof AbstractUnivariateStatistic) {
160             AbstractUnivariateStatistic original = (AbstractUnivariateStatistic) stat;
161             original.setData(testArray);
162             assertEquals(expectedValue(), original.evaluate(), getTolerance());
163 
164             AbstractUnivariateStatistic copy = (AbstractUnivariateStatistic) original.copy();
165             assertEquals(original.evaluate(), copy.evaluate(), getTolerance());
166 
167             assertArrayEquals(original.getData(), copy.getData(), 1e-10);
168             assertNotSame(original.getDataRef(), copy.getDataRef());
169         }
170     }
171 
172     /**
173      * Tests consistency of weighted statistic computation.
174      * For statistics that support weighted evaluation, this test case compares
175      * the result of direct computation on an array with repeated values with
176      * a weighted computation on the corresponding (shorter) array with each
177      * value appearing only once but with a weight value equal to its multiplicity
178      * in the repeating array.
179      */
180     @Test
181     public void testWeightedConsistency() {
182 
183         // See if this statistic computes weighted statistics
184         // If not, skip this test
185         UnivariateStatistic statistic = getUnivariateStatistic();
186         if (!(statistic instanceof WeightedEvaluation)) {
187             return;
188         }
189 
190         // Create arrays of values and corresponding integral weights
191         // and longer array with values repeated according to the weights
192         final int len = 10;        // length of values array
193         final double mu = 0;       // mean of test data
194         final double sigma = 5;    // std dev of test data
195         double[] values = new double[len];
196         double[] weights = new double[len];
197 
198         // Fill weights array with random int values between 1 and 5
199         int[] intWeights = new int[len];
200         final RandomDataGenerator randomDataGenerator = new RandomDataGenerator(100);
201         for (int i = 0; i < len; i++) {
202             intWeights[i] = randomDataGenerator.nextInt(1, 5);
203             weights[i] = intWeights[i];
204         }
205 
206         // Fill values array with random data from N(mu, sigma)
207         // and fill valuesList with values from values array with
208         // values[i] repeated weights[i] times, each i
209         List<Double> valuesList = new ArrayList<Double>();
210         for (int i = 0; i < len; i++) {
211             double value = randomDataGenerator.nextNormal(mu, sigma);
212             values[i] = value;
213             for (int j = 0; j < intWeights[i]; j++) {
214                 valuesList.add(Double.valueOf(value));
215             }
216         }
217 
218         // Dump valuesList into repeatedValues array
219         int sumWeights = valuesList.size();
220         double[] repeatedValues = new double[sumWeights];
221         for (int i = 0; i < sumWeights; i++) {
222             repeatedValues[i] = valuesList.get(i);
223         }
224 
225         // Compare result of weighted statistic computation with direct computation
226         // on array of repeated values
227         WeightedEvaluation weightedStatistic = (WeightedEvaluation) statistic;
228         UnitTestUtils.customAssertRelativelyEquals(statistic.evaluate(repeatedValues),
229                                                    weightedStatistic.evaluate(values, weights, 0, values.length),
230                                                    10E-12);
231 
232         // Check consistency of weighted evaluation methods
233         assertEquals(weightedStatistic.evaluate(values, weights, 0, values.length),
234                             weightedStatistic.evaluate(values, weights), Double.MIN_VALUE);
235 
236     }
237 
238 }