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