001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: import java.io.BufferedReader;
023: import java.io.InputStreamReader;
024:
025: import org.apache.commons.math.stat.descriptive.SummaryStatistics;
026: import org.apache.commons.math.stat.descriptive.SummaryStatisticsImpl;
027: import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
028:
029: /**
030: * Certified data test cases.
031: * @version $Revision: 208874 $ $Date: 2005-07-02 15:24:59 -0700 (Sat, 02 Jul 2005) $
032: */
033: public class CertifiedDataTest extends TestCase {
034:
035: protected double mean = Double.NaN;
036:
037: protected double std = Double.NaN;
038:
039: /**
040: * Certified Data Test Constructor
041: * @param name
042: */
043: public CertifiedDataTest(String name) {
044: super (name);
045: }
046:
047: /* (non-Javadoc)
048: * @see junit.framework.TestCase#setUp()
049: */
050: public void setUp() {
051: }
052:
053: /**
054: * @return The test suite
055: */
056: public static Test suite() {
057: TestSuite suite = new TestSuite(CertifiedDataTest.class);
058: suite.setName("Certified Tests");
059: return suite;
060: }
061:
062: /**
063: * Test StorelessDescriptiveStatistics
064: */
065: public void testUnivariateImpl() throws Exception {
066: SummaryStatistics u = SummaryStatistics
067: .newInstance(SummaryStatisticsImpl.class);
068: loadStats("data/PiDigits.txt", u);
069: assertEquals("PiDigits: std", std, u.getStandardDeviation(),
070: .0000000000001);
071: assertEquals("PiDigits: mean", mean, u.getMean(),
072: .0000000000001);
073:
074: loadStats("data/Mavro.txt", u);
075: assertEquals("Mavro: std", std, u.getStandardDeviation(),
076: .00000000000001);
077: assertEquals("Mavro: mean", mean, u.getMean(), .00000000000001);
078:
079: //loadStats("data/Michelso.txt");
080: //assertEquals("Michelso: std", std, u.getStandardDeviation(), .00000000000001);
081: //assertEquals("Michelso: mean", mean, u.getMean(), .00000000000001);
082:
083: loadStats("data/NumAcc1.txt", u);
084: assertEquals("NumAcc1: std", std, u.getStandardDeviation(),
085: .00000000000001);
086: assertEquals("NumAcc1: mean", mean, u.getMean(),
087: .00000000000001);
088:
089: //loadStats("data/NumAcc2.txt");
090: //assertEquals("NumAcc2: std", std, u.getStandardDeviation(), .000000001);
091: //assertEquals("NumAcc2: mean", mean, u.getMean(), .00000000000001);
092: }
093:
094: /**
095: * Test StorelessDescriptiveStatistics
096: */
097: public void testStoredUnivariateImpl() throws Exception {
098:
099: DescriptiveStatistics u = DescriptiveStatistics.newInstance();
100:
101: loadStats("data/PiDigits.txt", u);
102: assertEquals("PiDigits: std", std, u.getStandardDeviation(),
103: .0000000000001);
104: assertEquals("PiDigits: mean", mean, u.getMean(),
105: .0000000000001);
106:
107: loadStats("data/Mavro.txt", u);
108: assertEquals("Mavro: std", std, u.getStandardDeviation(),
109: .00000000000001);
110: assertEquals("Mavro: mean", mean, u.getMean(), .00000000000001);
111:
112: //loadStats("data/Michelso.txt");
113: //assertEquals("Michelso: std", std, u.getStandardDeviation(), .00000000000001);
114: //assertEquals("Michelso: mean", mean, u.getMean(), .00000000000001);
115:
116: loadStats("data/NumAcc1.txt", u);
117: assertEquals("NumAcc1: std", std, u.getStandardDeviation(),
118: .00000000000001);
119: assertEquals("NumAcc1: mean", mean, u.getMean(),
120: .00000000000001);
121:
122: //loadStats("data/NumAcc2.txt");
123: //assertEquals("NumAcc2: std", std, u.getStandardDeviation(), .000000001);
124: //assertEquals("NumAcc2: mean", mean, u.getMean(), .00000000000001);
125: }
126:
127: /**
128: * loads a DescriptiveStatistics off of a test file
129: * @param file
130: * @param statistical summary
131: */
132: private void loadStats(String resource, Object u) throws Exception {
133:
134: DescriptiveStatistics d = null;
135: SummaryStatistics s = null;
136: if (u instanceof DescriptiveStatistics) {
137: d = (DescriptiveStatistics) u;
138: } else {
139: s = (SummaryStatistics) u;
140: }
141: u.getClass().getDeclaredMethod("clear", new Class[] {}).invoke(
142: u, new Object[] {});
143: mean = Double.NaN;
144: std = Double.NaN;
145:
146: BufferedReader in = new BufferedReader(new InputStreamReader(
147: getClass().getResourceAsStream(resource)));
148:
149: String line = null;
150:
151: for (int j = 0; j < 60; j++) {
152: line = in.readLine();
153: if (j == 40) {
154: mean = Double.parseDouble(line.substring(
155: line.lastIndexOf(":") + 1).trim());
156: }
157: if (j == 41) {
158: std = Double.parseDouble(line.substring(
159: line.lastIndexOf(":") + 1).trim());
160: }
161: }
162:
163: line = in.readLine();
164:
165: while (line != null) {
166: if (d != null) {
167: d.addValue(Double.parseDouble(line.trim()));
168: } else {
169: s.addValue(Double.parseDouble(line.trim()));
170: }
171: line = in.readLine();
172: }
173:
174: in.close();
175: }
176: }
|