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
This commit is contained in:
Brent Worden 2004-04-27 16:42:34 +00:00
parent 4952dd85d6
commit c415dd79ae
32 changed files with 574 additions and 114 deletions

View File

@ -23,9 +23,10 @@ import org.apache.commons.math.MathException;
* Implements the <a href="http://mathworld.wolfram.com/Bisection.html">bisection algorithm</a>
* 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.

View File

@ -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;

View File

@ -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 <code>x</code>.
*/
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,8 +143,8 @@ 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.);
@ -171,8 +162,8 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
r + 1.27045825245236838258) * r +
3.64784832476320460504) * r + 5.7694972214606914055) *
r + 4.6303378461565452959) * r +
1.42343711074968357734)
/ (((((((r *
1.42343711074968357734) /
(((((((r *
1.05075007164441684324e-9 + 5.475938084995344946e-4) *
r + 0.0151986665636164571966) * r +
0.14810397642748007459) * r + 0.68976733498510000455) *
@ -193,8 +184,9 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
r + 0.13692988092273580531) * r +
0.59983220655588793769) * r + 1.0);
}
if(q < 0.0)
if(q < 0.0) {
val = -val;
}
}
return mean + standardDeviation * val;
}

View File

@ -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)));
}
/**

View File

@ -49,10 +49,11 @@ import org.apache.commons.math.distribution.TDistribution;
* the necessary computations to return the requested statistic.</li>
* </ul>
*
* @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</a>.
*
* @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 {
* <code>Double.NaN</code>.
*
* @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()));
}

View File

@ -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");

View File

@ -26,10 +26,11 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.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 */

View File

@ -22,10 +22,11 @@ import java.io.Serializable;
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.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 */

View File

@ -22,10 +22,11 @@ import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
/**
* Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
* geometric mean </a> of the available values
* @version $Revision: 1.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;
/** */

View File

@ -28,10 +28,11 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
* <p>
* 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;
/** */

View File

@ -29,10 +29,11 @@ import org.apache.commons.math.stat.univariate.summary.Sum;
/**
* Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
* arithmetic mean </a> 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;
}

View File

@ -22,10 +22,11 @@ import java.io.Serializable;
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.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 */

View File

@ -28,10 +28,11 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
* <p>
* 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.
* <p>
* See {@link Skewness} for the definition used in the computation.
*
* @return the skewness of the available values.
*/
public double getResult() {
if (n < moment.n) {

View File

@ -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;
/** */

View File

@ -22,10 +22,11 @@ import java.io.Serializable;
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.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 */

View File

@ -25,10 +25,11 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
* J. G. Lewis 1979, <i>Communications of the ACM</i>,
* vol. 22 no. 9, pp. 526-531.</a>.
*
* @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*/

View File

@ -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;
/** */

View File

@ -22,10 +22,11 @@ import java.io.Serializable;
* Returns the <a href="http://www.xycoon.com/median_2.htm">median</a> 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;
/**

View File

@ -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;
/** */

View File

@ -28,10 +28,11 @@ import org.apache.commons.math.stat.univariate.AbstractUnivariateStatistic;
* follows the first estimation procedure presented
* <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc252.htm">here.</a>
*
* @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);
}

View File

@ -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;
/** */

View File

@ -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;
/** */

View File

@ -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;
/** */

View File

@ -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;
/** */

View File

@ -50,10 +50,11 @@ import java.io.Serializable;
* internal storage array is swapped.
* </p>
*
* @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

View File

@ -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.
* </p>
* @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

View File

@ -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.
* </p>
* @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;
/**

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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 {
@ -329,5 +329,48 @@ public final class DescriptiveStatisticsTest extends TestCase {
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();
}
}
}

View File

@ -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 {

View File

@ -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;
}
}