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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.stat;
19
import junit.framework.Test;
20
import junit.framework.TestCase;
21
import junit.framework.TestSuite;
23
import java.io.BufferedReader;
24
import java.io.InputStreamReader;
26
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
27
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
30
* Certified data test cases.
31
* @version $Revision: 611490 $ $Date: 2008-01-12 14:36:35 -0700 (Sat, 12 Jan 2008) $
33
public class CertifiedDataTest extends TestCase {
35
protected double mean = Double.NaN;
37
protected double std = Double.NaN;
40
* Certified Data Test Constructor
43
public CertifiedDataTest(String name) {
48
* @see junit.framework.TestCase#setUp()
54
* @return The test suite
56
public static Test suite() {
57
TestSuite suite = new TestSuite(CertifiedDataTest.class);
58
suite.setName("Certified Tests");
63
* Test SummaryStatistics - implementations that do not store the data
64
* and use single pass algorithms to compute statistics
66
public void testSummaryStatistics() throws Exception {
67
SummaryStatistics u = new SummaryStatistics();
68
loadStats("data/PiDigits.txt", u);
69
assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-13);
70
assertEquals("PiDigits: mean", mean, u.getMean(), 1E-13);
72
loadStats("data/Mavro.txt", u);
73
assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14);
74
assertEquals("Mavro: mean", mean, u.getMean(), 1E-14);
76
loadStats("data/Michelso.txt", u);
77
assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-13);
78
assertEquals("Michelso: mean", mean, u.getMean(), 1E-13);
80
loadStats("data/NumAcc1.txt", u);
81
assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14);
82
assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14);
84
loadStats("data/NumAcc2.txt", u);
85
assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14);
86
assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14);
90
* Test DescriptiveStatistics - implementations that store full array of
91
* values and execute multi-pass algorithms
93
public void testDescriptiveStatistics() throws Exception {
95
DescriptiveStatistics u = new DescriptiveStatistics();
97
loadStats("data/PiDigits.txt", u);
98
assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-14);
99
assertEquals("PiDigits: mean", mean, u.getMean(), 1E-14);
101
loadStats("data/Mavro.txt", u);
102
assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14);
103
assertEquals("Mavro: mean", mean, u.getMean(), 1E-14);
105
loadStats("data/Michelso.txt", u);
106
assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-14);
107
assertEquals("Michelso: mean", mean, u.getMean(), 1E-14);
109
loadStats("data/NumAcc1.txt", u);
110
assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14);
111
assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14);
113
loadStats("data/NumAcc2.txt", u);
114
assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14);
115
assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14);
119
* loads a DescriptiveStatistics off of a test file
121
* @param statistical summary
123
private void loadStats(String resource, Object u) throws Exception {
125
DescriptiveStatistics d = null;
126
SummaryStatistics s = null;
127
if (u instanceof DescriptiveStatistics) {
128
d = (DescriptiveStatistics) u;
130
s = (SummaryStatistics) u;
132
u.getClass().getDeclaredMethod(
133
"clear", new Class[]{}).invoke(u, new Object[]{});
139
new InputStreamReader(
140
CertifiedDataTest.class.getResourceAsStream(resource)));
144
for (int j = 0; j < 60; j++) {
145
line = in.readLine();
149
line.substring(line.lastIndexOf(":") + 1).trim());
154
line.substring(line.lastIndexOf(":") + 1).trim());
158
line = in.readLine();
160
while (line != null) {
162
d.addValue(Double.parseDouble(line.trim()));
164
s.addValue(Double.parseDouble(line.trim()));
166
line = in.readLine();