brentworden    2004/03/17 21:52:37

  Modified:    math/src/test/org/apache/commons/math/stat
                        CertifiedDataTest.java
               math/src/test/org/apache/commons/math TestUtils.java
               math/src/test/org/apache/commons/math/stat/data Lottery.txt
                        Lew.txt
  Added:       math/src/test/org/apache/commons/math/stat/data
                        CertifiedDataAbstractTest.java LewTest.java
                        LotteryTest.java
  Log:
  new approach to testing against certified data sets.

  

  PR: 27692

  Obtained from: Brent Worden

  Submitted by: Brent Worden

  
  Revision  Changes    Path
  1.16      +2 -17     
jakarta-commons/math/src/test/org/apache/commons/math/stat/CertifiedDataTest.java
  
  Index: CertifiedDataTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/CertifiedDataTest.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- CertifiedDataTest.java    21 Feb 2004 21:35:17 -0000      1.15
  +++ CertifiedDataTest.java    18 Mar 2004 05:52:36 -0000      1.16
  @@ -25,6 +25,7 @@
   import java.io.InputStreamReader;
   import org.apache.commons.logging.LogFactory;
   import org.apache.commons.logging.Log;
  +
   /**
    * Test cases for the [EMAIL PROTECTED] DescriptiveStatistics} class.
    * @version $Revision$ $Date$
  @@ -75,14 +76,6 @@
                        e.printStackTrace();
                }
   
  -             loadStats("data/Lew.txt", u);
  -             assertEquals("Lew: std", std, u.getStandardDeviation(), .000000000001);
  -             assertEquals("Lew: mean", mean, u.getMean(), .000000000001);
  -             
  -             loadStats("data/Lottery.txt", u);
  -             assertEquals("Lottery: std", std, u.getStandardDeviation(), 
.000000000001);
  -             assertEquals("Lottery: mean", mean, u.getMean(), .000000000001);       
 
  -             
                loadStats("data/PiDigits.txt", u);
                assertEquals("PiDigits: std", std, u.getStandardDeviation(), 
.0000000000001);
                assertEquals("PiDigits: mean", mean, u.getMean(), .0000000000001);     
 
  @@ -111,14 +104,6 @@
   
                DescriptiveStatistics u = DescriptiveStatistics.newInstance();
                
  -             loadStats("data/Lew.txt", u);
  -             assertEquals("Lew: std", std, u.getStandardDeviation(), .000000000001);
  -             assertEquals("Lew: mean", mean, u.getMean(), .000000000001);
  -             
  -             loadStats("data/Lottery.txt", u);
  -             assertEquals("Lottery: std", std, u.getStandardDeviation(), 
.000000000001);
  -             assertEquals("Lottery: mean", mean, u.getMean(), .000000000001);       
         
  -                                                                                    
                                           
                loadStats("data/PiDigits.txt", u);
                assertEquals("PiDigits: std", std, u.getStandardDeviation(), 
.0000000000001);
                assertEquals("PiDigits: mean", mean, u.getMean(), .0000000000001);
  
  
  
  1.11      +13 -10    
jakarta-commons/math/src/test/org/apache/commons/math/TestUtils.java
  
  Index: TestUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/TestUtils.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- TestUtils.java    21 Feb 2004 21:35:16 -0000      1.10
  +++ TestUtils.java    18 Mar 2004 05:52:37 -0000      1.11
  @@ -19,14 +19,13 @@
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
  -import java.io.IOException;
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
   
  -import org.apache.commons.math.complex.Complex;
  -
   import junit.framework.Assert;
   
  +import org.apache.commons.math.complex.Complex;
  +
   /**
    * @version $Revision$ $Date$
    */
  @@ -39,14 +38,18 @@
       }
   
       public static void assertEquals(double expected, double actual, double delta) {
  -        // check for NaN
  -        if(Double.isNaN(expected)){
  -            Assert.assertTrue(Double.isNaN(actual));
  -        } else {
  -            Assert.assertEquals(expected, actual, delta);
  -        }
  +     assertEquals(null, expected, actual, delta);
       }
   
  +    public static void assertEquals(String msg, double expected, double actual, 
double delta) {
  +     // check for NaN
  +     if(Double.isNaN(expected)){
  +             Assert.assertTrue(msg, Double.isNaN(actual));
  +     } else {
  +             Assert.assertEquals(msg, expected, actual, delta);
  +     }
  +    }
  +    
       /**
        * 
        */
  
  
  
  1.4       +43 -60    
