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