You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by to...@apache.org on 2003/05/15 08:33:20 UTC

cvs commit: jakarta-commons-sandbox/math/src/test/org/apache/commons/math ListUnivariateImplTest.java

tobrien     2003/05/14 23:33:19

  Modified:    math/src/java/org/apache/commons/math
                        ExpandableDoubleArray.java StoreUnivariate.java
                        StoreUnivariateImpl.java UnivariateImpl.java
  Added:       math/src/java/org/apache/commons/math
                        AbstractStoreUnivariate.java
                        ListUnivariateImpl.java
               math/src/test/org/apache/commons/math
                        ListUnivariateImplTest.java
  Log:
  Another change to the stored Univariates.  The calculations are now abstracted
  into an AbstractStoreUnivariate class which take responsibility for
  all statistical calculations.  AbstractStoreUnivariate is implemented by
  two classes:
  
  * StoreUnivariateImpl - This class uses a ExpandableDoubleArray for
  internal storage.  This class is a more efficient class in terms
  of storage and cycles for users who are interested in gathering statistics
  not available in the UnivariateImpl implementation.
  
  * ListUnivariateImpl - This class is for a situation where a user might
  wish to maintain a List of numeric objects outside of a StoreUnivariate
  instance.  We still need to add serious error checking in the absence of
  1.5's generics, but this implementation will work with any list that
  contains Number objects - (BigDecimal, BigInteger, Byte, Double, Float,
  Integer, Long, Short).  This implementation ultimately transforms all
  numeric objects into double primitives via Number.doubleValue().
  
  Becuase AbstractStoreUnivariate does not hold on to any state, a user
  can add values through the Univariate.addValue() function OR one can
  directly manipulate the contents of the List directly.
  
  Revision  Changes    Path
  1.2       +2 -1      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/ExpandableDoubleArray.java
  
  Index: ExpandableDoubleArray.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/ExpandableDoubleArray.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ExpandableDoubleArray.java	15 May 2003 05:19:56 -0000	1.1
  +++ ExpandableDoubleArray.java	15 May 2003 06:33:19 -0000	1.2
  @@ -53,6 +53,7 @@
    */
   package org.apache.commons.math;
   
  +import java.io.Serializable;
   import java.util.NoSuchElementException;
   
   /**
  @@ -60,7 +61,7 @@
    * 
    * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
    */
  -public class ExpandableDoubleArray {
  +public class ExpandableDoubleArray implements Serializable {
   
   	// This is the internal storage array.
   	private double[] internalArray;
  
  
  
  1.2       +8 -0      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/StoreUnivariate.java
  
  Index: StoreUnivariate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/StoreUnivariate.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StoreUnivariate.java	15 May 2003 05:39:00 -0000	1.1
  +++ StoreUnivariate.java	15 May 2003 06:33:19 -0000	1.2
  @@ -114,5 +114,13 @@
   	 * 
   	 * @return returns the current set of numbers in the order in which they were added to this set
   	 */
  +	public abstract double[] getValues();
   	
  +	/**
  +	 * Returns the element at the specified index
  +	 * 
  +	 * @return return the element at the specified index
  +	 */
  +	public abstract double getElement(int index);
  +
   }
  
  
  
  1.2       +14 -187   jakarta-commons-sandbox/math/src/java/org/apache/commons/math/StoreUnivariateImpl.java
  
  Index: StoreUnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/StoreUnivariateImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StoreUnivariateImpl.java	15 May 2003 05:39:00 -0000	1.1
  +++ StoreUnivariateImpl.java	15 May 2003 06:33:19 -0000	1.2
  @@ -54,11 +54,9 @@
   package org.apache.commons.math;
   
   /**
  - * Provides univariate measures for an array of doubles.  
  - * 
    * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
    */
  -public class StoreUnivariateImpl implements StoreUnivariate {
  +public class StoreUnivariateImpl extends AbstractStoreUnivariate {
   
   	ExpandableDoubleArray eDA;
   
  @@ -66,207 +64,36 @@
   		eDA = new ExpandableDoubleArray();
   	}
   
  -	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.StoreUnivariate#getMode()
  -	 */
  -	public double getMode() {
  -		// Mode depends on a refactor Freq class
  -		throw new UnsupportedOperationException("getMode() is not yet implemented");
  -	}
  -
  -	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.StoreUnivariate#getSkewness()
  -	 */
  -	public double getSkewness() {
  -		// Initialize the skewness
  -		double skewness = Double.NaN;
  -		
  -		// Get the mean and the standard deviation
  -		double mean = getMean();
  -		double stdDev = getStandardDeviation();
  -
  -		// Sum the cubes of the distance from the mean divided by the standard deviation
  -		double accum = 0.0;
  -		for( int i = 0; i < eDA.getNumElements(); i++ ) {
  -			accum += Math.pow( (eDA.getElement(i) - mean) / stdDev, 3.0);
  -		}
  -		
  -		// Get N
  -		double n = getN();
  -		
  -		// Calculate skewness
  -		skewness = ( n / ( (n-1) * (n-2) ) ) * accum;
  -
  -		return skewness;
  -	}
  -
  -	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.StoreUnivariate#getKurtosis()
  -	 */
  -	public double getKurtosis() {
  -		// Initialize the kurtosis
  -		double kurtosis = Double.NaN;
  -		
  -		// Get the mean and the standard deviation
  -		double mean = getMean();
  -		double stdDev = getStandardDeviation();
  -
  -		// Sum the ^4 of the distance from the mean divided by the standard deviation
  -		double accum = 0.0;
  -		for( int i = 0; i < eDA.getNumElements(); i++ ) {
  -			accum += Math.pow( (eDA.getElement(i) - mean) / stdDev, 4.0);
  -		}
  -		
  -		// Get N
  -		double n = getN();
  -		
  -		double coefficientOne = ( n * (n+1)) / ( (n-1) * (n-2) * (n-3) );
  -		double termTwo = (  ( 3 * Math.pow( n - 1, 2.0)) /  ( (n-2) * (n-3) ) ); 
  -		// Calculate kurtosis
  -		kurtosis = ( coefficientOne * accum ) - termTwo;
  -
  -		return kurtosis;
  -	}
  -
  -	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.StoreUnivariate#getKurtosisClass()
  -	 */
  -	public int getKurtosisClass() {
  -
  -		int kClass = StoreUnivariate.MESOKURTIC;
  -		
  -		double kurtosis = getKurtosis();
  -		if( kurtosis > 0 ) {
  -			kClass = StoreUnivariate.LEPTOKURTIC;
  -		} else if( kurtosis < 0 ) {
  -			kClass = StoreUnivariate.PLATYKURTIC;
  -		}
  -		
  -		return( kClass );
  -
  -	}
  -
  -	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.Univariate#addValue(double)
  -	 */
  -	public void addValue(double v) {
  -		eDA.addElement( v );
  -	}
  -
  -	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.Univariate#getMean()
  -	 */
  -	public double getMean() {
  -		double arithMean = getSum() / getN();
  -		return arithMean;
  -	}
  -
  -	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.Univariate#getVariance()
  -	 */
  -	public double getVariance() {
  -		// Initialize variance
  -		double variance = Double.NaN;
  -
  -		if( getN() == 1 ) {
  -			// If this is a single value
  -			variance = 0;
  -		} else if( getN() > 1 ) {
  -			// Get the mean
  -			double mean = getMean();
  -
  -			// Calculate the sum of the squares of the distance between each value and the mean
  -			double accum = 0.0;		
  -			for( int i = 0; i < eDA.getNumElements(); i++ ){
  -					accum += Math.pow( (eDA.getElement(i) - mean), 2.0 );
  -			}
  -		
  -			// Divide the accumulator by N - Hmmm... unbiased or biased?
  -			variance = accum / (getN() - 1);
  -		 }
  -		
  -		return variance;
  -	}
  -
  -	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.Univariate#getStandardDeviation()
  -	 */
  -	public double getStandardDeviation() {
  -		double stdDev = Double.NaN;
  -		if( getN() != 0 ) {
  -			stdDev = Math.sqrt( getVariance() );
  -		}
  -		return( stdDev );
  -	}
   
   	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.Univariate#getMax()
  +	 * @see org.apache.commons.math.StoreUnivariate#getValues()
   	 */
  -	public double getMax() {
  -		
  -		// Initialize maximum to NaN
  -		double max = Double.NaN;
  -		
  -		for( int i = 0; i < eDA.getNumElements(); i++) {
  -			if( i == 0 ) {
  -				max = eDA.getElement(i);
  -			} else {
  -				if( eDA.getElement(i) > max ) {
  -					max = eDA.getElement(i);
  -				}
  -			}
  -		}
  +	public double[] getValues() {
   
  -		return max;
  +		double[] copiedArray = new double[ eDA.getNumElements() ];
  +		System.arraycopy( eDA.getValues(), 0, copiedArray, 0, eDA.getNumElements());
  +		return copiedArray;
   	}
   
   	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.Univariate#getMin()
  +	 * @see org.apache.commons.math.StoreUnivariate#getElement(int)
   	 */
  -	public double getMin() {
  -		// Initialize minimum to NaN
  -		double min = Double.NaN;
  -		
  -		for( int i = 0; i < eDA.getNumElements(); i++) {
  -			if( i == 0 ) {
  -				min = eDA.getElement(i);
  -			} else {
  -				if( eDA.getElement(i) < min ) {
  -					min = eDA.getElement(i);
  -				}
  -			}
  -		}
  -
  -		return min;
  +	public double getElement(int index) {
  +		return eDA.getElement(index);
   	}
  -
  +	
   	/* (non-Javadoc)
   	 * @see org.apache.commons.math.Univariate#getN()
   	 */
   	public double getN() {
   		return eDA.getNumElements();
   	}
  -
  -	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.Univariate#getSum()
  -	 */
  -	public double getSum() {
  -		double accum = 0.0;
  -		for( int i = 0; i < eDA.getNumElements(); i++) {
  -			accum += eDA.getElement(i);
  -		}
  -		return accum;
  -	}
  -
  +	
   	/* (non-Javadoc)
  -	 * @see org.apache.commons.math.Univariate#getSumsq()
  +	 * @see org.apache.commons.math.Univariate#addValue(double)
   	 */
  -	public double getSumsq() {
  -		double accum = 0.0;
  -		for( int i = 0; i < eDA.getNumElements(); i++) {
  -			accum += Math.pow(eDA.getElement(i), 2.0);
  -		}
  -		return accum;
  +	public void addValue(double v) {
  +		eDA.addElement( v );
   	}
   
   	/* (non-Javadoc)
  
  
  
  1.2       +4 -2      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/UnivariateImpl.java
  
  Index: UnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/UnivariateImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UnivariateImpl.java	15 May 2003 05:39:00 -0000	1.1
  +++ UnivariateImpl.java	15 May 2003 06:33:19 -0000	1.2
  @@ -53,6 +53,8 @@
    */
   package org.apache.commons.math;
   
  +import java.io.Serializable;
  +
   /**
    *
    * Accumulates univariate statistics for values fed in 
  @@ -65,7 +67,7 @@
    * @version $Revision$ $Date$
    * 
   */
  -public class UnivariateImpl implements Univariate {
  +public class UnivariateImpl implements Univariate, Serializable {
   
       /** running sum of values that have been added */
       private double sum = 0.0;
  
  
  
  1.1                  jakarta-commons-sandbox/math/src/java/org/apache/commons/math/AbstractStoreUnivariate.java
  
  Index: AbstractStoreUnivariate.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;
  
  /**
   * Provides univariate measures for an array of doubles.  
   * 
   * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
   */
  public abstract class AbstractStoreUnivariate implements StoreUnivariate {
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.StoreUnivariate#getMode()
  	 */
  	public double getMode() {
  		// Mode depends on a refactor Freq class
  		throw new UnsupportedOperationException("getMode() is not yet implemented");
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.StoreUnivariate#getSkewness()
  	 */
  	public double getSkewness() {
  		// Initialize the skewness
  		double skewness = Double.NaN;
  		
  		// Get the mean and the standard deviation
  		double mean = getMean();
  		double stdDev = getStandardDeviation();
  
  		// Sum the cubes of the distance from the mean divided by the standard deviation
  		double accum = 0.0;
  		for( int i = 0; i < getN(); i++ ) {
  			accum += Math.pow( (getElement(i) - mean) / stdDev, 3.0);
  		}
  		
  		// Get N
  		double n = getN();
  		
  		// Calculate skewness
  		skewness = ( n / ( (n-1) * (n-2) ) ) * accum;
  
  		return skewness;
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.StoreUnivariate#getKurtosis()
  	 */
  	public double getKurtosis() {
  		// Initialize the kurtosis
  		double kurtosis = Double.NaN;
  		
  		// Get the mean and the standard deviation
  		double mean = getMean();
  		double stdDev = getStandardDeviation();
  
  		// Sum the ^4 of the distance from the mean divided by the standard deviation
  		double accum = 0.0;
  		for( int i = 0; i < getN(); i++ ) {
  			accum += Math.pow( (getElement(i) - mean) / stdDev, 4.0);
  		}
  		
  		// Get N
  		double n = getN();
  		
  		double coefficientOne = ( n * (n+1)) / ( (n-1) * (n-2) * (n-3) );
  		double termTwo = (  ( 3 * Math.pow( n - 1, 2.0)) /  ( (n-2) * (n-3) ) ); 
  		// Calculate kurtosis
  		kurtosis = ( coefficientOne * accum ) - termTwo;
  
  		return kurtosis;
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.StoreUnivariate#getKurtosisClass()
  	 */
  	public int getKurtosisClass() {
  
  		int kClass = StoreUnivariate.MESOKURTIC;
  		
  		double kurtosis = getKurtosis();
  		if( kurtosis > 0 ) {
  			kClass = StoreUnivariate.LEPTOKURTIC;
  		} else if( kurtosis < 0 ) {
  			kClass = StoreUnivariate.PLATYKURTIC;
  		}
  		
  		return( kClass );
  
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#getMean()
  	 */
  	public double getMean() {
  		double arithMean = getSum() / getN();
  		return arithMean;
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#getVariance()
  	 */
  	public double getVariance() {
  		// Initialize variance
  		double variance = Double.NaN;
  
  		if( getN() == 1 ) {
  			// If this is a single value
  			variance = 0;
  		} else if( getN() > 1 ) {
  			// Get the mean
  			double mean = getMean();
  
  			// Calculate the sum of the squares of the distance between each value and the mean
  			double accum = 0.0;		
  			for( int i = 0; i < getN(); i++ ){
  					accum += Math.pow( (getElement(i) - mean), 2.0 );
  			}
  		
  			// Divide the accumulator by N - Hmmm... unbiased or biased?
  			variance = accum / (getN() - 1);
  		 }
  		
  		return variance;
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#getStandardDeviation()
  	 */
  	public double getStandardDeviation() {
  		double stdDev = Double.NaN;
  		if( getN() != 0 ) {
  			stdDev = Math.sqrt( getVariance() );
  		}
  		return( stdDev );
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#getMax()
  	 */
  	public double getMax() {
  		
  		// Initialize maximum to NaN
  		double max = Double.NaN;
  		
  		for( int i = 0; i < getN(); i++) {
  			if( i == 0 ) {
  				max = getElement(i);
  			} else {
  				if( getElement(i) > max ) {
  					max = getElement(i);
  				}
  			}
  		}
  
  		return max;
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#getMin()
  	 */
  	public double getMin() {
  		// Initialize minimum to NaN
  		double min = Double.NaN;
  		
  		for( int i = 0; i < getN(); i++) {
  			if( i == 0 ) {
  				min = getElement(i);
  			} else {
  				if( getElement(i) < min ) {
  					min = getElement(i);
  				}
  			}
  		}
  
  		return min;
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#getSum()
  	 */
  	public double getSum() {
  		double accum = 0.0;
  		for( int i = 0; i < getN(); i++) {
  			accum += getElement(i);
  		}
  		return accum;
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#getSumsq()
  	 */
  	public double getSumsq() {
  		double accum = 0.0;
  		for( int i = 0; i < getN(); i++) {
  			accum += Math.pow(getElement(i), 2.0);
  		}
  		return accum;
  	}
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/math/src/java/org/apache/commons/math/ListUnivariateImpl.java
  
  Index: ListUnivariateImpl.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;
  
  import java.util.Iterator;
  import java.util.List;
  
  /**
   * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
   */
  public class ListUnivariateImpl extends AbstractStoreUnivariate {
  
  	// Holds a reference to a list - GENERICs are going to make
  	// out lives easier here as we could only accept List<Number>
  	List list;
  
  	public ListUnivariateImpl( List list ) {
  		this.list = list;
  	}
  
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.StoreUnivariate#getValues()
  	 */
  	public double[] getValues() {
  
  		double[] copiedArray = new double[list.size()];
  
  		int i = 0;
  		Iterator it = list.iterator();
  		while( it.hasNext() ) {
  			Number n = (Number) it.next();
  			copiedArray[i] = n.doubleValue();
  			i++;
  		}
  
  		return copiedArray;
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.StoreUnivariate#getElement(int)
  	 */
  	public double getElement(int index) {
  		Number n = (Number) list.get(index);
  		return n.doubleValue();
  	}
  	
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#getN()
  	 */
  	public double getN() {
  		return list.size();
  	}
  	
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#addValue(double)
  	 */
  	public void addValue(double v) {
  		list.add( new Double(v));
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.commons.math.Univariate#clear()
  	 */
  	public void clear() {
  		list.clear();
  	}
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/math/src/test/org/apache/commons/math/ListUnivariateImplTest.java
  
  Index: ListUnivariateImplTest.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;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.List;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   * Test cases for the {@link Univariate} class.
   *
   * @author <a href="mailto:phil@steitz.com">Phil Steitz</a>
   * @version $Revision: 1.1 $ $Date: 2003/05/15 06:33:19 $
   */
  
  public final class ListUnivariateImplTest extends TestCase {
      private double one = 1;
      private float two = 2;
      private int three = 3;
  
      private double mean = 2;
      private double sumSq = 18;
      private double sum = 8;
      private double var = 0.666666666666666666667;
      private double std = Math.sqrt(var);
      private double n = 4;
      private double min = 1;
      private double max = 3;
      private double skewness = 0;
      private double kurtosis = 0.5;
      private int kClass = StoreUnivariate.LEPTOKURTIC;
      private double tolerance = 10E-15;
      
      public ListUnivariateImplTest(String name) {
          super(name);
      }
      
      public void setUp() {  
      }
      
      public static Test suite() {
          TestSuite suite = new TestSuite(ListUnivariateImplTest.class);
          suite.setName("Freq Tests");
          return suite;
      }
      
      /** test stats */
      public void testStats() {
      	List externalList = new ArrayList();
      	
          StoreUnivariate u = new ListUnivariateImpl( externalList ); 
  
          assertEquals("total count",0,u.getN(),tolerance);
          u.addValue(one);
          u.addValue(two);
          u.addValue(two);
          u.addValue(three);
          assertEquals("N",n,u.getN(),tolerance);
          assertEquals("sum",sum,u.getSum(),tolerance);
          assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
          assertEquals("var",var,u.getVariance(),tolerance);
          assertEquals("std",std,u.getStandardDeviation(),tolerance);
          assertEquals("mean",mean,u.getMean(),tolerance);
          assertEquals("min",min,u.getMin(),tolerance);
          assertEquals("max",max,u.getMax(),tolerance);
          u.clear();
          assertEquals("total count",0,u.getN(),tolerance);    
      }     
      
      public void testN0andN1Conditions() throws Exception {
      	List list = new ArrayList();
      	
      	StoreUnivariate u = new ListUnivariateImpl( list );
      	    	
  		assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
  		assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
  		assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
  
  		list.add( new Double(one));
  
  		assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
  		assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
  		assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);	
      }
      
      public void testSkewAndKurtosis() {
      	StoreUnivariate u = new StoreUnivariateImpl();
      	
      	double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
    	  										 9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
    	  	for( int i = 0; i < testArray.length; i++) {
    	  		u.addValue( testArray[i]);
    	  	}
    	  	
  		assertEquals("mean", 12.40455, u.getMean(), 0.0001);
  		assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
  		assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
    	  	assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
      }
  }
  
  
  
  

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