Consolidated AbstractDescriptiveStatistics into DescriptiveStatistics. Added Serialization interfaces where required.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141258 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2004-06-01 21:34:35 +00:00
parent 82b4bc1943
commit 01f63cf801
7 changed files with 112 additions and 204 deletions

View File

@ -1,179 +0,0 @@
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import java.util.Arrays;
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.rank.Percentile;
import org.apache.commons.math.stat.univariate.summary.Sum;
import org.apache.commons.math.stat.univariate.summary.SumOfSquares;
/**
* Abstract superclass for DescriptiveStatistics implementations.
*
* @version $Revision: 1.3 $ $Date: 2004/05/23 00:56:15 $
*/
public abstract class AbstractDescriptiveStatistics
extends DescriptiveStatistics {
/**
* Create an AbstractDescriptiveStatistics
*/
protected AbstractDescriptiveStatistics() {
super();
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getSum()
*/
public double getSum() {
return apply(new Sum());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getSumsq()
*/
public double getSumsq() {
return apply(new SumOfSquares());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getMean()
*/
public double getMean() {
return apply(new Mean());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getStandardDeviation()
*/
public double getStandardDeviation() {
double stdDev = Double.NaN;
if (getN() > 0) {
if (getN() > 1) {
stdDev = Math.sqrt(getVariance());
} else {
stdDev = 0.0;
}
}
return (stdDev);
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getVariance()
*/
public double getVariance() {
return apply(new Variance());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getSkewness()
*/
public double getSkewness() {
return apply(new Skewness());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getKurtosis()
*/
public double getKurtosis() {
return apply(new Kurtosis());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getMax()
*/
public double getMax() {
return apply(new Max());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getMin()
*/
public double getMin() {
return apply(new Min());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getGeometricMean()
*/
public double getGeometricMean() {
return apply(new GeometricMean());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getPercentile(double)
*/
public double getPercentile(double p) {
return apply(new Percentile(p));
}
/**
* 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: " + 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");
outBuffer.append("kurtosis: " + getKurtosis() + "\n");
return outBuffer.toString();
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getSortedValues()
*/
public double[] getSortedValues() {
double[] sort = getValues();
Arrays.sort(sort);
return sort;
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#addValue(double)
*/
public abstract void addValue(double value);
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getValues()
*/
public abstract double[] getValues();
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getElement(int)
*/
public abstract double getElement(int index);
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#apply(UnivariateStatistic)
*/
public abstract double apply(UnivariateStatistic stat);
}

View File

@ -15,18 +15,23 @@
*/
package org.apache.commons.math.stat.univariate;
import java.io.Serializable;
/**
*
* Abstract Implementation for the {@link StorelessUnivariateStatistic} interface.
* <p>
* Provides a default <code>evaluate()</code> implementation.
*
* @version $Revision: 1.14 $ $Date: 2004/03/21 21:57:18 $
* @version $Revision: 1.15 $ $Date: 2004/06/01 21:34:35 $
*/
public abstract class AbstractStorelessUnivariateStatistic
extends AbstractUnivariateStatistic
implements StorelessUnivariateStatistic {
implements StorelessUnivariateStatistic, Serializable {
/** Serialization UID */
static final long serialVersionUID = -44915725420072521L;
/**
* This default implementation just calls {@link #increment} in a loop over the input array and
* then {@link #getResult} to compute the return value.

View File

@ -15,14 +15,19 @@
*/
package org.apache.commons.math.stat.univariate;
import java.io.Serializable;
/**
* Abstract Implementation for UnivariateStatistics.
* Provides the ability to extend polymophically so that
* indiviual statistics do not need to implement these methods.
* @version $Revision: 1.16 $ $Date: 2004/04/27 16:42:34 $
* @version $Revision: 1.17 $ $Date: 2004/06/01 21:34:35 $
*/
public abstract class AbstractUnivariateStatistic
implements UnivariateStatistic {
implements UnivariateStatistic, Serializable {
/** Serialization UID */
static final long serialVersionUID = -8007759382851708045L;
/**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[])

View File

@ -16,17 +16,31 @@
package org.apache.commons.math.stat.univariate;
import java.io.Serializable;
import java.util.Arrays;
import org.apache.commons.discovery.tools.DiscoverClass;
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.rank.Percentile;
import org.apache.commons.math.stat.univariate.summary.Sum;
import org.apache.commons.math.stat.univariate.summary.SumOfSquares;
/**
* Abstract factory class for univariate statistical summaries.
*
* @version $Revision: 1.5 $ $Date: 2004/05/23 00:56:15 $
* @version $Revision: 1.6 $ $Date: 2004/06/01 21:34:35 $
*/
public abstract class DescriptiveStatistics implements Serializable, StatisticalSummary {
public abstract class DescriptiveStatistics implements StatisticalSummary, Serializable {
/** Serialization UID */
static final long serialVersionUID = 5188298269533339922L;
/**
* Create an instance of a <code>DescriptiveStatistics</code>
* @param cls the type of <code>DescriptiveStatistics</code> object to
@ -94,7 +108,9 @@ public abstract class DescriptiveStatistics implements Serializable, Statistical
* arithmetic mean </a> of the available values
* @return The mean or Double.NaN if no values have been added.
*/
public abstract double getMean();
public double getMean() {
return apply(new Mean());
}
/**
* Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
@ -102,21 +118,35 @@ public abstract class DescriptiveStatistics implements Serializable, Statistical
* @return The geometricMean, Double.NaN if no values have been added,
* or if the productof the available values is less than or equal to 0.
*/
public abstract double getGeometricMean();
public double getGeometricMean() {
return apply(new GeometricMean());
}
/**
* Returns the variance of the available values.
* @return The variance, Double.NaN if no values have been added
* or 0.0 for a single value set.
*/
public abstract double getVariance();
public double getVariance() {
return apply(new Variance());
}
/**
* Returns the standard deviation of the available values.
* @return The standard deviation, Double.NaN if no values have been added
* or 0.0 for a single value set.
*/
public abstract double 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 skewness of the available values. Skewness is a
@ -124,7 +154,9 @@ public abstract class DescriptiveStatistics implements Serializable, Statistical
* @return The skewness, Double.NaN if no values have been added
* or 0.0 for a value set &lt;=2.
*/
public abstract double getSkewness();
public double getSkewness() {
return apply(new Skewness());
}
/**
* Returns the Kurtosis of the available values. Kurtosis is a
@ -132,19 +164,25 @@ public abstract class DescriptiveStatistics implements Serializable, Statistical
* @return The kurtosis, Double.NaN if no values have been added, or 0.0
* for a value set &lt;=3.
*/
public abstract double getKurtosis();
public double getKurtosis() {
return apply(new Kurtosis());
}
/**
* Returns the maximum of the available values
* @return The max or Double.NaN if no values have been added.
*/
public abstract double getMax();
public double getMax() {
return apply(new Max());
}
/**
* Returns the minimum of the available values
* @return The min or Double.NaN if no values have been added.
*/
public abstract double getMin();
public double getMin() {
return apply(new Min());
}
/**
* Returns the number of available values
@ -156,14 +194,18 @@ public abstract class DescriptiveStatistics implements Serializable, Statistical
* Returns the sum of the values that have been added to Univariate.
* @return The sum or Double.NaN if no values have been added
*/
public abstract double getSum();
public double getSum() {
return apply(new Sum());
}
/**
* Returns the sum of the squares of the available values.
* @return The sum of the squares or Double.NaN if no
* values have been added.
*/
public abstract double getSumsq();
public double getSumsq() {
return apply(new SumOfSquares());
}
/**
* Resets all statistics and storage
@ -208,7 +250,11 @@ public abstract class DescriptiveStatistics implements Serializable, Statistical
* @return returns the current set of
* numbers sorted in ascending order
*/
public abstract double[] getSortedValues();
public double[] getSortedValues() {
double[] sort = getValues();
Arrays.sort(sort);
return sort;
}
/**
* Returns the element at the specified index
@ -234,8 +280,29 @@ public abstract class DescriptiveStatistics implements Serializable, Statistical
* @return An estimate for the pth percentile of the stored data
* values
*/
public abstract double getPercentile(double p);
public double getPercentile(double p) {
return apply(new Percentile(p));
}
/**
* 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: " + 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");
outBuffer.append("kurtosis: " + getKurtosis() + "\n");
return outBuffer.toString();
}
/**
* Apply the given statistic to the data associated with this set of statistics.
* @param stat the statistic to apply

View File

@ -23,10 +23,13 @@ import org.apache.commons.math.util.ContractableDoubleArray;
* Default implementation of
* {@link org.apache.commons.math.stat.univariate.DescriptiveStatistics}.
*
* @version $Revision: 1.5 $ $Date: 2004/05/23 00:30:01 $
* @version $Revision: 1.6 $ $Date: 2004/06/01 21:34:35 $
*/
public class DescriptiveStatisticsImpl extends AbstractDescriptiveStatistics implements Serializable {
public class DescriptiveStatisticsImpl extends DescriptiveStatistics implements Serializable {
/** Serializable version identifier */
static final long serialVersionUID = -1868088725461221010L;
/** hold the window size **/
protected int windowSize;

View File

@ -22,10 +22,13 @@ import org.apache.commons.discovery.tools.DiscoverClass;
/**
* Abstract factory class for univariate statistical summaries.
*
* @version $Revision: 1.5 $ $Date: 2004/05/19 14:16:31 $
* @version $Revision: 1.6 $ $Date: 2004/06/01 21:34:35 $
*/
public abstract class SummaryStatistics implements Serializable, StatisticalSummary {
public abstract class SummaryStatistics implements StatisticalSummary, Serializable {
/** Serialization UID */
static final long serialVersionUID = -6400596334135654825L;
/**
* Create an instance of a <code>SummaryStatistics</code>
* @param cls the type of <code>SummaryStatistics</code> object to

View File

@ -15,6 +15,7 @@
*/
package org.apache.commons.math.stat.univariate;
import java.io.Serializable;
import org.apache.commons.math.stat.univariate.moment.SecondMoment;
import org.apache.commons.math.stat.univariate.moment.GeometricMean;
import org.apache.commons.math.stat.univariate.moment.Mean;
@ -28,9 +29,12 @@ import org.apache.commons.math.stat.univariate.summary.SumOfSquares;
/**
* Provides a default {@link SummaryStatistics} implementation.
*
* @version $Revision: 1.2 $ $Date: 2004/04/27 04:37:59 $
* @version $Revision: 1.3 $ $Date: 2004/06/01 21:34:35 $
*/
public class SummaryStatisticsImpl extends SummaryStatistics {
public class SummaryStatisticsImpl extends SummaryStatistics implements Serializable {
/** Serializable version identifier */
static final long serialVersionUID = 8787174276883311692L;
/** count of values that have been added */
protected long n = 0;