2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with this
4
* work for additional information regarding copyright ownership. The ASF
5
* licenses this file to You under the Apache License, Version 2.0 (the
6
* "License"); you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
9
* or agreed to in writing, software distributed under the License is
10
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11
* KIND, either express or implied. See the License for the specific language
12
* governing permissions and limitations under the License.
14
package org.apache.commons.math.stat.descriptive;
16
import java.util.Locale;
18
import junit.framework.Test;
19
import junit.framework.TestCase;
20
import junit.framework.TestSuite;
22
import org.apache.commons.math.stat.descriptive.rank.Percentile;
23
import org.apache.commons.math.util.MathUtils;
26
* Test cases for the DescriptiveStatistics class.
28
* @version $Revision: 797742 $ $Date: 2007-08-16 15:36:33 -0500 (Thu, 16 Aug
31
public class DescriptiveStatisticsTest extends TestCase {
33
public DescriptiveStatisticsTest(String name) {
37
public static Test suite() {
38
TestSuite suite = new TestSuite(DescriptiveStatisticsTest.class);
39
suite.setName("DescriptiveStatistics Tests");
43
protected DescriptiveStatistics createDescriptiveStatistics() {
44
return new DescriptiveStatistics();
47
public void testSetterInjection() {
48
DescriptiveStatistics stats = createDescriptiveStatistics();
51
assertEquals(2, stats.getMean(), 1E-10);
52
// Now lets try some new math
53
stats.setMeanImpl(new deepMean());
54
assertEquals(42, stats.getMean(), 1E-10);
57
public void testCopy() {
58
DescriptiveStatistics stats = createDescriptiveStatistics();
61
DescriptiveStatistics copy = new DescriptiveStatistics(stats);
62
assertEquals(2, copy.getMean(), 1E-10);
63
// Now lets try some new math
64
stats.setMeanImpl(new deepMean());
66
assertEquals(42, copy.getMean(), 1E-10);
69
public void testWindowSize() {
70
DescriptiveStatistics stats = createDescriptiveStatistics();
71
stats.setWindowSize(300);
72
for (int i = 0; i < 100; ++i) {
73
stats.addValue(i + 1);
75
int refSum = (100 * 101) / 2;
76
assertEquals(refSum / 100.0, stats.getMean(), 1E-10);
77
assertEquals(300, stats.getWindowSize());
79
stats.setWindowSize(-3);
80
fail("an exception should have been thrown");
81
} catch (IllegalArgumentException iae) {
83
} catch (Exception e) {
84
fail("wrong exception caught: " + e.getMessage());
86
assertEquals(300, stats.getWindowSize());
87
stats.setWindowSize(50);
88
assertEquals(50, stats.getWindowSize());
89
int refSum2 = refSum - (50 * 51) / 2;
90
assertEquals(refSum2 / 50.0, stats.getMean(), 1E-10);
93
public void testGetValues() {
94
DescriptiveStatistics stats = createDescriptiveStatistics();
95
for (int i = 100; i > 0; --i) {
98
int refSum = (100 * 101) / 2;
99
assertEquals(refSum / 100.0, stats.getMean(), 1E-10);
100
double[] v = stats.getValues();
101
for (int i = 0; i < v.length; ++i) {
102
assertEquals(100.0 - i, v[i], 1.0e-10);
104
double[] s = stats.getSortedValues();
105
for (int i = 0; i < s.length; ++i) {
106
assertEquals(i + 1.0, s[i], 1.0e-10);
108
assertEquals(12.0, stats.getElement(88), 1.0e-10);
111
public void testToString() {
112
DescriptiveStatistics stats = createDescriptiveStatistics();
116
Locale d = Locale.getDefault();
117
Locale.setDefault(Locale.US);
118
assertEquals("DescriptiveStatistics:\n" +
126
"kurtosis: NaN\n", stats.toString());
127
Locale.setDefault(d);
130
public void testShuffledStatistics() {
131
// the purpose of this test is only to check the get/set methods
132
// we are aware shuffling statistics like this is really not
133
// something sensible to do in production ...
134
DescriptiveStatistics reference = createDescriptiveStatistics();
135
DescriptiveStatistics shuffled = createDescriptiveStatistics();
137
UnivariateStatistic tmp = shuffled.getGeometricMeanImpl();
138
shuffled.setGeometricMeanImpl(shuffled.getMeanImpl());
139
shuffled.setMeanImpl(shuffled.getKurtosisImpl());
140
shuffled.setKurtosisImpl(shuffled.getSkewnessImpl());
141
shuffled.setSkewnessImpl(shuffled.getVarianceImpl());
142
shuffled.setVarianceImpl(shuffled.getMaxImpl());
143
shuffled.setMaxImpl(shuffled.getMinImpl());
144
shuffled.setMinImpl(shuffled.getSumImpl());
145
shuffled.setSumImpl(shuffled.getSumsqImpl());
146
shuffled.setSumsqImpl(tmp);
148
for (int i = 100; i > 0; --i) {
149
reference.addValue(i);
150
shuffled.addValue(i);
153
assertEquals(reference.getMean(), shuffled.getGeometricMean(), 1.0e-10);
154
assertEquals(reference.getKurtosis(), shuffled.getMean(), 1.0e-10);
155
assertEquals(reference.getSkewness(), shuffled.getKurtosis(), 1.0e-10);
156
assertEquals(reference.getVariance(), shuffled.getSkewness(), 1.0e-10);
157
assertEquals(reference.getMax(), shuffled.getVariance(), 1.0e-10);
158
assertEquals(reference.getMin(), shuffled.getMax(), 1.0e-10);
159
assertEquals(reference.getSum(), shuffled.getMin(), 1.0e-10);
160
assertEquals(reference.getSumsq(), shuffled.getSum(), 1.0e-10);
161
assertEquals(reference.getGeometricMean(), shuffled.getSumsq(), 1.0e-10);
165
public void testPercentileSetter() throws Exception {
166
DescriptiveStatistics stats = createDescriptiveStatistics();
170
assertEquals(2, stats.getPercentile(50.0), 1E-10);
172
// Inject wrapped Percentile impl
173
stats.setPercentileImpl(new goodPercentile());
174
assertEquals(2, stats.getPercentile(50.0), 1E-10);
176
// Try "new math" impl
177
stats.setPercentileImpl(new subPercentile());
178
assertEquals(10.0, stats.getPercentile(10.0), 1E-10);
180
// Try to set bad impl
182
stats.setPercentileImpl(new badPercentile());
183
fail("Expecting IllegalArgumentException");
184
} catch (IllegalArgumentException ex) {
189
public void test20090720() {
190
DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics(100);
191
for (int i = 0; i < 161; i++) {
192
descriptiveStatistics.addValue(1.2);
194
descriptiveStatistics.clear();
195
descriptiveStatistics.addValue(1.2);
196
assertEquals(1, descriptiveStatistics.getN());
199
public void testRemoval() {
201
final DescriptiveStatistics dstat = createDescriptiveStatistics();
203
checkremoval(dstat, 1, 6.0, 0.0, Double.NaN);
204
checkremoval(dstat, 3, 5.0, 3.0, 4.5);
205
checkremoval(dstat, 6, 3.5, 2.5, 3.0);
206
checkremoval(dstat, 9, 3.5, 2.5, 3.0);
207
checkremoval(dstat, DescriptiveStatistics.INFINITE_WINDOW, 3.5, 2.5, 3.0);
211
public void checkremoval(DescriptiveStatistics dstat, int wsize,
212
double mean1, double mean2, double mean3) {
214
dstat.setWindowSize(wsize);
217
for (int i = 1 ; i <= 6 ; ++i) {
221
assertTrue(MathUtils.equals(mean1, dstat.getMean()));
222
dstat.replaceMostRecentValue(0);
223
assertTrue(MathUtils.equals(mean2, dstat.getMean()));
224
dstat.removeMostRecentValue();
225
assertTrue(MathUtils.equals(mean3, dstat.getMean()));
229
// Test UnivariateStatistics impls for setter injection tests
232
* A new way to compute the mean
234
static class deepMean implements UnivariateStatistic {
236
public double evaluate(double[] values, int begin, int length) {
240
public double evaluate(double[] values) {
243
public UnivariateStatistic copy() {
244
return new deepMean();
249
* Test percentile implementation - wraps a Percentile
251
static class goodPercentile implements UnivariateStatistic {
252
private Percentile percentile = new Percentile();
253
public void setQuantile(double quantile) {
254
percentile.setQuantile(quantile);
256
public double evaluate(double[] values, int begin, int length) {
257
return percentile.evaluate(values, begin, length);
259
public double evaluate(double[] values) {
260
return percentile.evaluate(values);
262
public UnivariateStatistic copy() {
263
goodPercentile result = new goodPercentile();
264
result.setQuantile(percentile.getQuantile());
270
* Test percentile subclass - another "new math" impl
271
* Always returns currently set quantile
273
static class subPercentile extends Percentile {
275
public double evaluate(double[] values, int begin, int length) {
276
return getQuantile();
279
public double evaluate(double[] values) {
280
return getQuantile();
282
private static final long serialVersionUID = 8040701391045914979L;
284
public Percentile copy() {
285
subPercentile result = new subPercentile();
291
* "Bad" test percentile implementation - no setQuantile
293
static class badPercentile implements UnivariateStatistic {
294
private Percentile percentile = new Percentile();
295
public double evaluate(double[] values, int begin, int length) {
296
return percentile.evaluate(values, begin, length);
298
public double evaluate(double[] values) {
299
return percentile.evaluate(values);
301
public UnivariateStatistic copy() {
302
return new badPercentile();