Here is a patch with new versions of the Univariate Facades. Included in this patch are:
1.) one new univariate "MixedListUnivariate" That accepts a TransformerMap to transform objects to primitive doubles.
2.) one new AbstractUnivariate implementation.
3.) There are many revisions to the current Implementations to make them work with both NumberTransformers and the individual UnivariateStatistics.
4.) all Moment based stats (like skew and kurt) are moved up into the Univariate interface.
5.) The StorelessUnivariateStatistics have been reorganized to move calculations that do not need to be performed on "increment" further upstream to "getValue". This reduces the amount of calculation being done at the addValue stage (eliminating variance, skew and kurtosis calculations from the moments at this stage).
6.) All moment based statistics have been modified to support sharing a common moment. This way internal calculations for m1, m2, m3 and m4 do not need to be replicated within the individual stats, they can all share the same object.
I would really like to get some input on these from the group as they represent a rather large commit change on others work in the stat directory.
Lastly, I do have a version of StatUtils that works with UnivariateStatistics, but I'm now convinced that we no longer need StatUtils any more.
-Mark
Index: AbstractStoreUnivariate.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java,v retrieving revision 1.8 diff -u -r1.8 AbstractStoreUnivariate.java --- AbstractStoreUnivariate.java 7 Jul 2003 23:25:13 -0000 1.8 +++ AbstractStoreUnivariate.java 8 Jul 2003 04:07:38 -0000 @@ -52,267 +52,69 @@ * <http://www.apache.org/>. */ package org.apache.commons.math.stat; + import java.util.Arrays; +import org.apache.commons.math.stat.univariate.rank.Percentile; + /** * Provides univariate measures for an array of doubles. - * - * @author <a href="mailto:[EMAIL PROTECTED]">Tim O'Brien</a> */ -public abstract class AbstractStoreUnivariate implements StoreUnivariate { +public abstract class AbstractStoreUnivariate + extends AbstractUnivariate + implements StoreUnivariate { + /** Percentile */ + protected Percentile percentile = new Percentile(50); + /** - * Returns the skewness of this collection of values - * @see org.apache.commons.math.stat.StoreUnivariate#getSkewness() + * Create an AbstractStoreUnivariate */ - 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; + public AbstractStoreUnivariate() { + super(); } /** - * Returns the kurtosis for this collection of values - * @see org.apache.commons.math.stat.StoreUnivariate#getKurtosis() + * Create an AbstractStoreUnivariate with a specific Window + * @param window WindowSIze for stat calculation */ - 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; + public AbstractStoreUnivariate(int window) { + super(window); } /** - * Returns the type or class of kurtosis that this collection of - * values exhibits - * @see org.apache.commons.math.stat.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); - - } - - /** - * Returns the mean for this collection of values - * @see org.apache.commons.math.stat.Univariate#getMean() - */ - public double getMean() { - double arithMean = getSum() / getN(); - return arithMean; - } - - /** - * Returns the geometric mean for this collection of values - * @see org.apache.commons.math.stat.Univariate#getGeometricMean() - */ - public double getGeometricMean() { - double gMean = Double.NaN; - - if (getN() > 0) { - double sumLog = 0.0; - for (int i = 0; i < getN(); i++) { - sumLog += Math.log(getElement(i)); - } - gMean = Math.exp(sumLog / (double)getN() ); - } - - return gMean; - } - - /** - * Returns the variance for this collection of values - * @see org.apache.commons.math.stat.Univariate#getVariance() + * @see org.apache.commons.math.stat.StoreUnivariate#getPercentile(double) */ - 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; + public double getPercentile(double p) { + percentile.setPercentile(p); + return percentile.evaluate(this.getValues(), this.start(), this.size()); } - + /** - * Returns the standard deviation for this collection of values - * @see org.apache.commons.math.stat.Univariate#getStandardDeviation() + * @see org.apache.commons.math.stat2.AbstractStoreUnivariate#getSortedValues() */ - public double getStandardDeviation() { - double stdDev = Double.NaN; - if (getN() != 0) { - stdDev = Math.sqrt(getVariance()); - } - return (stdDev); + public double[] getSortedValues() { + double[] sort = getValues(); + Arrays.sort(sort); + return sort; } - + /** - * Returns the maximum value contained herein. - * @see org.apache.commons.math.stat.Univariate#getMax() + * @see org.apache.commons.math.stat.Univariate#addValue(double) */ - 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; - } + public abstract void addValue(double value); /** - * Returns the minimum value contained herein - * @see org.apache.commons.math.stat.Univariate#getMin() + * @see org.apache.commons.math.stat.StoreUnivariate#getValues() */ - 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); - } - } - } + public abstract double[] getValues(); - return min; - } - - /** - * Returns the sum of all values contained herein - * @see org.apache.commons.math.stat.Univariate#getSum() - */ - public double getSum() { - double accum = 0.0; - for (int i = 0; i < getN(); i++) { - accum += getElement(i); - } - return accum; - } /** - * Returns the sun of the squares of all values contained herein - * @see org.apache.commons.math.stat.Univariate#getSumsq() + * @see org.apache.commons.math.stat.StoreUnivariate#getElement(int) */ - public double getSumsq() { - double accum = 0.0; - for (int i = 0; i < getN(); i++) { - accum += Math.pow(getElement(i), 2.0); - } - return accum; - } + public abstract double getElement(int index); - /** - * @see org.apache.commons.math.stat.StoreUnivariate#getSortedValues() - * - */ - public double[] getSortedValues() { - double[] values = getValues(); - Arrays.sort(values); - return values; - } - /** - * Returns an estimate for the pth percentile of the stored values - * @see org.apache.commons.math.stat.StoreUnivariate#getPercentile(double) - */ - public double getPercentile(double p) { - if ((p > 100) || (p <= 0)) { - throw new IllegalArgumentException("invalid percentile value"); - } - double n = (double) getN(); - if (n == 0) { - return Double.NaN; - } - if (n == 1) { - return getElement(0); // always return single value for n = 1 - } - double pos = p * (n + 1) / 100; - double fpos = Math.floor(pos); - int intPos = (int) fpos; - double d = pos - fpos; - double[] sorted = getSortedValues(); - if (pos < 1) { - return sorted[0]; - } - if (pos > n) { - return sorted[getN() - 1]; - } - double lower = sorted[intPos - 1]; - double upper = sorted[intPos]; - return lower + d * (upper - lower); - } } Index: AbstractUnivariate.java =================================================================== RCS file: AbstractUnivariate.java diff -N AbstractUnivariate.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ AbstractUnivariate.java 8 Jul 2003 04:07:38 -0000 @@ -0,0 +1,389 @@ +/* ==================================================================== + * 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 [EMAIL PROTECTED] + * + * 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 org.apache.commons.math.stat.univariate.moment.FourthMoment; +import org.apache.commons.math.stat.univariate.moment.GeometricMean; +import org.apache.commons.math.stat.univariate.moment.Kurtosis; +import org.apache.commons.math.stat.univariate.moment.Mean; +import org.apache.commons.math.stat.univariate.moment.Skewness; +import org.apache.commons.math.stat.univariate.moment.Variance; +import org.apache.commons.math.stat.univariate.rank.Max; +import org.apache.commons.math.stat.univariate.rank.Min; +import org.apache.commons.math.stat.univariate.summary.Sum; +import org.apache.commons.math.stat.univariate.summary.SumOfLogs; +import org.apache.commons.math.stat.univariate.summary.SumOfSquares; + +/** + * Provides univariate measures for an array of doubles. + * + */ +public abstract class AbstractUnivariate implements Univariate { + + /** hold the window size **/ + protected int windowSize = Univariate.INFINITE_WINDOW; + + /** count of values that have been added */ + protected int n = 0; + + /** FourthMoment is used in calculating mean, variance,skew and kurtosis */ + protected FourthMoment moment = null; + + /** sum of values that have been added */ + protected Sum sum = null; + + /** sum of the square of each value that has been added */ + protected SumOfSquares sumsq = null; + + /** min of values that have been added */ + protected Min min = null; + + /** max of values that have been added */ + protected Max max = null; + + /** sumLog of values that have been added */ + protected SumOfLogs sumLog = null; + + /** geoMean of values that have been added */ + protected GeometricMean geoMean = null; + + /** mean of values that have been added */ + protected Mean mean = null; + + /** variance of values that have been added */ + protected Variance variance = null; + + /** skewness of values that have been added */ + protected Skewness skewness = null; + + /** kurtosis of values that have been added */ + protected Kurtosis kurtosis = null; + + /** + * Construct an AbstractUnivariate + */ + public AbstractUnivariate() { + super(); + + sum = new Sum(); + sumsq = new SumOfSquares(); + min = new Min(); + max = new Max(); + sumLog = new SumOfLogs(); + geoMean = new GeometricMean(); + + moment = new FourthMoment(); + mean = new Mean(moment); + variance = new Variance(moment); + skewness = new Skewness(moment); + kurtosis = new Kurtosis(moment); + } + + /** + * Construct an AbstractUnivariate with a window + * @param window The Window Size + */ + public AbstractUnivariate(int window) { + this(); + setWindowSize(window); + } + + /** + * Returns the internalValues array. + * @return the array + */ + protected abstract double[] internalValues(); + + /** + * Returns the start index of the array + * @return start index + */ + protected abstract int start(); + + /** + * Returns the size of the array appropriate for doing calculations. + * @return Usually this is just numElements. + */ + protected abstract int size(); + + /** + * If windowSize is set to Infinite, + * statistics are calculated using the following + * <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf"> + * recursive strategy + * </a>. + * @see org.apache.commons.math.stat.Univariate#addValue(double) + */ + public abstract void addValue(double value); + + /** + * @see org.apache.commons.math.stat.Univariate#getN() + */ + public int getN() { + return n; + } + + /** + * @see org.apache.commons.math.stat.Univariate#getSum() + */ + public double getSum() { + double[] v = internalValues(); + if (v != null) { + return sum.evaluate(v, this.start(), this.size()); + } + + return sum.getValue(); + } + + /** + * @see org.apache.commons.math.stat.Univariate#getSumsq() + */ + public double getSumsq() { + double[] v = internalValues(); + if (v != null) { + return sumsq.evaluate(v, this.start(), this.size()); + } + + return sumsq.getValue(); + } + + /** + * @see org.apache.commons.math.stat.Univariate#getMean() + */ + public double getMean() { + double[] v = internalValues(); + if (v != null) { + return mean.evaluate(v, this.start(), this.size()); + } + + return mean.getValue(); + } + + /** + * Returns the standard deviation for this collection of values + * @see org.apache.commons.math.stat.Univariate#getStandardDeviation() + */ + public double getStandardDeviation() { + double stdDev = Double.NaN; + if (getN() > 0) { + if (getN() > 1) { + stdDev = Math.sqrt(getVariance()); + } else { + stdDev = 0.0; + } + } + return (stdDev); + } + + /** + * Returns the variance of the values that have been added via West's + * algorithm as described by + * <a href="http://doi.acm.org/10.1145/359146.359152">Chan, T. F. and + * J. G. Lewis 1979, <i>Communications of the ACM</i>, + * vol. 22 no. 9, pp. 526-531.</a>. + * + * @return The variance of a set of values. + * Double.NaN is returned for an empty + * set of values and 0.0 is returned for + * a <= 1 value set. + */ + public double getVariance() { + double[] v = internalValues(); + if (v != null) { + return variance.evaluate(v, this.start(), this.size()); + } + + return variance.getValue(); + } + + /** + * Returns the skewness of the values that have been added as described by + * <a href="http://mathworld.wolfram.com/k-Statistic.html"> + * Equation (6) for k-Statistics</a>. + * @return The skew of a set of values. Double.NaN is returned for + * an empty set of values and 0.0 is returned for a + * <= 2 value set. + */ + public double getSkewness() { + double[] v = internalValues(); + if (v != null) { + return skewness.evaluate(v, this.start(), this.size()); + } + + return skewness.getValue(); + } + + /** + * Returns the kurtosis of the values that have been added as described by + * <a href="http://mathworld.wolfram.com/k-Statistic.html"> + * Equation (7) for k-Statistics</a>. + * + * @return The kurtosis of a set of values. Double.NaN is returned for + * an empty set of values and 0.0 is returned for a <= 3 + * value set. + */ + public double getKurtosis() { + double[] v = internalValues(); + if (v != null) { + return kurtosis.evaluate(v, this.start(), this.size()); + } + + return kurtosis.getValue(); + } + + /** + * @see org.apache.commons.math.stat.StoreUnivariate#getKurtosisClass() + */ + public int getKurtosisClass() { + int kClass = Univariate.MESOKURTIC; + + double kurtosis = getKurtosis(); + if (kurtosis > 0) { + kClass = Univariate.LEPTOKURTIC; + } else if (kurtosis < 0) { + kClass = Univariate.PLATYKURTIC; + } + return (kClass); + } + + /** + * @see org.apache.commons.math.stat.Univariate#getMax() + */ + public double getMax() { + double[] v = internalValues(); + if (v != null) { + return max.evaluate(v, this.start(), this.size()); + } + + return max.getValue(); + } + + /** + * @see org.apache.commons.math.stat.Univariate#getMin() + */ + public double getMin() { + double[] v = internalValues(); + if (v != null) { + return min.evaluate(v, this.start(), this.size()); + } + + return min.getValue(); + } + + /** + * @see org.apache.commons.math.stat.Univariate#getGeometricMean() + */ + public double getGeometricMean() { + double[] v = internalValues(); + if (v != null) { + return geoMean.evaluate(v, this.start(), this.size()); + } + + return geoMean.getValue(); + } + + /** + * Generates a text report displaying + * univariate statistics from values that + * have been added. + * @return String with line feeds displaying statistics + */ + public String toString() { + StringBuffer outBuffer = new StringBuffer(); + outBuffer.append("UnivariateImpl:\n"); + outBuffer.append("n: " + n + "\n"); + outBuffer.append("min: " + min + "\n"); + outBuffer.append("max: " + max + "\n"); + outBuffer.append("mean: " + getMean() + "\n"); + outBuffer.append("std dev: " + getStandardDeviation() + "\n"); + outBuffer.append("skewness: " + getSkewness() + "\n"); + outBuffer.append("kurtosis: " + getKurtosis() + "\n"); + return outBuffer.toString(); + } + + /** + * @see org.apache.commons.math.Univariate#clear() + */ + public void clear() { + this.n = 0; + min.clear(); + max.clear(); + sum.clear(); + sumLog.clear(); + sumsq.clear(); + geoMean.clear(); + + moment.clear(); + mean.clear(); + variance.clear(); + skewness.clear(); + kurtosis.clear(); + } + + /** + * @see org.apache.commons.math.Univariate#getWindowSize() + */ + public int getWindowSize() { + return windowSize; + } + + /** + * @see org.apache.commons.math.Univariate#setWindowSize(int) + */ + public void setWindowSize(int windowSize) { + clear(); + this.windowSize = windowSize; + } + +} \ No newline at end of file Index: BeanListUnivariateImpl.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/BeanListUnivariateImpl.java,v retrieving revision 1.2 diff -u -r1.2 BeanListUnivariateImpl.java --- BeanListUnivariateImpl.java 22 Jun 2003 03:57:55 -0000 1.2 +++ BeanListUnivariateImpl.java 8 Jul 2003 04:07:39 -0000 @@ -54,7 +54,8 @@ package org.apache.commons.math.stat; import java.util.List; -import org.apache.commons.beanutils.PropertyUtils; + +import org.apache.commons.math.util.BeanTransformer; /** * This implementation of StoreUnivariate uses commons-beanutils to gather @@ -66,37 +67,59 @@ */ public class BeanListUnivariateImpl extends ListUnivariateImpl { + /** + * propertyName of the property to get from the bean + */ private String propertyName; + /** + * Construct a BeanListUnivariate with specified + * backing list + * @param list Backing List + */ public BeanListUnivariateImpl(List list) { - super( list ); + super(list); } + /** + * Construct a BeanListUnivariate with specified + * backing list and propertyName + * @param list Backing List + * @param propertyName Bean propertyName + */ public BeanListUnivariateImpl(List list, String propertyName) { - super( list ); - setPropertyName( propertyName ); + super(list); + setPropertyName(propertyName); + this.transformer = new BeanTransformer(propertyName); } + /** + * @return propertyName + */ public String getPropertyName() { return propertyName; } + /** + * @param propertyName Name of Property + */ public void setPropertyName(String propertyName) { - System.out.println( "Set prop name; " + propertyName ); + System.out.println("Set prop name; " + propertyName); this.propertyName = propertyName; + this.transformer = new BeanTransformer(propertyName); } - - /* (non-Javadoc) + /** * @see org.apache.commons.math.Univariate#addValue(double) */ public void addValue(double v) { - String msg = "The BeanListUnivariateImpl does not accept values " + - "through the addValue method. Because elements of this list " + - "are JavaBeans, one must be sure to set the 'propertyName' " + - "property and add new Beans to the underlying list via the " + - "addBean(Object bean) method"; - throw new UnsupportedOperationException( msg ); + String msg = + "The BeanListUnivariateImpl does not accept values " + + "through the addValue method. Because elements of this list " + + "are JavaBeans, one must be sure to set the 'propertyName' " + + "property and add new Beans to the underlying list via the " + + "addBean(Object bean) method"; + throw new UnsupportedOperationException(msg); } /** @@ -106,32 +129,6 @@ */ public void addObject(Object bean) { list.add(bean); - } - - /** - * Reads the property of an element in the list. - * - * @param index The location of the value in the internal List - * @return A Number object representing the value at a given - * index - */ - protected Number getInternalIndex(int index) { - - try { - Number n = (Number) PropertyUtils.getProperty( list.get( index ), - propertyName ); - - return n; - } catch( Exception e ) { - // TODO: We could use a better strategy for error handling - // here. - - // This is a somewhat foolish design decision, but until - // we figure out what needs to be done, let's return NaN - return new Double(Double.NaN); - } - - } } Index: ListUnivariateImpl.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/ListUnivariateImpl.java,v retrieving revision 1.2 diff -u -r1.2 ListUnivariateImpl.java --- ListUnivariateImpl.java 22 Jun 2003 03:57:55 -0000 1.2 +++ ListUnivariateImpl.java 8 Jul 2003 04:07:39 -0000 @@ -54,132 +54,135 @@ package org.apache.commons.math.stat; import java.util.List; +import org.apache.commons.math.util.DefaultTransformer; +import org.apache.commons.math.util.NumberTransformer; /** * @author <a href="mailto:[EMAIL PROTECTED]">Tim O'Brien</a> */ -public class ListUnivariateImpl extends AbstractStoreUnivariate { +public class ListUnivariateImpl + extends AbstractStoreUnivariate + implements StoreUnivariate { - // Holds the value of the windowSize, initial windowSize is the constant - // Univariate.INFINITE_WINDOW - private int windowSize = Univariate.INFINITE_WINDOW; - - // Holds a reference to a list - GENERICs are going to make - // out lives easier here as we could only accept List<Number> - List list; + /** + * Holds a reference to a list - GENERICs are going to make + * out lives easier here as we could only accept List<Number> + */ + protected List list; + + /** Number Transformer maps Objects to Number for us. */ + protected NumberTransformer transformer; + /** + * Construct a ListUnivariate with a specific List. + * @param list The list that will back this Univariate + */ public ListUnivariateImpl(List list) { + super(); this.list = list; + transformer = new DefaultTransformer(); } - /* (non-Javadoc) + /** * @see org.apache.commons.math.StoreUnivariate#getValues() */ public double[] getValues() { - int startIndex = 0; - int endIndex = list.size() - 1; - + int length = list.size(); // If the window size is not INFINITE_WINDOW AND // the current list is larger that the window size, we need to // take into account only the last n elements of the list // as definied by windowSize - if (windowSize != Univariate.INFINITE_WINDOW && - windowSize < list.size()) { - startIndex = (list.size() - 1) - windowSize; + + if (windowSize != Univariate.INFINITE_WINDOW + && windowSize < list.size()) { + length = list.size() - Math.max(0, list.size() - windowSize); } // Create an array to hold all values - double[] copiedArray = new double[list.size() - startIndex]; + double[] copiedArray = new double[length]; - for( int i = startIndex; i <= endIndex; i++ ) { - Number n = (Number) getInternalIndex( i ); - copiedArray[i] = n.doubleValue(); - i++; + for (int i = 0; i < copiedArray.length; i++) { + copiedArray[i] = getElement(i); } - return copiedArray; } - /* (non-Javadoc) + /** * @see org.apache.commons.math.StoreUnivariate#getElement(int) */ public double getElement(int index) { double value = Double.NaN; - if (windowSize != Univariate.INFINITE_WINDOW && - windowSize < list.size()) { - int calcIndex = (list.size() - windowSize) + index; + int calcIndex = index; - Number n = (Number) getInternalIndex(calcIndex); - value = n.doubleValue(); - } else { - Number n = (Number) getInternalIndex(index); - value = n.doubleValue(); + if (windowSize != Univariate.INFINITE_WINDOW + && windowSize < list.size()) { + calcIndex = (list.size() - windowSize) + index; + } + + try { + value = transformer.transform(list.get(calcIndex)); + } catch (Exception e) { + e.printStackTrace(); } + return value; } - /* (non-Javadoc) + /** * @see org.apache.commons.math.Univariate#getN() */ public int getN() { - int N = 0; + int n = 0; if (windowSize != Univariate.INFINITE_WINDOW) { if (list.size() > windowSize) { - N = windowSize; + n = windowSize; } else { - N = list.size(); + n = list.size(); } } else { - N = list.size(); + n = list.size(); } - return N; + return n; } - /* (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() { + super.clear(); list.clear(); } - /* (non-Javadoc) - * @see org.apache.commons.math.Univariate#getWindowSize() + /** + * @see org.apache.commons.math.stat.AbstractUnivariate#internalValues() */ - public int getWindowSize() { - return windowSize; + protected double[] internalValues() { + return getValues(); } - /* (non-Javadoc) - * @see org.apache.commons.math.Univariate#setWindowSize(int) + /** + * @see org.apache.commons.math.stat.AbstractUnivariate#start() */ - public void setWindowSize(int windowSize) { - this.windowSize = windowSize; + protected int start() { + return 0; } /** - * This function exists to support the function of classes which - * extend the ListUnivariateImpl. - * - * @param index The location of the value in the internal List - * @return A Number object representing the value at a given - * index + * @see org.apache.commons.math.stat.AbstractUnivariate#size() */ - protected Number getInternalIndex(int index) { - - Number n = (Number) list.get( index ); - return n; - + protected int size() { + return getN(); } -} +} \ No newline at end of file Index: MixedListUnivariateImpl.java =================================================================== RCS file: MixedListUnivariateImpl.java diff -N MixedListUnivariateImpl.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ MixedListUnivariateImpl.java 8 Jul 2003 04:07:39 -0000 @@ -0,0 +1,105 @@ +/* ==================================================================== + * 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 [EMAIL PROTECTED] + * + * 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 java.util.List; + +import org.apache.commons.math.util.TransformerMap; + +/** + * A mixed List Univariate that Accepts a TransformerMap to map + * objects to doubles. + * @author <a href="mailto:[EMAIL PROTECTED]">Mark R. Diggory</a> + */ +public class MixedListUnivariateImpl + extends ListUnivariateImpl + implements StoreUnivariate { + + /** + * Construct a MixedListUnivariate backed by an existing list + * and using a specified TransformerMap + * + * @param list Existing List + * @param transformer TransformerMap + */ + public MixedListUnivariateImpl(List list, TransformerMap transformer) { + super(list); + this.setTransformerMap(transformer); + } + + /** + * Adds an object to this list. + * @param o Object to add to the list + */ + public void addObject(Object o) { + list.add(o); + } + + /** + * Get the TransformerMap + * @return NumberTransformer + */ + public TransformerMap getTransformerMap() { + return (TransformerMap) transformer; + } + + /** + * Set the TransformerMap + * @param map TransformerMap + */ + public void setTransformerMap(TransformerMap map) { + transformer = map; + } + +} \ No newline at end of file Index: StoreUnivariate.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StoreUnivariate.java,v retrieving revision 1.6 diff -u -r1.6 StoreUnivariate.java --- StoreUnivariate.java 7 Jul 2003 23:25:13 -0000 1.6 +++ StoreUnivariate.java 8 Jul 2003 04:07:39 -0000 @@ -61,50 +61,8 @@ * such as. This additional functionality comes with * a price of increased storage costs. * - * @author <a href="mailto:[EMAIL PROTECTED]">Tim O'Brien</a> */ public interface StoreUnivariate extends Univariate { - - /** - * A LEPTOKURTIC set has a positive kurtosis (a high peak) - */ - public static int LEPTOKURTIC = 1; - - /** - * A MESOKURTIC set has a kurtosis of 0 - it is a normal distribution - */ - public static int MESOKURTIC = 0; - - /** - * A PLATYKURTIC set has a negative kurtosis (a flat "peak") - */ - public static int PLATYKURTIC = -1; - - /** - * Returns the skewness of a given distribution. Skewness is a - * measure of the assymetry of a given distribution. - * - * @return The skewness of this distribution - */ - double getSkewness(); - - /** - * Kurtosis is a measure of the "peakedness" of a distribution - * - * @return the mode - */ - double getKurtosis(); - - /** - * Returns the Kurtosis "classification" a distribution can be - * leptokurtic (high peak), platykurtic (flat peak), - * or mesokurtic (zero kurtosis). - * - * @return A static constant defined in this interface, - * StoredDeviation.LEPTOKURITC, StoredDeviation.PLATYKURTIC, or - * StoredDeviation.MESOKURTIC - */ - int getKurtosisClass(); /** * Returns the current set of values in an array of double primitives. Index: StoreUnivariateImpl.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StoreUnivariateImpl.java,v retrieving revision 1.3 diff -u -r1.3 StoreUnivariateImpl.java --- StoreUnivariateImpl.java 27 Jun 2003 20:58:28 -0000 1.3 +++ StoreUnivariateImpl.java 8 Jul 2003 04:07:39 -0000 @@ -56,81 +56,80 @@ import org.apache.commons.math.util.ContractableDoubleArray; /** - * @author <a href="mailto:[EMAIL PROTECTED]">Tim O'Brien</a> + * */ public class StoreUnivariateImpl extends AbstractStoreUnivariate { - // Use an internal double array - ContractableDoubleArray eDA; - - // Store the windowSize - private int windowSize = Univariate.INFINITE_WINDOW; + /** A contractable double array is used. memory is reclaimed when + * the storage of the array becomes too empty. + */ + protected ContractableDoubleArray eDA; + /** + * Construct a StoreUnivariateImpl + */ public StoreUnivariateImpl() { - // A contractable double array is used. memory is reclaimed when - // the storage of the array becomes too empty. eDA = new ContractableDoubleArray(); } - /* (non-Javadoc) + /** * @see org.apache.commons.math.StoreUnivariate#getValues() */ public double[] getValues() { - double[] copiedArray = new double[ eDA.getNumElements() ]; - System.arraycopy( eDA.getElements(), 0, - copiedArray, 0, eDA.getNumElements()); + double[] copiedArray = new double[eDA.getNumElements()]; + System.arraycopy( + eDA.getElements(), + 0, + copiedArray, + 0, + eDA.getNumElements()); return copiedArray; } - /* (non-Javadoc) + /** * @see org.apache.commons.math.StoreUnivariate#getElement(int) */ public double getElement(int index) { return eDA.getElement(index); } - /* (non-Javadoc) + /** * @see org.apache.commons.math.Univariate#getN() */ public int getN() { return eDA.getNumElements(); } - /* (non-Javadoc) + /** * @see org.apache.commons.math.Univariate#addValue(double) */ public synchronized void addValue(double v) { - if( windowSize != Univariate.INFINITE_WINDOW ) { - if( getN() == windowSize ) { - eDA.addElementRolling( v ); - } else if( getN() < windowSize ) { + if (windowSize != Univariate.INFINITE_WINDOW) { + if (getN() == windowSize) { + eDA.addElementRolling(v); + } else if (getN() < windowSize) { eDA.addElement(v); } else { - String msg = "A window Univariate had more element than " + - "the windowSize. This is an inconsistent state."; - throw new RuntimeException( msg ); + String msg = + "A window Univariate had more element than " + + "the windowSize. This is an inconsistent state."; + throw new RuntimeException(msg); } } else { eDA.addElement(v); } } - /* (non-Javadoc) + /** * @see org.apache.commons.math.Univariate#clear() */ public synchronized void clear() { + super.clear(); eDA.clear(); } - /* (non-Javadoc) - * @see org.apache.commons.math.Univariate#getWindowSize() - */ - public int getWindowSize() { - return windowSize; - } - - /* (non-Javadoc) + /** * @see org.apache.commons.math.Univariate#setWindowSize(int) */ public synchronized void setWindowSize(int windowSize) { @@ -139,8 +138,29 @@ // We need to check to see if we need to discard elements // from the front of the array. If the windowSize is less than // the current number of elements. - if( windowSize < eDA.getNumElements() ) { - eDA.discardFrontElements( eDA.getNumElements() - windowSize); + if (windowSize < eDA.getNumElements()) { + eDA.discardFrontElements(eDA.getNumElements() - windowSize); } } -} + + /** + * @see org.apache.commons.math.stat.AbstractUnivariate#internalValues() + */ + protected double[] internalValues() { + return eDA.getValues(); + } + + /** + * @see org.apache.commons.math.stat.AbstractUnivariate#start() + */ + protected int start() { + return eDA.start(); + } + + /** + * @see org.apache.commons.math.stat.AbstractUnivariate#size() + */ + protected int size() { + return eDA.getNumElements(); + } +} \ No newline at end of file Index: Univariate.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/Univariate.java,v retrieving revision 1.7 diff -u -r1.7 Univariate.java --- Univariate.java 7 Jul 2003 23:25:13 -0000 1.7 +++ Univariate.java 8 Jul 2003 04:07:39 -0000 @@ -69,12 +69,21 @@ * reported statistics will be based on these values<p> * The default windowSize is "infinite" -- i.e., all values added are included * in all computations. - * - * @author <a href="mailto:[EMAIL PROTECTED]">Tim O'Brien</a> - * @version $Revision: 1.7 $ $Date: 2003/07/07 23:25:13 $ - * + * @version $Revision: 1.5 $ $Date: 2003/07/05 19:25:38 $ */ public interface Univariate { + /** + * A LEPTOKURTIC set has a positive kurtosis (a high peak) + */ + public static int LEPTOKURTIC = 1; + /** + * A MESOKURTIC set has a kurtosis of 0 - it is a normal distribution + */ + public static int MESOKURTIC = 0; + /** + * A PLATYKURTIC set has a negative kurtosis (a flat "peak") + */ + public static int PLATYKURTIC = -1; /** * Adds the value to the set of numbers @@ -127,6 +136,17 @@ */ double getKurtosis(); + /** + * Returns the Kurtosis "classification" a distribution can be + * leptokurtic (high peak), platykurtic (flat peak), + * or mesokurtic (zero kurtosis). + * + * @return A static constant defined in this interface, + * StoredDeviation.LEPTOKURITC, StoredDeviation.PLATYKURTIC, or + * StoredDeviation.MESOKURTIC + */ + int getKurtosisClass(); + /** * Returns the maximum of the available values * @return The max or Double.NaN if no values have been added. Index: UnivariateImpl.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/UnivariateImpl.java,v retrieving revision 1.17 diff -u -r1.17 UnivariateImpl.java --- UnivariateImpl.java 7 Jul 2003 23:25:13 -0000 1.17 +++ UnivariateImpl.java 8 Jul 2003 04:07:39 -0000 @@ -54,8 +54,6 @@ package org.apache.commons.math.stat; import java.io.Serializable; - -import org.apache.commons.math.util.DoubleArray; import org.apache.commons.math.util.FixedDoubleArray; /** @@ -66,203 +64,33 @@ * Integers, floats and longs can be added, but they will be converted * to doubles by addValue(). * - * @author <a href="mailto:[EMAIL PROTECTED]">Tim O'Brien</a> - * @author Brent Worden - * @version $Revision: 1.17 $ $Date: 2003/07/07 23:25:13 $ + * @version $Revision: 1.1 $ $Date: 2003/07/04 05:58:16 $ * */ -public class UnivariateImpl implements Univariate, Serializable { - - /** hold the window size **/ - private int windowSize = Univariate.INFINITE_WINDOW; - - /** Just in case the windowSize is not infinite, we need to - * keep an array to remember values 0 to N - */ - private DoubleArray doubleArray; - - /** count of values that have been added */ - private int n = 0; - - /** sum of values that have been added */ - private double sum = Double.NaN; - - /** sum of the square of each value that has been added */ - private double sumsq = Double.NaN; - - /** min of values that have been added */ - private double min = Double.NaN; - - /** max of values that have been added */ - private double max = Double.NaN; - - /** sumLog of values that have been added */ - private double sumLog = Double.NaN; +public class UnivariateImpl + extends AbstractUnivariate + implements Univariate, Serializable { - /** mean of values that have been added */ - private double mean = Double.NaN; - - /** second moment of values that have been added */ - private double m2 = Double.NaN; - - /** third moment of values that have been added */ - private double m3 = Double.NaN; - - /** fourth moment of values that have been added */ - private double m4 = Double.NaN; - - /** variance of values that have been added */ - private double variance = Double.NaN; - - /** skewness of values that have been added */ - private double skewness = Double.NaN; - - /** kurtosis of values that have been added */ - private double kurtosis = Double.NaN; + /** fixed storage */ + private FixedDoubleArray storage = null; /** Creates new univariate with an infinite window */ public UnivariateImpl() { + super(); } - /** Creates a new univariate with a fixed window **/ - public UnivariateImpl(int window) { - setWindowSize(window); - } - - /* (non-Javadoc) - * @see org.apache.commons.math.stat.Univariate#getN() - */ - public int getN() { - return n; - } - - /* (non-Javadoc) - * @see org.apache.commons.math.stat.Univariate#getSum() - */ - public double getSum() { - if (windowSize != Univariate.INFINITE_WINDOW) { - return StatUtils.sum(doubleArray.getElements()); - } - - return sum; - } - - /* (non-Javadoc) - * @see org.apache.commons.math.stat.Univariate#getSumsq() - */ - public double getSumsq() { - if (windowSize != Univariate.INFINITE_WINDOW) { - return StatUtils.sumSq(doubleArray.getElements()); - } - - return sumsq; - } - - /* (non-Javadoc) - * @see org.apache.commons.math.stat.Univariate#getMean() - */ - public double getMean() { - if (windowSize != Univariate.INFINITE_WINDOW) { - return StatUtils.mean(doubleArray.getElements()); - } - - return mean; - } - - /** - * Returns the standard deviation for this collection of values - * @see org.apache.commons.math.stat.Univariate#getStandardDeviation() - */ - public double getStandardDeviation() { - double stdDev = Double.NaN; - if (getN() != 0) { - stdDev = Math.sqrt(getVariance()); - } - return (stdDev); - } - - /** - * Returns the variance of the values that have been added via West's - * algorithm as described by - * <a href="http://doi.acm.org/10.1145/359146.359152">Chan, T. F. and - * J. G. Lewis 1979, <i>Communications of the ACM</i>, - * vol. 22 no. 9, pp. 526-531.</a>. - * - * @return The variance of a set of values. Double.NaN is returned for - * an empty set of values and 0.0 is returned for a <= 1 value set. - */ - public double getVariance() { - if (windowSize != Univariate.INFINITE_WINDOW) { - variance = StatUtils.variance(doubleArray.getElements()); - } - return variance; - } - - /** - * Returns the skewness of the values that have been added as described by - * <a href="http://mathworld.wolfram.com/k-Statistic.html">Equation (6) for k-Statistics</a>. - * - * @return The skew of a set of values. Double.NaN is returned for - * an empty set of values and 0.0 is returned for a <= 2 value set. + /** + * Creates a new univariate with a fixed window + * @param window Window Size */ - public double getSkewness() { - if (windowSize != Univariate.INFINITE_WINDOW) { - return StatUtils.skewness(doubleArray.getElements()); - } - return skewness; + public UnivariateImpl(int window) { + super(window); + storage = new FixedDoubleArray(window); } /** - * Returns the kurtosis of the values that have been added as described by - * <a href="http://mathworld.wolfram.com/k-Statistic.html">Equation (7) for k-Statistics</a>. - * - * @return The kurtosis of a set of values. Double.NaN is returned for - * an empty set of values and 0.0 is returned for a <= 3 value set. - */ - public double getKurtosis() { - if (windowSize != Univariate.INFINITE_WINDOW) { - return StatUtils.kurtosis(doubleArray.getElements()); - } - return kurtosis; - } - - /* (non-Javadoc) - * @see org.apache.commons.math.stat.Univariate#getMax() - */ - public double getMax() { - if (windowSize != Univariate.INFINITE_WINDOW) { - return StatUtils.max(doubleArray.getElements()); - } - return max; - } - - /* (non-Javadoc) - * @see org.apache.commons.math.stat.Univariate#getMin() - */ - public double getMin() { - if (windowSize != Univariate.INFINITE_WINDOW) { - return StatUtils.min(doubleArray.getElements()); - } - return min; - } - - /* (non-Javadoc) - * @see org.apache.commons.math.stat.Univariate#getGeometricMean() - */ - public double getGeometricMean() { - - if (windowSize != Univariate.INFINITE_WINDOW) { - return StatUtils.geometricMean(doubleArray.getElements()); - } - - if (n == 0) { - return Double.NaN; - } else { - return Math.exp(sumLog / (double) n); - } - } - - /* If windowSize is set to Infinite, moments are calculated using the following + * If windowSize is set to Infinite, moments + * are calculated using the following * <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf"> * recursive strategy * </a>. @@ -271,15 +99,15 @@ */ public void addValue(double value) { - if (windowSize != Univariate.INFINITE_WINDOW) { + if (storage != null) { /* then all getters deligate to StatUtils * and this clause simply adds/rolls a value in the storage array */ - if (windowSize == n) { - doubleArray.addElementRolling(value); + if (getWindowSize() == n) { + storage.addElementRolling(value); } else { n++; - doubleArray.addElement(value); + storage.addElement(value); } } else { @@ -287,57 +115,18 @@ * is no need to discard the influence of any single item. */ n++; - - if (n <= 1) { - /* if n <= 1, initialize the sumLog, min, max, mean, variance and pre-variance */ - sumLog = 0.0; - sum = min = max = mean = value; - sumsq = value * value; - variance = m2 = 0.0; - skewness = kurtosis = 0.0; - m2 = m3 = m4 = 0.0; - } else { - /* otherwise calc these values */ - sumLog += Math.log(value); - sum += value; - sumsq += value * value; - min = Math.min(min, value); - max = Math.max(max, value); - - double dev = value - mean; - double v = dev / ((double) n); - double v2 = v * v; - - double n0 = (double) n; - double n1 = (double) (n - 1); - double n2 = (double) (n - 2); - double n3 = (double) (n - 3); - - m4 = - m4 - - (4.0 * v * m3) - + (6.0 * v2 * m2) - + ((n0 * n0) - 3 * n1) * (v2 * v2 * n1 * n0); - - m3 = m3 - (3.0 * v * m2) + (n0 * n1 * n2 * v2 * v); - - m2 += n1 * dev * v; - - mean += v; - - variance = (n <= 1) ? 0.0 : m2 / n1; - - skewness = - (n <= 2 || variance < 10E-20) - ? 0.0 - : (n0 * m3) / (n1 * n2 * Math.sqrt(variance) * variance); - - kurtosis = - (n <= 3 || variance < 10E-20) - ? 0.0 - : (n0 * (n0 + 1) * m4 - 3 * m2 * m2 * n1) - / (n1 * n2 * n3 * variance * variance); - } + min.increment(value); + max.increment(value); + sum.increment(value); + sumsq.increment(value); + sumLog.increment(value); + geoMean.increment(value); + + moment.increment(value); + //mean.increment(value); + //variance.increment(value); + //skewness.increment(value); + //kurtosis.increment(value); } } @@ -350,9 +139,9 @@ public String toString() { StringBuffer outBuffer = new StringBuffer(); outBuffer.append("UnivariateImpl:\n"); - outBuffer.append("n: " + n + "\n"); - outBuffer.append("min: " + min + "\n"); - outBuffer.append("max: " + max + "\n"); + outBuffer.append("n: " + getN() + "\n"); + outBuffer.append("min: " + getMin() + "\n"); + outBuffer.append("max: " + getMax() + "\n"); outBuffer.append("mean: " + getMean() + "\n"); outBuffer.append("std dev: " + getStandardDeviation() + "\n"); outBuffer.append("skewness: " + getSkewness() + "\n"); @@ -360,33 +149,35 @@ return outBuffer.toString(); } - /* (non-Javadoc) + /** * @see org.apache.commons.math.Univariate#clear() */ public void clear() { - this.n = 0; - this.min = this.max = Double.NaN; - this.sumLog = this.mean = Double.NaN; - this.variance = this.skewness = this.kurtosis = Double.NaN; - this.m2 = this.m3 = this.m4 = Double.NaN; - if (doubleArray != null) - doubleArray = new FixedDoubleArray(windowSize); + super.clear(); + if (getWindowSize() != INFINITE_WINDOW) { + storage = new FixedDoubleArray(getWindowSize()); + } + } + + /** + * @see org.apache.commons.math.stat.AbstractUnivariate#internalValues() + */ + protected double[] internalValues() { + return storage == null ? null : storage.getValues(); } - /* (non-Javadoc) - * @see org.apache.commons.math.Univariate#getWindowSize() + /** + * @see org.apache.commons.math.stat.AbstractUnivariate#start() */ - public int getWindowSize() { - return windowSize; + protected int start() { + return storage.start(); } - /* (non-Javadoc) - * @see org.apache.commons.math.Univariate#setWindowSize(int) + /** + * @see org.apache.commons.math.stat.AbstractUnivariate#size() */ - public void setWindowSize(int windowSize) { - clear(); - this.windowSize = windowSize; - doubleArray = new FixedDoubleArray(windowSize); + protected int size() { + return storage.getNumElements(); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
