commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdigg...@apache.org
Subject cvs commit: jakarta-commons-sandbox/math/src/test/org/apache/commons/math RandomDataTest.java
Date Sat, 21 Jun 2003 23:00:39 GMT
mdiggory    2003/06/21 16:00:39

  Modified:    math/src/test/org/apache/commons/math RandomDataTest.java
  Added:       math/src/test/org/apache/commons/math/stat
                        TestStatisticTest.java
               math/src/java/org/apache/commons/math/stat
                        TestStatisticImpl.java TestStatistic.java
  Log:
  Moving TestStatistic implementation / interface into stat package.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/TestStatisticTest.java
  
  Index: TestStatisticTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.math.stat;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  /**
   * Test cases for the TestStatistic class.
   *
   * @author Phil Steitz
   * @version $Revision: 1.1 $ $Date: 2003/06/21 23:00:39 $
   */
  
  public final class TestStatisticTest extends TestCase {
  
      private TestStatisticImpl testStatistic = new TestStatisticImpl();
      
      public TestStatisticTest(String name) {
          super(name);
      }
      
      
      public void setUp() { 
      }
  
      public static Test suite() {
          TestSuite suite = new TestSuite(TestStatisticTest.class);
          suite.setName("TestStatistic Tests");
          return suite;
      }
  
      public void testChiSquare() {
         double[] observed = {11,24,69,96};
         double[] expected = {8.2,25.2,65.8,100.8};
         assertEquals("chi-square statistic", 
             1.39743495,testStatistic.chiSquare(expected,observed),10E-5);
         
         double[] tooShortObs = {0};
         double[] tooShortEx = {1};
         try {
             testStatistic.chiSquare(tooShortObs,tooShortEx);
             fail("arguments too short, IllegalArgumentException expected");
         } catch (IllegalArgumentException ex) {
             ;
         }
         
         double[] unMatchedObs = {0,1,2,3};
         double[] unMatchedEx = {1,1,2};
         try {
             testStatistic.chiSquare(unMatchedEx,unMatchedObs);
             fail("arrays have different lengths, IllegalArgumentException expected");
         } catch (IllegalArgumentException ex) {
             ;
         }
         
         expected[0] = 0;
         assertEquals("chi-square statistic", Double.POSITIVE_INFINITY,
              testStatistic.chiSquare(expected,observed),Double.MIN_VALUE);
      }
         
      public void testT(){
  		double[] observed = {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0,
              94.0, 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0};
          double mu = 100.0;
          assertEquals("t statistic", -2.82, testStatistic.t(mu, observed),
              10E-3);
          
          double[] nullObserved = null;
          try {
              testStatistic.t(mu, nullObserved);
              fail("arguments too short, IllegalArgumentException expected");
          } catch (IllegalArgumentException ex) {
              ;
          }
          
          double[] emptyObs = {};
          try {
              testStatistic.t(mu, emptyObs);
              fail("arguments too short, IllegalArgumentException expected");
          } catch (IllegalArgumentException ex) {
              ;
          }
          
          double[] tooShortObs = {1.0};
          try {
              testStatistic.t(mu, tooShortObs);
              fail("arguments too short, IllegalArgumentException expected");
          } catch (IllegalArgumentException ex) {
              ;
          }
      }
  }
  
  
  
  
  1.1                  jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/TestStatisticImpl.java
  
  Index: TestStatisticImpl.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.math.stat;
  
  
  /**
   * Implements the following test statistics <ul>
   * <li>
   *   <a href ="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm">
   *   Chi-Square</a>
   * </li>
   * <li>
   *   <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm">
   *     One Sample t-test</a>
   * </li>
   * </ul>
   * @author Phil Steitz
   * @version $Revision: 1.1 $ $Date: 2003/06/21 23:00:39 $
   * 
   */
  public class TestStatisticImpl implements TestStatistic {
      
      /**
       * Default constructor.
       */
      public TestStatisticImpl() {
      }
      
      /**
       * Computes Chi-Square statistic given observed and expected counts <br>
       * <strong>Algorithm</strong>: 
       * http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm <br>
       * <strong>Numerical considerations</strong>: none <br>
       * @param observed array of observed frequency counts
       * @param expected array of expected frequency counts
       * @throws IllegalArgumentException if input arrays have different lengths
       * or length is less than 2
       */
      public double chiSquare(double[] expected, double[] observed) {
          double sumSq = 0.0d;
          double dev = 0.0d;
          if ((expected.length < 2) || (expected.length != observed.length)) {
              throw new IllegalArgumentException
                  ("observed, expected array lengths incorrect");
          }
          for (int i = 0; i < observed.length; i++) {
              dev = (observed[i] - expected[i]);
              sumSq += dev * dev / expected[i];
          }
          
          return sumSq;
      }           
  
      /**
       * Computes t statistic given observed values<br/>
       * <strong>Algorithm</strong>: 
       * http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm<br/>
       * <strong>Numerical considerations</strong>: none <br>
       * @param mu hypothesized mean value.
       * @param observed array of observed values
       * @return t-test statistic for the hypothesized mean and observed values.
       * @throws IllegalArgumentException if input array length is less than 2
       */
  	public double t(double mu, double[] observed) {
          if((observed == null) || (observed.length < 2)) {
              throw new IllegalArgumentException
                  ("observed array length incorrect");
          }
          
          // leverage Univariate to compute statistics
          Univariate univariate = new UnivariateImpl();
          for (int i = 0; i < observed.length; i++) {
  			univariate.addValue(observed[i]);
  		}
          double n = univariate.getN();
          double xbar = univariate.getMean();
          double std = univariate.getStandardDeviation();
  
          return (xbar - mu) / (std / Math.sqrt(n));
  	}
  }
  
  
  
  1.1                  jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/TestStatistic.java
  
  Index: TestStatistic.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.math.stat;
  
  /**
   * Interfaces for the following test statistics <ul>
   * <li><a href ="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm">
   *     Chi-Square</a></li>
   * <li><a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm">
   *     One Sample t-test</a></li>
   * </ul>
   * 
   * @author Phil Steitz
   * @version $Revision: 1.1 $ $Date: 2003/06/21 23:00:39 $
   * 
   */
  public interface TestStatistic {
      
      /**
       * <strong>Description</strong>:
       * Computes Chi-Square statistic given observed and expected freqeuncy counts <br>
       * This statistic can be used to perform Chi-Square tests for goodness
       * of fit.<br>
       * <strong>Definition</strong>: 
       * http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm <br>
       * <strong>Preconditions</strong>: <ul>
       * <li>Expected counts should all be positive. If any expected
       * counts are 0, the test will return INFINITY.  Negative expected or observed counts

       * make the statistic meaningless.</li>
       * <li>The observed and expected arrays <i>must</i> have the same
length and
       * their common length must be at least 2 </li>
       * </ul>
       * @param observed array of observed frequency counts
       * @param expected array of exptected frequency counts
       * @throws IllegalArgumentException if input arrays have different lengths
       * or length is less than 2
       */
      public double chiSquare(double[] expected, double[] observed);
      
      /**
       * <strong>Description</strong>:
       * Computes one sample, t-test statistic given observed values <br/>
       * This statistic can be used to perform one sample tests for means.<br/>
       * <strong>Definition</strong>: 
       * http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm<br/>
       * <strong>Preconditions</strong>: <ul>
       * <li>The observed array length <i>must</i> be at least 2.</li>
       * </ul>
       * @param mu hypothesized mean value.
       * @param observed array of observed values
       * @throws IllegalArgumentException if input array length is less than 2
       */
      public double t(double mu, double[] observed);
  }
  
  
  
  
  1.6       +2 -1      jakarta-commons-sandbox/math/src/test/org/apache/commons/math/RandomDataTest.java
  
  Index: RandomDataTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/RandomDataTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- RandomDataTest.java	4 Jun 2003 02:45:49 -0000	1.5
  +++ RandomDataTest.java	21 Jun 2003 23:00:39 -0000	1.6
  @@ -62,6 +62,7 @@
   import java.util.Collection;
   import java.util.HashSet;
   
  +import org.apache.commons.math.stat.TestStatisticImpl;
   import org.apache.commons.math.stat.Univariate;
   import org.apache.commons.math.stat.UnivariateImpl;
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message