1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
119
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
171
172
173
174
175
176
177 @Test
178 public void testWeightedConsistency() {
179
180
181
182 UnivariateStatistic statistic = getUnivariateStatistic();
183 if (!(statistic instanceof WeightedEvaluation)) {
184 return;
185 }
186
187
188
189 final int len = 10;
190 final double mu = 0;
191 final double sigma = 5;
192 double[] values = new double[len];
193 double[] weights = new double[len];
194
195
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
204
205
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
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
223
224 WeightedEvaluation weightedStatistic = (WeightedEvaluation) statistic;
225 UnitTestUtils.assertRelativelyEquals(statistic.evaluate(repeatedValues),
226 weightedStatistic.evaluate(values, weights, 0, values.length),
227 10E-12);
228
229
230 Assert.assertEquals(weightedStatistic.evaluate(values, weights, 0, values.length),
231 weightedStatistic.evaluate(values, weights), Double.MIN_VALUE);
232
233 }
234
235 }