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;
23
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27
28 import org.hipparchus.stat.descriptive.DescriptiveStatistics;
29 import org.hipparchus.stat.descriptive.StreamingStatistics;
30 import org.junit.Assert;
31 import org.junit.Test;
32
33
34
35
36 public class CertifiedDataTest {
37
38 protected double mean = Double.NaN;
39
40 protected double std = Double.NaN;
41
42
43
44
45
46 @Test
47 public void testSummaryStatistics() throws Exception {
48 StreamingStatistics u = new StreamingStatistics();
49 loadStats("data/PiDigits.txt", u);
50 Assert.assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-13);
51 Assert.assertEquals("PiDigits: mean", mean, u.getMean(), 1E-13);
52
53 loadStats("data/Mavro.txt", u);
54 Assert.assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14);
55 Assert.assertEquals("Mavro: mean", mean, u.getMean(), 1E-14);
56
57 loadStats("data/Michelso.txt", u);
58 Assert.assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-13);
59 Assert.assertEquals("Michelso: mean", mean, u.getMean(), 1E-13);
60
61 loadStats("data/NumAcc1.txt", u);
62 Assert.assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14);
63 Assert.assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14);
64
65 loadStats("data/NumAcc2.txt", u);
66 Assert.assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14);
67 Assert.assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14);
68 }
69
70
71
72
73
74 @Test
75 public void testDescriptiveStatistics() throws Exception {
76
77 DescriptiveStatistics u = new DescriptiveStatistics();
78
79 loadStats("data/PiDigits.txt", u);
80 Assert.assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-14);
81 Assert.assertEquals("PiDigits: mean", mean, u.getMean(), 1E-14);
82
83 loadStats("data/Mavro.txt", u);
84 Assert.assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14);
85 Assert.assertEquals("Mavro: mean", mean, u.getMean(), 1E-14);
86
87 loadStats("data/Michelso.txt", u);
88 Assert.assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-14);
89 Assert.assertEquals("Michelso: mean", mean, u.getMean(), 1E-14);
90
91 loadStats("data/NumAcc1.txt", u);
92 Assert.assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14);
93 Assert.assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14);
94
95 loadStats("data/NumAcc2.txt", u);
96 Assert.assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14);
97 Assert.assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14);
98 }
99
100
101
102
103 private void loadStats(String resource, Object u) throws Exception {
104
105 DescriptiveStatistics d = null;
106 StreamingStatistics s = null;
107 if (u instanceof DescriptiveStatistics) {
108 d = (DescriptiveStatistics) u;
109 d.clear();
110 } else {
111 s = (StreamingStatistics) u;
112 s.clear();
113 }
114
115 mean = Double.NaN;
116 std = Double.NaN;
117
118 InputStream resourceAsStream = CertifiedDataTest.class.getResourceAsStream(resource);
119 Assert.assertNotNull("Could not find resource "+resource,resourceAsStream);
120 BufferedReader in =
121 new BufferedReader(
122 new InputStreamReader(
123 resourceAsStream));
124
125 String line = null;
126
127 for (int j = 0; j < 60; j++) {
128 line = in.readLine();
129 if (j == 40) {
130 mean =
131 Double.parseDouble(
132 line.substring(line.lastIndexOf(":") + 1).trim());
133 }
134 if (j == 41) {
135 std =
136 Double.parseDouble(
137 line.substring(line.lastIndexOf(":") + 1).trim());
138 }
139 }
140
141 line = in.readLine();
142
143 while (line != null) {
144 if (d != null) {
145 d.addValue(Double.parseDouble(line.trim()));
146 } else {
147 s.addValue(Double.parseDouble(line.trim()));
148 }
149 line = in.readLine();
150 }
151
152 resourceAsStream.close();
153 in.close();
154 }
155 }