jakarta-commons/math/src/test/org/apache/commons/math/stat/data/Lottery.txt
  
  Index: Lottery.txt
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/data/Lottery.txt,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Lottery.txt       14 Nov 2003 22:22:20 -0000      1.3
  +++ Lottery.txt       18 Mar 2004 05:52:37 -0000      1.4
  @@ -1,63 +1,46 @@
  -File Name:     Lottery.dat
  -
  -File Format:   ASCII
  -               Header          : lines  1 to  60     (=  60)
  -               Certified Values: lines 41 to  43     (=   3)
  -               Data            : lines 61 to 278     (= 218)
  -
  -Dataset Name:  Lottery
  -
  -Description:   This is an observed/"real world" data set
  -               consisting of 218 lottery values
  -               from September 3, 1989 to April 14, 1990 (32 weeks).
  -               One 3-digit random number (from 000 to 999)
  -               is drawn per day, 7 days per week for most
  -               weeks, but fewer days per week for some weeks.
  -               We here use this data to test accuracy
  -               in summary statistics calculations.
  -
  -Stat Category: Univariate: Summary Statistics
  -
  -Reference:     None
  -
  -Data:          "Real World"
  -               1    Response          : y = 3-digit random number
  -               0    Predictors
  -               218  Observations
  -
  -Model:         Lower Level of Difficulty
  -               2    Parameters        : mu, sigma
  -               1    Response Variable : y
  -               0    Predictor Variables
  -
  -               y    = mu + e
  -
  -
  -
  -
  -
  -
  -                                                  Certified Values
  -Sample Mean                                ybar:  518.958715596330
  -Sample Standard Deviation (denom. = n-1)      s:  291.699727470969
  -Sample Autocorrelation Coefficient (lag 1) r(1):  -0.120948622967393
  -
  -Number of Observations:                             218
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -Data: Y
  -----------
  +#####################################################################
  +# Dataset Name:  Lottery
  +#
  +#Description:   This is an observed/"real world" data set
  +#               consisting of 218 lottery values
  +#               from September 3, 1989 to April 14, 1990 (32 weeks).
  +#               One 3-digit random number (from 000 to 999)
  +#               is drawn per day, 7 days per week for most
  +#               weeks, but fewer days per week for some weeks.
  +#               We here use this data to test accuracy
  +#               in summary statistics calculations.
  +#
  +# Stat Category: Univariate: Summary Statistics
  +#
  +# Reference:     http://www.itl.nist.gov/div898/strd/univ/lottery.html
  +#
  +# Data:          "Real World"
  +#               1    Response          : y = 3-digit random number
  +#               0    Predictors
  +#               218  Observations
  +#
  +# Model:         Lower Level of Difficulty
  +#               2    Parameters        : mu, sigma
  +#               1    Response Variable : y
  +#               0    Predictor Variables#
  +#               y    = mu + e
  +#####################################################################
  +
  +#####################################################################
  +#
  +# Certified Values
  +#
  +#####################################################################
  +mean                       = 518.958715596330
  +standardDeviation          = 291.699727470969
  +autocorrelationCoefficient =  -0.120948622967393
  +n                          = 218
  +
  +#####################################################################
  +#
  +# Data
  +#
  +#####################################################################
        162
        671
        933
  
  
  
  1.4       +52 -60    
jakarta-commons/math/src/test/org/apache/commons/math/stat/data/Lew.txt
  
  Index: Lew.txt
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/data/Lew.txt,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Lew.txt   14 Nov 2003 22:22:20 -0000      1.3
  +++ Lew.txt   18 Mar 2004 05:52:37 -0000      1.4
  @@ -1,63 +1,55 @@
  -File Name:     Lew.dat
  -
  -File Format:   ASCII
  -               Header          : lines  1 to  60     (=  60)
  -               Certified Values: lines 41 to  43     (=   3)
  -               Data            : lines 61 to 260     (= 200)
  -
  -Dataset Name:  Lew (Beam Deflection Data)
  -
  -Description:   This is an observed/"real world" data set
  -               consisting of 200 deflections of a steel-concrete
  -               beam while subjected to periodic pressure.
  -               The experimenter was H. S. Lew of the
  -               Center for Building Technology at NIST.
  -               We here use this data to test accuracy
  -               in summary statistics calculations.
  -
  -Stat Category: Univariate: Summary Statistics
  -
  -Reference:     None
  -
  -Data:          "Real World"
  -               1    Response          : y = beam deflection
  -               0    Predictors
  -               200  Observations
  -
  -Model:         Lower Level of Difficulty
  -               2    Parameters        : mu, sigma
  -               1    Response Variable : y
  -               0    Predictor Variables
  -
  -               y    = mu + e
  -
  -
  -
  -
  -
  -
  -
  -                                                  Certified Values
  -Sample Mean                                ybar:  -177.435000000000
  -Sample Standard Deviation (denom. = n-1)      s:   277.332168044316
  -Sample Autocorrelation Coefficient (lag 1) r(1):  -0.307304800605679
  -
  -Number of Observations:                             200
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -Data: Y
  -----------
  +#####################################################################
  +# Dataset Name:  Lew (Beam Deflection Data)
  +#
  +# Description:   This is an observed/"real world" data set
  +#               consisting of 200 deflections of a steel-concrete
  +#               beam while subjected to periodic pressure.
  +#               The experimenter was H. S. Lew of the
  +#               Center for Building Technology at NIST.
  +#               We here use this data to test accuracy
  +#               in summary statistics calculations.
  +#
  +# Stat Category: Univariate: Summary Statistics
  +#
  +# Reference:     http://www.itl.nist.gov/div898/strd/univ/lew.html
  +#
  +# Data:         "Real World"
  +#               1    Response          : y = beam deflection
  +#               0    Predictors
  +#               200  Observations
  +#
  +# Model:        Lower Level of Difficulty
  +#               2    Parameters        : mu, sigma
  +#               1    Response Variable : y
  +#               0    Predictor Variables
  +#               y    = mu + e
  +#####################################################################
  +
  +#####################################################################
  +#
  +# Certified Values
  +#
  +#####################################################################
  +n                          =  200
  +mean                       = -177.435000000000
  +standardDeviation          =  277.332168044316
  +autocorrelationCoefficient = -0.307304800605679
  +
  +#####################################################################
  +#
  +# R Generated Values
  +#
  +#####################################################################
  +variance =  76913.13143
  +max      =  300
  +min      = -579
  +sum      = -35487
  +
  +#####################################################################
  +#
  +# Data
  +#
  +#####################################################################
       -213
       -564
        -35
  
  
  
  1.1                  
