KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > stat > CertifiedDataTest


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.math.stat;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24
25 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
26 import org.apache.commons.math.stat.descriptive.SummaryStatisticsImpl;
27 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
28
29 /**
30  * Certified data test cases.
31  * @version $Revision$ $Date: 2005-07-02 15:24:59 -0700 (Sat, 02 Jul 2005) $
32  */

33 public class CertifiedDataTest extends TestCase {
34
35     protected double mean = Double.NaN;
36
37     protected double std = Double.NaN;
38
39     /**
40      * Certified Data Test Constructor
41      * @param name
42      */

43     public CertifiedDataTest(String JavaDoc name) {
44         super(name);
45     }
46
47     /* (non-Javadoc)
48      * @see junit.framework.TestCase#setUp()
49      */

50     public void setUp() {
51     }
52
53     /**
54      * @return The test suite
55      */

56     public static Test suite() {
57         TestSuite suite = new TestSuite(CertifiedDataTest.class);
58         suite.setName("Certified Tests");
59         return suite;
60     }
61
62     /**
63      * Test StorelessDescriptiveStatistics
64     */

65     public void testUnivariateImpl() throws Exception JavaDoc {
66         SummaryStatistics u = SummaryStatistics.newInstance(SummaryStatisticsImpl.class);
67         loadStats("data/PiDigits.txt", u);
68         assertEquals("PiDigits: std", std, u.getStandardDeviation(), .0000000000001);
69         assertEquals("PiDigits: mean", mean, u.getMean(), .0000000000001);
70
71         loadStats("data/Mavro.txt", u);
72         assertEquals("Mavro: std", std, u.getStandardDeviation(), .00000000000001);
73         assertEquals("Mavro: mean", mean, u.getMean(), .00000000000001);
74         
75         //loadStats("data/Michelso.txt");
76
//assertEquals("Michelso: std", std, u.getStandardDeviation(), .00000000000001);
77
//assertEquals("Michelso: mean", mean, u.getMean(), .00000000000001);
78

79         loadStats("data/NumAcc1.txt", u);
80         assertEquals("NumAcc1: std", std, u.getStandardDeviation(), .00000000000001);
81         assertEquals("NumAcc1: mean", mean, u.getMean(), .00000000000001);
82         
83         //loadStats("data/NumAcc2.txt");
84
//assertEquals("NumAcc2: std", std, u.getStandardDeviation(), .000000001);
85
//assertEquals("NumAcc2: mean", mean, u.getMean(), .00000000000001);
86
}
87
88     /**
89      * Test StorelessDescriptiveStatistics
90      */

91     public void testStoredUnivariateImpl() throws Exception JavaDoc {
92
93         DescriptiveStatistics u = DescriptiveStatistics.newInstance();
94         
95         loadStats("data/PiDigits.txt", u);
96         assertEquals("PiDigits: std", std, u.getStandardDeviation(), .0000000000001);
97         assertEquals("PiDigits: mean", mean, u.getMean(), .0000000000001);
98         
99         loadStats("data/Mavro.txt", u);
100         assertEquals("Mavro: std", std, u.getStandardDeviation(), .00000000000001);
101         assertEquals("Mavro: mean", mean, u.getMean(), .00000000000001);
102         
103         //loadStats("data/Michelso.txt");
104
//assertEquals("Michelso: std", std, u.getStandardDeviation(), .00000000000001);
105
//assertEquals("Michelso: mean", mean, u.getMean(), .00000000000001);
106

107         loadStats("data/NumAcc1.txt", u);
108         assertEquals("NumAcc1: std", std, u.getStandardDeviation(), .00000000000001);
109         assertEquals("NumAcc1: mean", mean, u.getMean(), .00000000000001);
110         
111         //loadStats("data/NumAcc2.txt");
112
//assertEquals("NumAcc2: std", std, u.getStandardDeviation(), .000000001);
113
//assertEquals("NumAcc2: mean", mean, u.getMean(), .00000000000001);
114
}
115
116     /**
117      * loads a DescriptiveStatistics off of a test file
118      * @param file
119      * @param statistical summary
120      */

121     private void loadStats(String JavaDoc resource, Object JavaDoc u) throws Exception JavaDoc {
122         
123         DescriptiveStatistics d = null;
124         SummaryStatistics s = null;
125         if (u instanceof DescriptiveStatistics) {
126             d = (DescriptiveStatistics) u;
127         } else {
128             s = (SummaryStatistics) u;
129         }
130         u.getClass().getDeclaredMethod(
131                 "clear", new Class JavaDoc[]{}).invoke(u, new Object JavaDoc[]{});
132         mean = Double.NaN;
133         std = Double.NaN;
134         
135         BufferedReader JavaDoc in =
136             new BufferedReader JavaDoc(
137                     new InputStreamReader JavaDoc(
138                             getClass().getResourceAsStream(resource)));
139         
140         String JavaDoc line = null;
141         
142         for (int j = 0; j < 60; j++) {
143             line = in.readLine();
144             if (j == 40) {
145                 mean =
146                     Double.parseDouble(
147                             line.substring(line.lastIndexOf(":") + 1).trim());
148             }
149             if (j == 41) {
150                 std =
151                     Double.parseDouble(
152                             line.substring(line.lastIndexOf(":") + 1).trim());
153             }
154         }
155         
156         line = in.readLine();
157         
158         while (line != null) {
159             if (d != null) {
160                 d.addValue(Double.parseDouble(line.trim()));
161             } else {
162                 s.addValue(Double.parseDouble(line.trim()));
163             }
164             line = in.readLine();
165         }
166         
167         in.close();
168     }
169 }
170
Popular Tags