Refactored statistical aggregates to separate stored, storeless implementations. Changed internal sample size counters to longs.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141064 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
985aad0b1a
commit
1b96f28e41
|
@ -59,7 +59,7 @@ import java.io.File;
|
|||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
import org.apache.commons.math.stat.SummaryStatistics;
|
||||
|
||||
/**
|
||||
* Represents an <a href="http://random.mat.sbg.ac.at/~ste/dipl/node11.html">
|
||||
|
@ -81,7 +81,7 @@ import org.apache.commons.math.stat.DescriptiveStatistics;
|
|||
* build grouped frequnecy histograms representing the input data or to
|
||||
* generate random values "like" those in the input file -- i.e., the values
|
||||
* generated will follow the distribution of the values in the file.
|
||||
* @version $Revision: 1.12 $ $Date: 2004/01/15 05:22:08 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public interface EmpiricalDistribution {
|
||||
|
||||
|
@ -123,7 +123,7 @@ public interface EmpiricalDistribution {
|
|||
* @return the sample statistics
|
||||
* @throws IllegalStateException if the distribution has not been loaded
|
||||
*/
|
||||
DescriptiveStatistics getSampleStats() throws IllegalStateException;
|
||||
SummaryStatistics getSampleStats() throws IllegalStateException;
|
||||
|
||||
/**
|
||||
* Loads a saved distribution from a file.
|
||||
|
|
|
@ -65,7 +65,7 @@ import java.io.InputStreamReader;
|
|||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
||||
import org.apache.commons.math.stat.SummaryStatistics;
|
||||
|
||||
/**
|
||||
* Implements <code>EmpiricalDistribution</code> interface. This implementation
|
||||
|
@ -92,7 +92,7 @@ import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
|||
* entry per line.</li>
|
||||
* </ol></p>
|
||||
*
|
||||
* @version $Revision: 1.13 $ $Date: 2004/01/15 05:22:08 $
|
||||
* @version $Revision: 1.14 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistribution {
|
||||
|
||||
|
@ -101,7 +101,7 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
|
|||
private ArrayList binStats = null;
|
||||
|
||||
/** Sample statistics */
|
||||
DescriptiveStatistics sampleStats = null;
|
||||
SummaryStatistics sampleStats = null;
|
||||
|
||||
/** number of bins */
|
||||
private int binCount = 1000;
|
||||
|
@ -175,7 +175,7 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
|
|||
private void computeStats(BufferedReader in) throws IOException {
|
||||
String str = null;
|
||||
double val = 0.0;
|
||||
sampleStats = new StorelessDescriptiveStatisticsImpl();
|
||||
sampleStats = SummaryStatistics.newInstance();
|
||||
while ((str = in.readLine()) != null) {
|
||||
val = new Double(str).doubleValue();
|
||||
sampleStats.addValue(val);
|
||||
|
@ -205,7 +205,7 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
|
|||
binStats.clear();
|
||||
}
|
||||
for (int i = 0; i < binCount; i++) {
|
||||
DescriptiveStatistics stats = new StorelessDescriptiveStatisticsImpl();
|
||||
SummaryStatistics stats = SummaryStatistics.newInstance();
|
||||
binStats.add(i,stats);
|
||||
}
|
||||
|
||||
|
@ -224,7 +224,7 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
|
|||
}
|
||||
if (val <= binUpperBounds[i]) {
|
||||
found = true;
|
||||
DescriptiveStatistics stats = (DescriptiveStatistics)binStats.get(i);
|
||||
SummaryStatistics stats = (SummaryStatistics)binStats.get(i);
|
||||
stats.addValue(val);
|
||||
}
|
||||
i++;
|
||||
|
@ -236,11 +236,11 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
|
|||
// Assign upperBounds based on bin counts
|
||||
upperBounds = new double[binCount];
|
||||
upperBounds[0] =
|
||||
((double)((DescriptiveStatistics)binStats.get(0)).getN())/
|
||||
((double)((SummaryStatistics)binStats.get(0)).getN())/
|
||||
(double)sampleStats.getN();
|
||||
for (int i = 1; i < binCount-1; i++) {
|
||||
upperBounds[i] = upperBounds[i-1] +
|
||||
((double)((DescriptiveStatistics)binStats.get(i)).getN())/
|
||||
((double)((SummaryStatistics)binStats.get(i)).getN())/
|
||||
(double)sampleStats.getN();
|
||||
}
|
||||
upperBounds[binCount-1] = 1.0d;
|
||||
|
@ -263,7 +263,7 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
|
|||
// Use this to select the bin and generate a Gaussian within the bin
|
||||
for (int i = 0; i < binCount; i++) {
|
||||
if (x <= upperBounds[i]) {
|
||||
DescriptiveStatistics stats = (DescriptiveStatistics)binStats.get(i);
|
||||
SummaryStatistics stats = (SummaryStatistics)binStats.get(i);
|
||||
if (stats.getN() > 0) {
|
||||
if (stats.getStandardDeviation() > 0) { // more than one obs
|
||||
return randomData.nextGaussian
|
||||
|
@ -295,7 +295,7 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
|
|||
throw new UnsupportedOperationException("Not Implemented yet :-(");
|
||||
}
|
||||
|
||||
public DescriptiveStatistics getSampleStats() {
|
||||
public SummaryStatistics getSampleStats() {
|
||||
return sampleStats;
|
||||
}
|
||||
|
||||
|
|
|
@ -78,7 +78,7 @@ import java.net.MalformedURLException;
|
|||
* standard deviation = <code>sigma</code></li>
|
||||
* <li> CONSTANT_MODE -- returns <code>mu</code> every time.</li></ul>
|
||||
*
|
||||
* @version $Revision: 1.10 $ $Date: 2004/01/15 05:22:08 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/25 21:30:41 $
|
||||
*
|
||||
*/
|
||||
public class ValueServer implements Serializable {
|
||||
|
@ -240,7 +240,6 @@ public class ValueServer implements Serializable {
|
|||
* Sets the <code>valuesFileURL</code> using a string URL representation
|
||||
* @param url String representation for new valuesFileURL.
|
||||
* @throws MalformedURLException if url is not well formed
|
||||
* @deprecated use {@link #setValuesFileURL(URL)} to be removed before 0.1 release
|
||||
*/
|
||||
public void setValuesFileURL(String url) throws MalformedURLException {
|
||||
this.valuesFileURL = new URL(url);
|
||||
|
|
|
@ -55,40 +55,157 @@ package org.apache.commons.math.stat;
|
|||
|
||||
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;
|
||||
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
|
||||
|
||||
/**
|
||||
* Extends {@link AbstractStorelessDescriptiveStatistics} to include univariate statistics
|
||||
* that may require access to the full set of sample values.
|
||||
* @version $Revision: 1.2 $ $Date: 2004/01/18 03:45:02 $
|
||||
* Abstract superclass for DescriptiveStatistics implementations.
|
||||
*
|
||||
* @version $Revision: 1.3 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public abstract class AbstractDescriptiveStatistics
|
||||
extends AbstractStorelessDescriptiveStatistics {
|
||||
|
||||
/** Percentile */
|
||||
protected Percentile percentile = new Percentile(50);
|
||||
extends DescriptiveStatistics {
|
||||
|
||||
/**
|
||||
* Create an AbstractDescriptiveStatistics
|
||||
*/
|
||||
public AbstractDescriptiveStatistics() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an AbstractDescriptiveStatistics with a specific Window
|
||||
* @param window WindowSIze for stat calculation
|
||||
*/
|
||||
public AbstractDescriptiveStatistics(int window) {
|
||||
super(window);
|
||||
public AbstractDescriptiveStatistics(int window) {
|
||||
setWindowSize(window);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getPercentile(double)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getSum()
|
||||
*/
|
||||
public double getSum() {
|
||||
return apply(new Sum());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getSumsq()
|
||||
*/
|
||||
public double getSumsq() {
|
||||
return apply(new SumOfSquares());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getMean()
|
||||
*/
|
||||
public double getMean() {
|
||||
return apply(new Mean());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.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.DescriptiveStatistics#getVariance()
|
||||
*/
|
||||
public double getVariance() {
|
||||
return apply(new Variance());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getSkewness()
|
||||
*/
|
||||
public double getSkewness() {
|
||||
return apply(new Skewness());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getKurtosis()
|
||||
*/
|
||||
public double getKurtosis() {
|
||||
return apply(new Kurtosis());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getKurtosisClass()
|
||||
*/
|
||||
public int getKurtosisClass() {
|
||||
int kClass = MESOKURTIC;
|
||||
|
||||
double kurtosis = getKurtosis();
|
||||
if (kurtosis > 0) {
|
||||
kClass = LEPTOKURTIC;
|
||||
} else if (kurtosis < 0) {
|
||||
kClass = PLATYKURTIC;
|
||||
}
|
||||
return (kClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getMax()
|
||||
*/
|
||||
public double getMax() {
|
||||
return apply(new Max());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getMin()
|
||||
*/
|
||||
public double getMin() {
|
||||
return apply(new Min());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getGeometricMean()
|
||||
*/
|
||||
public double getGeometricMean() {
|
||||
return apply(new GeometricMean());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getPercentile()
|
||||
*/
|
||||
public double getPercentile(double p) {
|
||||
percentile.setPercentile(p);
|
||||
return apply(percentile);
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -101,7 +218,7 @@ public abstract class AbstractDescriptiveStatistics
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#addValue(double)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#addValue(double)
|
||||
*/
|
||||
public abstract void addValue(double value);
|
||||
|
||||
|
@ -110,12 +227,14 @@ public abstract class AbstractDescriptiveStatistics
|
|||
*/
|
||||
public abstract double[] getValues();
|
||||
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getElement(int)
|
||||
*/
|
||||
public abstract double getElement(int index);
|
||||
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#apply(UnivariateStatistic)
|
||||
*/
|
||||
public abstract double apply(UnivariateStatistic stat);
|
||||
|
||||
}
|
||||
|
|
|
@ -57,12 +57,14 @@ import java.io.Serializable;
|
|||
|
||||
import org.apache.commons.discovery.tools.DiscoverClass;
|
||||
|
||||
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
|
||||
|
||||
/**
|
||||
* Abstract factory class for univariate statistical summaries.
|
||||
*
|
||||
* @version $Revision: 1.3 $ $Date: 2004/01/18 03:45:02 $
|
||||
* @version $Revision: 1.4 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public abstract class DescriptiveStatistics implements Serializable{
|
||||
public abstract class DescriptiveStatistics implements Serializable, StatisticalSummary {
|
||||
|
||||
/**
|
||||
* Create an instance of a <code>DescriptiveStatistics</code>
|
||||
|
@ -195,7 +197,7 @@ public abstract class DescriptiveStatistics implements Serializable{
|
|||
* Returns the number of available values
|
||||
* @return The number of available values
|
||||
*/
|
||||
public abstract int getN();
|
||||
public abstract long getN();
|
||||
|
||||
/**
|
||||
* Returns the sum of the values that have been added to Univariate.
|
||||
|
@ -280,4 +282,11 @@ public abstract class DescriptiveStatistics implements Serializable{
|
|||
*/
|
||||
public abstract double getPercentile(double p);
|
||||
|
||||
/**
|
||||
* Apply the given statistic to the data associated with this set of statistics.
|
||||
* @param stat the statistic to apply
|
||||
* @return the computed value of the statistic.
|
||||
*/
|
||||
public abstract double apply(UnivariateStatistic stat);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -55,26 +55,44 @@ package org.apache.commons.math.stat;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.math.stat.univariate.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
|
||||
import org.apache.commons.math.util.ContractableDoubleArray;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.2 $ $Date: 2003/11/19 03:28:23 $
|
||||
* @version $Revision: 1.3 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class DescriptiveStatisticsImpl extends AbstractDescriptiveStatistics implements Serializable {
|
||||
|
||||
/** A contractable double array is used. memory is reclaimed when
|
||||
* the storage of the array becomes too empty.
|
||||
/** hold the window size **/
|
||||
protected int windowSize = INFINITE_WINDOW;
|
||||
|
||||
/**
|
||||
* Stored data values
|
||||
*/
|
||||
protected ContractableDoubleArray eDA;
|
||||
|
||||
/**
|
||||
* Construct a DescriptiveStatisticsImpl
|
||||
* Construct a DescriptiveStatisticsImpl with infinite window
|
||||
*/
|
||||
public DescriptiveStatisticsImpl() {
|
||||
super();
|
||||
eDA = new ContractableDoubleArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a DescriptiveStatisticsImpl with finite window
|
||||
*/
|
||||
public DescriptiveStatisticsImpl(int window) {
|
||||
super(window);
|
||||
eDA = new ContractableDoubleArray();
|
||||
}
|
||||
|
||||
public int getWindowSize() {
|
||||
return windowSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getValues()
|
||||
*/
|
||||
|
@ -90,6 +108,15 @@ public class DescriptiveStatisticsImpl extends AbstractDescriptiveStatistics imp
|
|||
return copiedArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getSortedValues()
|
||||
*/
|
||||
public double[] getSortedValues() {
|
||||
double[] sort = getValues();
|
||||
Arrays.sort(sort);
|
||||
return sort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getElement(int)
|
||||
*/
|
||||
|
@ -98,14 +125,14 @@ public class DescriptiveStatisticsImpl extends AbstractDescriptiveStatistics imp
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#getN()
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getN()
|
||||
*/
|
||||
public int getN() {
|
||||
public long getN() {
|
||||
return eDA.getNumElements();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#addValue(double)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#addValue(double)
|
||||
*/
|
||||
public synchronized void addValue(double v) {
|
||||
if (windowSize != INFINITE_WINDOW) {
|
||||
|
@ -125,15 +152,14 @@ public class DescriptiveStatisticsImpl extends AbstractDescriptiveStatistics imp
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#clear()
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#clear()
|
||||
*/
|
||||
public synchronized void clear() {
|
||||
super.clear();
|
||||
eDA.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#setWindowSize(int)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#setWindowSize(int)
|
||||
*/
|
||||
public synchronized void setWindowSize(int windowSize) {
|
||||
this.windowSize = windowSize;
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2004 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 acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements 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 name 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;
|
||||
|
||||
/**
|
||||
* Reporting interface for basic univariate statistics.
|
||||
*
|
||||
* @version $Revision: 1.1 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public interface StatisticalSummary {
|
||||
/**
|
||||
* Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
|
||||
* arithmetic mean </a> of the available values
|
||||
* @return The mean or Double.NaN if no values have been added.
|
||||
*/
|
||||
public abstract double getMean();
|
||||
/**
|
||||
* 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();
|
||||
/**
|
||||
* 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();
|
||||
/**
|
||||
* Returns the maximum of the available values
|
||||
* @return The max or Double.NaN if no values have been added.
|
||||
*/
|
||||
public abstract double getMax();
|
||||
/**
|
||||
* Returns the minimum of the available values
|
||||
* @return The min or Double.NaN if no values have been added.
|
||||
*/
|
||||
public abstract double getMin();
|
||||
/**
|
||||
* Returns the number of available values
|
||||
* @return The number of available values
|
||||
*/
|
||||
public abstract long getN();
|
||||
/**
|
||||
* 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();
|
||||
}
|
|
@ -1,208 +0,0 @@
|
|||
/* ====================================================================
|
||||
* 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 acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements 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 name 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.io.Serializable;
|
||||
|
||||
import org.apache.commons.math.stat.univariate.*;
|
||||
import org.apache.commons.math.util.FixedDoubleArray;
|
||||
|
||||
/**
|
||||
*
|
||||
* Accumulates univariate statistics for values fed in
|
||||
* through the addValue() method. Does not store raw data values.
|
||||
* All data are represented internally as doubles.
|
||||
* Integers, floats and longs can be added, but they will be converted
|
||||
* to doubles by addValue().
|
||||
*
|
||||
* @version $Revision: 1.2 $ $Date: 2003/11/19 03:28:23 $
|
||||
*/
|
||||
public class StorelessDescriptiveStatisticsImpl extends AbstractStorelessDescriptiveStatistics implements Serializable {
|
||||
|
||||
/** fixed storage */
|
||||
private FixedDoubleArray storage = null;
|
||||
|
||||
/** Creates new univariate with an infinite window */
|
||||
public StorelessDescriptiveStatisticsImpl() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new univariate with a fixed window
|
||||
* @param window Window Size
|
||||
*/
|
||||
public StorelessDescriptiveStatisticsImpl(int window) {
|
||||
super(window);
|
||||
storage = new FixedDoubleArray(window);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>.
|
||||
* Otherwise, stat methods delegate to StatUtils.
|
||||
* @see org.apache.commons.math.stat.Univariate#addValue(double)
|
||||
*/
|
||||
public void addValue(double value) {
|
||||
|
||||
if (storage != null) {
|
||||
/* then all getters deligate to StatUtils
|
||||
* and this clause simply adds/rolls a value in the storage array
|
||||
*/
|
||||
if (getWindowSize() == n) {
|
||||
storage.addElementRolling(value);
|
||||
} else {
|
||||
n++;
|
||||
storage.addElement(value);
|
||||
}
|
||||
|
||||
} else {
|
||||
/* If the windowSize is infinite don't store any values and there
|
||||
* is no need to discard the influence of any single item.
|
||||
*/
|
||||
n++;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
super.clear();
|
||||
if (getWindowSize() != INFINITE_WINDOW) {
|
||||
storage = new FixedDoubleArray(getWindowSize());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the given statistic to this univariate collection.
|
||||
* @param stat the statistic to apply
|
||||
* @return the computed value of the statistic.
|
||||
*/
|
||||
public double apply(UnivariateStatistic stat) {
|
||||
|
||||
if (storage != null) {
|
||||
return stat.evaluate(
|
||||
storage.getValues(),
|
||||
storage.start(),
|
||||
storage.getNumElements());
|
||||
} else if (stat instanceof StorelessUnivariateStatistic) {
|
||||
return ((StorelessUnivariateStatistic) stat).getResult();
|
||||
}
|
||||
|
||||
return Double.NaN;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getValues()
|
||||
*/
|
||||
public double[] getValues() {
|
||||
throw new UnsupportedOperationException("Only Available with Finite Window");
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getSortedValues()
|
||||
*/
|
||||
public double[] getSortedValues() {
|
||||
throw new UnsupportedOperationException("Only Available with Finite Window");
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getElement(int)
|
||||
*/
|
||||
public double getElement(int index) {
|
||||
throw new UnsupportedOperationException("Only Available with Finite Window");
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getPercentile(double)
|
||||
*/
|
||||
public double getPercentile(double p) {
|
||||
throw new UnsupportedOperationException("Only Available with Finite Window");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,170 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2004 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 acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements 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 name 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.io.Serializable;
|
||||
|
||||
import org.apache.commons.discovery.tools.DiscoverClass;
|
||||
|
||||
/**
|
||||
* Abstract factory class for univariate statistical summaries.
|
||||
*
|
||||
* @version $Revision: 1.1 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public abstract class SummaryStatistics implements Serializable, StatisticalSummary{
|
||||
|
||||
/**
|
||||
* Create an instance of a <code>SummaryStatistics</code>
|
||||
* @return a new factory.
|
||||
*/
|
||||
public static SummaryStatistics newInstance(String cls) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
|
||||
return newInstance(Class.forName(cls));
|
||||
}
|
||||
/**
|
||||
* Create an instance of a <code>DescriptiveStatistics</code>
|
||||
* @return a new factory.
|
||||
*/
|
||||
public static SummaryStatistics newInstance(Class cls) throws InstantiationException, IllegalAccessException {
|
||||
return (SummaryStatistics)cls.newInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of a <code>DescriptiveStatistics</code>
|
||||
* @return a new factory.
|
||||
*/
|
||||
public static SummaryStatistics newInstance() {
|
||||
SummaryStatistics factory = null;
|
||||
try {
|
||||
DiscoverClass dc = new DiscoverClass();
|
||||
factory = (SummaryStatistics) dc.newInstance(
|
||||
SummaryStatistics.class,
|
||||
"org.apache.commons.math.stat.SummaryStatisticsImpl");
|
||||
} catch(Exception ex) {
|
||||
// ignore as default implementation will be used.
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the value to the data to be summarized
|
||||
* @param v the value to be added
|
||||
*/
|
||||
public abstract void addValue(double v);
|
||||
|
||||
/**
|
||||
* Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
|
||||
* arithmetic mean </a> of the available values
|
||||
* @return The mean or Double.NaN if no values have been added.
|
||||
*/
|
||||
public abstract double getMean();
|
||||
|
||||
/**
|
||||
* Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
|
||||
* geometric mean </a> of the available values
|
||||
* @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();
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
/**
|
||||
* Returns the maximum of the available values
|
||||
* @return The max or Double.NaN if no values have been added.
|
||||
*/
|
||||
public abstract double getMax();
|
||||
|
||||
/**
|
||||
* Returns the minimum of the available values
|
||||
* @return The min or Double.NaN if no values have been added.
|
||||
*/
|
||||
public abstract double getMin();
|
||||
|
||||
/**
|
||||
* Returns the number of available values
|
||||
* @return The number of available values
|
||||
*/
|
||||
public abstract long getN();
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
/**
|
||||
* Resets all statistics
|
||||
*/
|
||||
public abstract void clear();
|
||||
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -53,12 +53,10 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
|
||||
import org.apache.commons.math.stat.univariate.moment.FourthMoment;
|
||||
import org.apache.commons.math.stat.univariate.moment.SecondMoment;
|
||||
import org.apache.commons.math.stat.univariate.moment.FirstMoment;
|
||||
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;
|
||||
|
@ -67,21 +65,20 @@ import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
|
|||
import org.apache.commons.math.stat.univariate.summary.SumOfSquares;
|
||||
|
||||
/**
|
||||
* Provides a default {@link DescriptiveStatistics} implementation, including only statistics
|
||||
* that can be computed in one pass through the data without storing the full set of sample
|
||||
* data values.
|
||||
* @version $Revision: 1.2 $ $Date: 2004/01/18 03:45:02 $
|
||||
* Provides a default {@link SummaryStatistics} implementation.
|
||||
*
|
||||
* @version $Revision: 1.1 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public abstract class AbstractStorelessDescriptiveStatistics extends DescriptiveStatistics {
|
||||
|
||||
/** hold the window size **/
|
||||
protected int windowSize = INFINITE_WINDOW;
|
||||
public class SummaryStatisticsImpl extends SummaryStatistics {
|
||||
|
||||
/** count of values that have been added */
|
||||
protected int n = 0;
|
||||
protected long n = 0;
|
||||
|
||||
/** FourthMoment is used in calculating mean, variance,skew and kurtosis */
|
||||
protected FourthMoment moment = null;
|
||||
/** FirstMoment is used to compute the mean */
|
||||
protected FirstMoment firstMoment = null;
|
||||
|
||||
/** SecondMoment is used to compute the variance */
|
||||
protected SecondMoment secondMoment = null;
|
||||
|
||||
/** sum of values that have been added */
|
||||
protected Sum sum = null;
|
||||
|
@ -107,63 +104,41 @@ public abstract class AbstractStorelessDescriptiveStatistics extends Descriptive
|
|||
/** 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 AbstractStorelessDescriptiveStatistics
|
||||
* Construct a SummaryStatistics
|
||||
*/
|
||||
public AbstractStorelessDescriptiveStatistics() {
|
||||
super();
|
||||
|
||||
public SummaryStatisticsImpl() {
|
||||
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);
|
||||
secondMoment = new SecondMoment();
|
||||
firstMoment = new FirstMoment();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an AbstractStorelessDescriptiveStatistics with a window
|
||||
* @param window The Window Size
|
||||
* Add a value to the data
|
||||
*
|
||||
* @param value the value to add
|
||||
*/
|
||||
public AbstractStorelessDescriptiveStatistics(int window) {
|
||||
this();
|
||||
setWindowSize(window);
|
||||
public void addValue(double value) {
|
||||
sum.increment(value);
|
||||
sumsq.increment(value);
|
||||
min.increment(value);
|
||||
max.increment(value);
|
||||
sumLog.increment(value);
|
||||
geoMean.increment(value);
|
||||
firstMoment.increment(value);
|
||||
secondMoment.increment(value);
|
||||
n++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the given statistic to this univariate collection.
|
||||
* @param stat the statistic to apply
|
||||
* @return the computed value of the statistic.
|
||||
*/
|
||||
public abstract double apply(UnivariateStatistic stat);
|
||||
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
public long getN() {
|
||||
return n;
|
||||
}
|
||||
|
||||
|
@ -171,26 +146,37 @@ public abstract class AbstractStorelessDescriptiveStatistics extends Descriptive
|
|||
* @see org.apache.commons.math.stat.Univariate#getSum()
|
||||
*/
|
||||
public double getSum() {
|
||||
return apply(sum);
|
||||
return sum.getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#getSumsq()
|
||||
* Returns the sum of the squares of the values that have been added.
|
||||
* <p>
|
||||
* Double.NaN is returned if no values have been added.</p>
|
||||
*
|
||||
* @return The sum of squares
|
||||
*/
|
||||
public double getSumsq() {
|
||||
return apply(sumsq);
|
||||
return sumsq.getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#getMean()
|
||||
* Returns the mean of the values that have been added.
|
||||
* <p>
|
||||
* Double.NaN is returned if no values have been added.</p>
|
||||
*
|
||||
* @return the mean
|
||||
*/
|
||||
public double getMean() {
|
||||
return apply(mean);
|
||||
return new Mean(firstMoment).getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the standard deviation for this collection of values
|
||||
* @see org.apache.commons.math.stat.Univariate#getStandardDeviation()
|
||||
* Returns the standard deviation of the values that have been added.
|
||||
* <p>
|
||||
* Double.NaN is returned if no values have been added.</p>
|
||||
*
|
||||
* @return the standard deviation
|
||||
*/
|
||||
public double getStandardDeviation() {
|
||||
double stdDev = Double.NaN;
|
||||
|
@ -205,104 +191,69 @@ public abstract class AbstractStorelessDescriptiveStatistics extends Descriptive
|
|||
}
|
||||
|
||||
/**
|
||||
* 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>.
|
||||
* Returns the variance of the values that have been added.
|
||||
* <p>
|
||||
* Double.NaN is returned if no values have been added.</p>
|
||||
*
|
||||
* @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.
|
||||
* @return the variance
|
||||
*/
|
||||
public double getVariance() {
|
||||
return apply(variance);
|
||||
return new Variance(secondMoment).getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
return apply(skewness);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>.
|
||||
* Returns the maximum of the values that have been added.
|
||||
* <p>
|
||||
* Double.NaN is returned if no values have been added.</p>
|
||||
*
|
||||
* @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() {
|
||||
return apply(kurtosis);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getKurtosisClass()
|
||||
*/
|
||||
public int getKurtosisClass() {
|
||||
int kClass = MESOKURTIC;
|
||||
|
||||
double kurtosis = getKurtosis();
|
||||
if (kurtosis > 0) {
|
||||
kClass = LEPTOKURTIC;
|
||||
} else if (kurtosis < 0) {
|
||||
kClass = PLATYKURTIC;
|
||||
}
|
||||
return (kClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#getMax()
|
||||
* @return the maximum
|
||||
*/
|
||||
public double getMax() {
|
||||
return apply(max);
|
||||
return max.getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#getMin()
|
||||
* Returns the minimum of the values that have been added.
|
||||
* <p>
|
||||
* Double.NaN is returned if no values have been added.</p>
|
||||
*
|
||||
* @return the minimum
|
||||
*/
|
||||
public double getMin() {
|
||||
return apply(min);
|
||||
return min.getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#getGeometricMean()
|
||||
*/
|
||||
* Returns the geometric mean of the values that have been added.
|
||||
* <p>
|
||||
* Double.NaN is returned if no values have been added.</p>
|
||||
*
|
||||
* @return the geometric mean
|
||||
*/
|
||||
public double getGeometricMean() {
|
||||
return apply(geoMean);
|
||||
return geoMean.getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a text report displaying
|
||||
* univariate statistics from values that
|
||||
* summary 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("SummaryStatistics:\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.stat.Univariate#clear()
|
||||
*/
|
||||
* Resets all statistics and storage
|
||||
*/
|
||||
public void clear() {
|
||||
this.n = 0;
|
||||
min.clear();
|
||||
|
@ -311,27 +262,8 @@ public abstract class AbstractStorelessDescriptiveStatistics extends Descriptive
|
|||
sumLog.clear();
|
||||
sumsq.clear();
|
||||
geoMean.clear();
|
||||
|
||||
moment.clear();
|
||||
mean.clear();
|
||||
variance.clear();
|
||||
skewness.clear();
|
||||
kurtosis.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#getWindowSize()
|
||||
*/
|
||||
public int getWindowSize() {
|
||||
return windowSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#setWindowSize(int)
|
||||
*/
|
||||
public void setWindowSize(int windowSize) {
|
||||
clear();
|
||||
this.windowSize = windowSize;
|
||||
firstMoment.clear();
|
||||
secondMoment.clear();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -58,7 +58,7 @@ import org.apache.commons.math.MathException;
|
|||
/**
|
||||
* A collection of commonly used test statistics and statistical tests.
|
||||
*
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/19 03:22:54 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public interface TestStatistic {
|
||||
|
||||
|
@ -356,7 +356,7 @@ public interface TestStatistic {
|
|||
* @return t statistic
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
double t(double mu, DescriptiveStatistics sampleStats)
|
||||
double t(double mu, StatisticalSummary sampleStats)
|
||||
throws IllegalArgumentException, MathException;
|
||||
|
||||
/**
|
||||
|
@ -377,7 +377,7 @@ public interface TestStatistic {
|
|||
* @return t statistic
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
double t(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
|
||||
double t(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)
|
||||
throws IllegalArgumentException, MathException;
|
||||
|
||||
/**
|
||||
|
@ -406,12 +406,12 @@ public interface TestStatistic {
|
|||
* at least 5 observations.
|
||||
* </li></ul>
|
||||
*
|
||||
* @param sampleStats1 DescriptiveStatistics describing data from the first sample
|
||||
* @param sampleStats2 DescriptiveStatistics describing data from the second sample
|
||||
* @param sampleStats1 StatisticalSummary describing data from the first sample
|
||||
* @param sampleStats2 StatisticalSummary describing data from the second sample
|
||||
* @return p-value for t-test
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
double tTest(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
|
||||
double tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)
|
||||
throws IllegalArgumentException, MathException;
|
||||
|
||||
/**
|
||||
|
@ -453,14 +453,14 @@ public interface TestStatistic {
|
|||
* <li> <code> 0 < alpha < 0.5 </code>
|
||||
* </li></ul>
|
||||
*
|
||||
* @param sampleStats1 DescriptiveStatistics describing sample data values
|
||||
* @param sampleStats2 DescriptiveStatistics describing sample data values
|
||||
* @param sampleStats1 StatisticalSummary describing sample data values
|
||||
* @param sampleStats2 StatisticalSummary describing sample data values
|
||||
* @param alpha significance level of the test
|
||||
* @return true if the null hypothesis can be rejected with
|
||||
* confidence 1 - alpha
|
||||
* @throws IllegalArgumentException if the preconditions are not met
|
||||
*/
|
||||
boolean tTest(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2,
|
||||
boolean tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2,
|
||||
double alpha)
|
||||
throws IllegalArgumentException, MathException;
|
||||
|
||||
|
@ -495,12 +495,12 @@ public interface TestStatistic {
|
|||
* </li></ul>
|
||||
*
|
||||
* @param mu constant value to compare sample mean against
|
||||
* @param sampleStats DescriptiveStatistics describing sample data values
|
||||
* @param sampleStats StatisticalSummary describing sample data values
|
||||
* @param alpha significance level of the test
|
||||
* @return p-value
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
boolean tTest(double mu, DescriptiveStatistics sampleStats, double alpha)
|
||||
boolean tTest(double mu, StatisticalSummary sampleStats, double alpha)
|
||||
throws IllegalArgumentException, MathException;
|
||||
|
||||
/**
|
||||
|
@ -526,11 +526,11 @@ public interface TestStatistic {
|
|||
* </li></ul>
|
||||
*
|
||||
* @param mu constant value to compare sample mean against
|
||||
* @param sampleStats DescriptiveStatistics describing sample data
|
||||
* @param sampleStats StatisticalSummary describing sample data
|
||||
* @return p-value
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
double tTest(double mu, DescriptiveStatistics sampleStats)
|
||||
double tTest(double mu, StatisticalSummary sampleStats)
|
||||
throws IllegalArgumentException, MathException;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -64,7 +64,7 @@ import org.apache.commons.math.distribution.ChiSquaredDistribution;
|
|||
/**
|
||||
* Implements test statistics defined in the TestStatistic interface.
|
||||
*
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/19 03:22:54 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class TestStatisticImpl implements TestStatistic, Serializable {
|
||||
|
||||
|
@ -255,11 +255,11 @@ public class TestStatisticImpl implements TestStatistic, Serializable {
|
|||
|
||||
/**
|
||||
* @param mu comparison constant
|
||||
* @param sampleStats DescriptiveStatistics holding sample summary statitstics
|
||||
* @param sampleStats StatisticalSummary holding sample summary statitstics
|
||||
* @return t statistic
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
public double t(double mu, DescriptiveStatistics sampleStats)
|
||||
public double t(double mu, StatisticalSummary sampleStats)
|
||||
throws IllegalArgumentException {
|
||||
if ((sampleStats == null) || (sampleStats.getN() < 5)) {
|
||||
throw new IllegalArgumentException("insufficient data for t statistic");
|
||||
|
@ -272,14 +272,14 @@ public class TestStatisticImpl implements TestStatistic, Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param sampleStats1 DescriptiveStatistics describing data from the first sample
|
||||
* @param sampleStats2 DescriptiveStatistics describing data from the second sample
|
||||
* @param sampleStats1 StatisticalSummary describing data from the first sample
|
||||
* @param sampleStats2 StatisticalSummary describing data from the second sample
|
||||
* @return t statistic
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
public double t(
|
||||
DescriptiveStatistics sampleStats1,
|
||||
DescriptiveStatistics sampleStats2)
|
||||
StatisticalSummary sampleStats1,
|
||||
StatisticalSummary sampleStats2)
|
||||
throws IllegalArgumentException {
|
||||
if ((sampleStats1 == null)
|
||||
|| (sampleStats2 == null
|
||||
|
@ -296,14 +296,14 @@ public class TestStatisticImpl implements TestStatistic, Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param sampleStats1 DescriptiveStatistics describing data from the first sample
|
||||
* @param sampleStats2 DescriptiveStatistics describing data from the second sample
|
||||
* @param sampleStats1 StatisticalSummary describing data from the first sample
|
||||
* @param sampleStats2 StatisticalSummary describing data from the second sample
|
||||
* @return p-value for t-test
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
public double tTest(
|
||||
DescriptiveStatistics sampleStats1,
|
||||
DescriptiveStatistics sampleStats2)
|
||||
StatisticalSummary sampleStats1,
|
||||
StatisticalSummary sampleStats2)
|
||||
throws IllegalArgumentException, MathException {
|
||||
if ((sampleStats1 == null)
|
||||
|| (sampleStats2 == null
|
||||
|
@ -320,16 +320,16 @@ public class TestStatisticImpl implements TestStatistic, Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param sampleStats1 DescriptiveStatistics describing sample data values
|
||||
* @param sampleStats2 DescriptiveStatistics describing sample data values
|
||||
* @param sampleStats1 StatisticalSummary describing sample data values
|
||||
* @param sampleStats2 StatisticalSummary describing sample data values
|
||||
* @param alpha significance level of the test
|
||||
* @return true if the null hypothesis can be rejected with
|
||||
* confidence 1 - alpha
|
||||
* @throws IllegalArgumentException if the preconditions are not met
|
||||
*/
|
||||
public boolean tTest(
|
||||
DescriptiveStatistics sampleStats1,
|
||||
DescriptiveStatistics sampleStats2,
|
||||
StatisticalSummary sampleStats1,
|
||||
StatisticalSummary sampleStats2,
|
||||
double alpha)
|
||||
throws IllegalArgumentException, MathException {
|
||||
if ((alpha <= 0) || (alpha > 0.5)) {
|
||||
|
@ -341,14 +341,14 @@ public class TestStatisticImpl implements TestStatistic, Serializable {
|
|||
|
||||
/**
|
||||
* @param mu constant value to compare sample mean against
|
||||
* @param sampleStats DescriptiveStatistics describing sample data values
|
||||
* @param sampleStats StatisticalSummary describing sample data values
|
||||
* @param alpha significance level of the test
|
||||
* @return p-value
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
public boolean tTest(
|
||||
double mu,
|
||||
DescriptiveStatistics sampleStats,
|
||||
StatisticalSummary sampleStats,
|
||||
double alpha)
|
||||
throws IllegalArgumentException, MathException {
|
||||
if ((alpha <= 0) || (alpha > 0.5)) {
|
||||
|
@ -360,11 +360,11 @@ public class TestStatisticImpl implements TestStatistic, Serializable {
|
|||
|
||||
/**
|
||||
* @param mu constant value to compare sample mean against
|
||||
* @param sampleStats DescriptiveStatistics describing sample data
|
||||
* @param sampleStats StatisticalSummary describing sample data
|
||||
* @return p-value
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
public double tTest(double mu, DescriptiveStatistics sampleStats)
|
||||
public double tTest(double mu, StatisticalSummary sampleStats)
|
||||
throws IllegalArgumentException, MathException {
|
||||
if ((sampleStats == null) || (sampleStats.getN() < 5)) {
|
||||
throw new IllegalArgumentException("insufficient data for t statistic");
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -64,14 +64,14 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
|
|||
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
|
||||
* recursive strategy
|
||||
* </a>. Both incremental and evaluation strategies currently use this approach.
|
||||
* @version $Revision: 1.11 $ $Date: 2003/11/19 03:28:24 $
|
||||
* @version $Revision: 1.12 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class FirstMoment extends AbstractStorelessUnivariateStatistic implements Serializable{
|
||||
|
||||
static final long serialVersionUID = -803343206421984070L;
|
||||
|
||||
/** count of values that have been added */
|
||||
protected int n = 0;
|
||||
protected long n = 0;
|
||||
|
||||
/** first moment of values that have been added */
|
||||
protected double m1 = Double.NaN;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -60,14 +60,14 @@ import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
|
|||
/**
|
||||
* Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
|
||||
* geometric mean </a> of the available values
|
||||
* @version $Revision: 1.14 $ $Date: 2003/11/19 03:28:24 $
|
||||
* @version $Revision: 1.15 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class GeometricMean extends SumOfLogs implements Serializable{
|
||||
|
||||
static final long serialVersionUID = -8178734905303459453L;
|
||||
|
||||
/** */
|
||||
protected int n = 0;
|
||||
protected long n = 0;
|
||||
|
||||
/** */
|
||||
private double geoMean = Double.NaN;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -64,7 +64,7 @@ import org
|
|||
.AbstractStorelessUnivariateStatistic;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.14 $ $Date: 2003/11/19 03:28:24 $
|
||||
* @version $Revision: 1.15 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class Kurtosis extends AbstractStorelessUnivariateStatistic implements Serializable {
|
||||
|
||||
|
@ -80,7 +80,7 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements Se
|
|||
private double kurtosis = Double.NaN;
|
||||
|
||||
/** */
|
||||
private int n = 0;
|
||||
private long n = 0;
|
||||
|
||||
/**
|
||||
* Construct a Kurtosis
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -58,7 +58,7 @@ import java.io.Serializable;
|
|||
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.14 $ $Date: 2003/11/19 03:28:24 $
|
||||
* @version $Revision: 1.15 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class Skewness extends AbstractStorelessUnivariateStatistic implements Serializable {
|
||||
|
||||
|
@ -74,7 +74,7 @@ public class Skewness extends AbstractStorelessUnivariateStatistic implements Se
|
|||
protected double skewness = Double.NaN;
|
||||
|
||||
/** */
|
||||
private int n = 0;
|
||||
private long n = 0;
|
||||
|
||||
/**
|
||||
* Constructs a Skewness
|
||||
|
|
|
@ -58,8 +58,12 @@ import java.io.Serializable;
|
|||
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
|
||||
|
||||
/**
|
||||
* Updating forumulas use West's algorithm as described in
|
||||
* <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>.
|
||||
*
|
||||
* @version $Revision: 1.14 $ $Date: 2003/11/19 03:28:24 $
|
||||
* @version $Revision: 1.15 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class Variance extends AbstractStorelessUnivariateStatistic implements Serializable {
|
||||
|
||||
|
@ -87,7 +91,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se
|
|||
* If the external SecondMoment is used, the this is updated from
|
||||
* that moments counter
|
||||
*/
|
||||
protected int n = 0;
|
||||
protected long n = 0;
|
||||
|
||||
/**
|
||||
* Constructs a Variance.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -64,14 +64,14 @@ import org
|
|||
.AbstractStorelessUnivariateStatistic;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.12 $ $Date: 2003/11/19 03:28:24 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class Max extends AbstractStorelessUnivariateStatistic implements Serializable {
|
||||
|
||||
static final long serialVersionUID = -5593383832225844641L;
|
||||
|
||||
/** */
|
||||
private int n = 0;
|
||||
private long n = 0;
|
||||
|
||||
/** */
|
||||
private double value = Double.NaN;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -64,14 +64,14 @@ import org
|
|||
.AbstractStorelessUnivariateStatistic;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.12 $ $Date: 2003/11/19 03:28:24 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class Min extends AbstractStorelessUnivariateStatistic implements Serializable {
|
||||
|
||||
static final long serialVersionUID = -2941995784909003131L;
|
||||
|
||||
/** */
|
||||
private int n = 0;
|
||||
private long n = 0;
|
||||
|
||||
/** */
|
||||
private double value = Double.NaN;
|
||||
|
|
|
@ -60,13 +60,12 @@ import java.io.File;
|
|||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
||||
import org.apache.commons.math.stat.SummaryStatistics;
|
||||
|
||||
/**
|
||||
* Test cases for the EmpiricalDistribution class
|
||||
*
|
||||
* @version $Revision: 1.10 $ $Date: 2004/01/15 05:22:08 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
|
||||
public final class EmpiricalDistributionTest extends TestCase {
|
||||
|
@ -150,7 +149,7 @@ public final class EmpiricalDistributionTest extends TestCase {
|
|||
|
||||
private void tstGen(double tolerance)throws Exception {
|
||||
empiricalDistribution.load(file);
|
||||
DescriptiveStatistics stats = new StorelessDescriptiveStatisticsImpl();
|
||||
SummaryStatistics stats = SummaryStatistics.newInstance();
|
||||
for (int i = 1; i < 1000; i++) {
|
||||
stats.addValue(empiricalDistribution.getNextValue());
|
||||
}
|
||||
|
|
|
@ -61,14 +61,13 @@ import java.security.NoSuchAlgorithmException;
|
|||
import java.util.HashSet;
|
||||
|
||||
import org.apache.commons.math.stat.Frequency;
|
||||
import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
||||
import org.apache.commons.math.stat.SummaryStatistics;
|
||||
import org.apache.commons.math.stat.TestStatisticImpl;
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
|
||||
/**
|
||||
* Test cases for the RandomData class.
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:40 $
|
||||
* @version $Revision: 1.9 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
|
||||
public final class RandomDataTest extends TestCase {
|
||||
|
@ -405,7 +404,7 @@ public final class RandomDataTest extends TestCase {
|
|||
} catch (IllegalArgumentException ex) {
|
||||
;
|
||||
}
|
||||
DescriptiveStatistics u = new StorelessDescriptiveStatisticsImpl();
|
||||
SummaryStatistics u = SummaryStatistics.newInstance();
|
||||
for (int i = 0; i<largeSampleSize; i++) {
|
||||
u.addValue(randomData.nextGaussian(0,1));
|
||||
}
|
||||
|
|
|
@ -58,13 +58,12 @@ import junit.framework.TestCase;
|
|||
import junit.framework.TestSuite;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
||||
import org.apache.commons.math.stat.SummaryStatistics;
|
||||
|
||||
/**
|
||||
* Test cases for the ValueServer class.
|
||||
*
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/15 07:31:44 $
|
||||
* @version $Revision: 1.12 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
|
||||
public final class ValueServerTest extends TestCase {
|
||||
|
@ -103,7 +102,7 @@ public final class ValueServerTest extends TestCase {
|
|||
vs.computeDistribution();
|
||||
assertTrue("empirical distribution property",
|
||||
vs.getEmpiricalDistribution() != null);
|
||||
DescriptiveStatistics stats = new StorelessDescriptiveStatisticsImpl();
|
||||
SummaryStatistics stats = SummaryStatistics.newInstance();
|
||||
for (int i = 1; i < 1000; i++) {
|
||||
next = vs.getNext();
|
||||
stats.addValue(next);
|
||||
|
@ -114,7 +113,7 @@ public final class ValueServerTest extends TestCase {
|
|||
tolerance);
|
||||
|
||||
vs.computeDistribution(500);
|
||||
stats = new StorelessDescriptiveStatisticsImpl();
|
||||
stats = SummaryStatistics.newInstance();
|
||||
for (int i = 1; i < 1000; i++) {
|
||||
next = vs.getNext();
|
||||
stats.addValue(next);
|
||||
|
|
|
@ -61,14 +61,13 @@ import java.io.BufferedReader;
|
|||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import org.apache.commons.logging.*;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.logging.Log;
|
||||
/**
|
||||
* Test cases for the {@link DescriptiveStatistics} class.
|
||||
* @version $Revision: 1.12 $ $Date: 2003/11/15 16:01:40 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class CertifiedDataTest extends TestCase {
|
||||
|
||||
protected DescriptiveStatistics u = null;
|
||||
public class CertifiedDataTest extends TestCase {
|
||||
|
||||
protected double mean = Double.NaN;
|
||||
|
||||
|
@ -103,9 +102,9 @@ public class CertifiedDataTest extends TestCase {
|
|||
* Test StorelessDescriptiveStatistics
|
||||
*/
|
||||
public void testUnivariateImpl() {
|
||||
|
||||
SummaryStatistics u = null;
|
||||
try {
|
||||
u = DescriptiveStatistics.newInstance(StorelessDescriptiveStatisticsImpl.class);
|
||||
u = SummaryStatistics.newInstance(SummaryStatisticsImpl.class);
|
||||
} catch (InstantiationException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
@ -114,19 +113,19 @@ public class CertifiedDataTest extends TestCase {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
loadStats("data/Lew.txt");
|
||||
loadStats("data/Lew.txt", u);
|
||||
assertEquals("Lew: std", std, u.getStandardDeviation(), .000000000001);
|
||||
assertEquals("Lew: mean", mean, u.getMean(), .000000000001);
|
||||
|
||||
loadStats("data/Lottery.txt");
|
||||
loadStats("data/Lottery.txt", u);
|
||||
assertEquals("Lottery: std", std, u.getStandardDeviation(), .000000000001);
|
||||
assertEquals("Lottery: mean", mean, u.getMean(), .000000000001);
|
||||
|
||||
loadStats("data/PiDigits.txt");
|
||||
loadStats("data/PiDigits.txt", u);
|
||||
assertEquals("PiDigits: std", std, u.getStandardDeviation(), .0000000000001);
|
||||
assertEquals("PiDigits: mean", mean, u.getMean(), .0000000000001);
|
||||
|
||||
loadStats("data/Mavro.txt");
|
||||
loadStats("data/Mavro.txt", u);
|
||||
assertEquals("Mavro: std", std, u.getStandardDeviation(), .00000000000001);
|
||||
assertEquals("Mavro: mean", mean, u.getMean(), .00000000000001);
|
||||
|
||||
|
@ -134,7 +133,7 @@ public class CertifiedDataTest extends TestCase {
|
|||
//assertEquals("Michelso: std", std, u.getStandardDeviation(), .00000000000001);
|
||||
//assertEquals("Michelso: mean", mean, u.getMean(), .00000000000001);
|
||||
|
||||
loadStats("data/NumAcc1.txt");
|
||||
loadStats("data/NumAcc1.txt", u);
|
||||
assertEquals("NumAcc1: std", std, u.getStandardDeviation(), .00000000000001);
|
||||
assertEquals("NumAcc1: mean", mean, u.getMean(), .00000000000001);
|
||||
|
||||
|
@ -148,21 +147,21 @@ public class CertifiedDataTest extends TestCase {
|
|||
*/
|
||||
public void testStoredUnivariateImpl() {
|
||||
|
||||
u = DescriptiveStatistics.newInstance();
|
||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
||||
|
||||
loadStats("data/Lew.txt");
|
||||
loadStats("data/Lew.txt", u);
|
||||
assertEquals("Lew: std", std, u.getStandardDeviation(), .000000000001);
|
||||
assertEquals("Lew: mean", mean, u.getMean(), .000000000001);
|
||||
|
||||
loadStats("data/Lottery.txt");
|
||||
loadStats("data/Lottery.txt", u);
|
||||
assertEquals("Lottery: std", std, u.getStandardDeviation(), .000000000001);
|
||||
assertEquals("Lottery: mean", mean, u.getMean(), .000000000001);
|
||||
|
||||
loadStats("data/PiDigits.txt");
|
||||
loadStats("data/PiDigits.txt", u);
|
||||
assertEquals("PiDigits: std", std, u.getStandardDeviation(), .0000000000001);
|
||||
assertEquals("PiDigits: mean", mean, u.getMean(), .0000000000001);
|
||||
|
||||
loadStats("data/Mavro.txt");
|
||||
loadStats("data/Mavro.txt", u);
|
||||
assertEquals("Mavro: std", std, u.getStandardDeviation(), .00000000000001);
|
||||
assertEquals("Mavro: mean", mean, u.getMean(), .00000000000001);
|
||||
|
||||
|
@ -170,7 +169,7 @@ public class CertifiedDataTest extends TestCase {
|
|||
//assertEquals("Michelso: std", std, u.getStandardDeviation(), .00000000000001);
|
||||
//assertEquals("Michelso: mean", mean, u.getMean(), .00000000000001);
|
||||
|
||||
loadStats("data/NumAcc1.txt");
|
||||
loadStats("data/NumAcc1.txt", u);
|
||||
assertEquals("NumAcc1: std", std, u.getStandardDeviation(), .00000000000001);
|
||||
assertEquals("NumAcc1: mean", mean, u.getMean(), .00000000000001);
|
||||
|
||||
|
@ -182,12 +181,19 @@ public class CertifiedDataTest extends TestCase {
|
|||
/**
|
||||
* loads a DescriptiveStatistics off of a test file
|
||||
* @param file
|
||||
* @param statistical summary
|
||||
*/
|
||||
private void loadStats(String resource) {
|
||||
private void loadStats(String resource, Object u) {
|
||||
|
||||
DescriptiveStatistics d = null;
|
||||
SummaryStatistics s = null;
|
||||
if (u instanceof DescriptiveStatistics) {
|
||||
d = (DescriptiveStatistics) u;
|
||||
} else {
|
||||
s = (SummaryStatistics) u;
|
||||
}
|
||||
try {
|
||||
|
||||
u.clear();
|
||||
u.getClass().getDeclaredMethod("clear", null).invoke(u, null);
|
||||
mean = Double.NaN;
|
||||
std = Double.NaN;
|
||||
|
||||
|
@ -215,8 +221,11 @@ public class CertifiedDataTest extends TestCase {
|
|||
line = in.readLine();
|
||||
|
||||
while (line != null) {
|
||||
|
||||
u.addValue(Double.parseDouble(line.trim()));
|
||||
if (d != null) {
|
||||
d.addValue(Double.parseDouble(line.trim()));
|
||||
} else {
|
||||
s.addValue(Double.parseDouble(line.trim()));
|
||||
}
|
||||
line = in.readLine();
|
||||
}
|
||||
|
||||
|
@ -226,6 +235,8 @@ public class CertifiedDataTest extends TestCase {
|
|||
log.error(fnfe.getMessage(), fnfe);
|
||||
} catch (IOException ioe) {
|
||||
log.error(ioe.getMessage(), ioe);
|
||||
} catch (Exception ioe) {
|
||||
log.error(ioe.getMessage(), ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -61,7 +61,7 @@ import org.apache.commons.math.util.DefaultTransformer;
|
|||
import org.apache.commons.math.util.NumberTransformer;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:41 $
|
||||
* @version $Revision: 1.2 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
|
||||
|
||||
|
@ -74,6 +74,9 @@ public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
|
|||
/** Number Transformer maps Objects to Number for us. */
|
||||
protected NumberTransformer transformer;
|
||||
|
||||
/** hold the window size **/
|
||||
protected int windowSize = DescriptiveStatistics.INFINITE_WINDOW;
|
||||
|
||||
/**
|
||||
* Construct a ListUnivariate with a specific List.
|
||||
* @param list The list that will back this DescriptiveStatistics
|
||||
|
@ -149,7 +152,7 @@ public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getN()
|
||||
*/
|
||||
public int getN() {
|
||||
public long getN() {
|
||||
int n = 0;
|
||||
|
||||
if (windowSize != DescriptiveStatistics.INFINITE_WINDOW) {
|
||||
|
@ -183,7 +186,6 @@ public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
|
|||
* @see org.apache.commons.math.stat.DescriptiveStatistics#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
super.clear();
|
||||
list.clear();
|
||||
}
|
||||
|
||||
|
@ -217,4 +219,21 @@ public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
|
|||
this.transformer = transformer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#setWindowSize(int)
|
||||
*/
|
||||
public synchronized void setWindowSize(int windowSize) {
|
||||
this.windowSize = windowSize;
|
||||
//Discard elements from the front of the list if the windowSize is less than
|
||||
// the size of the list.
|
||||
int extra = list.size() - windowSize;
|
||||
for (int i = 0; i < extra; i++) {
|
||||
list.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
public int getWindowSize() {
|
||||
return windowSize;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -63,7 +63,7 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Test cases for the {@link Univariate} class.
|
||||
*
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:41 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
|
||||
public final class ListUnivariateImplTest extends TestCase {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -63,7 +63,7 @@ import org.apache.commons.math.random.RandomDataImpl;
|
|||
/**
|
||||
* Test cases for the {@link Univariate} class.
|
||||
*
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:41 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
|
||||
public final class StoreUnivariateImplTest extends TestCase {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -61,7 +61,7 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Test cases for the TestStatistic class.
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/19 03:22:54 $
|
||||
* @version $Revision: 1.10 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
|
||||
public final class TestStatisticTest extends TestCase {
|
||||
|
@ -186,11 +186,11 @@ public final class TestStatisticTest extends TestCase {
|
|||
92.0,
|
||||
95.0 };
|
||||
double mu = 100.0;
|
||||
DescriptiveStatistics sampleStats = null;
|
||||
SummaryStatistics sampleStats = null;
|
||||
try {
|
||||
sampleStats =
|
||||
DescriptiveStatistics.newInstance(
|
||||
StorelessDescriptiveStatisticsImpl.class);
|
||||
SummaryStatistics.newInstance(
|
||||
SummaryStatisticsImpl.class);
|
||||
} catch (InstantiationException e5) {
|
||||
// TODO Auto-generated catch block
|
||||
e5.printStackTrace();
|
||||
|
@ -221,18 +221,8 @@ public final class TestStatisticTest extends TestCase {
|
|||
;
|
||||
}
|
||||
|
||||
DescriptiveStatistics nullStats = null;
|
||||
try {
|
||||
nullStats =
|
||||
DescriptiveStatistics.newInstance(
|
||||
StorelessDescriptiveStatisticsImpl.class);
|
||||
} catch (InstantiationException e6) {
|
||||
// TODO Auto-generated catch block
|
||||
e6.printStackTrace();
|
||||
} catch (IllegalAccessException e6) {
|
||||
// TODO Auto-generated catch block
|
||||
e6.printStackTrace();
|
||||
}
|
||||
SummaryStatistics nullStats = SummaryStatistics.newInstance();
|
||||
|
||||
try {
|
||||
testStatistic.t(mu, nullStats);
|
||||
fail("arguments too short, IllegalArgumentException expected");
|
||||
|
@ -249,18 +239,8 @@ public final class TestStatisticTest extends TestCase {
|
|||
;
|
||||
}
|
||||
|
||||
DescriptiveStatistics emptyStats = null;
|
||||
try {
|
||||
emptyStats =
|
||||
DescriptiveStatistics.newInstance(
|
||||
StorelessDescriptiveStatisticsImpl.class);
|
||||
} catch (InstantiationException e4) {
|
||||
// TODO Auto-generated catch block
|
||||
e4.printStackTrace();
|
||||
} catch (IllegalAccessException e4) {
|
||||
// TODO Auto-generated catch block
|
||||
e4.printStackTrace();
|
||||
}
|
||||
SummaryStatistics emptyStats =SummaryStatistics.newInstance();
|
||||
|
||||
try {
|
||||
testStatistic.t(mu, emptyStats);
|
||||
fail("arguments too short, IllegalArgumentException expected");
|
||||
|
@ -285,18 +265,8 @@ public final class TestStatisticTest extends TestCase {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
DescriptiveStatistics tooShortStats = null;
|
||||
try {
|
||||
tooShortStats =
|
||||
DescriptiveStatistics.newInstance(
|
||||
StorelessDescriptiveStatisticsImpl.class);
|
||||
} catch (InstantiationException e3) {
|
||||
// TODO Auto-generated catch block
|
||||
e3.printStackTrace();
|
||||
} catch (IllegalAccessException e3) {
|
||||
// TODO Auto-generated catch block
|
||||
e3.printStackTrace();
|
||||
}
|
||||
SummaryStatistics tooShortStats = SummaryStatistics.newInstance();
|
||||
|
||||
tooShortStats.addValue(0d);
|
||||
tooShortStats.addValue(2d);
|
||||
try {
|
||||
|
@ -339,18 +309,8 @@ public final class TestStatisticTest extends TestCase {
|
|||
3d,
|
||||
3d };
|
||||
|
||||
DescriptiveStatistics oneSidedPStats = null;
|
||||
try {
|
||||
oneSidedPStats =
|
||||
DescriptiveStatistics.newInstance(
|
||||
StorelessDescriptiveStatisticsImpl.class);
|
||||
} catch (InstantiationException e2) {
|
||||
// TODO Auto-generated catch block
|
||||
e2.printStackTrace();
|
||||
} catch (IllegalAccessException e2) {
|
||||
// TODO Auto-generated catch block
|
||||
e2.printStackTrace();
|
||||
}
|
||||
SummaryStatistics oneSidedPStats = SummaryStatistics.newInstance();;
|
||||
|
||||
for (int i = 0; i < oneSidedP.length; i++) {
|
||||
oneSidedPStats.addValue(oneSidedP[i]);
|
||||
}
|
||||
|
@ -418,34 +378,14 @@ public final class TestStatisticTest extends TestCase {
|
|||
double[] sample2 =
|
||||
{ -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d };
|
||||
|
||||
DescriptiveStatistics sampleStats1 = null;
|
||||
try {
|
||||
sampleStats1 =
|
||||
DescriptiveStatistics.newInstance(
|
||||
StorelessDescriptiveStatisticsImpl.class);
|
||||
} catch (InstantiationException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
} catch (IllegalAccessException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
SummaryStatistics sampleStats1 = SummaryStatistics.newInstance();
|
||||
|
||||
for (int i = 0; i < sample1.length; i++) {
|
||||
sampleStats1.addValue(sample1[i]);
|
||||
}
|
||||
|
||||
DescriptiveStatistics sampleStats2 = null;
|
||||
try {
|
||||
sampleStats2 =
|
||||
DescriptiveStatistics.newInstance(
|
||||
StorelessDescriptiveStatisticsImpl.class);
|
||||
} catch (InstantiationException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
SummaryStatistics sampleStats2 = SummaryStatistics.newInstance();
|
||||
|
||||
for (int i = 0; i < sample2.length; i++) {
|
||||
sampleStats2.addValue(sample2[i]);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -57,7 +57,7 @@ import org.apache.commons.math.TestUtils;
|
|||
|
||||
/**
|
||||
* Test cases for the {@link UnivariateStatistic} class.
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/19 13:35:10 $
|
||||
* @version $Revision: 1.10 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public abstract class StorelessUnivariateStatisticAbstractTest
|
||||
extends UnivariateStatisticAbstractTest {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -53,8 +53,7 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat.univariate;
|
||||
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
||||
import org.apache.commons.math.stat.SummaryStatistics;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
@ -63,7 +62,7 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Test cases for the {@link DescriptiveStatistics} class.
|
||||
*
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:41 $
|
||||
* @version $Revision: 1.2 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
|
||||
public final class UnivariateImplTest extends TestCase {
|
||||
|
@ -96,7 +95,7 @@ public final class UnivariateImplTest extends TestCase {
|
|||
|
||||
/** test stats */
|
||||
public void testStats() {
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl();
|
||||
SummaryStatistics u = SummaryStatistics.newInstance();
|
||||
assertEquals("total count",0,u.getN(),tolerance);
|
||||
u.addValue(one);
|
||||
u.addValue(twoF);
|
||||
|
@ -115,18 +114,13 @@ public final class UnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testN0andN1Conditions() throws Exception {
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl();
|
||||
SummaryStatistics u = SummaryStatistics.newInstance();
|
||||
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() ) );
|
||||
assertTrue("skew of n = 0 set should be NaN",
|
||||
Double.isNaN(u.getSkewness() ) );
|
||||
assertTrue("kurtosis of n = 0 set should be NaN",
|
||||
Double.isNaN(u.getKurtosis() ) );
|
||||
|
||||
|
||||
/* n=1 */
|
||||
u.addValue(one);
|
||||
|
@ -138,10 +132,6 @@ public final class UnivariateImplTest extends TestCase {
|
|||
u.getStandardDeviation() == 0.0);
|
||||
assertTrue("variance should be zero (n = 1)",
|
||||
u.getVariance() == 0.0);
|
||||
assertTrue("skew should be zero (n = 1)",
|
||||
u.getSkewness() == 0.0);
|
||||
assertTrue("kurtosis should be zero (n = 1)",
|
||||
u.getKurtosis() == 0.0);
|
||||
|
||||
/* n=2 */
|
||||
u.addValue(twoF);
|
||||
|
@ -149,27 +139,11 @@ public final class UnivariateImplTest extends TestCase {
|
|||
u.getStandardDeviation() != 0.0);
|
||||
assertTrue("variance should not be zero (n = 2)",
|
||||
u.getVariance() != 0.0);
|
||||
assertTrue("skew should not be zero (n = 2)",
|
||||
u.getSkewness() == 0.0);
|
||||
assertTrue("kurtosis should be zero (n = 2)",
|
||||
u.getKurtosis() == 0.0);
|
||||
|
||||
/* n=3 */
|
||||
u.addValue(twoL);
|
||||
assertTrue("skew should not be zero (n = 3)",
|
||||
u.getSkewness() != 0.0);
|
||||
assertTrue("kurtosis should be zero (n = 3)",
|
||||
u.getKurtosis() == 0.0);
|
||||
|
||||
/* n=4 */
|
||||
u.addValue(three);
|
||||
assertTrue("kurtosis should not be zero (n = 4)",
|
||||
u.getKurtosis() != 0.0);
|
||||
|
||||
}
|
||||
|
||||
public void testProductAndGeometricMean() throws Exception {
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl(10);
|
||||
SummaryStatistics u = SummaryStatistics.newInstance();
|
||||
|
||||
u.addValue( 1.0 );
|
||||
u.addValue( 2.0 );
|
||||
|
@ -178,33 +152,10 @@ public final class UnivariateImplTest extends TestCase {
|
|||
|
||||
assertEquals( "Geometric mean not expected", 2.213364,
|
||||
u.getGeometricMean(), 0.00001 );
|
||||
|
||||
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
||||
// of a discarded element
|
||||
for( int i = 0; i < 10; i++ ) {
|
||||
u.addValue( i + 2 );
|
||||
}
|
||||
// Values should be (2,3,4,5,6,7,8,9,10,11)
|
||||
|
||||
assertEquals( "Geometric mean not expected", 5.755931,
|
||||
u.getGeometricMean(), 0.00001 );
|
||||
}
|
||||
|
||||
public void testRollingMinMax() {
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl(3);
|
||||
u.addValue( 1.0 );
|
||||
u.addValue( 5.0 );
|
||||
u.addValue( 3.0 );
|
||||
u.addValue( 4.0 ); // discarding min
|
||||
assertEquals( "min not expected", 3.0,
|
||||
u.getMin(), Double.MIN_VALUE);
|
||||
u.addValue(1.0); // discarding max
|
||||
assertEquals( "max not expected", 4.0,
|
||||
u.getMax(), Double.MIN_VALUE);
|
||||
}
|
||||
|
||||
public void testNaNContracts() {
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl();
|
||||
SummaryStatistics u = SummaryStatistics.newInstance();
|
||||
double nan = Double.NaN;
|
||||
assertTrue("mean not NaN",Double.isNaN(u.getMean()));
|
||||
assertTrue("min not NaN",Double.isNaN(u.getMin()));
|
||||
|
@ -231,20 +182,4 @@ public final class UnivariateImplTest extends TestCase {
|
|||
|
||||
//FiXME: test all other NaN contract specs
|
||||
}
|
||||
|
||||
public void testSkewAndKurtosis() {
|
||||
DescriptiveStatistics u = new StorelessDescriptiveStatisticsImpl();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -57,7 +57,7 @@ import junit.framework.TestCase;
|
|||
|
||||
/**
|
||||
* Test cases for the {@link UnivariateStatistic} class.
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:23 $
|
||||
* @version $Revision: 1.9 $ $Date: 2004/01/25 21:30:41 $
|
||||
*/
|
||||
public abstract class UnivariateStatisticAbstractTest extends TestCase {
|
||||
|
||||
|
|
Loading…
Reference in New Issue