001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.math.stat; 018 019 import junit.framework.Test; 020 import junit.framework.TestCase; 021 import junit.framework.TestSuite; 022 023 import java.io.BufferedReader; 024 import java.io.InputStreamReader; 025 026 import org.apache.commons.math.stat.descriptive.SummaryStatistics; 027 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; 028 029 /** 030 * Certified data test cases. 031 * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $ 032 */ 033 public class CertifiedDataTest extends TestCase { 034 035 protected double mean = Double.NaN; 036 037 protected double std = Double.NaN; 038 039 /** 040 * Certified Data Test Constructor 041 * @param name 042 */ 043 public CertifiedDataTest(String name) { 044 super(name); 045 } 046 047 /** 048 * @return The test suite 049 */ 050 public static Test suite() { 051 TestSuite suite = new TestSuite(CertifiedDataTest.class); 052 suite.setName("Certified Tests"); 053 return suite; 054 } 055 056 /** 057 * Test SummaryStatistics - implementations that do not store the data 058 * and use single pass algorithms to compute statistics 059 */ 060 public void testSummaryStatistics() throws Exception { 061 SummaryStatistics u = new SummaryStatistics(); 062 loadStats("data/PiDigits.txt", u); 063 assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-13); 064 assertEquals("PiDigits: mean", mean, u.getMean(), 1E-13); 065 066 loadStats("data/Mavro.txt", u); 067 assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14); 068 assertEquals("Mavro: mean", mean, u.getMean(), 1E-14); 069 070 loadStats("data/Michelso.txt", u); 071 assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-13); 072 assertEquals("Michelso: mean", mean, u.getMean(), 1E-13); 073 074 loadStats("data/NumAcc1.txt", u); 075 assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14); 076 assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14); 077 078 loadStats("data/NumAcc2.txt", u); 079 assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14); 080 assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14); 081 } 082 083 /** 084 * Test DescriptiveStatistics - implementations that store full array of 085 * values and execute multi-pass algorithms 086 */ 087 public void testDescriptiveStatistics() throws Exception { 088 089 DescriptiveStatistics u = new DescriptiveStatistics(); 090 091 loadStats("data/PiDigits.txt", u); 092 assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-14); 093 assertEquals("PiDigits: mean", mean, u.getMean(), 1E-14); 094 095 loadStats("data/Mavro.txt", u); 096 assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14); 097 assertEquals("Mavro: mean", mean, u.getMean(), 1E-14); 098 099 loadStats("data/Michelso.txt", u); 100 assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-14); 101 assertEquals("Michelso: mean", mean, u.getMean(), 1E-14); 102 103 loadStats("data/NumAcc1.txt", u); 104 assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14); 105 assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14); 106 107 loadStats("data/NumAcc2.txt", u); 108 assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14); 109 assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14); 110 } 111 112 /** 113 * loads a DescriptiveStatistics off of a test file 114 * @param file 115 * @param statistical summary 116 */ 117 private void loadStats(String resource, Object u) throws Exception { 118 119 DescriptiveStatistics d = null; 120 SummaryStatistics s = null; 121 if (u instanceof DescriptiveStatistics) { 122 d = (DescriptiveStatistics) u; 123 } else { 124 s = (SummaryStatistics) u; 125 } 126 u.getClass().getDeclaredMethod( 127 "clear", new Class[]{}).invoke(u, new Object[]{}); 128 mean = Double.NaN; 129 std = Double.NaN; 130 131 BufferedReader in = 132 new BufferedReader( 133 new InputStreamReader( 134 CertifiedDataTest.class.getResourceAsStream(resource))); 135 136 String line = null; 137 138 for (int j = 0; j < 60; j++) { 139 line = in.readLine(); 140 if (j == 40) { 141 mean = 142 Double.parseDouble( 143 line.substring(line.lastIndexOf(":") + 1).trim()); 144 } 145 if (j == 41) { 146 std = 147 Double.parseDouble( 148 line.substring(line.lastIndexOf(":") + 1).trim()); 149 } 150 } 151 152 line = in.readLine(); 153 154 while (line != null) { 155 if (d != null) { 156 d.addValue(Double.parseDouble(line.trim())); 157 } else { 158 s.addValue(Double.parseDouble(line.trim())); 159 } 160 line = in.readLine(); 161 } 162 163 in.close(); 164 } 165 }