From c415dd79aeccea246fe39881cda0b1404d5c33c0 Mon Sep 17 00:00:00 2001 From: Brent Worden Date: Tue, 27 Apr 2004 16:42:34 +0000 Subject: [PATCH] Fixed some checkstyle warnings. Added some unit tests. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141201 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/analysis/BisectionSolver.java | 3 +- .../analysis/UnivariateRealSolverImpl.java | 7 +- .../distribution/NormalDistributionImpl.java | 100 ++++---- .../stat/inference/TestStatisticImpl.java | 37 ++- .../multivariate/BivariateRegression.java | 14 +- .../AbstractUnivariateStatistic.java | 11 +- .../stat/univariate/moment/FirstMoment.java | 3 +- .../stat/univariate/moment/FourthMoment.java | 3 +- .../stat/univariate/moment/GeometricMean.java | 3 +- .../math/stat/univariate/moment/Kurtosis.java | 3 +- .../math/stat/univariate/moment/Mean.java | 5 +- .../stat/univariate/moment/SecondMoment.java | 3 +- .../math/stat/univariate/moment/Skewness.java | 5 +- .../univariate/moment/StandardDeviation.java | 3 +- .../stat/univariate/moment/ThirdMoment.java | 3 +- .../math/stat/univariate/moment/Variance.java | 3 +- .../math/stat/univariate/rank/Max.java | 3 +- .../math/stat/univariate/rank/Median.java | 3 +- .../math/stat/univariate/rank/Min.java | 3 +- .../math/stat/univariate/rank/Percentile.java | 4 +- .../math/stat/univariate/summary/Product.java | 3 +- .../math/stat/univariate/summary/Sum.java | 3 +- .../stat/univariate/summary/SumOfLogs.java | 3 +- .../stat/univariate/summary/SumOfSquares.java | 3 +- .../math/util/ContractableDoubleArray.java | 3 +- .../math/util/ExpandableDoubleArray.java | 3 +- .../commons/math/util/FixedDoubleArray.java | 3 +- .../UnivariateRealSolverFactoryImplTest.java | 111 +++++++++ .../commons/math/stat/StatUtilsTest.java | 231 +++++++++++++++++- .../univariate/DescriptiveStatisticsTest.java | 47 +++- ...relessUnivariateStatisticAbstractTest.java | 4 +- .../univariate/moment/FirstMomentTest.java | 55 +++++ 32 files changed, 574 insertions(+), 114 deletions(-) create mode 100644 src/test/org/apache/commons/math/analysis/UnivariateRealSolverFactoryImplTest.java create mode 100644 src/test/org/apache/commons/math/stat/univariate/moment/FirstMomentTest.java diff --git a/src/java/org/apache/commons/math/analysis/BisectionSolver.java b/src/java/org/apache/commons/math/analysis/BisectionSolver.java index 4c14fdfff..8911f2c14 100644 --- a/src/java/org/apache/commons/math/analysis/BisectionSolver.java +++ b/src/java/org/apache/commons/math/analysis/BisectionSolver.java @@ -23,9 +23,10 @@ import org.apache.commons.math.MathException; * Implements the bisection algorithm * for finding zeros of univariate real functions. This algorithm will find only one zero in the given interval. * The function should be continuous but not necessarily smooth. - * @version $Revision: 1.14 $ $Date: 2004/04/27 04:37:58 $ + * @version $Revision: 1.15 $ $Date: 2004/04/27 16:42:33 $ */ public class BisectionSolver extends UnivariateRealSolverImpl implements Serializable { + /** * Construct a solver for the given function. * @param f function to solve. diff --git a/src/java/org/apache/commons/math/analysis/UnivariateRealSolverImpl.java b/src/java/org/apache/commons/math/analysis/UnivariateRealSolverImpl.java index df84a5a3c..eabeb1434 100644 --- a/src/java/org/apache/commons/math/analysis/UnivariateRealSolverImpl.java +++ b/src/java/org/apache/commons/math/analysis/UnivariateRealSolverImpl.java @@ -24,7 +24,7 @@ import org.apache.commons.math.MathException; * Provide a default implementation for several functions useful to generic * solvers. * - * @version $Revision: 1.11 $ $Date: 2004/04/23 18:20:12 $ + * @version $Revision: 1.12 $ $Date: 2004/04/27 16:42:33 $ */ public abstract class UnivariateRealSolverImpl implements UnivariateRealSolver, Serializable { @@ -71,6 +71,7 @@ public abstract class UnivariateRealSolverImpl * @param f the function to solve. * @param defaultAbsoluteAccuracy maximum absolue error. * @param defaultMaximalIterationCount maximum number of iterations. + * @throws IllegalArgumentException if function is null. */ protected UnivariateRealSolverImpl( UnivariateRealFunction f, @@ -79,6 +80,10 @@ public abstract class UnivariateRealSolverImpl super(); + if (f == null) { + throw new IllegalArgumentException("function can not be null."); + } + this.f = f; this.defaultAbsoluteAccuracy = defaultAbsoluteAccuracy; this.defaultRelativeAccuracy = 1E-14; diff --git a/src/java/org/apache/commons/math/distribution/NormalDistributionImpl.java b/src/java/org/apache/commons/math/distribution/NormalDistributionImpl.java index 83b2ab368..042874522 100644 --- a/src/java/org/apache/commons/math/distribution/NormalDistributionImpl.java +++ b/src/java/org/apache/commons/math/distribution/NormalDistributionImpl.java @@ -25,7 +25,7 @@ import java.io.Serializable; * using method {@link #setCdfAlgorithm}. The deafault is the Cody algorithm * {@link org.apache.commons.math.distribution.NormalCDFPreciseAlgorithm} * - * @version $Revision: 1.5 $ $Date: 2004/04/27 04:37:58 $ + * @version $Revision: 1.6 $ $Date: 2004/04/27 16:42:34 $ */ public class NormalDistributionImpl extends AbstractContinuousDistribution implements NormalDistribution, Serializable { @@ -84,8 +84,9 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution * @param sd standard deviation for this distribution */ public void setStandardDeviation(double sd) { - if (sd < 0.0) { - throw new IllegalArgumentException("Standard deviation must be" + "positive or zero."); + if (sd <= 0.0) { + throw new IllegalArgumentException( + "Standard deviation must be positive."); } standardDeviation = sd; } @@ -96,12 +97,7 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution * @return CDF evaluted at x. */ public double cumulativeProbability(double x) { - double z = x; - if(standardDeviation > 0){ - z = (x - mean) / standardDeviation; - }else{ - return 0.0; - } + double z = (x - mean) / standardDeviation; return cdfAlgorithm.cdf(z); } @@ -137,11 +133,6 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution "p must be between 0.0 and 1.0, inclusive."); } - //TODO is this ok? - if(standardDeviation == 0){ - return mean; - } - double r, val; double q = p - 0.5; @@ -152,49 +143,50 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution 33430.575583588128105) * r + 67265.770927008700853) * r + 45921.953931549871457) * r + 13731.693765509461125) * r + 1971.5909503065514427) * r + 133.14166789178437745) * r + - 3.387132872796366608) - / (((((((r * 5226.495278852854561 + + 3.387132872796366608) / + (((((((r * 5226.495278852854561 + 28729.085735721942674) * r + 39307.89580009271061) * r + 21213.794301586595867) * r + 5394.1960214247511077) * r + 687.1870074920579083) * r + 42.313330701600911252) * r + 1.); - }else { //closer than 0.075 from {0,1} boundary - if (q > 0) { - r = 1 - p; - } else { - r = p; - } - r = Math.sqrt(- Math.log(r)); - if (r <= 5.0) { - r += -1.6; - val = (((((((r * 7.7454501427834140764e-4 + - 0.0227238449892691845833) * r + 0.24178072517745061177) * - r + 1.27045825245236838258) * r + - 3.64784832476320460504) * r + 5.7694972214606914055) * - r + 4.6303378461565452959) * r + - 1.42343711074968357734) - / (((((((r * - 1.05075007164441684324e-9 + 5.475938084995344946e-4) * - r + 0.0151986665636164571966) * r + - 0.14810397642748007459) * r + 0.68976733498510000455) * - r + 1.6763848301838038494) * r + - 2.05319162663775882187) * r + 1.0); - }else { //very close to 0 or 1 - r += -5.; - val = (((((((r * 2.01033439929228813265e-7 + - 2.71155556874348757815e-5) * r + - 0.0012426609473880784386) * r + 0.026532189526576123093) * - r + 0.29656057182850489123) * r + - 1.7848265399172913358) * r + 5.4637849111641143699) * - r + 6.6579046435011037772) / - (((((((r * - 2.04426310338993978564e-15 + 1.4215117583164458887e-7)* - r + 1.8463183175100546818e-5) * r + - 7.868691311456132591e-4) * r + 0.0148753612908506148525) * - r + 0.13692988092273580531) * r + - 0.59983220655588793769) * r + 1.0); - } - if(q < 0.0) - val = -val; + } else { //closer than 0.075 from {0,1} boundary + if (q > 0) { + r = 1 - p; + } else { + r = p; + } + r = Math.sqrt(- Math.log(r)); + if (r <= 5.0) { + r += -1.6; + val = (((((((r * 7.7454501427834140764e-4 + + 0.0227238449892691845833) * r + 0.24178072517745061177) * + r + 1.27045825245236838258) * r + + 3.64784832476320460504) * r + 5.7694972214606914055) * + r + 4.6303378461565452959) * r + + 1.42343711074968357734) / + (((((((r * + 1.05075007164441684324e-9 + 5.475938084995344946e-4) * + r + 0.0151986665636164571966) * r + + 0.14810397642748007459) * r + 0.68976733498510000455) * + r + 1.6763848301838038494) * r + + 2.05319162663775882187) * r + 1.0); + } else { //very close to 0 or 1 + r += -5.; + val = (((((((r * 2.01033439929228813265e-7 + + 2.71155556874348757815e-5) * r + + 0.0012426609473880784386) * r + 0.026532189526576123093) * + r + 0.29656057182850489123) * r + + 1.7848265399172913358) * r + 5.4637849111641143699) * + r + 6.6579046435011037772) / + (((((((r * + 2.04426310338993978564e-15 + 1.4215117583164458887e-7)* + r + 1.8463183175100546818e-5) * r + + 7.868691311456132591e-4) * r + 0.0148753612908506148525) * + r + 0.13692988092273580531) * r + + 0.59983220655588793769) * r + 1.0); + } + if(q < 0.0) { + val = -val; + } } return mean + standardDeviation * val; } diff --git a/src/java/org/apache/commons/math/stat/inference/TestStatisticImpl.java b/src/java/org/apache/commons/math/stat/inference/TestStatisticImpl.java index 4c49df434..e8f9f19a4 100644 --- a/src/java/org/apache/commons/math/stat/inference/TestStatisticImpl.java +++ b/src/java/org/apache/commons/math/stat/inference/TestStatisticImpl.java @@ -28,18 +28,13 @@ import org.apache.commons.math.stat.univariate.StatisticalSummary; /** * Implements test statistics defined in the TestStatistic interface. * - * @version $Revision: 1.4 $ $Date: 2004/04/12 02:27:49 $ + * @version $Revision: 1.5 $ $Date: 2004/04/27 16:42:34 $ */ public class TestStatisticImpl implements TestStatistic, Serializable { + /** Serializable version identifier */ static final long serialVersionUID = 3357444126133491679L; - /** - * Default constructor - */ - public TestStatisticImpl() { - } - /** * @param observed array of observed frequency counts * @param expected array of expected frequency counts @@ -55,8 +50,8 @@ public class TestStatisticImpl implements TestStatistic, Serializable { throw new IllegalArgumentException("observed, expected array lengths incorrect"); } if ((StatUtils.min(expected) <= 0) || (StatUtils.min(observed) < 0)) { - throw new IllegalArgumentException( "observed counts must be non-negative," - + " expected counts must be postive"); + throw new IllegalArgumentException( + "observed counts must be non-negative expected counts must be postive"); } for (int i = 0; i < observed.length; i++) { dev = (observed[i] - expected[i]); @@ -134,8 +129,8 @@ public class TestStatisticImpl implements TestStatistic, Serializable { */ public double t(double[] sample1, double[] sample2) throws IllegalArgumentException { - if ((sample1 == null) || (sample2 == null - || Math.min(sample1.length, sample2.length) < 5)) { + if ((sample1 == null) || (sample2 == null || + Math.min(sample1.length, sample2.length) < 5)) { throw new IllegalArgumentException("insufficient data for t statistic"); } return t(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1), @@ -152,8 +147,8 @@ public class TestStatisticImpl implements TestStatistic, Serializable { */ public double tTest(double[] sample1, double[] sample2) throws IllegalArgumentException, MathException { - if ((sample1 == null) || (sample2 == null - || Math.min(sample1.length, sample2.length) < 5)) { + if ((sample1 == null) || (sample2 == null || + Math.min(sample1.length, sample2.length) < 5)) { throw new IllegalArgumentException("insufficient data"); } return tTest(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1), @@ -214,9 +209,9 @@ public class TestStatisticImpl implements TestStatistic, Serializable { */ public double t(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws IllegalArgumentException { - if ((sampleStats1 == null) - || (sampleStats2 == null - || Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) { + if ((sampleStats1 == null) || + (sampleStats2 == null || + Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) { throw new IllegalArgumentException("insufficient data for t statistic"); } return t(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(), @@ -232,8 +227,8 @@ public class TestStatisticImpl implements TestStatistic, Serializable { */ public double tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws IllegalArgumentException, MathException { - if ((sampleStats1 == null) || (sampleStats2 == null - || Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) { + if ((sampleStats1 == null) || (sampleStats2 == null || + Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) { throw new IllegalArgumentException("insufficient data for t statistic"); } return tTest(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(), @@ -301,9 +296,9 @@ public class TestStatisticImpl implements TestStatistic, Serializable { * @return approximate degrees of freedom */ private double df(double v1, double v2, double n1, double n2) { - return (((v1 / n1) + (v2 / n2)) * ((v1 / n1) + (v2 / n2))) - / ((v1 * v1) / (n1 * n1 * (n1 - 1d)) - + (v2 * v2) / (n2 * n2 * (n2 - 1d))); + return (((v1 / n1) + (v2 / n2)) * ((v1 / n1) + (v2 / n2))) / + ((v1 * v1) / (n1 * n1 * (n1 - 1d)) + (v2 * v2) / + (n2 * n2 * (n2 - 1d))); } /** diff --git a/src/java/org/apache/commons/math/stat/multivariate/BivariateRegression.java b/src/java/org/apache/commons/math/stat/multivariate/BivariateRegression.java index 3c576b3d6..c386e7428 100644 --- a/src/java/org/apache/commons/math/stat/multivariate/BivariateRegression.java +++ b/src/java/org/apache/commons/math/stat/multivariate/BivariateRegression.java @@ -49,10 +49,11 @@ import org.apache.commons.math.distribution.TDistribution; * the necessary computations to return the requested statistic. * * - * @version $Revision: 1.1 $ $Date: 2004/04/11 21:52:28 $ + * @version $Revision: 1.2 $ $Date: 2004/04/27 16:42:34 $ */ public class BivariateRegression implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -3004689053607543335L; /** sum of x values */ @@ -386,6 +387,8 @@ public class BivariateRegression implements Serializable { * Bivariate Normal Distribution. * * @return half-width of 95% confidence interval for the slope estimate + * + * @exception MathException if the confidence interval can not be computed. */ public double getSlopeConfidenceInterval() throws MathException { return getSlopeConfidenceInterval(0.05d); @@ -420,14 +423,15 @@ public class BivariateRegression implements Serializable { * * @param alpha the desired significance level * @return half-width of 95% confidence interval for the slope estimate + * @exception MathException if the confidence interval can not be computed. */ public double getSlopeConfidenceInterval(double alpha) throws MathException { if (alpha >= 1 || alpha <= 0) { throw new IllegalArgumentException(); } - return getSlopeStdErr() - * getTDistribution().inverseCumulativeProbability(1d - alpha / 2d); + return getSlopeStdErr() * + getTDistribution().inverseCumulativeProbability(1d - alpha / 2d); } /** @@ -449,11 +453,11 @@ public class BivariateRegression implements Serializable { * Double.NaN. * * @return significance level for slope/correlation + * @exception MathException if the significance level can not be computed. */ public double getSignificance() throws MathException { return ( - 1d - - getTDistribution().cumulativeProbability( + 1.0 - getTDistribution().cumulativeProbability( Math.abs(getSlope()) / getSlopeStdErr())); } diff --git a/src/java/org/apache/commons/math/stat/univariate/AbstractUnivariateStatistic.java b/src/java/org/apache/commons/math/stat/univariate/AbstractUnivariateStatistic.java index a9b79322d..0ca7b77b2 100644 --- a/src/java/org/apache/commons/math/stat/univariate/AbstractUnivariateStatistic.java +++ b/src/java/org/apache/commons/math/stat/univariate/AbstractUnivariateStatistic.java @@ -19,7 +19,7 @@ package org.apache.commons.math.stat.univariate; * Abstract Implementation for UnivariateStatistics. * Provides the ability to extend polymophically so that * indiviual statistics do not need to implement these methods. - * @version $Revision: 1.15 $ $Date: 2004/04/12 05:22:11 $ + * @version $Revision: 1.16 $ $Date: 2004/04/27 16:42:34 $ */ public abstract class AbstractUnivariateStatistic implements UnivariateStatistic { @@ -28,6 +28,7 @@ public abstract class AbstractUnivariateStatistic * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[]) */ public double evaluate(final double[] values) { + test(values, 0, 0); return evaluate(values, 0, values.length); } @@ -56,6 +57,10 @@ public abstract class AbstractUnivariateStatistic final int begin, final int length) { + if (values == null) { + throw new IllegalArgumentException("input value array is null"); + } + if (begin < 0) { throw new IllegalArgumentException("start position cannot be negative"); } @@ -64,10 +69,6 @@ public abstract class AbstractUnivariateStatistic throw new IllegalArgumentException("length cannot be negative"); } - if (values == null) { - throw new IllegalArgumentException("input value array is null"); - } - if (begin + length > values.length) { throw new IllegalArgumentException( "begin + length > values.length"); diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/FirstMoment.java b/src/java/org/apache/commons/math/stat/univariate/moment/FirstMoment.java index 4997aca8a..2260260b3 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/FirstMoment.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/FirstMoment.java @@ -26,10 +26,11 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis * * recursive strategy * . Both incremental and evaluation strategies currently use this approach. - * @version $Revision: 1.13 $ $Date: 2004/02/21 21:35:15 $ + * @version $Revision: 1.14 $ $Date: 2004/04/27 16:42:30 $ */ public class FirstMoment extends AbstractStorelessUnivariateStatistic implements Serializable{ + /** Serializable version identifier */ static final long serialVersionUID = -803343206421984070L; /** count of values that have been added */ diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/FourthMoment.java b/src/java/org/apache/commons/math/stat/univariate/moment/FourthMoment.java index 22eed4ae8..c9bcc89e4 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/FourthMoment.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/FourthMoment.java @@ -22,10 +22,11 @@ import java.io.Serializable; * * recursive strategy * . Both incremental and evaluation strategies currently use this approach. - * @version $Revision: 1.15 $ $Date: 2004/02/21 21:35:15 $ + * @version $Revision: 1.16 $ $Date: 2004/04/27 16:42:30 $ */ public class FourthMoment extends ThirdMoment implements Serializable{ + /** Serializable version identifier */ static final long serialVersionUID = 4763990447117157611L; /** fourth moment of values that have been added */ diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java b/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java index 356b4f7e1..ad5b440ec 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java @@ -22,10 +22,11 @@ import org.apache.commons.math.stat.univariate.summary.SumOfLogs; /** * Returns the * geometric mean of the available values - * @version $Revision: 1.17 $ $Date: 2004/03/04 04:25:09 $ + * @version $Revision: 1.18 $ $Date: 2004/04/27 16:42:30 $ */ public class GeometricMean extends SumOfLogs implements Serializable{ + /** Serializable version identifier */ static final long serialVersionUID = -8178734905303459453L; /** */ diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/Kurtosis.java b/src/java/org/apache/commons/math/stat/univariate/moment/Kurtosis.java index ffa84a3e0..900616000 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/Kurtosis.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/Kurtosis.java @@ -28,10 +28,11 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis *

* where n is the number of values, mean is the {@link Mean} and std is the {@link StandardDeviation} * - * @version $Revision: 1.18 $ $Date: 2004/03/21 00:22:26 $ + * @version $Revision: 1.19 $ $Date: 2004/04/27 16:42:30 $ */ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = 2784465764798260919L; /** */ diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java b/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java index a0d62276c..4b7ec7658 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java @@ -29,10 +29,11 @@ import org.apache.commons.math.stat.univariate.summary.Sum; /** * Returns the * arithmetic mean of the available values. - * @version $Revision: 1.16 $ $Date: 2004/03/04 04:25:09 $ + * @version $Revision: 1.17 $ $Date: 2004/04/27 16:42:30 $ */ public class Mean extends AbstractStorelessUnivariateStatistic implements Serializable{ + /** Serializable version identifier */ static final long serialVersionUID = -1296043746617791564L; /** first moment of values that have been added */ @@ -106,7 +107,7 @@ public class Mean extends AbstractStorelessUnivariateStatistic implements Serial final int begin, final int length) { if (test(values, begin, length)) { - return sum.evaluate(values) / ((double) length); + return sum.evaluate(values, begin, length) / ((double) length); } return Double.NaN; } diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/SecondMoment.java b/src/java/org/apache/commons/math/stat/univariate/moment/SecondMoment.java index 8c647c4b2..f38f49b51 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/SecondMoment.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/SecondMoment.java @@ -22,10 +22,11 @@ import java.io.Serializable; * * recursive strategy * . Both incremental and evaluation strategies currently use this approach. - * @version $Revision: 1.14 $ $Date: 2004/02/21 21:35:15 $ + * @version $Revision: 1.15 $ $Date: 2004/04/27 16:42:30 $ */ public class SecondMoment extends FirstMoment implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = 3942403127395076445L; /** second moment of values that have been added */ diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/Skewness.java b/src/java/org/apache/commons/math/stat/univariate/moment/Skewness.java index 285540713..d1172d0e5 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/Skewness.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/Skewness.java @@ -28,10 +28,11 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis *

* where n is the number of values, mean is the {@link Mean} and std is the {@link StandardDeviation} * - * @version $Revision: 1.19 $ $Date: 2004/03/21 00:23:29 $ + * @version $Revision: 1.20 $ $Date: 2004/04/27 16:42:30 $ */ public class Skewness extends AbstractStorelessUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = 7101857578996691352L; /** */ @@ -75,6 +76,8 @@ public class Skewness extends AbstractStorelessUnivariateStatistic implements Se * Returns the value of the statistic based on the values that have been added. *

* See {@link Skewness} for the definition used in the computation. + * + * @return the skewness of the available values. */ public double getResult() { if (n < moment.n) { diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/StandardDeviation.java b/src/java/org/apache/commons/math/stat/univariate/moment/StandardDeviation.java index eaf018fea..5c762572c 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/StandardDeviation.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/StandardDeviation.java @@ -19,10 +19,11 @@ import java.io.Serializable; /** * - * @version $Revision: 1.15 $ $Date: 2004/03/04 04:25:09 $ + * @version $Revision: 1.16 $ $Date: 2004/04/27 16:42:30 $ */ public class StandardDeviation extends Variance implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = 5728716329662425188L; /** */ diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/ThirdMoment.java b/src/java/org/apache/commons/math/stat/univariate/moment/ThirdMoment.java index 5e4b048fa..7fe67ef5e 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/ThirdMoment.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/ThirdMoment.java @@ -22,10 +22,11 @@ import java.io.Serializable; * * recursive strategy * . Both incremental and evaluation strategies currently use this approach. - * @version $Revision: 1.14 $ $Date: 2004/02/21 21:35:15 $ + * @version $Revision: 1.15 $ $Date: 2004/04/27 16:42:30 $ */ public class ThirdMoment extends SecondMoment implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -7818711964045118679L; /** third moment of values that have been added */ diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java b/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java index 92ffabc2e..add61928d 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java @@ -25,10 +25,11 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis * J. G. Lewis 1979, Communications of the ACM, * vol. 22 no. 9, pp. 526-531.. * - * @version $Revision: 1.18 $ $Date: 2004/03/04 04:25:09 $ + * @version $Revision: 1.19 $ $Date: 2004/04/27 16:42:30 $ */ public class Variance extends AbstractStorelessUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -9111962718267217978L; /** SecondMoment is used in incremental calculation of Variance*/ diff --git a/src/java/org/apache/commons/math/stat/univariate/rank/Max.java b/src/java/org/apache/commons/math/stat/univariate/rank/Max.java index 8c9d7e359..b6a13622e 100644 --- a/src/java/org/apache/commons/math/stat/univariate/rank/Max.java +++ b/src/java/org/apache/commons/math/stat/univariate/rank/Max.java @@ -28,10 +28,11 @@ import org /** * Returns the maximum of the available values. * - * @version $Revision: 1.15 $ $Date: 2004/04/26 19:15:48 $ + * @version $Revision: 1.16 $ $Date: 2004/04/27 16:42:33 $ */ public class Max extends AbstractStorelessUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -5593383832225844641L; /** */ diff --git a/src/java/org/apache/commons/math/stat/univariate/rank/Median.java b/src/java/org/apache/commons/math/stat/univariate/rank/Median.java index cb8ddf547..475709aeb 100644 --- a/src/java/org/apache/commons/math/stat/univariate/rank/Median.java +++ b/src/java/org/apache/commons/math/stat/univariate/rank/Median.java @@ -22,10 +22,11 @@ import java.io.Serializable; * Returns the median of the * available values. * - * @version $Revision: 1.12 $ $Date: 2004/04/26 19:15:48 $ + * @version $Revision: 1.13 $ $Date: 2004/04/27 16:42:33 $ */ public class Median extends Percentile implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -3961477041290915687L; /** diff --git a/src/java/org/apache/commons/math/stat/univariate/rank/Min.java b/src/java/org/apache/commons/math/stat/univariate/rank/Min.java index a1ccb5270..95d5b9f55 100644 --- a/src/java/org/apache/commons/math/stat/univariate/rank/Min.java +++ b/src/java/org/apache/commons/math/stat/univariate/rank/Min.java @@ -28,10 +28,11 @@ import org /** * Returns the minimum of the available values. * - * @version $Revision: 1.15 $ $Date: 2004/04/26 19:15:48 $ + * @version $Revision: 1.16 $ $Date: 2004/04/27 16:42:33 $ */ public class Min extends AbstractStorelessUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -2941995784909003131L; /** */ diff --git a/src/java/org/apache/commons/math/stat/univariate/rank/Percentile.java b/src/java/org/apache/commons/math/stat/univariate/rank/Percentile.java index 6882f6167..e697fefae 100644 --- a/src/java/org/apache/commons/math/stat/univariate/rank/Percentile.java +++ b/src/java/org/apache/commons/math/stat/univariate/rank/Percentile.java @@ -28,10 +28,11 @@ import org.apache.commons.math.stat.univariate.AbstractUnivariateStatistic; * follows the first estimation procedure presented * here. * - * @version $Revision: 1.17 $ $Date: 2004/04/26 19:15:48 $ + * @version $Revision: 1.18 $ $Date: 2004/04/27 16:42:33 $ */ public class Percentile extends AbstractUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -8091216485095130416L; /** Determines what percentile is computed when evaluate() is activated with no quantile argument */ @@ -68,6 +69,7 @@ public class Percentile extends AbstractUnivariateStatistic implements Serializa * if the array is empty */ public double evaluate(final double[] values, final double p) { + test(values, 0, 0); return evaluate(values, 0, values.length, p); } diff --git a/src/java/org/apache/commons/math/stat/univariate/summary/Product.java b/src/java/org/apache/commons/math/stat/univariate/summary/Product.java index 705a93e0b..baf45733a 100644 --- a/src/java/org/apache/commons/math/stat/univariate/summary/Product.java +++ b/src/java/org/apache/commons/math/stat/univariate/summary/Product.java @@ -28,10 +28,11 @@ import org /** * Returns the product for this collection of values. * - * @version $Revision: 1.16 $ $Date: 2004/04/26 19:15:48 $ + * @version $Revision: 1.17 $ $Date: 2004/04/27 16:42:32 $ */ public class Product extends AbstractStorelessUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = 2824226005990582538L; /** */ diff --git a/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java b/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java index 71b948a73..a0b326a2e 100644 --- a/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java +++ b/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java @@ -22,10 +22,11 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis /** * The sum of the values that have been added to Univariate. * - * @version $Revision: 1.18 $ $Date: 2004/04/26 19:15:48 $ + * @version $Revision: 1.19 $ $Date: 2004/04/27 16:42:32 $ */ public class Sum extends AbstractStorelessUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -8231831954703408316L; /** */ diff --git a/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java b/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java index 75811916b..051eaf6c3 100644 --- a/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java +++ b/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java @@ -28,10 +28,11 @@ import org /** * Returns the sum of the natural logs for this collection of values. * - * @version $Revision: 1.16 $ $Date: 2004/04/26 19:15:48 $ + * @version $Revision: 1.17 $ $Date: 2004/04/27 16:42:32 $ */ public class SumOfLogs extends AbstractStorelessUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -370076995648386763L; /** */ diff --git a/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java b/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java index 20bd807ce..43c6181a2 100644 --- a/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java +++ b/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java @@ -28,10 +28,11 @@ import org /** * Returns the sum of the squares of the available values. * - * @version $Revision: 1.16 $ $Date: 2004/04/26 19:15:48 $ + * @version $Revision: 1.17 $ $Date: 2004/04/27 16:42:32 $ */ public class SumOfSquares extends AbstractStorelessUnivariateStatistic implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = 1460986908574398008L; /** */ diff --git a/src/java/org/apache/commons/math/util/ContractableDoubleArray.java b/src/java/org/apache/commons/math/util/ContractableDoubleArray.java index a7573c451..c9f4b7af9 100644 --- a/src/java/org/apache/commons/math/util/ContractableDoubleArray.java +++ b/src/java/org/apache/commons/math/util/ContractableDoubleArray.java @@ -50,10 +50,11 @@ import java.io.Serializable; * internal storage array is swapped. *

* - * @version $Revision: 1.12 $ $Date: 2004/02/21 21:35:16 $ + * @version $Revision: 1.13 $ $Date: 2004/04/27 16:42:34 $ */ public class ContractableDoubleArray extends ExpandableDoubleArray implements Serializable { + /** Serializable version identifier */ static final long serialVersionUID = -3485529955529426875L; /** The contraction criteria defines the conditions under which this diff --git a/src/java/org/apache/commons/math/util/ExpandableDoubleArray.java b/src/java/org/apache/commons/math/util/ExpandableDoubleArray.java index b7455e16f..8982390b6 100644 --- a/src/java/org/apache/commons/math/util/ExpandableDoubleArray.java +++ b/src/java/org/apache/commons/math/util/ExpandableDoubleArray.java @@ -50,10 +50,11 @@ import java.io.Serializable; * expand the array 10 times - first from 2 -> 4. then 4 -> 8, 8 -> 16, * and so on until we reach 4096 which is sufficient to hold 3546 elements. *

- * @version $Revision: 1.13 $ $Date: 2004/02/21 21:35:16 $ + * @version $Revision: 1.14 $ $Date: 2004/04/27 16:42:34 $ */ public class ExpandableDoubleArray implements Serializable, DoubleArray { + /** Serializable version identifier */ static final long serialVersionUID = -5697417774251632284L; // TODO: expansionFactor is valuable, by if I only need storage diff --git a/src/java/org/apache/commons/math/util/FixedDoubleArray.java b/src/java/org/apache/commons/math/util/FixedDoubleArray.java index a46289117..25a5dd732 100644 --- a/src/java/org/apache/commons/math/util/FixedDoubleArray.java +++ b/src/java/org/apache/commons/math/util/FixedDoubleArray.java @@ -46,10 +46,11 @@ import java.io.Serializable; * "fixed" in memory, this implementation will never allocate, or copy * the internal storage array to a new array instance. *

- * @version $Revision: 1.14 $ $Date: 2004/02/21 21:35:16 $ + * @version $Revision: 1.15 $ $Date: 2004/04/27 16:42:34 $ */ public class FixedDoubleArray implements DoubleArray, Serializable { + /** Serializable version identifier */ static final long serialVersionUID = 1247853239629842963L; /** diff --git a/src/test/org/apache/commons/math/analysis/UnivariateRealSolverFactoryImplTest.java b/src/test/org/apache/commons/math/analysis/UnivariateRealSolverFactoryImplTest.java new file mode 100644 index 000000000..47cab228f --- /dev/null +++ b/src/test/org/apache/commons/math/analysis/UnivariateRealSolverFactoryImplTest.java @@ -0,0 +1,111 @@ +/* + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.math.analysis; + +import junit.framework.TestCase; + +/** + * @version $Revision: 1.1 $ $Date: 2004/04/27 16:42:32 $ + */ +public class UnivariateRealSolverFactoryImplTest extends TestCase { + + /** solver factory */ + private UnivariateRealSolverFactory factory; + + /** function */ + private DifferentiableUnivariateRealFunction function; + /** + * @throws java.lang.Exception + * @see junit.framework.TestCase#tearDown() + */ + protected void setUp() throws Exception { + super.setUp(); + factory = new UnivariateRealSolverFactoryImpl(); + function = new SinFunction(); + } + + /** + * @throws java.lang.Exception + * @see junit.framework.TestCase#tearDown() + */ + protected void tearDown() throws Exception { + factory = null; + function = null; + super.tearDown(); + } + + public void testNewBisectionSolverNull() { + try { + UnivariateRealSolver solver = factory.newBisectionSolver(null); + fail(); + } catch(IllegalArgumentException ex) { + // success + } + } + + public void testNewBisectionSolverValid() { + UnivariateRealSolver solver = factory.newBisectionSolver(function); + assertNotNull(solver); + assertTrue(solver instanceof BisectionSolver); + } + + public void testNewNewtonSolverNull() { + try { + UnivariateRealSolver solver = factory.newNewtonSolver(null); + fail(); + } catch(IllegalArgumentException ex) { + // success + } + } + + public void testNewNewtonSolverValid() { + UnivariateRealSolver solver = factory.newNewtonSolver(function); + assertNotNull(solver); + assertTrue(solver instanceof NewtonSolver); + } + + public void testNewBrentSolverNull() { + try { + UnivariateRealSolver solver = factory.newBrentSolver(null); + fail(); + } catch(IllegalArgumentException ex) { + // success + } + } + + public void testNewBrentSolverValid() { + UnivariateRealSolver solver = factory.newBrentSolver(function); + assertNotNull(solver); + assertTrue(solver instanceof BrentSolver); + } + + public void testNewSecantSolverNull() { + try { + UnivariateRealSolver solver = factory.newSecantSolver(null); + fail(); + } catch(IllegalArgumentException ex) { + // success + } + } + + public void testNewSecantSolverValid() { + UnivariateRealSolver solver = factory.newSecantSolver(function); + assertNotNull(solver); + assertTrue(solver instanceof SecantSolver); + } +} diff --git a/src/test/org/apache/commons/math/stat/StatUtilsTest.java b/src/test/org/apache/commons/math/stat/StatUtilsTest.java index 1bc4fb3bf..f4acdc0eb 100644 --- a/src/test/org/apache/commons/math/stat/StatUtilsTest.java +++ b/src/test/org/apache/commons/math/stat/StatUtilsTest.java @@ -19,11 +19,12 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.commons.math.TestUtils; import org.apache.commons.math.stat.univariate.DescriptiveStatistics; /** * Test cases for the {@link StatUtils} class. - * @version $Revision: 1.14 $ $Date: 2004/04/12 02:27:49 $ + * @version $Revision: 1.15 $ $Date: 2004/04/27 16:42:31 $ */ public final class StatUtilsTest extends TestCase { @@ -163,4 +164,232 @@ public final class StatUtilsTest extends TestCase { } } + + public void testSumSq() { + double[] x = null; + + // test null + try { + StatUtils.sumSq(x); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + try { + StatUtils.sumSq(x, 0, 4); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + // test empty + x = new double[] {}; + TestUtils.assertEquals(Double.NaN, StatUtils.sumSq(x), tolerance); + TestUtils.assertEquals(Double.NaN, StatUtils.sumSq(x, 0, 0), tolerance); + + // test one + x = new double[] {two}; + TestUtils.assertEquals(4, StatUtils.sumSq(x), tolerance); + TestUtils.assertEquals(4, StatUtils.sumSq(x, 0, 1), tolerance); + + // test many + x = new double[] {one, two, two, three}; + TestUtils.assertEquals(18, StatUtils.sumSq(x), tolerance); + TestUtils.assertEquals(8, StatUtils.sumSq(x, 1, 2), tolerance); + } + + public void testProduct() { + double[] x = null; + + // test null + try { + StatUtils.product(x); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + try { + StatUtils.product(x, 0, 4); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + // test empty + x = new double[] {}; + TestUtils.assertEquals(Double.NaN, StatUtils.product(x), tolerance); + TestUtils.assertEquals(Double.NaN, StatUtils.product(x, 0, 0), tolerance); + + // test one + x = new double[] {two}; + TestUtils.assertEquals(two, StatUtils.product(x), tolerance); + TestUtils.assertEquals(two, StatUtils.product(x, 0, 1), tolerance); + + // test many + x = new double[] {one, two, two, three}; + TestUtils.assertEquals(12, StatUtils.product(x), tolerance); + TestUtils.assertEquals(4, StatUtils.product(x, 1, 2), tolerance); + } + + public void testSumLog() { + double[] x = null; + + // test null + try { + StatUtils.sumLog(x); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + try { + StatUtils.sumLog(x, 0, 4); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + // test empty + x = new double[] {}; + TestUtils.assertEquals(Double.NaN, StatUtils.sumLog(x), tolerance); + TestUtils.assertEquals(Double.NaN, StatUtils.sumLog(x, 0, 0), tolerance); + + // test one + x = new double[] {two}; + TestUtils.assertEquals(Math.log(two), StatUtils.sumLog(x), tolerance); + TestUtils.assertEquals(Math.log(two), StatUtils.sumLog(x, 0, 1), tolerance); + + // test many + x = new double[] {one, two, two, three}; + TestUtils.assertEquals(Math.log(one) + 2.0 * Math.log(two) + Math.log(three), StatUtils.sumLog(x), tolerance); + TestUtils.assertEquals(2.0 * Math.log(two), StatUtils.sumLog(x, 1, 2), tolerance); + } + + public void testMean() { + double[] x = null; + + try { + StatUtils.mean(x, 0, 4); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + // test empty + x = new double[] {}; + TestUtils.assertEquals(Double.NaN, StatUtils.mean(x, 0, 0), tolerance); + + // test one + x = new double[] {two}; + TestUtils.assertEquals(two, StatUtils.mean(x, 0, 1), tolerance); + + // test many + x = new double[] {one, two, two, three}; + TestUtils.assertEquals(2.5, StatUtils.mean(x, 2, 2), tolerance); + } + + public void testVariance() { + double[] x = null; + + try { + StatUtils.variance(x, 0, 4); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + // test empty + x = new double[] {}; + TestUtils.assertEquals(Double.NaN, StatUtils.variance(x, 0, 0), tolerance); + + // test one + x = new double[] {two}; + TestUtils.assertEquals(0.0, StatUtils.variance(x, 0, 1), tolerance); + + // test many + x = new double[] {one, two, two, three}; + TestUtils.assertEquals(0.5, StatUtils.variance(x, 2, 2), tolerance); + } + + public void testMax() { + double[] x = null; + + try { + StatUtils.max(x, 0, 4); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + // test empty + x = new double[] {}; + TestUtils.assertEquals(Double.NaN, StatUtils.max(x, 0, 0), tolerance); + + // test one + x = new double[] {two}; + TestUtils.assertEquals(two, StatUtils.max(x, 0, 1), tolerance); + + // test many + x = new double[] {one, two, two, three}; + TestUtils.assertEquals(three, StatUtils.max(x, 1, 3), tolerance); + } + + public void testMin() { + double[] x = null; + + try { + StatUtils.min(x, 0, 4); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + // test empty + x = new double[] {}; + TestUtils.assertEquals(Double.NaN, StatUtils.min(x, 0, 0), tolerance); + + // test one + x = new double[] {two}; + TestUtils.assertEquals(two, StatUtils.min(x, 0, 1), tolerance); + + // test many + x = new double[] {one, two, two, three}; + TestUtils.assertEquals(two, StatUtils.min(x, 1, 3), tolerance); + } + + public void testPercentile() { + double[] x = null; + + // test null + try { + StatUtils.percentile(x, .25); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + try { + StatUtils.percentile(x, 0, 4, 0.25); + fail("null is not a valid data array."); + } catch (IllegalArgumentException ex) { + // success + } + + // test empty + x = new double[] {}; + TestUtils.assertEquals(Double.NaN, StatUtils.percentile(x, 25), tolerance); + TestUtils.assertEquals(Double.NaN, StatUtils.percentile(x, 0, 0, 25), tolerance); + + // test one + x = new double[] {two}; + TestUtils.assertEquals(two, StatUtils.percentile(x, 25), tolerance); + TestUtils.assertEquals(two, StatUtils.percentile(x, 0, 1, 25), tolerance); + + // test many + x = new double[] {one, two, two, three}; + TestUtils.assertEquals(2.5, StatUtils.percentile(x, 70), tolerance); + TestUtils.assertEquals(2.5, StatUtils.percentile(x, 1, 3, 62.5), tolerance); + } } \ No newline at end of file diff --git a/src/test/org/apache/commons/math/stat/univariate/DescriptiveStatisticsTest.java b/src/test/org/apache/commons/math/stat/univariate/DescriptiveStatisticsTest.java index 521d32def..6e00a9b40 100644 --- a/src/test/org/apache/commons/math/stat/univariate/DescriptiveStatisticsTest.java +++ b/src/test/org/apache/commons/math/stat/univariate/DescriptiveStatisticsTest.java @@ -26,7 +26,7 @@ import org.apache.commons.math.random.RandomDataImpl; /** * Test cases for the {@link Univariate} class. * - * @version $Revision: 1.1 $ $Date: 2004/04/12 02:27:50 $ + * @version $Revision: 1.2 $ $Date: 2004/04/27 16:42:32 $ */ public final class DescriptiveStatisticsTest extends TestCase { @@ -328,6 +328,49 @@ public final class DescriptiveStatisticsTest extends TestCase { u2.clear(); assertEquals("total count",0,u2.getN(),tolerance); } - + + public void testNewInstanceStringNull() { + try { + DescriptiveStatistics u = DescriptiveStatistics.newInstance((String)null); + fail("null is not a valid descriptive statistics class name"); + } catch (NullPointerException ex) { + // success + } catch (Exception ex) { + fail(); + } + + } + public void testNewInstanceStringValid() { + try { + DescriptiveStatistics u = DescriptiveStatistics.newInstance( + "org.apache.commons.math.stat.univariate.DescriptiveStatisticsImpl"); + assertNotNull(u); + assertTrue(u instanceof DescriptiveStatisticsImpl); + } catch (Exception ex) { + fail(); + } + } + + public void testNewInstanceClassNull() { + try { + DescriptiveStatistics u = DescriptiveStatistics.newInstance((Class)null); + fail("null is not a valid descriptive statistics class"); + } catch (NullPointerException ex) { + // success + } catch (Exception ex) { + fail(); + } + + } + public void testNewInstanceClassValid() { + try { + DescriptiveStatistics u = DescriptiveStatistics.newInstance( + DescriptiveStatisticsImpl.class); + assertNotNull(u); + assertTrue(u instanceof DescriptiveStatisticsImpl); + } catch (Exception ex) { + fail(); + } + } } diff --git a/src/test/org/apache/commons/math/stat/univariate/StorelessUnivariateStatisticAbstractTest.java b/src/test/org/apache/commons/math/stat/univariate/StorelessUnivariateStatisticAbstractTest.java index f0be62ac9..f8fc4a6af 100644 --- a/src/test/org/apache/commons/math/stat/univariate/StorelessUnivariateStatisticAbstractTest.java +++ b/src/test/org/apache/commons/math/stat/univariate/StorelessUnivariateStatisticAbstractTest.java @@ -19,7 +19,7 @@ import org.apache.commons.math.TestUtils; /** * Test cases for the {@link UnivariateStatistic} class. - * @version $Revision: 1.11 $ $Date: 2004/02/21 21:35:17 $ + * @version $Revision: 1.12 $ $Date: 2004/04/27 16:42:32 $ */ public abstract class StorelessUnivariateStatisticAbstractTest extends UnivariateStatisticAbstractTest { @@ -31,7 +31,7 @@ public abstract class StorelessUnivariateStatisticAbstractTest public abstract UnivariateStatistic getUnivariateStatistic(); public abstract double expectedValue(); - + public void testIncrementation() throws Exception { StorelessUnivariateStatistic statistic = diff --git a/src/test/org/apache/commons/math/stat/univariate/moment/FirstMomentTest.java b/src/test/org/apache/commons/math/stat/univariate/moment/FirstMomentTest.java new file mode 100644 index 000000000..9b69606d7 --- /dev/null +++ b/src/test/org/apache/commons/math/stat/univariate/moment/FirstMomentTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2003-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.stat.univariate.moment; + +import org.apache.commons.math.stat.univariate.StorelessUnivariateStatisticAbstractTest; +import org.apache.commons.math.stat.univariate.UnivariateStatistic; + +/** + * Test cases for the {@link UnivariateStatistic} class. + * @version $Revision: 1.1 $ $Date: 2004/04/27 16:42:33 $ + */ +public class FirstMomentTest extends StorelessUnivariateStatisticAbstractTest{ + + /** descriptive statistic. */ + protected FirstMoment stat; + + /** + * @param name + */ + public FirstMomentTest(String name) { + super(name); + } + + /** + * @see org.apache.commons.math.stat.univariate.UnivariateStatisticAbstractTest#getUnivariateStatistic() + */ + public UnivariateStatistic getUnivariateStatistic() { + + if(stat == null) + stat = new FirstMoment(); + + return stat; + } + + /** + * @see org.apache.commons.math.stat.univariate.UnivariateStatisticAbstractTest#expectedValue() + */ + public double expectedValue() { + return this.mean; + } + +}