You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2004/04/12 04:27:50 UTC
cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate BeanListUnivariateImplTest.java DescriptiveStatisticsTest.java ListUnivariateImpl.java ListUnivariateImplTest.java MixedListUnivariateImplTest.java StoreUnivariateImplTest.java UnivariateImplTest.java
psteitz 2004/04/11 19:27:50
Modified: math/src/java/org/apache/commons/math/random
EmpiricalDistribution.java
EmpiricalDistributionImpl.java
math/src/java/org/apache/commons/math/stat/inference
TestStatistic.java TestStatisticImpl.java
math/src/test/org/apache/commons/math/random
EmpiricalDistributionTest.java RandomDataTest.java
ValueServerTest.java
math/src/test/org/apache/commons/math/stat
CertifiedDataTest.java StatUtilsTest.java
math/src/test/org/apache/commons/math/stat/data
CertifiedDataAbstractTest.java
math/src/test/org/apache/commons/math/stat/inference
TestStatisticTest.java
math/src/test/org/apache/commons/math/stat/univariate
UnivariateImplTest.java
Added: math/src/java/org/apache/commons/math/stat/univariate
AbstractDescriptiveStatistics.java
DescriptiveStatistics.java
DescriptiveStatisticsImpl.java
StatisticalSummary.java SummaryStatistics.java
SummaryStatisticsImpl.java
math/src/test/org/apache/commons/math/stat/univariate
BeanListUnivariateImplTest.java
DescriptiveStatisticsTest.java
ListUnivariateImpl.java ListUnivariateImplTest.java
MixedListUnivariateImplTest.java
StoreUnivariateImplTest.java
Removed: math/src/java/org/apache/commons/math/stat
AbstractDescriptiveStatistics.java
DescriptiveStatistics.java
DescriptiveStatisticsImpl.java
StatisticalSummary.java SummaryStatistics.java
SummaryStatisticsImpl.java
math/src/test/org/apache/commons/math/stat
BeanListUnivariateImpl.java
BeanListUnivariateImplTest.java
DescriptiveStatisticsTest.java
ListUnivariateImpl.java ListUnivariateImplTest.java
MixedListUnivariateImplTest.java
StoreUnivariateImplTest.java
Log:
Moved univariate statistical aggregates into univariate package.
Revision Changes Path
1.17 +2 -2 jakarta-commons/math/src/java/org/apache/commons/math/random/EmpiricalDistribution.java
Index: EmpiricalDistribution.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/random/EmpiricalDistribution.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- EmpiricalDistribution.java 21 Mar 2004 21:57:18 -0000 1.16
+++ EmpiricalDistribution.java 12 Apr 2004 02:27:49 -0000 1.17
@@ -21,7 +21,7 @@
import java.net.URL;
import java.util.ArrayList;
-import org.apache.commons.math.stat.SummaryStatistics;
+import org.apache.commons.math.stat.univariate.SummaryStatistics;
/**
* Represents an <a href="http://random.mat.sbg.ac.at/~ste/dipl/node11.html">
1.18 +2 -2 jakarta-commons/math/src/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java
Index: EmpiricalDistributionImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- EmpiricalDistributionImpl.java 21 Feb 2004 21:35:15 -0000 1.17
+++ EmpiricalDistributionImpl.java 12 Apr 2004 02:27:49 -0000 1.18
@@ -26,7 +26,7 @@
import java.io.InputStreamReader;
import java.net.URL;
-import org.apache.commons.math.stat.SummaryStatistics;
+import org.apache.commons.math.stat.univariate.SummaryStatistics;
/**
* Implements <code>EmpiricalDistribution</code> interface. This implementation
1.4 +2 -2 jakarta-commons/math/src/java/org/apache/commons/math/stat/inference/TestStatistic.java
Index: TestStatistic.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/inference/TestStatistic.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestStatistic.java 11 Apr 2004 20:42:33 -0000 1.3
+++ TestStatistic.java 12 Apr 2004 02:27:49 -0000 1.4
@@ -16,7 +16,7 @@
package org.apache.commons.math.stat.inference;
import org.apache.commons.math.MathException;
-import org.apache.commons.math.stat.StatisticalSummary;
+import org.apache.commons.math.stat.univariate.StatisticalSummary;
/**
* A collection of commonly used test statistics and statistical tests.
1.4 +2 -2 jakarta-commons/math/src/java/org/apache/commons/math/stat/inference/TestStatisticImpl.java
Index: TestStatisticImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/inference/TestStatisticImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestStatisticImpl.java 11 Apr 2004 20:42:33 -0000 1.3
+++ TestStatisticImpl.java 12 Apr 2004 02:27:49 -0000 1.4
@@ -22,8 +22,8 @@
import org.apache.commons.math.distribution.DistributionFactory;
import org.apache.commons.math.distribution.TDistribution;
import org.apache.commons.math.distribution.ChiSquaredDistribution;
-import org.apache.commons.math.stat.StatisticalSummary;
import org.apache.commons.math.stat.StatUtils;
+import org.apache.commons.math.stat.univariate.StatisticalSummary;
/**
* Implements test statistics defined in the TestStatistic interface.
1.1 jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/AbstractDescriptiveStatistics.java
Index: AbstractDescriptiveStatistics.java
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import java.util.Arrays;
import org.apache.commons.math.stat.univariate.moment.GeometricMean;
import org.apache.commons.math.stat.univariate.moment.Kurtosis;
import org.apache.commons.math.stat.univariate.moment.Mean;
import org.apache.commons.math.stat.univariate.moment.Skewness;
import org.apache.commons.math.stat.univariate.moment.Variance;
import org.apache.commons.math.stat.univariate.rank.Max;
import org.apache.commons.math.stat.univariate.rank.Min;
import org.apache.commons.math.stat.univariate.rank.Percentile;
import org.apache.commons.math.stat.univariate.summary.Sum;
import org.apache.commons.math.stat.univariate.summary.SumOfSquares;
/**
* Abstract superclass for DescriptiveStatistics implementations.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:49 $
*/
public abstract class AbstractDescriptiveStatistics
extends DescriptiveStatistics {
/**
* Create an AbstractDescriptiveStatistics
*/
public AbstractDescriptiveStatistics() {
}
/**
* Create an AbstractDescriptiveStatistics with a specific Window
* @param window WindowSIze for stat calculation
*/
public AbstractDescriptiveStatistics(int window) {
setWindowSize(window);
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getSum()
*/
public double getSum() {
return apply(new Sum());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getSumsq()
*/
public double getSumsq() {
return apply(new SumOfSquares());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getMean()
*/
public double getMean() {
return apply(new Mean());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getStandardDeviation()
*/
public double getStandardDeviation() {
double stdDev = Double.NaN;
if (getN() > 0) {
if (getN() > 1) {
stdDev = Math.sqrt(getVariance());
} else {
stdDev = 0.0;
}
}
return (stdDev);
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getVariance()
*/
public double getVariance() {
return apply(new Variance());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getSkewness()
*/
public double getSkewness() {
return apply(new Skewness());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getKurtosis()
*/
public double getKurtosis() {
return apply(new Kurtosis());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#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.DescriptiveStatistics#getMax()
*/
public double getMax() {
return apply(new Max());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getMin()
*/
public double getMin() {
return apply(new Min());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getGeometricMean()
*/
public double getGeometricMean() {
return apply(new GeometricMean());
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getPercentile(double)
*/
public double getPercentile(double p) {
return apply(new Percentile(p));
}
/**
* Generates a text report displaying
* univariate statistics from values that
* have been added.
* @return String with line feeds displaying statistics
*/
public String toString() {
StringBuffer outBuffer = new StringBuffer();
outBuffer.append("UnivariateImpl:\n");
outBuffer.append("n: " + getN() + "\n");
outBuffer.append("min: " + getMin() + "\n");
outBuffer.append("max: " + getMax() + "\n");
outBuffer.append("mean: " + getMean() + "\n");
outBuffer.append("std dev: " + getStandardDeviation() + "\n");
outBuffer.append("skewness: " + getSkewness() + "\n");
outBuffer.append("kurtosis: " + getKurtosis() + "\n");
return outBuffer.toString();
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getSortedValues()
*/
public double[] getSortedValues() {
double[] sort = getValues();
Arrays.sort(sort);
return sort;
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#addValue(double)
*/
public abstract void addValue(double value);
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getValues()
*/
public abstract double[] getValues();
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getElement(int)
*/
public abstract double getElement(int index);
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#apply(UnivariateStatistic)
*/
public abstract double apply(UnivariateStatistic stat);
}
1.1 jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/DescriptiveStatistics.java
Index: DescriptiveStatistics.java
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import java.io.Serializable;
import org.apache.commons.discovery.tools.DiscoverClass;
/**
* Abstract factory class for univariate statistical summaries.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:49 $
*/
public abstract class DescriptiveStatistics implements Serializable, StatisticalSummary {
/**
* Create an instance of a <code>DescriptiveStatistics</code>
* @return a new factory.
*/
public static DescriptiveStatistics 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 DescriptiveStatistics newInstance(Class cls) throws InstantiationException, IllegalAccessException {
return (DescriptiveStatistics)cls.newInstance();
}
/**
* Create an instance of a <code>DescriptiveStatistics</code>
* @return a new factory.
*/
public static DescriptiveStatistics newInstance() {
DescriptiveStatistics factory = null;
try {
DiscoverClass dc = new DiscoverClass();
factory = (DescriptiveStatistics) dc.newInstance(
DescriptiveStatistics.class,
"org.apache.commons.math.stat.univariate.DescriptiveStatisticsImpl");
} catch(Exception ex) {
// ignore as default implementation will be used.
}
return factory;
}
/**
* This constant signals that a Univariate implementation
* takes into account the contributions of an infinite number of
* elements. In other words, if getWindow returns this
* constant, there is, in effect, no "window".
*/
public static final int INFINITE_WINDOW = -1;
/**
* A LEPTOKURTIC set has a positive kurtosis (a high peak)
*/
public static int LEPTOKURTIC = 1;
/**
* A MESOKURTIC set has a kurtosis of 0 - it is a normal distribution
*/
public static int MESOKURTIC = 0;
/**
* A PLATYKURTIC set has a negative kurtosis (a flat "peak")
*/
public static int PLATYKURTIC = -1;
/**
* Adds the value to the set of numbers
* @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 skewness of the available values. Skewness is a
* measure of the assymetry of a given distribution.
* @return The skewness, Double.NaN if no values have been added
* or 0.0 for a value set <=2.
*/
public abstract double getSkewness();
/**
* Returns the Kurtosis of the available values. Kurtosis is a
* measure of the "peakedness" of a distribution
* @return The kurtosis, Double.NaN if no values have been added, or 0.0
* for a value set <=3.
*/
public abstract double getKurtosis();
/**
* Returns the Kurtosis "classification" a distribution can be
* leptokurtic (high peak), platykurtic (flat peak),
* or mesokurtic (zero kurtosis).
*
* @return A static constant defined in this interface,
* StoredDeviation.LEPTOKURITC, StoredDeviation.PLATYKURTIC, or
* StoredDeviation.MESOKURTIC
*/
public abstract int getKurtosisClass();
/**
* 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 and storage
*/
public abstract void clear();
/**
* Univariate has the ability to return only measures for the
* last N elements added to the set of values.
* @return The current window size or -1 if its Infinite.
*/
public abstract int getWindowSize();
/**
* WindowSize controls the number of values which contribute
* to the values returned by Univariate. For example, if
* windowSize is set to 3 and the values {1,2,3,4,5}
* have been added <strong> in that order</strong>
* then the <i>available values</i> are {3,4,5} and all
* reported statistics will be based on these values
* @param windowSize sets the size of the window.
*/
public abstract void setWindowSize(int windowSize);
/**
* Returns the current set of values in an array of double primitives.
* The order of addition is preserved. The returned array is a fresh
* copy of the underlying data -- i.e., it is not a reference to the
* stored data.
*
* @return returns the current set of numbers in the order in which they
* were added to this set
*/
public abstract double[] getValues();
/**
* Returns the current set of values in an array of double primitives,
* sorted in ascending order. The returned array is a fresh
* copy of the underlying data -- i.e., it is not a reference to the
* stored data.
* @return returns the current set of
* numbers sorted in ascending order
*/
public abstract double[] getSortedValues();
/**
* Returns the element at the specified index
* @param index The Index of the element
* @return return the element at the specified index
*/
public abstract double getElement(int index);
/**
* Returns an estimate for the pth percentile of the stored values.
* <p>
* The implementation provided here follows the first estimation procedure presented
* <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc252.htm">here.</a>
* <p>
* <strong>Preconditions</strong>:<ul>
* <li><code>0 < p < 100</code> (otherwise an
* <code>IllegalArgumentException</code> is thrown)</li>
* <li>at least one value must be stored (returns <code>Double.NaN
* </code> otherwise)</li>
* </ul>
*
* @param p the requested percentile (scaled from 0 - 100)
* @return An estimate for the pth percentile of the stored data
* values
*/
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.1 jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/DescriptiveStatisticsImpl.java
Index: DescriptiveStatisticsImpl.java
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import java.io.Serializable;
import java.util.Arrays;
import org.apache.commons.math.util.ContractableDoubleArray;
/**
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:49 $
*/
public class DescriptiveStatisticsImpl extends AbstractDescriptiveStatistics implements Serializable {
/** hold the window size **/
protected int windowSize = INFINITE_WINDOW;
/**
* Stored data values
*/
protected ContractableDoubleArray eDA;
/**
* 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.univariate.DescriptiveStatistics#getValues()
*/
public double[] getValues() {
double[] copiedArray = new double[eDA.getNumElements()];
System.arraycopy(
eDA.getElements(),
0,
copiedArray,
0,
eDA.getNumElements());
return copiedArray;
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getSortedValues()
*/
public double[] getSortedValues() {
double[] sort = getValues();
Arrays.sort(sort);
return sort;
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getElement(int)
*/
public double getElement(int index) {
return eDA.getElement(index);
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getN()
*/
public long getN() {
return eDA.getNumElements();
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#addValue(double)
*/
public synchronized void addValue(double v) {
if (windowSize != INFINITE_WINDOW) {
if (getN() == windowSize) {
eDA.addElementRolling(v);
} else if (getN() < windowSize) {
eDA.addElement(v);
} else {
String msg =
"A window Univariate had more element than " +
"the windowSize. This is an inconsistent state.";
throw new RuntimeException(msg);
}
} else {
eDA.addElement(v);
}
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#clear()
*/
public synchronized void clear() {
eDA.clear();
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#setWindowSize(int)
*/
public synchronized void setWindowSize(int windowSize) {
this.windowSize = windowSize;
// We need to check to see if we need to discard elements
// from the front of the array. If the windowSize is less than
// the current number of elements.
if (windowSize < eDA.getNumElements()) {
eDA.discardFrontElements(eDA.getNumElements() - windowSize);
}
}
/**
* 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 (eDA != null) {
return stat.evaluate(eDA.getValues(), eDA.start(), eDA.getNumElements());
}
return Double.NaN;
}
}
1.1 jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/StatisticalSummary.java
Index: StatisticalSummary.java
===================================================================
/*
* Copyright 2004,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
/**
* Reporting interface for basic univariate statistics.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:49 $
*/
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.1 jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/SummaryStatistics.java
Index: SummaryStatistics.java
===================================================================
/*
* Copyright 2004,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import java.io.Serializable;
import org.apache.commons.discovery.tools.DiscoverClass;
/**
* Abstract factory class for univariate statistical summaries.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:49 $
*/
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.univariate.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.1 jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/SummaryStatisticsImpl.java
Index: SummaryStatisticsImpl.java
===================================================================
/*
* Copyright 2004,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import org.apache.commons.math.stat.univariate.moment.SecondMoment;
import org.apache.commons.math.stat.univariate.moment.GeometricMean;
import org.apache.commons.math.stat.univariate.moment.Mean;
import org.apache.commons.math.stat.univariate.moment.Variance;
import org.apache.commons.math.stat.univariate.rank.Max;
import org.apache.commons.math.stat.univariate.rank.Min;
import org.apache.commons.math.stat.univariate.summary.Sum;
import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
import org.apache.commons.math.stat.univariate.summary.SumOfSquares;
/**
* Provides a default {@link SummaryStatistics} implementation.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:49 $
*/
public class SummaryStatisticsImpl extends SummaryStatistics {
/** count of values that have been added */
protected long n = 0;
/** SecondMoment is used to compute the mean and variance */
protected SecondMoment secondMoment = null;
/** sum of values that have been added */
protected Sum sum = null;
/** sum of the square of each value that has been added */
protected SumOfSquares sumsq = null;
/** min of values that have been added */
protected Min min = null;
/** max of values that have been added */
protected Max max = null;
/** sumLog of values that have been added */
protected SumOfLogs sumLog = null;
/** geoMean of values that have been added */
protected GeometricMean geoMean = null;
/** mean of values that have been added */
protected Mean mean = null;
/** variance of values that have been added */
protected Variance variance = null;
/**
* Construct a SummaryStatistics
*/
public SummaryStatisticsImpl() {
sum = new Sum();
sumsq = new SumOfSquares();
min = new Min();
max = new Max();
sumLog = new SumOfLogs();
geoMean = new GeometricMean();
secondMoment = new SecondMoment();
}
/**
* Add a value to the data
*
* @param value the value to add
*/
public void addValue(double value) {
sum.increment(value);
sumsq.increment(value);
min.increment(value);
max.increment(value);
sumLog.increment(value);
geoMean.increment(value);
secondMoment.increment(value);
n++;
}
/**
* Returns the number of available values
* @return The number of available values
*/
public long getN() {
return n;
}
/**
* 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 double getSum() {
return sum.getResult();
}
/**
* 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 sumsq.getResult();
}
/**
* 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 new Mean(secondMoment).getResult();
}
/**
* 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;
if (getN() > 0) {
if (getN() > 1) {
stdDev = Math.sqrt(getVariance());
} else {
stdDev = 0.0;
}
}
return (stdDev);
}
/**
* Returns the variance of the values that have been added.
* <p>
* Double.NaN is returned if no values have been added.</p>
*
* @return the variance
*/
public double getVariance() {
return new Variance(secondMoment).getResult();
}
/**
* Returns the maximum of the values that have been added.
* <p>
* Double.NaN is returned if no values have been added.</p>
*
* @return the maximum
*/
public double getMax() {
return max.getResult();
}
/**
* 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 min.getResult();
}
/**
* 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 geoMean.getResult();
}
/**
* Generates a text report displaying
* summary statistics from values that
* have been added.
* @return String with line feeds displaying statistics
*/
public String toString() {
StringBuffer outBuffer = new StringBuffer();
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");
return outBuffer.toString();
}
/**
* Resets all statistics and storage
*/
public void clear() {
this.n = 0;
min.clear();
max.clear();
sum.clear();
sumLog.clear();
sumsq.clear();
geoMean.clear();
secondMoment.clear();
}
}
1.16 +2 -2 jakarta-commons/math/src/test/org/apache/commons/math/random/EmpiricalDistributionTest.java
Index: EmpiricalDistributionTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/random/EmpiricalDistributionTest.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- EmpiricalDistributionTest.java 7 Mar 2004 16:32:44 -0000 1.15
+++ EmpiricalDistributionTest.java 12 Apr 2004 02:27:49 -0000 1.16
@@ -28,7 +28,7 @@
import java.util.Iterator;
import org.apache.commons.math.RetryTestCase;
-import org.apache.commons.math.stat.SummaryStatistics;
+import org.apache.commons.math.stat.univariate.SummaryStatistics;
/**
* Test cases for the EmpiricalDistribution class
1.15 +2 -2 jakarta-commons/math/src/test/org/apache/commons/math/random/RandomDataTest.java
Index: RandomDataTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/random/RandomDataTest.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- RandomDataTest.java 11 Apr 2004 20:42:33 -0000 1.14
+++ RandomDataTest.java 12 Apr 2004 02:27:49 -0000 1.15
@@ -24,8 +24,8 @@
import org.apache.commons.math.RetryTestCase;
import org.apache.commons.math.stat.Frequency;
-import org.apache.commons.math.stat.SummaryStatistics;
import org.apache.commons.math.stat.inference.TestStatisticImpl;
+import org.apache.commons.math.stat.univariate.SummaryStatistics;
/**
* Test cases for the RandomData class.
1.15 +2 -2 jakarta-commons/math/src/test/org/apache/commons/math/random/ValueServerTest.java
Index: ValueServerTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/random/ValueServerTest.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- ValueServerTest.java 7 Mar 2004 16:32:44 -0000 1.14
+++ ValueServerTest.java 12 Apr 2004 02:27:49 -0000 1.15
@@ -21,7 +21,7 @@
import java.net.URL;
import org.apache.commons.math.RetryTestCase;
-import org.apache.commons.math.stat.SummaryStatistics;
+import org.apache.commons.math.stat.univariate.SummaryStatistics;
/**
* Test cases for the ValueServer class.
1.17 +8 -14 jakarta-commons/math/src/test/org/apache/commons/math/stat/CertifiedDataTest.java
Index: CertifiedDataTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/CertifiedDataTest.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- CertifiedDataTest.java 18 Mar 2004 05:52:36 -0000 1.16
+++ CertifiedDataTest.java 12 Apr 2004 02:27:49 -0000 1.17
@@ -26,8 +26,12 @@
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
+import org.apache.commons.math.stat.univariate.SummaryStatistics;
+import org.apache.commons.math.stat.univariate.SummaryStatisticsImpl;
+import org.apache.commons.math.stat.univariate.DescriptiveStatistics;
+
/**
- * Test cases for the {@link DescriptiveStatistics} class.
+ * Certified data test cases.
* @version $Revision$ $Date$
*/
public class CertifiedDataTest extends TestCase {
@@ -64,18 +68,8 @@
/**
* Test StorelessDescriptiveStatistics
*/
- public void testUnivariateImpl() {
- SummaryStatistics u = null;
- try {
- u = SummaryStatistics.newInstance(SummaryStatisticsImpl.class);
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
+ public void testUnivariateImpl() throws Exception {
+ SummaryStatistics u = SummaryStatistics.newInstance(SummaryStatisticsImpl.class);
loadStats("data/PiDigits.txt", u);
assertEquals("PiDigits: std", std, u.getStandardDeviation(), .0000000000001);
assertEquals("PiDigits: mean", mean, u.getMean(), .0000000000001);
1.14 +3 -1 jakarta-commons/math/src/test/org/apache/commons/math/stat/StatUtilsTest.java
Index: StatUtilsTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/StatUtilsTest.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- StatUtilsTest.java 21 Feb 2004 21:35:17 -0000 1.13
+++ StatUtilsTest.java 12 Apr 2004 02:27:49 -0000 1.14
@@ -19,6 +19,8 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
+import org.apache.commons.math.stat.univariate.DescriptiveStatistics;
+
/**
* Test cases for the {@link StatUtils} class.
* @version $Revision$ $Date$
1.2 +3 -3 jakarta-commons/math/src/test/org/apache/commons/math/stat/data/CertifiedDataAbstractTest.java
Index: CertifiedDataAbstractTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/data/CertifiedDataAbstractTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CertifiedDataAbstractTest.java 18 Mar 2004 05:52:37 -0000 1.1
+++ CertifiedDataAbstractTest.java 12 Apr 2004 02:27:50 -0000 1.2
@@ -27,8 +27,8 @@
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.math.TestUtils;
-import org.apache.commons.math.stat.DescriptiveStatistics;
-import org.apache.commons.math.stat.SummaryStatistics;
+import org.apache.commons.math.stat.univariate.DescriptiveStatistics;
+import org.apache.commons.math.stat.univariate.SummaryStatistics;
import junit.framework.TestCase;
1.3 +2 -2 jakarta-commons/math/src/test/org/apache/commons/math/stat/inference/TestStatisticTest.java
Index: TestStatisticTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/inference/TestStatisticTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestStatisticTest.java 11 Apr 2004 20:42:33 -0000 1.2
+++ TestStatisticTest.java 12 Apr 2004 02:27:50 -0000 1.3
@@ -21,7 +21,7 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.apache.commons.math.stat.SummaryStatistics;
+import org.apache.commons.math.stat.univariate.SummaryStatistics;
/**
* Test cases for the TestStatistic class.
*
1.4 +1 -2 jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/UnivariateImplTest.java
Index: UnivariateImplTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/UnivariateImplTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- UnivariateImplTest.java 21 Feb 2004 21:35:17 -0000 1.3
+++ UnivariateImplTest.java 12 Apr 2004 02:27:50 -0000 1.4
@@ -15,7 +15,6 @@
*/
package org.apache.commons.math.stat.univariate;
-import org.apache.commons.math.stat.SummaryStatistics;
import junit.framework.Test;
import junit.framework.TestCase;
1.1 jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/BeanListUnivariateImplTest.java
Index: BeanListUnivariateImplTest.java
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.math.beans.*;
import org.apache.commons.math.stat.multivariate.*;
/**
* Test cases for the {@link BeanListUnivariateImpl} class.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:50 $
*/
public final class BeanListUnivariateImplTest extends TestCase {
private List patientList = null;
private double tolerance = Double.MIN_VALUE;
public BeanListUnivariateImplTest(String name) {
super(name);
}
public void setUp() {
patientList = new ArrayList();
// Create and add patient bean 1
VitalStats vs1 = new VitalStats( new Double(120.0),
new Double(96.4) );
Patient p1 = new Patient( vs1, new Integer( 35 ) );
patientList.add( p1 );
// Create and add patient bean 2
VitalStats vs2 = new VitalStats( new Double(70.0),
new Double(97.4) );
Patient p2 = new Patient( vs2, new Integer( 23 ) );
patientList.add( p2 );
// Create and add patient bean 3
VitalStats vs3 = new VitalStats( new Double(90.0),
new Double(98.6) );
Patient p3 = new Patient( vs3, new Integer( 42 ) );
patientList.add( p3 );
}
public static Test suite() {
TestSuite suite = new TestSuite(BeanListUnivariateImplTest.class);
suite.setName("Frequency Tests");
return suite;
}
/** test stats */
public void testStats() {
DescriptiveStatistics u = new BeanListUnivariateImpl( patientList );
assertEquals("total count",3,u.getN(),tolerance);
u.clear();
assertEquals("total count",0,u.getN(),tolerance);
}
public void testPropStats() {
DescriptiveStatistics heartU = new BeanListUnivariateImpl( patientList,
"vitalStats.heartRate" );
assertEquals( "Mean heart rate unexpected", 93.333,
heartU.getMean(), 0.001 );
assertEquals( "Max heart rate unexpected", 120.0,
heartU.getMax(), 0.001 );
DescriptiveStatistics ageU = new BeanListUnivariateImpl( patientList,
"age" );
assertEquals( "Mean age unexpected", 33.333,
ageU.getMean(), 0.001 );
assertEquals( "Max age unexpected", 42.0,
ageU.getMax(), 0.001 );
}
public void testSetPropertyName(){
BeanListUnivariateImpl u = new BeanListUnivariateImpl(null);
String expected = "property";
u.setPropertyName(expected);
assertEquals(expected, u.getPropertyName());
}
}
1.1 jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/DescriptiveStatisticsTest.java
Index: DescriptiveStatisticsTest.java
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.random.RandomData;
import org.apache.commons.math.random.RandomDataImpl;
/**
* Test cases for the {@link Univariate} class.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:50 $
*/
public final class DescriptiveStatisticsTest extends TestCase {
private double one = 1;
private float two = 2;
private int three = 3;
private double mean = 2;
private double sumSq = 18;
private double sum = 8;
private double var = 0.666666666666666666667;
private double std = Math.sqrt(var);
private double n = 4;
private double min = 1;
private double max = 3;
private double skewness = 0;
private double kurtosis = 0.5;
private int kClass = DescriptiveStatistics.LEPTOKURTIC;
private double tolerance = 10E-15;
public DescriptiveStatisticsTest(String name) {
super(name);
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(DescriptiveStatisticsTest.class);
suite.setName("Descriptive Statistics Tests");
return suite;
}
/** test stats */
public void testStats() {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(two);
u.addValue(two);
u.addValue(three);
assertEquals("N",n,u.getN(),tolerance);
assertEquals("sum",sum,u.getSum(),tolerance);
assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
assertEquals("var",var,u.getVariance(),tolerance);
assertEquals("std",std,u.getStandardDeviation(),tolerance);
assertEquals("mean",mean,u.getMean(),tolerance);
assertEquals("min",min,u.getMin(),tolerance);
assertEquals("max",max,u.getMax(),tolerance);
u.clear();
assertEquals("total count",0,u.getN(),tolerance);
}
public void testN0andN1Conditions() throws Exception {
DescriptiveStatistics u = DescriptiveStatistics.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() ) );
u.addValue(one);
assertTrue( "Mean of n = 1 set should be value of single item n1",
u.getMean() == one);
assertTrue( "StdDev of n = 1 set should be zero, instead it is: "
+ u.getStandardDeviation(), u.getStandardDeviation() == 0);
assertTrue( "Variance of n = 1 set should be zero",
u.getVariance() == 0);
}
public void testSkewAndKurtosis() {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
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);
}
public void testProductAndGeometricMean() throws Exception {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
u.setWindowSize(10);
u.addValue( 1.0 );
u.addValue( 2.0 );
u.addValue( 3.0 );
u.addValue( 4.0 );
//assertEquals( "Product not expected",
// 24.0, u.getProduct(), Double.MIN_VALUE );
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( "Product not expected", 39916800.0,
// u.getProduct(), 0.00001 );
assertEquals( "Geometric mean not expected", 5.755931,
u.getGeometricMean(), 0.00001 );
}
public void testAddValue() {
double[] test1 = {5,4,3,2,1,0};
double[] test2 = {5,2,1,0,4,3};
DescriptiveStatistics stats = DescriptiveStatistics.newInstance();
stats.setWindowSize(12);
for(int i = 0; i < test1.length; i++){
stats.addValue(test1[i]);
}
double[] test3 = stats.getValues();
for(int i = 0; i < 6; i++){
assertEquals( "Added value ["+i+"] not equal",
test3[i], test1[i],0.0);
//System.out.println(test3[i] + " "+test1[i]);
}
for(int i = 0; i < test2.length; i++){
stats.addValue(test2[i]);
}
test3 = stats.getValues();
for(int i = 6; i < 12; i++){
assertEquals( "Added value ["+i+"] not equal",
test3[i], test2[i-6],0.0);
//System.out.println(test3[i] + " "+test2[i-6]);
}
for(int i = 0; i < test2.length; i++){
stats.addValue(test2[i]);
}
test3 = stats.getValues();
for(int i = 0; i < 6; i++){
assertEquals( "Added value ["+i+"] not equal",
test3[i], test2[i],0.0);
//System.out.println(test3[i] + " "+test2[i]);
}
for(int i = 6; i < 12; i++){
assertEquals( "Added value ["+i+"] not equal",
test3[i], test2[i-6],0.0);
//System.out.println(test3[i] + " "+test2[i-6]);
}
}
public void testGetSortedValues() {
double[] test1 = {5,4,3,2,1};
double[] test2 = {5,2,1,3,4,0};
double[] test3 = {1};
int[] testi = null;
double[] test4 = null;
RandomData rd = new RandomDataImpl();
tstGetSortedValues(test1);
tstGetSortedValues(test2);
tstGetSortedValues(test3);
for (int i = 0; i < 10; i++) {
testi = rd.nextPermutation(10,6);
test4 = new double[6];
for (int j = 0; j < testi.length; j++) {
test4[j] = (double) testi[j];
}
tstGetSortedValues(test4);
}
for (int i = 0; i < 10; i++) {
testi = rd.nextPermutation(10,5);
test4 = new double[5];
for (int j = 0; j < testi.length; j++) {
test4[j] = (double) testi[j];
}
tstGetSortedValues(test4);
}
}
private void tstGetSortedValues(double[] test) {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
u.setWindowSize(test.length);
for (int i = 0; i < test.length; i++) {
u.addValue(test[i]);
}
double[] sorted = u.getSortedValues();
if (sorted.length != test.length) {
fail("wrong length for sorted values array");
}
for (int i = 0; i < sorted.length-1; i++) {
if (sorted[i] > sorted[i+1]) {
fail("sorted values out of sequence");
}
}
}
public void testPercentiles() {
double[] test = {5,4,3,2,1};
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
u.setWindowSize(110);
for (int i = 0; i < test.length; i++) {
u.addValue(test[i]);
}
assertEquals("expecting min",1,u.getPercentile(5),10E-12);
assertEquals("expecting max",5,u.getPercentile(99),10E-12);
assertEquals("expecting middle",3,u.getPercentile(50),10E-12);
try {
double x = u.getPercentile(0);
fail("expecting IllegalArgumentException for getPercentile(0)");
} catch (IllegalArgumentException ex) {
;
}
try {
double x = u.getPercentile(120);
fail("expecting IllegalArgumentException for getPercentile(120)");
} catch (IllegalArgumentException ex) {
;
}
u.clear();
double[] test2 = {1,2,3,4};
for (int i = 0; i < test2.length; i++) {
u.addValue(test2[i]);
}
assertEquals("Q1",1.25,u.getPercentile(25),10E-12);
assertEquals("Q3",3.75,u.getPercentile(75),10E-12);
assertEquals("Q2",2.5,u.getPercentile(50),10E-12);
u.clear();
double[] test3 = {1};
for (int i = 0; i < test3.length; i++) {
u.addValue(test3[i]);
}
assertEquals("Q1",1,u.getPercentile(25),10E-12);
assertEquals("Q3",1,u.getPercentile(75),10E-12);
assertEquals("Q2",1,u.getPercentile(50),10E-12);
u.clear();
RandomData rd = new RandomDataImpl();
int[] testi = rd.nextPermutation(100,100); // will contain 0-99
for (int j = 0; j < testi.length; j++) {
u.addValue((double) testi[j]); //OK, laugh at me for the cast
}
for (int i = 1; i < 100; i++) {
assertEquals("percentile " + i,
(double) i-1 + (double) i*(.01), u.getPercentile(i),10E-12);
}
u.clear();
double[] test4 = {1,2,3,4,100};
for (int i = 0; i < test4.length; i++) {
u.addValue(test4[i]);
}
assertEquals("80th",80.8,u.getPercentile(80),10E-12);
u.clear();
assertTrue("empty value set should return NaN",
Double.isNaN(u.getPercentile(50)));
}
/** test stats */
public void testSerialization() {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(two);
DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
u2.addValue(two);
u2.addValue(three);
assertEquals("N",n,u2.getN(),tolerance);
assertEquals("sum",sum,u2.getSum(),tolerance);
assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
assertEquals("var",var,u2.getVariance(),tolerance);
assertEquals("std",std,u2.getStandardDeviation(),tolerance);
assertEquals("mean",mean,u2.getMean(),tolerance);
assertEquals("min",min,u2.getMin(),tolerance);
assertEquals("max",max,u2.getMax(),tolerance);
u2.clear();
assertEquals("total count",0,u2.getN(),tolerance);
}
}
1.1 jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/ListUnivariateImpl.java
Index: ListUnivariateImpl.java
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import java.util.List;
import org.apache.commons.math.MathException;
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.stat.univariate.AbstractDescriptiveStatistics;
import org.apache.commons.math.util.DefaultTransformer;
import org.apache.commons.math.util.NumberTransformer;
/**
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:50 $
*/
public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
/**
* Holds a reference to a list - GENERICs are going to make
* out lives easier here as we could only accept List<Number>
*/
protected List list;
/** Number Transformer maps Objects to Number for us. */
protected NumberTransformer transformer;
/** 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
*/
public ListUnivariateImpl(List list) {
this(list, new DefaultTransformer());
}
/**
* Construct a ListUnivariate with a specific List.
* @param list The list that will back this DescriptiveStatistics
* @param transformer the number transformer used to convert the list items.
*/
public ListUnivariateImpl(List list, NumberTransformer transformer) {
super();
this.list = list;
this.transformer = transformer;
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getValues()
*/
public double[] getValues() {
int length = list.size();
// If the window size is not INFINITE_WINDOW AND
// the current list is larger that the window size, we need to
// take into account only the last n elements of the list
// as definied by windowSize
if (windowSize != DescriptiveStatistics.INFINITE_WINDOW &&
windowSize < list.size())
{
length = list.size() - Math.max(0, list.size() - windowSize);
}
// Create an array to hold all values
double[] copiedArray = new double[length];
for (int i = 0; i < copiedArray.length; i++) {
copiedArray[i] = getElement(i);
}
return copiedArray;
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getElement(int)
*/
public double getElement(int index) {
double value = Double.NaN;
int calcIndex = index;
if (windowSize != DescriptiveStatistics.INFINITE_WINDOW &&
windowSize < list.size())
{
calcIndex = (list.size() - windowSize) + index;
}
try {
value = transformer.transform(list.get(calcIndex));
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#getN()
*/
public long getN() {
int n = 0;
if (windowSize != DescriptiveStatistics.INFINITE_WINDOW) {
if (list.size() > windowSize) {
n = windowSize;
} else {
n = list.size();
}
} else {
n = list.size();
}
return n;
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#addValue(double)
*/
public void addValue(double v) {
list.add(new Double(v));
}
/**
* Adds an object to this list.
* @param o Object to add to the list
*/
public void addObject(Object o) {
list.add(o);
}
/**
* @see org.apache.commons.math.stat.univariate.DescriptiveStatistics#clear()
*/
public void clear() {
list.clear();
}
/**
* 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) {
double[] v = this.getValues();
if (v != null) {
return stat.evaluate(v, 0, v.length);
}
return Double.NaN;
}
/**
* Access the number transformer.
* @return the number transformer.
*/
public NumberTransformer getTransformer() {
return transformer;
}
/**
* Modify the number transformer.
* @param transformer the new number transformer.
*/
public void setTransformer(NumberTransformer transformer) {
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.1 jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/ListUnivariateImplTest.java
Index: ListUnivariateImplTest.java
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link Univariate} class.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:50 $
*/
public final class ListUnivariateImplTest extends TestCase {
private double one = 1;
private float two = 2;
private int three = 3;
private double mean = 2;
private double sumSq = 18;
private double sum = 8;
private double var = 0.666666666666666666667;
private double std = Math.sqrt(var);
private double n = 4;
private double min = 1;
private double max = 3;
private double skewness = 0;
private double kurtosis = 0.5;
private int kClass = DescriptiveStatistics.LEPTOKURTIC;
private double tolerance = 10E-15;
public ListUnivariateImplTest(String name) {
super(name);
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(ListUnivariateImplTest.class);
suite.setName("Frequency Tests");
return suite;
}
/** test stats */
public void testStats() {
List externalList = new ArrayList();
DescriptiveStatistics u = new ListUnivariateImpl( externalList );
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(two);
u.addValue(two);
u.addValue(three);
assertEquals("N",n,u.getN(),tolerance);
assertEquals("sum",sum,u.getSum(),tolerance);
assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
assertEquals("var",var,u.getVariance(),tolerance);
assertEquals("std",std,u.getStandardDeviation(),tolerance);
assertEquals("mean",mean,u.getMean(),tolerance);
assertEquals("min",min,u.getMin(),tolerance);
assertEquals("max",max,u.getMax(),tolerance);
u.clear();
assertEquals("total count",0,u.getN(),tolerance);
}
public void testN0andN1Conditions() throws Exception {
List list = new ArrayList();
DescriptiveStatistics u = new ListUnivariateImpl( list );
assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
list.add( new Double(one));
assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);
}
public void testSkewAndKurtosis() {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
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);
}
public void testProductAndGeometricMean() throws Exception {
ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList());
u.setWindowSize(10);
u.addValue( 1.0 );
u.addValue( 2.0 );
u.addValue( 3.0 );
u.addValue( 4.0 );
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 );
}
}
1.1 jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/MixedListUnivariateImplTest.java
Index: MixedListUnivariateImplTest.java
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.math.util.NumberTransformer;
import org.apache.commons.math.util.TransformerMap;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link Univariate} class.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:50 $
*/
public final class MixedListUnivariateImplTest extends TestCase {
private double one = 1;
private float two = 2;
private int three = 3;
private double mean = 2;
private double sumSq = 18;
private double sum = 8;
private double var = 0.666666666666666666667;
private double std = Math.sqrt(var);
private double n = 4;
private double min = 1;
private double max = 3;
private double skewness = 0;
private double kurtosis = 0.5;
private int kClass = DescriptiveStatistics.LEPTOKURTIC;
private double tolerance = 10E-15;
private TransformerMap transformers = new TransformerMap();
public MixedListUnivariateImplTest(String name) {
super(name);
transformers = new TransformerMap();
transformers.putTransformer(Foo.class, new NumberTransformer() {
public double transform(Object o) {
return Double.parseDouble(((Foo) o).heresFoo());
}
});
transformers.putTransformer(Bar.class, new NumberTransformer() {
public double transform(Object o) {
return Double.parseDouble(((Bar) o).heresBar());
}
});
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(MixedListUnivariateImplTest.class);
suite.setName("Mixed List Tests");
return suite;
}
/** test stats */
public void testStats() {
List externalList = new ArrayList();
DescriptiveStatistics u = new ListUnivariateImpl(externalList,transformers);
assertEquals("total count", 0, u.getN(), tolerance);
u.addValue(one);
u.addValue(two);
u.addValue(two);
u.addValue(three);
assertEquals("N", n, u.getN(), tolerance);
assertEquals("sum", sum, u.getSum(), tolerance);
assertEquals("sumsq", sumSq, u.getSumsq(), tolerance);
assertEquals("var", var, u.getVariance(), tolerance);
assertEquals("std", std, u.getStandardDeviation(), tolerance);
assertEquals("mean", mean, u.getMean(), tolerance);
assertEquals("min", min, u.getMin(), tolerance);
assertEquals("max", max, u.getMax(), tolerance);
u.clear();
assertEquals("total count", 0, u.getN(), tolerance);
}
public void testN0andN1Conditions() throws Exception {
List list = new ArrayList();
DescriptiveStatistics u = new ListUnivariateImpl(new ArrayList(),transformers);
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()));
u.addValue(one);
assertTrue(
"Mean of n = 1 set should be value of single item n1, instead it is " + u.getMean() ,
u.getMean() == one);
assertTrue(
"StdDev of n = 1 set should be zero, instead it is: "
+ u.getStandardDeviation(),
u.getStandardDeviation() == 0);
assertTrue(
"Variance of n = 1 set should be zero",
u.getVariance() == 0);
}
public void testSkewAndKurtosis() {
ListUnivariateImpl u =
new ListUnivariateImpl(new ArrayList(), transformers);
u.addObject("12.5");
u.addObject(new Integer(12));
u.addObject("11.8");
u.addObject("14.2");
u.addObject(new Foo());
u.addObject("14.5");
u.addObject(new Long(21));
u.addObject("8.2");
u.addObject("10.3");
u.addObject("11.3");
u.addObject(new Float(14.1));
u.addObject("9.9");
u.addObject("12.2");
u.addObject(new Bar());
u.addObject("12.1");
u.addObject("11");
u.addObject(new Double(19.8));
u.addObject("11");
u.addObject("10");
u.addObject("8.8");
u.addObject("9");
u.addObject("12.3");
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);
}
public void testProductAndGeometricMean() throws Exception {
ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList(),transformers);
u.setWindowSize(10);
u.addValue(1.0);
u.addValue(2.0);
u.addValue(3.0);
u.addValue(4.0);
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 final class Foo {
public String heresFoo() {
return "14.9";
}
}
public final class Bar {
public String heresBar() {
return "12.0";
}
}
}
1.1 jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/StoreUnivariateImplTest.java
Index: StoreUnivariateImplTest.java
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.univariate;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.math.random.RandomData;
import org.apache.commons.math.random.RandomDataImpl;
/**
* Test cases for the {@link Univariate} class.
*
* @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:50 $
*/
public final class StoreUnivariateImplTest extends TestCase {
private double one = 1;
private float two = 2;
private int three = 3;
private double mean = 2;
private double sumSq = 18;
private double sum = 8;
private double var = 0.666666666666666666667;
private double std = Math.sqrt(var);
private double n = 4;
private double min = 1;
private double max = 3;
private double skewness = 0;
private double kurtosis = 0.5;
private int kClass = DescriptiveStatistics.LEPTOKURTIC;
private double tolerance = 10E-15;
public StoreUnivariateImplTest(String name) {
super(name);
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(StoreUnivariateImplTest.class);
suite.setName("Frequency Tests");
return suite;
}
/** test stats */
public void testStats() {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(two);
u.addValue(two);
u.addValue(three);
assertEquals("N",n,u.getN(),tolerance);
assertEquals("sum",sum,u.getSum(),tolerance);
assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
assertEquals("var",var,u.getVariance(),tolerance);
assertEquals("std",std,u.getStandardDeviation(),tolerance);
assertEquals("mean",mean,u.getMean(),tolerance);
assertEquals("min",min,u.getMin(),tolerance);
assertEquals("max",max,u.getMax(),tolerance);
u.clear();
assertEquals("total count",0,u.getN(),tolerance);
}
public void testN0andN1Conditions() throws Exception {
DescriptiveStatistics u = DescriptiveStatistics.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() ) );
u.addValue(one);
assertTrue( "Mean of n = 1 set should be value of single item n1",
u.getMean() == one);
assertTrue( "StdDev of n = 1 set should be zero, instead it is: "
+ u.getStandardDeviation(), u.getStandardDeviation() == 0);
assertTrue( "Variance of n = 1 set should be zero",
u.getVariance() == 0);
}
public void testSkewAndKurtosis() {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
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);
}
public void testProductAndGeometricMean() throws Exception {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
u.setWindowSize(10);
u.addValue( 1.0 );
u.addValue( 2.0 );
u.addValue( 3.0 );
u.addValue( 4.0 );
//assertEquals( "Product not expected",
// 24.0, u.getProduct(), Double.MIN_VALUE );
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( "Product not expected", 39916800.0,
// u.getProduct(), 0.00001 );
assertEquals( "Geometric mean not expected", 5.755931,
u.getGeometricMean(), 0.00001 );
}
public void testGetSortedValues() {
double[] test1 = {5,4,3,2,1};
double[] test2 = {5,2,1,3,4,0};
double[] test3 = {1};
int[] testi = null;
double[] test4 = null;
RandomData rd = new RandomDataImpl();
tstGetSortedValues(test1);
tstGetSortedValues(test2);
tstGetSortedValues(test3);
for (int i = 0; i < 10; i++) {
testi = rd.nextPermutation(10,6);
test4 = new double[6];
for (int j = 0; j < testi.length; j++) {
test4[j] = (double) testi[j];
}
tstGetSortedValues(test4);
}
for (int i = 0; i < 10; i++) {
testi = rd.nextPermutation(10,5);
test4 = new double[5];
for (int j = 0; j < testi.length; j++) {
test4[j] = (double) testi[j];
}
tstGetSortedValues(test4);
}
}
private void tstGetSortedValues(double[] test) {
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
for (int i = 0; i < test.length; i++) {
u.addValue(test[i]);
}
double[] sorted = u.getSortedValues();
if (sorted.length != test.length) {
fail("wrong length for sorted values array");
}
for (int i = 0; i < sorted.length-1; i++) {
if (sorted[i] > sorted[i+1]) {
fail("sorted values out of sequence");
}
}
}
public void testPercentiles() {
double[] test = {5,4,3,2,1};
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
for (int i = 0; i < test.length; i++) {
u.addValue(test[i]);
}
assertEquals("expecting min",1,u.getPercentile(5),10E-12);
assertEquals("expecting max",5,u.getPercentile(99),10E-12);
assertEquals("expecting middle",3,u.getPercentile(50),10E-12);
try {
double x = u.getPercentile(0);
fail("expecting IllegalArgumentException for getPercentile(0)");
} catch (IllegalArgumentException ex) {
;
}
try {
double x = u.getPercentile(120);
fail("expecting IllegalArgumentException for getPercentile(120)");
} catch (IllegalArgumentException ex) {
;
}
u.clear();
double[] test2 = {1,2,3,4};
for (int i = 0; i < test2.length; i++) {
u.addValue(test2[i]);
}
assertEquals("Q1",1.25,u.getPercentile(25),10E-12);
assertEquals("Q3",3.75,u.getPercentile(75),10E-12);
assertEquals("Q2",2.5,u.getPercentile(50),10E-12);
u.clear();
double[] test3 = {1};
for (int i = 0; i < test3.length; i++) {
u.addValue(test3[i]);
}
assertEquals("Q1",1,u.getPercentile(25),10E-12);
assertEquals("Q3",1,u.getPercentile(75),10E-12);
assertEquals("Q2",1,u.getPercentile(50),10E-12);
u.clear();
RandomData rd = new RandomDataImpl();
int[] testi = rd.nextPermutation(100,100); // will contain 0-99
for (int j = 0; j < testi.length; j++) {
u.addValue((double) testi[j]); //OK, laugh at me for the cast
}
for (int i = 1; i < 100; i++) {
assertEquals("percentile " + i,
(double) i-1 + (double) i*(.01), u.getPercentile(i),10E-12);
}
u.clear();
double[] test4 = {1,2,3,4,100};
for (int i = 0; i < test4.length; i++) {
u.addValue(test4[i]);
}
assertEquals("80th",80.8,u.getPercentile(80),10E-12);
u.clear();
assertTrue("empty value set should return NaN",
Double.isNaN(u.getPercentile(50)));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org