View Javadoc
1   /*
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
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
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.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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   * Certified data test cases.
35   */
36  public class CertifiedDataTest {
37  
38      protected double mean = Double.NaN;
39  
40      protected double std = Double.NaN;
41  
42      /**
43       * Test SummaryStatistics - implementations that do not store the data
44       * and use single pass algorithms to compute statistics
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       * Test DescriptiveStatistics - implementations that store full array of
72       * values and execute multi-pass algorithms
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      * loads a DescriptiveStatistics off of a test file
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 }