Reapply changes (mdiggory 2003/11/10 09:43:34)
Moving Distributions to higher level location from o.a.c.m.stat.distirbutions to o.a.c.m.distirbutions Changing Names of Univariates to DescriptiveStatistics and replacing Interface with Abstract Factory Implementation Moving BeanTransformer and ListUnivariate implmenetations to test directory. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141016 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8cf1bdb6b9
commit
e5833affa1
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.analysis.UnivariateRealFunction;
|
||||
|
@ -62,7 +62,7 @@ import org.apache.commons.math.analysis.UnivariateRealSolverUtils;
|
|||
* implementations for some of the methods that do not vary from distribution
|
||||
* to distribution.
|
||||
*
|
||||
* @version $Revision: 1.13 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.14 $ $Date: 2003/11/15 16:01:35 $
|
||||
*/
|
||||
public abstract class AbstractContinuousDistribution
|
||||
implements ContinuousDistribution {
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -59,7 +59,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
* implementations for some of the methods that do not vary from distribution
|
||||
* to distribution.
|
||||
*
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/15 16:01:35 $
|
||||
*/
|
||||
public abstract class AbstractDiscreteDistribution
|
||||
implements DiscreteDistribution {
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* The Binomial Distribution.
|
||||
|
@ -65,7 +65,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
* Binomial Distribution</a></li>
|
||||
* </ul>
|
||||
*
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/15 16:01:35 $
|
||||
*/
|
||||
public interface BinomialDistribution extends DiscreteDistribution {
|
||||
/**
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.math.MathException;
|
||||
|
@ -61,7 +61,7 @@ import org.apache.commons.math.util.MathUtils;
|
|||
/**
|
||||
* The default implementation of {@link BinomialDistribution}.
|
||||
*
|
||||
* @version $Revision: 1.5 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:35 $
|
||||
*/
|
||||
public class BinomialDistributionImpl extends AbstractDiscreteDistribution
|
||||
implements BinomialDistribution {
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* The Chi-Squared Distribution.
|
||||
|
@ -65,7 +65,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
* Chi-Squared Distribution</a></li>
|
||||
* </ul>
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:35 $
|
||||
*/
|
||||
public interface ChiSquaredDistribution extends ContinuousDistribution {
|
||||
/**
|
||||
|
|
|
@ -51,12 +51,12 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* The default implementation of {@link ChiSquaredDistribution}
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:35 $
|
||||
*/
|
||||
public class ChiSquaredDistributionImpl
|
||||
extends AbstractContinuousDistribution
|
||||
|
|
|
@ -51,12 +51,12 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* Base interface for various continuous distributions.
|
||||
*
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:35 $
|
||||
*/
|
||||
public interface ContinuousDistribution {
|
||||
/**
|
||||
|
|
|
@ -51,12 +51,12 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* Base interface for various discrete distributions.
|
||||
*
|
||||
* @version $Revision: 1.5 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:35 $
|
||||
*/
|
||||
public interface DiscreteDistribution {
|
||||
/**
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import org.apache.commons.discovery.tools.DiscoverClass;
|
||||
|
||||
|
@ -75,7 +75,7 @@ import org.apache.commons.discovery.tools.DiscoverClass;
|
|||
* ChiSquaredDistribution chi = factory.createChiSquareDistribution(5.0);
|
||||
* </pre>
|
||||
*
|
||||
* @version $Revision: 1.16 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.17 $ $Date: 2003/11/15 16:01:35 $
|
||||
*/
|
||||
public abstract class DistributionFactory {
|
||||
/**
|
||||
|
@ -95,7 +95,7 @@ public abstract class DistributionFactory {
|
|||
DiscoverClass dc = new DiscoverClass();
|
||||
factory = (DistributionFactory) dc.newInstance(
|
||||
DistributionFactory.class,
|
||||
"org.apache.commons.math.stat.distribution.DistributionFactoryImpl");
|
||||
"org.apache.commons.math.distribution.DistributionFactoryImpl");
|
||||
} catch(Exception ex) {
|
||||
// ignore as default implementation will be used.
|
||||
}
|
||||
|
|
|
@ -51,14 +51,14 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
|
||||
/**
|
||||
* A concrete distribution factory. This is the default factory used by
|
||||
* Commons-Math.
|
||||
*
|
||||
* @version $Revision: 1.14 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.15 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public class DistributionFactoryImpl extends DistributionFactory {
|
||||
/**
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* The Exponential Distribution.
|
||||
|
@ -64,7 +64,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
* <li><a href="http://mathworld.wolfram.com/ExponentialDistribution.html">
|
||||
* Exponential Distribution</a></li>
|
||||
*
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public interface ExponentialDistribution extends ContinuousDistribution {
|
||||
/**
|
||||
|
|
|
@ -51,12 +51,12 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* The default implementation of {@link ExponentialDistribution}
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public class ExponentialDistributionImpl
|
||||
implements ExponentialDistribution {
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* F-Distribution.
|
||||
|
@ -65,7 +65,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
* F-Distribution</a></li>
|
||||
* </ul>
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public interface FDistribution extends ContinuousDistribution {
|
||||
/**
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.math.MathException;
|
||||
|
@ -61,7 +61,7 @@ import org.apache.commons.math.special.Beta;
|
|||
* Default implementation of
|
||||
* {@link org.apache.commons.math.stat.distribution.FDistribution}.
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public class FDistributionImpl
|
||||
extends AbstractContinuousDistribution
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* The Gamma Distribution.
|
||||
|
@ -65,7 +65,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
* Gamma Distribution</a></li>
|
||||
* </ul>
|
||||
*
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.11 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public interface GammaDistribution extends ContinuousDistribution {
|
||||
/**
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.math.MathException;
|
||||
|
@ -60,7 +60,7 @@ import org.apache.commons.math.special.Gamma;
|
|||
/**
|
||||
* The default implementation of {@link GammaDistribution}
|
||||
*
|
||||
* @version $Revision: 1.11 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.12 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public class GammaDistributionImpl extends AbstractContinuousDistribution
|
||||
implements GammaDistribution {
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* The Hypergeometric Distribution.
|
||||
|
@ -66,7 +66,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
* Hypergeometric Distribution</a></li>
|
||||
* </ul>
|
||||
*
|
||||
* @version $Revision: 1.5 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public interface HypergeometricDistribution extends DiscreteDistribution {
|
||||
/**
|
||||
|
|
|
@ -52,14 +52,14 @@
|
|||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import org.apache.commons.math.util.MathUtils;
|
||||
|
||||
/**
|
||||
* The default implementation of {@link HypergeometricDistribution}.
|
||||
*
|
||||
* @version $Revision: 1.5 $ $Date: 2003/11/14 22:22:19 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public class HypergeometricDistributionImpl extends AbstractDiscreteDistribution
|
||||
implements HypergeometricDistribution
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
/**
|
||||
* Student's t-Distribution.
|
||||
|
@ -65,7 +65,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
* Student's t-Distribution</a></li>
|
||||
* </ul>
|
||||
*
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:25:28 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public interface TDistribution extends ContinuousDistribution {
|
||||
/**
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.math.MathException;
|
||||
|
@ -61,7 +61,7 @@ import org.apache.commons.math.special.Beta;
|
|||
* Default implementation of
|
||||
* {@link org.apache.commons.math.stat.distribution.TDistribution}.
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:25:28 $
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:36 $
|
||||
*/
|
||||
public class TDistributionImpl
|
||||
extends AbstractContinuousDistribution
|
||||
|
|
|
@ -58,7 +58,7 @@ import java.io.IOException;
|
|||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.commons.math.stat.Univariate;
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
|
||||
/**
|
||||
* Represents an <a href="http://random.mat.sbg.ac.at/~ste/dipl/node11.html">
|
||||
|
@ -80,7 +80,7 @@ import org.apache.commons.math.stat.Univariate;
|
|||
* 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.9 $ $Date: 2003/11/14 22:22:21 $
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:37 $
|
||||
*/
|
||||
public interface EmpiricalDistribution {
|
||||
|
||||
|
@ -109,13 +109,13 @@ public interface EmpiricalDistribution {
|
|||
|
||||
|
||||
/**
|
||||
* Returns a Univariate describing this distribution.
|
||||
* Returns a DescriptiveStatistics describing this distribution.
|
||||
* <strong>Preconditions:</strong><ul>
|
||||
* <li>the distribution must be loaded before invoking this method</li></ul>
|
||||
* @return the sample statistics
|
||||
* @throws IllegalStateException if the distribution has not been loaded
|
||||
*/
|
||||
Univariate getSampleStats() throws IllegalStateException;
|
||||
DescriptiveStatistics getSampleStats() throws IllegalStateException;
|
||||
|
||||
/**
|
||||
* Loads a saved distribution from a file.
|
||||
|
|
|
@ -61,8 +61,8 @@ import java.io.FileReader;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.math.stat.Univariate;
|
||||
import org.apache.commons.math.stat.UnivariateImpl;
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
||||
|
||||
/**
|
||||
* Implements <code>EmpiricalDistribution</code> interface. This implementation
|
||||
|
@ -89,16 +89,16 @@ import org.apache.commons.math.stat.UnivariateImpl;
|
|||
* entry per line.</li>
|
||||
* </ol></p>
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/14 22:22:21 $
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:37 $
|
||||
*/
|
||||
public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribution {
|
||||
|
||||
|
||||
/** List of Univariate objects characterizing the bins */
|
||||
/** List of DescriptiveStatistics objects characterizing the bins */
|
||||
private ArrayList binStats = null;
|
||||
|
||||
/** Sample statistics */
|
||||
Univariate sampleStats = null;
|
||||
DescriptiveStatistics sampleStats = null;
|
||||
|
||||
/** number of bins */
|
||||
private int binCount = 1000;
|
||||
|
@ -142,7 +142,7 @@ public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribu
|
|||
in = new BufferedReader(new FileReader(file));
|
||||
String str = null;
|
||||
double val = 0.0;
|
||||
sampleStats = new UnivariateImpl();
|
||||
sampleStats = new StorelessDescriptiveStatisticsImpl();
|
||||
while ((str = in.readLine()) != null) {
|
||||
val = new Double(str).doubleValue();
|
||||
sampleStats.addValue(val);
|
||||
|
@ -169,7 +169,7 @@ public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribu
|
|||
binStats.clear();
|
||||
}
|
||||
for (int i = 0; i < binCount; i++) {
|
||||
Univariate stats = new UnivariateImpl();
|
||||
DescriptiveStatistics stats = new StorelessDescriptiveStatisticsImpl();
|
||||
binStats.add(i,stats);
|
||||
}
|
||||
|
||||
|
@ -190,7 +190,7 @@ public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribu
|
|||
}
|
||||
if (val <= binUpperBounds[i]) {
|
||||
found = true;
|
||||
Univariate stats = (Univariate)binStats.get(i);
|
||||
DescriptiveStatistics stats = (DescriptiveStatistics)binStats.get(i);
|
||||
stats.addValue(val);
|
||||
}
|
||||
i++;
|
||||
|
@ -205,11 +205,11 @@ public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribu
|
|||
// Assign upperBounds based on bin counts
|
||||
upperBounds = new double[binCount];
|
||||
upperBounds[0] =
|
||||
((double)((Univariate)binStats.get(0)).getN())/
|
||||
((double)((DescriptiveStatistics)binStats.get(0)).getN())/
|
||||
(double)sampleStats.getN();
|
||||
for (int i = 1; i < binCount-1; i++) {
|
||||
upperBounds[i] = upperBounds[i-1] +
|
||||
((double)((Univariate)binStats.get(i)).getN())/
|
||||
((double)((DescriptiveStatistics)binStats.get(i)).getN())/
|
||||
(double)sampleStats.getN();
|
||||
}
|
||||
upperBounds[binCount-1] = 1.0d;
|
||||
|
@ -234,7 +234,7 @@ public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribu
|
|||
// Use this to select the bin and generate a Gaussian within the bin
|
||||
for (int i = 0; i < binCount; i++) {
|
||||
if (x <= upperBounds[i]) {
|
||||
Univariate stats = (Univariate)binStats.get(i);
|
||||
DescriptiveStatistics stats = (DescriptiveStatistics)binStats.get(i);
|
||||
if (stats.getN() > 0) {
|
||||
if (stats.getStandardDeviation() > 0) { // more than one obs
|
||||
return randomData.nextGaussian
|
||||
|
@ -266,7 +266,7 @@ public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribu
|
|||
throw new UnsupportedOperationException("Not Implemented yet :-(");
|
||||
}
|
||||
|
||||
public Univariate getSampleStats() {
|
||||
public DescriptiveStatistics getSampleStats() {
|
||||
return sampleStats;
|
||||
}
|
||||
|
||||
|
|
|
@ -59,32 +59,31 @@ import org.apache.commons.math.stat.univariate.rank.Percentile;
|
|||
|
||||
/**
|
||||
* Provides univariate measures for an array of doubles.
|
||||
* @version $Revision: 1.15 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:37 $
|
||||
*/
|
||||
public abstract class AbstractStoreUnivariate
|
||||
extends AbstractUnivariate
|
||||
implements StoreUnivariate {
|
||||
public abstract class AbstractDescriptiveStatistics
|
||||
extends AbstractStorelessDescriptiveStatistics {
|
||||
|
||||
/** Percentile */
|
||||
protected Percentile percentile = new Percentile(50);
|
||||
|
||||
/**
|
||||
* Create an AbstractStoreUnivariate
|
||||
* Create an AbstractDescriptiveStatistics
|
||||
*/
|
||||
public AbstractStoreUnivariate() {
|
||||
public AbstractDescriptiveStatistics() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an AbstractStoreUnivariate with a specific Window
|
||||
* Create an AbstractDescriptiveStatistics with a specific Window
|
||||
* @param window WindowSIze for stat calculation
|
||||
*/
|
||||
public AbstractStoreUnivariate(int window) {
|
||||
public AbstractDescriptiveStatistics(int window) {
|
||||
super(window);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getPercentile(double)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getPercentile(double)
|
||||
*/
|
||||
public double getPercentile(double p) {
|
||||
percentile.setPercentile(p);
|
||||
|
@ -92,7 +91,7 @@ public abstract class AbstractStoreUnivariate
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getSortedValues()
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getSortedValues()
|
||||
*/
|
||||
public double[] getSortedValues() {
|
||||
double[] sort = getValues();
|
||||
|
@ -106,13 +105,13 @@ public abstract class AbstractStoreUnivariate
|
|||
public abstract void addValue(double value);
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getValues()
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getValues()
|
||||
*/
|
||||
public abstract double[] getValues();
|
||||
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getElement(int)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getElement(int)
|
||||
*/
|
||||
public abstract double getElement(int index);
|
||||
|
|
@ -68,12 +68,12 @@ import org.apache.commons.math.stat.univariate.summary.SumOfSquares;
|
|||
|
||||
/**
|
||||
* Provides univariate measures for an array of doubles.
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:37 $
|
||||
*/
|
||||
public abstract class AbstractUnivariate implements Univariate {
|
||||
public abstract class AbstractStorelessDescriptiveStatistics extends DescriptiveStatistics {
|
||||
|
||||
/** hold the window size **/
|
||||
protected int windowSize = Univariate.INFINITE_WINDOW;
|
||||
protected int windowSize = INFINITE_WINDOW;
|
||||
|
||||
/** count of values that have been added */
|
||||
protected int n = 0;
|
||||
|
@ -112,9 +112,9 @@ public abstract class AbstractUnivariate implements Univariate {
|
|||
protected Kurtosis kurtosis = null;
|
||||
|
||||
/**
|
||||
* Construct an AbstractUnivariate
|
||||
* Construct an AbstractStorelessDescriptiveStatistics
|
||||
*/
|
||||
public AbstractUnivariate() {
|
||||
public AbstractStorelessDescriptiveStatistics() {
|
||||
super();
|
||||
|
||||
sum = new Sum();
|
||||
|
@ -132,10 +132,10 @@ public abstract class AbstractUnivariate implements Univariate {
|
|||
}
|
||||
|
||||
/**
|
||||
* Construct an AbstractUnivariate with a window
|
||||
* Construct an AbstractStorelessDescriptiveStatistics with a window
|
||||
* @param window The Window Size
|
||||
*/
|
||||
public AbstractUnivariate(int window) {
|
||||
public AbstractStorelessDescriptiveStatistics(int window) {
|
||||
this();
|
||||
setWindowSize(window);
|
||||
}
|
||||
|
@ -244,16 +244,16 @@ public abstract class AbstractUnivariate implements Univariate {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getKurtosisClass()
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getKurtosisClass()
|
||||
*/
|
||||
public int getKurtosisClass() {
|
||||
int kClass = Univariate.MESOKURTIC;
|
||||
int kClass = MESOKURTIC;
|
||||
|
||||
double kurtosis = getKurtosis();
|
||||
if (kurtosis > 0) {
|
||||
kClass = Univariate.LEPTOKURTIC;
|
||||
kClass = LEPTOKURTIC;
|
||||
} else if (kurtosis < 0) {
|
||||
kClass = Univariate.PLATYKURTIC;
|
||||
kClass = PLATYKURTIC;
|
||||
}
|
||||
return (kClass);
|
||||
}
|
|
@ -1,75 +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 org.apache.commons.math.stat.univariate.UnivariateStatistic;
|
||||
|
||||
/**
|
||||
* Applyable.java
|
||||
*
|
||||
* TODO: add javadocs
|
||||
*
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/14 22:22:18 $
|
||||
*/
|
||||
public interface Applyable {
|
||||
|
||||
/**
|
||||
* Applies a UnivariateStatistic object against this object
|
||||
* and returns the result.
|
||||
* @param stat The stat to apply.
|
||||
* @return The result value of the application.
|
||||
*/
|
||||
double apply(UnivariateStatistic stat);
|
||||
|
||||
}
|
|
@ -53,8 +53,8 @@
|
|||
*/
|
||||
|
||||
package org.apache.commons.math.stat;
|
||||
import org.apache.commons.math.stat.distribution.DistributionFactory;
|
||||
import org.apache.commons.math.stat.distribution.TDistribution;
|
||||
import org.apache.commons.math.distribution.DistributionFactory;
|
||||
import org.apache.commons.math.distribution.TDistribution;
|
||||
|
||||
/**
|
||||
* Estimates an ordinary least squares regression model
|
||||
|
@ -84,7 +84,7 @@ import org.apache.commons.math.stat.distribution.TDistribution;
|
|||
* the necessary computations to return the requested statistic.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:38 $
|
||||
*/
|
||||
public class BivariateRegression {
|
||||
|
||||
|
|
|
@ -0,0 +1,285 @@
|
|||
/* ====================================================================
|
||||
* 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 org.apache.commons.discovery.tools.DiscoverClass;
|
||||
|
||||
/**
|
||||
* DescriptiveStatistics implements the Univariate interface
|
||||
* but maintains the set of values which contribute to
|
||||
* the values being returned. This implementation of
|
||||
* Univariate provides additional percentile functionality
|
||||
* such as. This additional functionality comes with
|
||||
* a price of increased storage costs.
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:38 $
|
||||
*/
|
||||
public abstract class DescriptiveStatistics {
|
||||
|
||||
/**
|
||||
* 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.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 int 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.
|
||||
* This estimate follows the interpolation-adjusted defintion presented
|
||||
* <a href="http://www.utdallas.edu/~ammann/stat5311/node8.html">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);
|
||||
|
||||
}
|
|
@ -53,13 +53,13 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
|
||||
import org.apache.commons.math.stat.univariate.*;
|
||||
import org.apache.commons.math.util.ContractableDoubleArray;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.12 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:38 $
|
||||
*/
|
||||
public class StoreUnivariateImpl extends AbstractStoreUnivariate {
|
||||
public class DescriptiveStatisticsImpl extends AbstractDescriptiveStatistics {
|
||||
|
||||
/** A contractable double array is used. memory is reclaimed when
|
||||
* the storage of the array becomes too empty.
|
||||
|
@ -67,14 +67,14 @@ public class StoreUnivariateImpl extends AbstractStoreUnivariate {
|
|||
protected ContractableDoubleArray eDA;
|
||||
|
||||
/**
|
||||
* Construct a StoreUnivariateImpl
|
||||
* Construct a DescriptiveStatisticsImpl
|
||||
*/
|
||||
public StoreUnivariateImpl() {
|
||||
public DescriptiveStatisticsImpl() {
|
||||
eDA = new ContractableDoubleArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getValues()
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getValues()
|
||||
*/
|
||||
public double[] getValues() {
|
||||
|
||||
|
@ -89,7 +89,7 @@ public class StoreUnivariateImpl extends AbstractStoreUnivariate {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getElement(int)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getElement(int)
|
||||
*/
|
||||
public double getElement(int index) {
|
||||
return eDA.getElement(index);
|
||||
|
@ -106,7 +106,7 @@ public class StoreUnivariateImpl extends AbstractStoreUnivariate {
|
|||
* @see org.apache.commons.math.stat.Univariate#addValue(double)
|
||||
*/
|
||||
public synchronized void addValue(double v) {
|
||||
if (windowSize != Univariate.INFINITE_WINDOW) {
|
||||
if (windowSize != INFINITE_WINDOW) {
|
||||
if (getN() == windowSize) {
|
||||
eDA.addElementRolling(v);
|
||||
} else if (getN() < windowSize) {
|
|
@ -1,113 +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;
|
||||
|
||||
/**
|
||||
* StoreUnivariate implements the Univariate interface
|
||||
* but maintains the set of values which contribute to
|
||||
* the values being returned. This implementation of
|
||||
* Univariate provides additional percentile functionality
|
||||
* such as. This additional functionality comes with
|
||||
* a price of increased storage costs.
|
||||
* @version $Revision: 1.11 $ $Date: 2003/11/14 22:22:18 $
|
||||
*/
|
||||
public interface StoreUnivariate extends Univariate {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
double[] getSortedValues();
|
||||
|
||||
/**
|
||||
* Returns the element at the specified index
|
||||
* @param index The Index of the element
|
||||
* @return return the element at the specified index
|
||||
*/
|
||||
double getElement(int index);
|
||||
|
||||
/**
|
||||
* Returns an estimate for the pth percentile of the stored values.
|
||||
* This estimate follows the interpolation-adjusted defintion presented
|
||||
* <a href="http://www.utdallas.edu/~ammann/stat5311/node8.html">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
|
||||
*/
|
||||
double getPercentile(double p);
|
||||
|
||||
}
|
|
@ -0,0 +1,210 @@
|
|||
/* ====================================================================
|
||||
* 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.1 $ $Date: 2003/11/15 16:01:38 $
|
||||
*/
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
|
@ -55,7 +55,7 @@ package org.apache.commons.math.stat;
|
|||
/**
|
||||
* A collection of commonly used test statistics and statistical tests.
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:38 $
|
||||
*/
|
||||
public interface TestStatistic {
|
||||
|
||||
|
@ -349,11 +349,11 @@ public interface TestStatistic {
|
|||
* </li></ul>
|
||||
*
|
||||
* @param mu comparison constant
|
||||
* @param sampleStats Univariate holding sample summary statitstics
|
||||
* @param sampleStats DescriptiveStatistics holding sample summary statitstics
|
||||
* @return t statistic
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
double t(double mu, Univariate sampleStats)
|
||||
double t(double mu, DescriptiveStatistics sampleStats)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
|
@ -369,12 +369,12 @@ public interface TestStatistic {
|
|||
* at least 5 observations.
|
||||
* </li></ul>
|
||||
*
|
||||
* @param sampleStats1 Univariate describing data from the first sample
|
||||
* @param sampleStats2 Univariate describing data from the second sample
|
||||
* @param sampleStats1 DescriptiveStatistics describing data from the first sample
|
||||
* @param sampleStats2 DescriptiveStatistics describing data from the second sample
|
||||
* @return t statistic
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
double t(Univariate sampleStats1, Univariate sampleStats2)
|
||||
double t(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
|
@ -403,12 +403,12 @@ public interface TestStatistic {
|
|||
* at least 5 observations.
|
||||
* </li></ul>
|
||||
*
|
||||
* @param sampleStats1 Univariate describing data from the first sample
|
||||
* @param sampleStats2 Univariate describing data from the second sample
|
||||
* @param sampleStats1 DescriptiveStatistics describing data from the first sample
|
||||
* @param sampleStats2 DescriptiveStatistics describing data from the second sample
|
||||
* @return p-value for t-test
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
double tTest(Univariate sampleStats1, Univariate sampleStats2)
|
||||
double tTest(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
|
@ -450,14 +450,14 @@ public interface TestStatistic {
|
|||
* <li> <code> 0 < alpha < 0.5 </code>
|
||||
* </li></ul>
|
||||
*
|
||||
* @param sampleStats1 Univariate describing sample data values
|
||||
* @param sampleStats2 Univariate describing sample data values
|
||||
* @param sampleStats1 DescriptiveStatistics describing sample data values
|
||||
* @param sampleStats2 DescriptiveStatistics 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(Univariate sampleStats1, Univariate sampleStats2,
|
||||
boolean tTest(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2,
|
||||
double alpha)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
|
@ -492,12 +492,12 @@ public interface TestStatistic {
|
|||
* </li></ul>
|
||||
*
|
||||
* @param mu constant value to compare sample mean against
|
||||
* @param sampleStats Univariate describing sample data values
|
||||
* @param sampleStats DescriptiveStatistics 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, Univariate sampleStats, double alpha)
|
||||
boolean tTest(double mu, DescriptiveStatistics sampleStats, double alpha)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
|
@ -523,11 +523,11 @@ public interface TestStatistic {
|
|||
* </li></ul>
|
||||
*
|
||||
* @param mu constant value to compare sample mean against
|
||||
* @param sampleStats Univariate describing sample data
|
||||
* @param sampleStats DescriptiveStatistics describing sample data
|
||||
* @return p-value
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
double tTest(double mu, Univariate sampleStats)
|
||||
double tTest(double mu, DescriptiveStatistics sampleStats)
|
||||
throws IllegalArgumentException;
|
||||
}
|
||||
|
||||
|
|
|
@ -54,14 +54,14 @@
|
|||
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
import org.apache.commons.math.stat.distribution.DistributionFactory;
|
||||
import org.apache.commons.math.stat.distribution.TDistribution;
|
||||
import org.apache.commons.math.stat.distribution.ChiSquaredDistribution;
|
||||
import org.apache.commons.math.distribution.DistributionFactory;
|
||||
import org.apache.commons.math.distribution.TDistribution;
|
||||
import org.apache.commons.math.distribution.ChiSquaredDistribution;
|
||||
|
||||
/**
|
||||
* Implements test statistics defined in the TestStatistic interface.
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:39 $
|
||||
*/
|
||||
public class TestStatisticImpl implements TestStatistic {
|
||||
|
||||
|
@ -236,11 +236,11 @@ public class TestStatisticImpl implements TestStatistic {
|
|||
|
||||
/**
|
||||
* @param mu comparison constant
|
||||
* @param sampleStats Univariate holding sample summary statitstics
|
||||
* @param sampleStats DescriptiveStatistics holding sample summary statitstics
|
||||
* @return t statistic
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
public double t(double mu, Univariate sampleStats)
|
||||
public double t(double mu, DescriptiveStatistics sampleStats)
|
||||
throws IllegalArgumentException {
|
||||
if ((sampleStats == null) || (sampleStats.getN() < 5)) {
|
||||
throw new IllegalArgumentException
|
||||
|
@ -251,12 +251,12 @@ public class TestStatisticImpl implements TestStatistic {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param sampleStats1 Univariate describing data from the first sample
|
||||
* @param sampleStats2 Univariate describing data from the second sample
|
||||
* @param sampleStats1 DescriptiveStatistics describing data from the first sample
|
||||
* @param sampleStats2 DescriptiveStatistics describing data from the second sample
|
||||
* @return t statistic
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
public double t(Univariate sampleStats1, Univariate sampleStats2)
|
||||
public double t(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
|
||||
throws IllegalArgumentException {
|
||||
if ((sampleStats1 == null) || (sampleStats2 == null ||
|
||||
Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) {
|
||||
|
@ -269,12 +269,12 @@ public class TestStatisticImpl implements TestStatistic {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param sampleStats1 Univariate describing data from the first sample
|
||||
* @param sampleStats2 Univariate describing data from the second sample
|
||||
* @param sampleStats1 DescriptiveStatistics describing data from the first sample
|
||||
* @param sampleStats2 DescriptiveStatistics describing data from the second sample
|
||||
* @return p-value for t-test
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
public double tTest(Univariate sampleStats1, Univariate sampleStats2)
|
||||
public double tTest(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
|
||||
throws IllegalArgumentException {
|
||||
if ((sampleStats1 == null) || (sampleStats2 == null ||
|
||||
Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) {
|
||||
|
@ -287,14 +287,14 @@ public class TestStatisticImpl implements TestStatistic {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param sampleStats1 Univariate describing sample data values
|
||||
* @param sampleStats2 Univariate describing sample data values
|
||||
* @param sampleStats1 DescriptiveStatistics describing sample data values
|
||||
* @param sampleStats2 DescriptiveStatistics 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(Univariate sampleStats1, Univariate sampleStats2,
|
||||
public boolean tTest(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2,
|
||||
double alpha) throws IllegalArgumentException {
|
||||
if ((alpha <= 0) || (alpha > 0.5)) {
|
||||
throw new IllegalArgumentException
|
||||
|
@ -305,12 +305,12 @@ public class TestStatisticImpl implements TestStatistic {
|
|||
|
||||
/**
|
||||
* @param mu constant value to compare sample mean against
|
||||
* @param sampleStats Univariate describing sample data values
|
||||
* @param sampleStats DescriptiveStatistics 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, Univariate sampleStats, double alpha)
|
||||
public boolean tTest(double mu, DescriptiveStatistics sampleStats, double alpha)
|
||||
throws IllegalArgumentException {
|
||||
if ((alpha <= 0) || (alpha > 0.5)) {
|
||||
throw new IllegalArgumentException
|
||||
|
@ -321,11 +321,11 @@ public class TestStatisticImpl implements TestStatistic {
|
|||
|
||||
/**
|
||||
* @param mu constant value to compare sample mean against
|
||||
* @param sampleStats Univariate describing sample data
|
||||
* @param sampleStats DescriptiveStatistics describing sample data
|
||||
* @return p-value
|
||||
* @throws IllegalArgumentException if the precondition is not met
|
||||
*/
|
||||
public double tTest(double mu, Univariate sampleStats)
|
||||
public double tTest(double mu, DescriptiveStatistics sampleStats)
|
||||
throws IllegalArgumentException {
|
||||
if ((sampleStats == null) || (sampleStats.getN() < 5)) {
|
||||
throw new IllegalArgumentException
|
||||
|
|
|
@ -1,212 +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;
|
||||
|
||||
/**
|
||||
*
|
||||
* Accumulates univariate statistics for values fed in
|
||||
* through the addValue() method. This interface defines the LCD interface
|
||||
* which all Univariate implementations must implement. <p>
|
||||
* A "rolling" capability is supported by all implementations with the following
|
||||
* contract: <p>
|
||||
* <i> Setting the windowSize property limits the domain of all statistics to
|
||||
* the last <code>windowSize</code> values added.</i><p>
|
||||
* We use the term <i>available values</i> throughout the API documentation
|
||||
* to refer to these values when the windowSize is set. For example, if the
|
||||
* 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<p>
|
||||
* The default windowSize is "infinite" -- i.e., all values added are included
|
||||
* in all computations.
|
||||
* @version $Revision: 1.13 $ $Date: 2003/11/14 22:22:18 $
|
||||
*/
|
||||
public interface Univariate extends Applyable{
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
double getKurtosis();
|
||||
|
||||
/**
|
||||
* Returns the Kurtosis "classification" a distribution can be
|
||||
* leptokurtic (high peak), platykurtic (flat peak),
|
||||
* or mesokurtic (zero kurtosis).
|
||||
*
|
||||
* @return A static constant defined in this interface,
|
||||
* StoredDeviation.LEPTOKURITC, StoredDeviation.PLATYKURTIC, or
|
||||
* StoredDeviation.MESOKURTIC
|
||||
*/
|
||||
int getKurtosisClass();
|
||||
|
||||
/**
|
||||
* Returns the maximum of the available values
|
||||
* @return The max or Double.NaN if no values have been added.
|
||||
*/
|
||||
double getMax();
|
||||
|
||||
/**
|
||||
* Returns the minimum of the available values
|
||||
* @return The min or Double.NaN if no values have been added.
|
||||
*/
|
||||
double getMin();
|
||||
|
||||
/**
|
||||
* Returns the number of available values
|
||||
* @return The number of available values
|
||||
*/
|
||||
int 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
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
double getSumsq();
|
||||
|
||||
/**
|
||||
* Resets all statistics and storage
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* 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".
|
||||
*/
|
||||
static final int INFINITE_WINDOW = -1;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
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.
|
||||
*/
|
||||
void setWindowSize(int windowSize);
|
||||
}
|
|
@ -1,180 +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.StorelessUnivariateStatistic;
|
||||
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
|
||||
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.25 $ $Date: 2003/11/14 22:22:18 $
|
||||
*/
|
||||
public class UnivariateImpl
|
||||
extends AbstractUnivariate
|
||||
implements Univariate, Serializable {
|
||||
|
||||
/** fixed storage */
|
||||
private FixedDoubleArray storage = null;
|
||||
|
||||
/** Creates new univariate with an infinite window */
|
||||
public UnivariateImpl() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new univariate with a fixed window
|
||||
* @param window Window Size
|
||||
*/
|
||||
public UnivariateImpl(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;
|
||||
}
|
||||
|
||||
}
|
|
@ -51,12 +51,12 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:39 $
|
||||
*/
|
||||
public class BinomialDistributionTest extends TestCase {
|
||||
private BinomialDistribution b;
|
||||
|
|
|
@ -52,12 +52,12 @@
|
|||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:39 $
|
||||
*/
|
||||
public class ChiSquareDistributionTest extends TestCase {
|
||||
private ChiSquaredDistribution chiSquare;
|
||||
|
|
|
@ -52,12 +52,12 @@
|
|||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.13 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.14 $ $Date: 2003/11/15 16:01:39 $
|
||||
*/
|
||||
public class DistributionFactoryImplTest extends TestCase {
|
||||
/** */
|
||||
|
|
|
@ -51,14 +51,14 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import org.apache.commons.math.TestUtils;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:39 $
|
||||
*/
|
||||
public class ExponentialDistributionTest extends TestCase {
|
||||
private ExponentialDistribution exp;
|
||||
|
|
|
@ -51,12 +51,12 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
public class FDistributionTest extends TestCase {
|
||||
private FDistribution f;
|
||||
|
|
|
@ -52,12 +52,12 @@
|
|||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
public class GammaDistributionTest extends TestCase {
|
||||
public void testProbabilities(){
|
||||
|
|
|
@ -52,12 +52,12 @@
|
|||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.5 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
public class HypergeometricDistributionTest extends TestCase {
|
||||
private HypergeometricDistribution h;
|
||||
|
|
|
@ -51,12 +51,12 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
package org.apache.commons.math.distribution;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
public class TDistributionTest extends TestCase {
|
||||
private TDistribution t;
|
||||
|
|
|
@ -59,13 +59,13 @@ import junit.framework.TestSuite;
|
|||
import java.io.File;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.math.stat.Univariate;
|
||||
import org.apache.commons.math.stat.UnivariateImpl;
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
||||
|
||||
/**
|
||||
* Test cases for the EmpiricalDistribution class
|
||||
*
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
|
||||
public final class EmpiricalDistributionTest extends TestCase {
|
||||
|
@ -147,7 +147,7 @@ public final class EmpiricalDistributionTest extends TestCase {
|
|||
|
||||
private void tstGen(double tolerance)throws Exception {
|
||||
empiricalDistribution.load(file);
|
||||
Univariate stats = new UnivariateImpl();
|
||||
DescriptiveStatistics stats = new StorelessDescriptiveStatisticsImpl();
|
||||
for (int i = 1; i < 1000; i++) {
|
||||
stats.addValue(empiricalDistribution.getNextValue());
|
||||
}
|
||||
|
|
|
@ -61,14 +61,14 @@ 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.TestStatisticImpl;
|
||||
import org.apache.commons.math.stat.Univariate;
|
||||
import org.apache.commons.math.stat.UnivariateImpl;
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
|
||||
/**
|
||||
* Test cases for the RandomData class.
|
||||
*
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
|
||||
public final class RandomDataTest extends TestCase {
|
||||
|
@ -405,7 +405,7 @@ public final class RandomDataTest extends TestCase {
|
|||
} catch (IllegalArgumentException ex) {
|
||||
;
|
||||
}
|
||||
Univariate u = new UnivariateImpl();
|
||||
DescriptiveStatistics u = new StorelessDescriptiveStatisticsImpl();
|
||||
for (int i = 0; i<largeSampleSize; i++) {
|
||||
u.addValue(randomData.nextGaussian(0,1));
|
||||
}
|
||||
|
|
|
@ -58,13 +58,13 @@ import junit.framework.TestCase;
|
|||
import junit.framework.TestSuite;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.math.stat.Univariate;
|
||||
import org.apache.commons.math.stat.UnivariateImpl;
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
||||
|
||||
/**
|
||||
* Test cases for the ValueServer class.
|
||||
*
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:22 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
|
||||
public final class ValueServerTest extends TestCase {
|
||||
|
@ -103,7 +103,7 @@ public final class ValueServerTest extends TestCase {
|
|||
vs.computeDistribution();
|
||||
assertTrue("empirical distribution property",
|
||||
vs.getEmpiricalDistribution() != null);
|
||||
Univariate stats = new UnivariateImpl();
|
||||
DescriptiveStatistics stats = new StorelessDescriptiveStatisticsImpl();
|
||||
for (int i = 1; i < 1000; i++) {
|
||||
next = vs.getNext();
|
||||
stats.addValue(next);
|
||||
|
@ -114,7 +114,7 @@ public final class ValueServerTest extends TestCase {
|
|||
tolerance);
|
||||
|
||||
vs.computeDistribution(500);
|
||||
stats = new UnivariateImpl();
|
||||
stats = new StorelessDescriptiveStatisticsImpl();
|
||||
for (int i = 1; i < 1000; i++) {
|
||||
next = vs.getNext();
|
||||
stats.addValue(next);
|
||||
|
|
|
@ -53,79 +53,110 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math.util.BeanTransformer;
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.util.NumberTransformer;
|
||||
|
||||
/**
|
||||
* This implementation of StoreUnivariate uses commons-beanutils to gather
|
||||
* This implementation of DescriptiveStatistics uses commons-beanutils to gather
|
||||
* univariate statistics for a List of Java Beans by property. This
|
||||
* implementation uses beanutils' PropertyUtils to get a simple, nested,
|
||||
* indexed, mapped, or combined property from an element of a List.
|
||||
* @version $Revision: 1.11 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
public class BeanListUnivariateImpl extends ListUnivariateImpl {
|
||||
|
||||
/**
|
||||
* propertyName of the property to get from the bean
|
||||
*/
|
||||
private String propertyName;
|
||||
/**
|
||||
* propertyName of the property to get from the bean
|
||||
*/
|
||||
private String propertyName;
|
||||
|
||||
/**
|
||||
* Construct a BeanListUnivariate with specified
|
||||
* backing list
|
||||
* @param list Backing List
|
||||
*/
|
||||
public BeanListUnivariateImpl(List list) {
|
||||
this(list, null);
|
||||
}
|
||||
/**
|
||||
* Construct a BeanListUnivariate with specified
|
||||
* backing list
|
||||
* @param list Backing List
|
||||
*/
|
||||
public BeanListUnivariateImpl(List list) {
|
||||
this(list, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a BeanListUnivariate with specified
|
||||
* backing list and propertyName
|
||||
* @param list Backing List
|
||||
* @param propertyName Bean propertyName
|
||||
*/
|
||||
public BeanListUnivariateImpl(List list, String propertyName) {
|
||||
super(list);
|
||||
setPropertyName(propertyName);
|
||||
}
|
||||
/**
|
||||
* Construct a BeanListUnivariate with specified
|
||||
* backing list and propertyName
|
||||
* @param list Backing List
|
||||
* @param propertyName Bean propertyName
|
||||
*/
|
||||
public BeanListUnivariateImpl(List list, String propertyName) {
|
||||
super(list);
|
||||
setPropertyName(propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return propertyName
|
||||
*/
|
||||
public String getPropertyName() {
|
||||
return propertyName;
|
||||
}
|
||||
/**
|
||||
* @return propertyName
|
||||
*/
|
||||
public String getPropertyName() {
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param propertyName Name of Property
|
||||
*/
|
||||
public void setPropertyName(String propertyName) {
|
||||
this.propertyName = propertyName;
|
||||
setTransformer(new BeanTransformer(propertyName));
|
||||
}
|
||||
/**
|
||||
* @param propertyName Name of Property
|
||||
*/
|
||||
public void setPropertyName(String propertyName) {
|
||||
this.propertyName = propertyName;
|
||||
this.transformer = new NumberTransformer() {
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#addValue(double)
|
||||
*/
|
||||
public void addValue(double v) {
|
||||
String msg =
|
||||
"The BeanListUnivariateImpl does not accept values " +
|
||||
"through the addValue method. Because elements of this list " +
|
||||
"are JavaBeans, one must be sure to set the 'propertyName' " +
|
||||
"property and add new Beans to the underlying list via the " +
|
||||
"addBean(Object bean) method";
|
||||
throw new UnsupportedOperationException(msg);
|
||||
}
|
||||
/**
|
||||
* @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
|
||||
*/
|
||||
public double transform(final Object o) throws MathException {
|
||||
try {
|
||||
return (
|
||||
(Number) PropertyUtils.getProperty(
|
||||
o,
|
||||
getPropertyName()))
|
||||
.doubleValue();
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new MathException(
|
||||
"IllegalAccessException in Transformation: "
|
||||
+ e.getMessage(),
|
||||
e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new MathException(
|
||||
"InvocationTargetException in Transformation: "
|
||||
+ e.getMessage(),
|
||||
e);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new MathException(
|
||||
"oSuchMethodException in Transformation: "
|
||||
+ e.getMessage(),
|
||||
e);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a bean to this list.
|
||||
*
|
||||
* @param bean Bean to add to the list
|
||||
*/
|
||||
public void addObject(Object bean) {
|
||||
list.add(bean);
|
||||
}
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#addValue(double)
|
||||
*/
|
||||
public void addValue(double v) {
|
||||
String msg =
|
||||
"The BeanListUnivariateImpl does not accept values "
|
||||
+ "through the addValue method. Because elements of this list "
|
||||
+ "are JavaBeans, one must be sure to set the 'propertyName' "
|
||||
+ "property and add new Beans to the underlying list via the "
|
||||
+ "addBean(Object bean) method";
|
||||
throw new UnsupportedOperationException(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a bean to this list.
|
||||
*
|
||||
* @param bean Bean to add to the list
|
||||
*/
|
||||
public void addObject(Object bean) {
|
||||
list.add(bean);
|
||||
}
|
||||
}
|
|
@ -65,7 +65,7 @@ import org.apache.commons.math.beans.*;
|
|||
/**
|
||||
* Test cases for the {@link BeanListUnivariateImpl} class.
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
|
||||
public final class BeanListUnivariateImplTest extends TestCase {
|
||||
|
@ -108,7 +108,7 @@ public final class BeanListUnivariateImplTest extends TestCase {
|
|||
/** test stats */
|
||||
public void testStats() {
|
||||
|
||||
StoreUnivariate u = new BeanListUnivariateImpl( patientList );
|
||||
DescriptiveStatistics u = new BeanListUnivariateImpl( patientList );
|
||||
|
||||
assertEquals("total count",3,u.getN(),tolerance);
|
||||
|
||||
|
@ -118,7 +118,7 @@ public final class BeanListUnivariateImplTest extends TestCase {
|
|||
|
||||
public void testPropStats() {
|
||||
|
||||
StoreUnivariate heartU = new BeanListUnivariateImpl( patientList,
|
||||
DescriptiveStatistics heartU = new BeanListUnivariateImpl( patientList,
|
||||
"vitalStats.heartRate" );
|
||||
|
||||
|
||||
|
@ -128,7 +128,7 @@ public final class BeanListUnivariateImplTest extends TestCase {
|
|||
assertEquals( "Max heart rate unexpected", 120.0,
|
||||
heartU.getMax(), 0.001 );
|
||||
|
||||
StoreUnivariate ageU = new BeanListUnivariateImpl( patientList,
|
||||
DescriptiveStatistics ageU = new BeanListUnivariateImpl( patientList,
|
||||
"age" );
|
||||
|
||||
assertEquals( "Mean age unexpected", 33.333,
|
||||
|
|
|
@ -63,12 +63,12 @@ import java.io.IOException;
|
|||
import java.io.InputStreamReader;
|
||||
import org.apache.commons.logging.*;
|
||||
/**
|
||||
* Test cases for the {@link Univariate} class.
|
||||
* @version $Revision: 1.11 $ $Date: 2003/11/14 22:22:18 $
|
||||
* Test cases for the {@link DescriptiveStatistics} class.
|
||||
* @version $Revision: 1.12 $ $Date: 2003/11/15 16:01:40 $
|
||||
*/
|
||||
public class CertifiedDataTest extends TestCase {
|
||||
|
||||
protected Univariate u = null;
|
||||
protected DescriptiveStatistics u = null;
|
||||
|
||||
protected double mean = Double.NaN;
|
||||
|
||||
|
@ -100,11 +100,19 @@ public class CertifiedDataTest extends TestCase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Test UnivariateImpl
|
||||
* Test StorelessDescriptiveStatistics
|
||||
*/
|
||||
public void testUnivariateImpl() {
|
||||
|
||||
u = new UnivariateImpl();
|
||||
try {
|
||||
u = DescriptiveStatistics.newInstance(StorelessDescriptiveStatisticsImpl.class);
|
||||
} catch (InstantiationException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
loadStats("data/Lew.txt");
|
||||
assertEquals("Lew: std", std, u.getStandardDeviation(), .000000000001);
|
||||
|
@ -136,11 +144,11 @@ public class CertifiedDataTest extends TestCase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Test UnivariateImpl
|
||||
* Test StorelessDescriptiveStatistics
|
||||
*/
|
||||
public void testStoredUnivariateImpl() {
|
||||
|
||||
u = new StoreUnivariateImpl();
|
||||
u = DescriptiveStatistics.newInstance();
|
||||
|
||||
loadStats("data/Lew.txt");
|
||||
assertEquals("Lew: std", std, u.getStandardDeviation(), .000000000001);
|
||||
|
@ -172,7 +180,7 @@ public class CertifiedDataTest extends TestCase {
|
|||
}
|
||||
|
||||
/**
|
||||
* loads a Univariate off of a test file
|
||||
* loads a DescriptiveStatistics off of a test file
|
||||
* @param file
|
||||
*/
|
||||
private void loadStats(String resource) {
|
||||
|
|
|
@ -0,0 +1,345 @@
|
|||
/* ====================================================================
|
||||
* 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 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: 2003/11/15 16:01:41 $
|
||||
*/
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -55,16 +55,15 @@ package org.apache.commons.math.stat;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
|
||||
import org.apache.commons.math.util.DefaultTransformer;
|
||||
import org.apache.commons.math.util.NumberTransformer;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.11 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:41 $
|
||||
*/
|
||||
public class ListUnivariateImpl
|
||||
extends AbstractStoreUnivariate
|
||||
implements StoreUnivariate {
|
||||
public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
|
||||
|
||||
/**
|
||||
* Holds a reference to a list - GENERICs are going to make
|
||||
|
@ -77,7 +76,7 @@ public class ListUnivariateImpl
|
|||
|
||||
/**
|
||||
* Construct a ListUnivariate with a specific List.
|
||||
* @param list The list that will back this Univariate
|
||||
* @param list The list that will back this DescriptiveStatistics
|
||||
*/
|
||||
public ListUnivariateImpl(List list) {
|
||||
this(list, new DefaultTransformer());
|
||||
|
@ -85,7 +84,7 @@ public class ListUnivariateImpl
|
|||
|
||||
/**
|
||||
* Construct a ListUnivariate with a specific List.
|
||||
* @param list The list that will back this Univariate
|
||||
* @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) {
|
||||
|
@ -95,7 +94,7 @@ public class ListUnivariateImpl
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getValues()
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getValues()
|
||||
*/
|
||||
public double[] getValues() {
|
||||
|
||||
|
@ -106,7 +105,7 @@ public class ListUnivariateImpl
|
|||
// take into account only the last n elements of the list
|
||||
// as definied by windowSize
|
||||
|
||||
if (windowSize != Univariate.INFINITE_WINDOW &&
|
||||
if (windowSize != DescriptiveStatistics.INFINITE_WINDOW &&
|
||||
windowSize < list.size())
|
||||
{
|
||||
length = list.size() - Math.max(0, list.size() - windowSize);
|
||||
|
@ -122,7 +121,7 @@ public class ListUnivariateImpl
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getElement(int)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getElement(int)
|
||||
*/
|
||||
public double getElement(int index) {
|
||||
|
||||
|
@ -130,28 +129,30 @@ public class ListUnivariateImpl
|
|||
|
||||
int calcIndex = index;
|
||||
|
||||
if (windowSize != Univariate.INFINITE_WINDOW &&
|
||||
if (windowSize != DescriptiveStatistics.INFINITE_WINDOW &&
|
||||
windowSize < list.size())
|
||||
{
|
||||
calcIndex = (list.size() - windowSize) + index;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
value = transformer.transform(list.get(calcIndex));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
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#getN()
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#getN()
|
||||
*/
|
||||
public int getN() {
|
||||
int n = 0;
|
||||
|
||||
if (windowSize != Univariate.INFINITE_WINDOW) {
|
||||
if (windowSize != DescriptiveStatistics.INFINITE_WINDOW) {
|
||||
if (list.size() > windowSize) {
|
||||
n = windowSize;
|
||||
} else {
|
||||
|
@ -164,7 +165,7 @@ public class ListUnivariateImpl
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#addValue(double)
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#addValue(double)
|
||||
*/
|
||||
public void addValue(double v) {
|
||||
list.add(new Double(v));
|
||||
|
@ -179,7 +180,7 @@ public class ListUnivariateImpl
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.Univariate#clear()
|
||||
* @see org.apache.commons.math.stat.DescriptiveStatistics#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
super.clear();
|
|
@ -63,7 +63,7 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Test cases for the {@link Univariate} class.
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:41 $
|
||||
*/
|
||||
|
||||
public final class ListUnivariateImplTest extends TestCase {
|
||||
|
@ -81,7 +81,7 @@ public final class ListUnivariateImplTest extends TestCase {
|
|||
private double max = 3;
|
||||
private double skewness = 0;
|
||||
private double kurtosis = 0.5;
|
||||
private int kClass = StoreUnivariate.LEPTOKURTIC;
|
||||
private int kClass = DescriptiveStatistics.LEPTOKURTIC;
|
||||
private double tolerance = 10E-15;
|
||||
|
||||
public ListUnivariateImplTest(String name) {
|
||||
|
@ -101,7 +101,7 @@ public final class ListUnivariateImplTest extends TestCase {
|
|||
public void testStats() {
|
||||
List externalList = new ArrayList();
|
||||
|
||||
StoreUnivariate u = new ListUnivariateImpl( externalList );
|
||||
DescriptiveStatistics u = new ListUnivariateImpl( externalList );
|
||||
|
||||
assertEquals("total count",0,u.getN(),tolerance);
|
||||
u.addValue(one);
|
||||
|
@ -123,7 +123,7 @@ public final class ListUnivariateImplTest extends TestCase {
|
|||
public void testN0andN1Conditions() throws Exception {
|
||||
List list = new ArrayList();
|
||||
|
||||
StoreUnivariate u = new ListUnivariateImpl( list );
|
||||
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() ) );
|
||||
|
@ -137,7 +137,7 @@ public final class ListUnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testSkewAndKurtosis() {
|
||||
StoreUnivariate u = new StoreUnivariateImpl();
|
||||
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 };
|
||||
|
@ -162,7 +162,7 @@ public final class ListUnivariateImplTest extends TestCase {
|
|||
|
||||
assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 );
|
||||
|
||||
// Now test rolling - UnivariateImpl should discount the contribution
|
||||
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
||||
// of a discarded element
|
||||
for( int i = 0; i < 10; i++ ) {
|
||||
u.addValue( i + 2 );
|
||||
|
|
|
@ -64,7 +64,7 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Test cases for the {@link Univariate} class.
|
||||
*
|
||||
* @version $Revision: 1.5 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:41 $
|
||||
*/
|
||||
|
||||
public final class MixedListUnivariateImplTest extends TestCase {
|
||||
|
@ -82,7 +82,7 @@ public final class MixedListUnivariateImplTest extends TestCase {
|
|||
private double max = 3;
|
||||
private double skewness = 0;
|
||||
private double kurtosis = 0.5;
|
||||
private int kClass = StoreUnivariate.LEPTOKURTIC;
|
||||
private int kClass = DescriptiveStatistics.LEPTOKURTIC;
|
||||
private double tolerance = 10E-15;
|
||||
|
||||
private TransformerMap transformers = new TransformerMap();
|
||||
|
@ -119,7 +119,7 @@ public final class MixedListUnivariateImplTest extends TestCase {
|
|||
public void testStats() {
|
||||
List externalList = new ArrayList();
|
||||
|
||||
StoreUnivariate u = new ListUnivariateImpl(externalList,transformers);
|
||||
DescriptiveStatistics u = new ListUnivariateImpl(externalList,transformers);
|
||||
|
||||
assertEquals("total count", 0, u.getN(), tolerance);
|
||||
u.addValue(one);
|
||||
|
@ -141,7 +141,7 @@ public final class MixedListUnivariateImplTest extends TestCase {
|
|||
public void testN0andN1Conditions() throws Exception {
|
||||
List list = new ArrayList();
|
||||
|
||||
StoreUnivariate u = new ListUnivariateImpl(new ArrayList(),transformers);
|
||||
DescriptiveStatistics u = new ListUnivariateImpl(new ArrayList(),transformers);
|
||||
|
||||
assertTrue(
|
||||
"Mean of n = 0 set should be NaN",
|
||||
|
@ -217,7 +217,7 @@ public final class MixedListUnivariateImplTest extends TestCase {
|
|||
u.getGeometricMean(),
|
||||
0.00001);
|
||||
|
||||
// Now test rolling - UnivariateImpl should discount the contribution
|
||||
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
||||
// of a discarded element
|
||||
for (int i = 0; i < 10; i++) {
|
||||
u.addValue(i + 2);
|
||||
|
|
|
@ -59,7 +59,7 @@ import junit.framework.TestSuite;
|
|||
|
||||
/**
|
||||
* Test cases for the {@link StatUtils} class.
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.11 $ $Date: 2003/11/15 16:01:41 $
|
||||
*/
|
||||
|
||||
public final class StatUtilsTest extends TestCase {
|
||||
|
@ -77,7 +77,7 @@ public final class StatUtilsTest extends TestCase {
|
|||
private double max = 3;
|
||||
private double skewness = 0;
|
||||
private double kurtosis = 0.5;
|
||||
private int kClass = StoreUnivariate.LEPTOKURTIC;
|
||||
private int kClass = DescriptiveStatistics.LEPTOKURTIC;
|
||||
private double tolerance = 10E-15;
|
||||
|
||||
public StatUtilsTest(String name) {
|
||||
|
|
|
@ -63,7 +63,7 @@ import org.apache.commons.math.random.RandomDataImpl;
|
|||
/**
|
||||
* Test cases for the {@link Univariate} class.
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:41 $
|
||||
*/
|
||||
|
||||
public final class StoreUnivariateImplTest extends TestCase {
|
||||
|
@ -80,7 +80,7 @@ public final class StoreUnivariateImplTest extends TestCase {
|
|||
private double max = 3;
|
||||
private double skewness = 0;
|
||||
private double kurtosis = 0.5;
|
||||
private int kClass = StoreUnivariate.LEPTOKURTIC;
|
||||
private int kClass = DescriptiveStatistics.LEPTOKURTIC;
|
||||
private double tolerance = 10E-15;
|
||||
|
||||
public StoreUnivariateImplTest(String name) {
|
||||
|
@ -98,7 +98,7 @@ public final class StoreUnivariateImplTest extends TestCase {
|
|||
|
||||
/** test stats */
|
||||
public void testStats() {
|
||||
StoreUnivariate u = new StoreUnivariateImpl();
|
||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
||||
assertEquals("total count",0,u.getN(),tolerance);
|
||||
u.addValue(one);
|
||||
u.addValue(two);
|
||||
|
@ -117,7 +117,7 @@ public final class StoreUnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testN0andN1Conditions() throws Exception {
|
||||
StoreUnivariate u = new StoreUnivariateImpl();
|
||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
||||
|
||||
assertTrue("Mean of n = 0 set should be NaN",
|
||||
Double.isNaN( u.getMean() ) );
|
||||
|
@ -137,7 +137,7 @@ public final class StoreUnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testSkewAndKurtosis() {
|
||||
StoreUnivariate u = new StoreUnivariateImpl();
|
||||
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,
|
||||
|
@ -153,7 +153,7 @@ public final class StoreUnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testProductAndGeometricMean() throws Exception {
|
||||
StoreUnivariateImpl u = new StoreUnivariateImpl();
|
||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
||||
u.setWindowSize(10);
|
||||
|
||||
u.addValue( 1.0 );
|
||||
|
@ -166,7 +166,7 @@ public final class StoreUnivariateImplTest extends TestCase {
|
|||
assertEquals( "Geometric mean not expected",
|
||||
2.213364, u.getGeometricMean(), 0.00001 );
|
||||
|
||||
// Now test rolling - UnivariateImpl should discount the contribution
|
||||
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
||||
// of a discarded element
|
||||
for( int i = 0; i < 10; i++ ) {
|
||||
u.addValue( i + 2 );
|
||||
|
@ -209,7 +209,7 @@ public final class StoreUnivariateImplTest extends TestCase {
|
|||
|
||||
|
||||
private void tstGetSortedValues(double[] test) {
|
||||
StoreUnivariateImpl u = new StoreUnivariateImpl();
|
||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
||||
for (int i = 0; i < test.length; i++) {
|
||||
u.addValue(test[i]);
|
||||
}
|
||||
|
@ -226,7 +226,7 @@ public final class StoreUnivariateImplTest extends TestCase {
|
|||
|
||||
public void testPercentiles() {
|
||||
double[] test = {5,4,3,2,1};
|
||||
StoreUnivariateImpl u = new StoreUnivariateImpl();
|
||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
||||
for (int i = 0; i < test.length; i++) {
|
||||
u.addValue(test[i]);
|
||||
}
|
||||
|
|
|
@ -53,13 +53,14 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
/**
|
||||
* Test cases for the TestStatistic class.
|
||||
*
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:41 $
|
||||
*/
|
||||
|
||||
public final class TestStatisticTest extends TestCase {
|
||||
|
@ -141,8 +142,17 @@ public final class TestStatisticTest extends TestCase {
|
|||
double[] observed = {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0,
|
||||
94.0, 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0};
|
||||
double mu = 100.0;
|
||||
Univariate sampleStats = new UnivariateImpl();
|
||||
for (int i = 0; i < observed.length; i++) {
|
||||
DescriptiveStatistics sampleStats = null;
|
||||
try {
|
||||
sampleStats = DescriptiveStatistics.newInstance(StorelessDescriptiveStatisticsImpl.class);
|
||||
} catch (InstantiationException e5) {
|
||||
// TODO Auto-generated catch block
|
||||
e5.printStackTrace();
|
||||
} catch (IllegalAccessException e5) {
|
||||
// TODO Auto-generated catch block
|
||||
e5.printStackTrace();
|
||||
}
|
||||
for (int i = 0; i < observed.length; i++) {
|
||||
sampleStats.addValue(observed[i]);
|
||||
}
|
||||
|
||||
|
@ -159,8 +169,17 @@ public final class TestStatisticTest extends TestCase {
|
|||
;
|
||||
}
|
||||
|
||||
UnivariateImpl nullStats = null;
|
||||
try {
|
||||
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();
|
||||
}
|
||||
try {
|
||||
testStatistic.t(mu, nullStats);
|
||||
fail("arguments too short, IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
|
@ -174,9 +193,18 @@ public final class TestStatisticTest extends TestCase {
|
|||
} catch (IllegalArgumentException ex) {
|
||||
;
|
||||
}
|
||||
|
||||
Univariate emptyStats = new UnivariateImpl();
|
||||
try {
|
||||
|
||||
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();
|
||||
}
|
||||
try {
|
||||
testStatistic.t(mu, emptyStats);
|
||||
fail("arguments too short, IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
|
@ -196,9 +224,18 @@ public final class TestStatisticTest extends TestCase {
|
|||
} catch (IllegalArgumentException ex) {
|
||||
;
|
||||
}
|
||||
|
||||
Univariate tooShortStats = new UnivariateImpl();
|
||||
tooShortStats.addValue(0d);
|
||||
|
||||
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();
|
||||
}
|
||||
tooShortStats.addValue(0d);
|
||||
tooShortStats.addValue(2d);
|
||||
try {
|
||||
testStatistic.t(mu, tooShortStats);
|
||||
|
@ -216,8 +253,19 @@ public final class TestStatisticTest extends TestCase {
|
|||
/** Moore and McCabe Example 8.3, p 516 */
|
||||
double[] oneSidedP = {2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d, 6d,
|
||||
6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d};
|
||||
Univariate oneSidedPStats = new UnivariateImpl();
|
||||
for (int i = 0; i < oneSidedP.length; i++) {
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
for (int i = 0; i < oneSidedP.length; i++) {
|
||||
oneSidedPStats.addValue(oneSidedP[i]);
|
||||
}
|
||||
assertEquals("one sample t stat",3.86,
|
||||
|
@ -252,12 +300,32 @@ public final class TestStatisticTest extends TestCase {
|
|||
/** Moore and McCabe Example 8.12, p 552 */
|
||||
double[] sample1 = {7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d};
|
||||
double[] sample2 = {-1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d};
|
||||
Univariate sampleStats1 = new UnivariateImpl();
|
||||
for (int i = 0; i < sample1.length; i++) {
|
||||
|
||||
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();
|
||||
}
|
||||
for (int i = 0; i < sample1.length; i++) {
|
||||
sampleStats1.addValue(sample1[i]);
|
||||
}
|
||||
Univariate sampleStats2 = new UnivariateImpl();
|
||||
for (int i = 0; i < sample2.length; 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();
|
||||
}
|
||||
for (int i = 0; i < sample2.length; i++) {
|
||||
sampleStats2.addValue(sample2[i]);
|
||||
}
|
||||
//FIXME: textbook example reported t stat uses pooled variance
|
||||
|
|
|
@ -51,16 +51,19 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat;
|
||||
package org.apache.commons.math.stat.univariate;
|
||||
|
||||
import org.apache.commons.math.stat.DescriptiveStatistics;
|
||||
import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Test cases for the {@link Univariate} class.
|
||||
* Test cases for the {@link DescriptiveStatistics} class.
|
||||
*
|
||||
* @version $Revision: 1.11 $ $Date: 2003/11/14 22:22:18 $
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:41 $
|
||||
*/
|
||||
|
||||
public final class UnivariateImplTest extends TestCase {
|
||||
|
@ -93,7 +96,7 @@ public final class UnivariateImplTest extends TestCase {
|
|||
|
||||
/** test stats */
|
||||
public void testStats() {
|
||||
UnivariateImpl u = new UnivariateImpl();
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl();
|
||||
assertEquals("total count",0,u.getN(),tolerance);
|
||||
u.addValue(one);
|
||||
u.addValue(twoF);
|
||||
|
@ -112,7 +115,7 @@ public final class UnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testN0andN1Conditions() throws Exception {
|
||||
UnivariateImpl u = new UnivariateImpl();
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl();
|
||||
assertTrue("Mean of n = 0 set should be NaN",
|
||||
Double.isNaN( u.getMean() ) );
|
||||
assertTrue("Standard Deviation of n = 0 set should be NaN",
|
||||
|
@ -166,7 +169,7 @@ public final class UnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testProductAndGeometricMean() throws Exception {
|
||||
UnivariateImpl u = new UnivariateImpl(10);
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl(10);
|
||||
|
||||
u.addValue( 1.0 );
|
||||
u.addValue( 2.0 );
|
||||
|
@ -176,7 +179,7 @@ public final class UnivariateImplTest extends TestCase {
|
|||
assertEquals( "Geometric mean not expected", 2.213364,
|
||||
u.getGeometricMean(), 0.00001 );
|
||||
|
||||
// Now test rolling - UnivariateImpl should discount the contribution
|
||||
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
||||
// of a discarded element
|
||||
for( int i = 0; i < 10; i++ ) {
|
||||
u.addValue( i + 2 );
|
||||
|
@ -188,7 +191,7 @@ public final class UnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testRollingMinMax() {
|
||||
UnivariateImpl u = new UnivariateImpl(3);
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl(3);
|
||||
u.addValue( 1.0 );
|
||||
u.addValue( 5.0 );
|
||||
u.addValue( 3.0 );
|
||||
|
@ -201,7 +204,7 @@ public final class UnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testNaNContracts() {
|
||||
UnivariateImpl u = new UnivariateImpl();
|
||||
StorelessDescriptiveStatisticsImpl u = new StorelessDescriptiveStatisticsImpl();
|
||||
double nan = Double.NaN;
|
||||
assertTrue("mean not NaN",Double.isNaN(u.getMean()));
|
||||
assertTrue("min not NaN",Double.isNaN(u.getMin()));
|
||||
|
@ -230,7 +233,7 @@ public final class UnivariateImplTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testSkewAndKurtosis() {
|
||||
Univariate u = new UnivariateImpl();
|
||||
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,
|
|
@ -54,12 +54,11 @@
|
|||
package org.apache.commons.math.util;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.math.MathException;
|
||||
|
||||
/**
|
||||
* Uses PropertyUtils to map a Bean getter to a double value.
|
||||
* @version $Revision: 1.10 $ $Date: 2003/11/14 22:22:17 $
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:42 $
|
||||
*/
|
||||
public class BeanTransformer implements NumberTransformer {
|
||||
|
||||
|
@ -103,23 +102,15 @@ public class BeanTransformer implements NumberTransformer {
|
|||
/**
|
||||
* @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
|
||||
*/
|
||||
public double transform(final Object o) {
|
||||
double d = Double.NaN;
|
||||
public double transform(final Object o) throws MathException {
|
||||
try {
|
||||
d =
|
||||
((Number) PropertyUtils.getProperty(o, getPropertyName()))
|
||||
.doubleValue();
|
||||
return ((Number) org.apache.commons.beanutils.PropertyUtils.getProperty(o, getPropertyName())).doubleValue();
|
||||
} catch (IllegalAccessException e) {
|
||||
LogFactory.getLog(getClass()).error(
|
||||
"Property can not be accessed. Using NaN.", e);
|
||||
throw new MathException("IllegalAccessException in Transformation: " + e.getMessage(), e);
|
||||
} catch (InvocationTargetException e) {
|
||||
LogFactory.getLog(getClass()).error(
|
||||
"Property accessor method threw an exception. Using NaN.", e);
|
||||
e.printStackTrace();
|
||||
throw new MathException("InvocationTargetException in Transformation: " + e.getMessage(), e);
|
||||
} catch (NoSuchMethodException e) {
|
||||
LogFactory.getLog(getClass()).error(
|
||||
"Property accessor method does not exist. Using NaN.", e);
|
||||
throw new MathException("oSuchMethodException in Transformation: " + e.getMessage(), e);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
}
|
|
@ -54,12 +54,13 @@
|
|||
|
||||
package org.apache.commons.math.util;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.TestUtils;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.5 $ $Date: 2003/11/14 22:22:23 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:42 $
|
||||
*/
|
||||
public class BeanTransformerTest extends TestCase {
|
||||
|
||||
|
@ -96,8 +97,14 @@ public class BeanTransformerTest extends TestCase {
|
|||
public void testTransformNoSuchMethod(){
|
||||
BeanTransformer b = new BeanTransformer("z");
|
||||
TestBean target = new TestBean();
|
||||
double value = b.transform(target);
|
||||
TestUtils.assertEquals(Double.NaN, value, 1.0e-2);
|
||||
double value = Double.NaN;
|
||||
try {
|
||||
value = b.transform(target);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
TestUtils.assertEquals(Double.NaN, value, 1.0e-2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -106,8 +113,14 @@ public class BeanTransformerTest extends TestCase {
|
|||
public void testTransform(){
|
||||
BeanTransformer b = new BeanTransformer("x");
|
||||
TestBean target = new TestBean();
|
||||
double value = b.transform(target);
|
||||
TestUtils.assertEquals(1.0, value, 1.0e-2);
|
||||
double value = Double.NaN;
|
||||
try {
|
||||
value = b.transform(target);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
TestUtils.assertEquals(1.0, value, 1.0e-2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -116,8 +129,14 @@ public class BeanTransformerTest extends TestCase {
|
|||
public void testTransformInvocationError(){
|
||||
BeanTransformer b = new BeanTransformer("z");
|
||||
TestBean target = new TestBean();
|
||||
double value = b.transform(target);
|
||||
TestUtils.assertEquals(Double.NaN, value, 1.0e-2);
|
||||
double value = Double.NaN;
|
||||
try {
|
||||
value = b.transform(target);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
TestUtils.assertEquals(Double.NaN, value, 1.0e-2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,7 +146,12 @@ public class BeanTransformerTest extends TestCase {
|
|||
BeanTransformer b = new BeanTransformer("y");
|
||||
TestBean target = new TestBean();
|
||||
try {
|
||||
b.transform(target);
|
||||
try {
|
||||
b.transform(target);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
fail();
|
||||
} catch(ClassCastException ex){
|
||||
// success
|
||||
|
|
Loading…
Reference in New Issue