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