jakarta-commons/math/src/test/org/apache/commons/math/stat/data/CertifiedDataAbstractTest.java
  
  Index: CertifiedDataAbstractTest.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.math.stat.data;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.URL;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  import org.apache.commons.beanutils.PropertyUtils;
  import org.apache.commons.lang.StringUtils;
  import org.apache.commons.math.TestUtils;
  import org.apache.commons.math.stat.DescriptiveStatistics;
  import org.apache.commons.math.stat.SummaryStatistics;
  
  import junit.framework.TestCase;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2004/03/18 05:52:37 $
   */
  public abstract class CertifiedDataAbstractTest extends TestCase {
        
        private DescriptiveStatistics descriptives;
        
        private SummaryStatistics summaries;
        
        private Map certifiedValues;
        
        protected void setUp() throws Exception {
                descriptives = DescriptiveStatistics.newInstance();
                summaries = SummaryStatistics.newInstance();
                certifiedValues = new HashMap();
                
                loadData();
        }
  
        private void loadData() throws IOException {
                BufferedReader in = null;
  
                try {
                        URL resourceURL = 
getClass().getClassLoader().getResource(getResourceName());
                        in = new BufferedReader(new 
InputStreamReader(resourceURL.openStream()));
                        
                        String line = in.readLine();
                        while (line != null) {
                                line = StringUtils.trimToNull(line);
                                if (line == null) {
                                        // empty line
                                } else if (line.startsWith("#")) {
                                        // comment
                                } else {
                                        int n = line.indexOf('=');
                                        if (n == -1) {
                                                // data value
                                                double value = 
Double.parseDouble(line);
                                                descriptives.addValue(value);
                                                summaries.addValue(value);
                                        } else {
                                                // certified value
                                                String name = line.substring(0, 
n).trim();
                                                String valueString = line.substring(n 
+ 1).trim();
                                                Double value = new Double(valueString);
                                                certifiedValues.put(name, value);
                                        }
                                }
                                line = in.readLine();
                        }
                } finally {
                        if (in != null) {
                                in.close();
                        }
                }
        }
  
        /**
         * @return
         */
        protected abstract String getResourceName();
  
        protected double getMaximumAbsoluteError() {
                return 1.0e-5;
        }
        
        protected void tearDown() throws Exception {
                descriptives.clear();
                descriptives = null;
                
                summaries.clear();
                summaries = null;
                
                certifiedValues.clear();
                certifiedValues = null;
        }
        
        public void testCertifiedValues() throws Exception {
                Iterator iter = certifiedValues.keySet().iterator();
                while (iter.hasNext()) {
                        String name = iter.next().toString();
                        Double expectedValue = (Double)certifiedValues.get(name);
                        try {
                                Double summariesValue = 
(Double)PropertyUtils.getProperty(summaries, name);
                                TestUtils.assertEquals("summary value for " + name + " 
is incorrect.",
                                                summariesValue.doubleValue(), 
expectedValue.doubleValue(), getMaximumAbsoluteError());
                        } catch (Exception ex) {
                        }
                        
                        try {
                                Double descriptivesValue = 
(Double)PropertyUtils.getProperty(descriptives, name);
                                TestUtils.assertEquals("descriptive value for " + name 
+ " is incorrect.",
                                                descriptivesValue.doubleValue(), 
expectedValue.doubleValue(), getMaximumAbsoluteError());
                        } catch (Exception ex) {
                        }
                }
        }
  }
  
  
  
  1.1                  
jakarta-commons/math/src/test/org/apache/commons/math/stat/data/LewTest.java
  
  Index: LewTest.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.math.stat.data;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2004/03/18 05:52:37 $
   */
  public class LewTest extends CertifiedDataAbstractTest {
  
        protected String getResourceName() {
                return "org/apache/commons/math/stat/data/Lew.txt";
        }
  }
  
  
  
  1.1                  
jakarta-commons/math/src/test/org/apache/commons/math/stat/data/LotteryTest.java
  
  Index: LotteryTest.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.math.stat.data;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2004/03/18 05:52:37 $
   */
  public class LotteryTest extends CertifiedDataAbstractTest {
  
        protected String getResourceName() {
                return "org/apache/commons/math/stat/data/Lottery.txt";
        }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to