Exposed MathException handling in Distributions by adding thows MathException to methods where MathExceptions are caught and replaced with Double.NaN. Added Serialization Interface to Distributions, BivariateRegression and TestStatistic

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141035 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-11-19 03:22:54 +00:00
parent 0810e249ac
commit 0718895962
24 changed files with 1614 additions and 1117 deletions

View File

@ -62,11 +62,11 @@ import org.apache.commons.math.analysis.UnivariateRealSolverUtils;
* implementations for some of the methods that do not vary from distribution
* to distribution.
*
* @version $Revision: 1.15 $ $Date: 2003/11/15 18:47:09 $
* @version $Revision: 1.16 $ $Date: 2003/11/19 03:22:53 $
*/
public abstract class AbstractContinuousDistribution
implements ContinuousDistribution {
/**
* Default constructor.
*/
@ -83,10 +83,11 @@ public abstract class AbstractContinuousDistribution
* @param x1 the upper bound
* @return the cummulative probability.
*/
public double cummulativeProbability(double x0, double x1) {
public double cummulativeProbability(double x0, double x1)
throws MathException {
return cummulativeProbability(x1) - cummulativeProbability(x0);
}
/**
* For this distribution, X, this method returns the critical point x, such
* that P(X &lt; x) = <code>p</code>.
@ -94,39 +95,40 @@ public abstract class AbstractContinuousDistribution
* @param p the desired probability
* @return x, such that P(X &lt; x) = <code>p</code>
*/
public double inverseCummulativeProbability(final double p) {
public double inverseCummulativeProbability(final double p)
throws MathException {
if (p < 0.0 || p > 1.0) {
throw new IllegalArgumentException(
"p must be between 0.0 and 1.0, inclusive.");
throw new IllegalArgumentException("p must be between 0.0 and 1.0, inclusive.");
}
// by default, do simple root finding using bracketing and bisection.
// subclasses can overide if there is a better method.
UnivariateRealFunction rootFindingFunction =
new UnivariateRealFunction() {
public double value(double x) throws MathException {
return cummulativeProbability(x) - p;
}
};
try {
// bracket root
double[] bracket = UnivariateRealSolverUtils.bracket(rootFindingFunction,
getInitialDomain(p), getDomainLowerBound(p),
// bracket root
double[] bracket =
UnivariateRealSolverUtils.bracket(
rootFindingFunction,
getInitialDomain(p),
getDomainLowerBound(p),
getDomainUpperBound(p));
// find root
double root = UnivariateRealSolverUtils.solve(
rootFindingFunction, bracket[0], bracket[1]);
return root;
} catch (MathException ex) {
// this should never happen.
return Double.NaN;
}
// find root
double root =
UnivariateRealSolverUtils.solve(
rootFindingFunction,
bracket[0],
bracket[1]);
return root;
}
/**
* Access the initial domain value, based on <code>p</code>, used to
* bracket a CDF root. This method is used by
@ -136,7 +138,7 @@ public abstract class AbstractContinuousDistribution
* @return initial domain value
*/
protected abstract double getInitialDomain(double p);
/**
* Access the domain value lower bound, based on <code>p</code>, used to
* bracket a CDF root. This method is used by
@ -147,7 +149,7 @@ public abstract class AbstractContinuousDistribution
* P(X &lt; <i>lower bound</i>) &lt; <code>p</code>
*/
protected abstract double getDomainLowerBound(double p);
/**
* Access the domain value upper bound, based on <code>p</code>, used to
* bracket a CDF root. This method is used by

View File

@ -53,13 +53,15 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
/**
* Base class for various discrete distributions. It provides default
* implementations for some of the methods that do not vary from distribution
* to distribution.
*
* @version $Revision: 1.7 $ $Date: 2003/11/15 16:01:35 $
* @version $Revision: 1.8 $ $Date: 2003/11/19 03:22:53 $
*/
public abstract class AbstractDiscreteDistribution
implements DiscreteDistribution {
@ -77,7 +79,7 @@ public abstract class AbstractDiscreteDistribution
* @param x1 the inclusive, upper bound
* @return the cummulative probability.
*/
public double cummulativeProbability(int x0, int x1) {
public double cummulativeProbability(int x0, int x1) throws MathException{
return cummulativeProbability(x1) -
cummulativeProbability(x0 - 1);
}
@ -89,7 +91,7 @@ public abstract class AbstractDiscreteDistribution
* @param p the desired probability
* @return x, such that P(X &lt; x) = <code>p</code>
*/
public int inverseCummulativeProbability(final double p) {
public int inverseCummulativeProbability(final double p) throws MathException{
if (p < 0.0 || p > 1.0) {
throw new IllegalArgumentException(
"p must be between 0.0 and 1.0, inclusive.");

View File

@ -53,6 +53,8 @@
*/
package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.special.Beta;
import org.apache.commons.math.util.MathUtils;
@ -60,17 +62,18 @@ import org.apache.commons.math.util.MathUtils;
/**
* The default implementation of {@link BinomialDistribution}.
*
* @version $Revision: 1.7 $ $Date: 2003/11/15 18:59:10 $
* @version $Revision: 1.8 $ $Date: 2003/11/19 03:22:53 $
*/
public class BinomialDistributionImpl extends AbstractDiscreteDistribution
implements BinomialDistribution {
public class BinomialDistributionImpl
extends AbstractDiscreteDistribution
implements BinomialDistribution, Serializable {
/** The number of trials. */
private int numberOfTrials;
/** The probability of success. */
private double probabilityOfSuccess;
/**
* Create a binomial distribution with the given number of trials and
* probability of success.
@ -82,7 +85,7 @@ public class BinomialDistributionImpl extends AbstractDiscreteDistribution
setNumberOfTrials(trials);
setProbabilityOfSuccess(p);
}
/**
* Access the number of trials for this distribution.
* @return the number of trials.
@ -105,8 +108,7 @@ public class BinomialDistributionImpl extends AbstractDiscreteDistribution
*/
public void setNumberOfTrials(int trials) {
if (trials < 0) {
throw new IllegalArgumentException(
"number of trials must be non-negative.");
throw new IllegalArgumentException("number of trials must be non-negative.");
}
numberOfTrials = trials;
}
@ -117,12 +119,11 @@ public class BinomialDistributionImpl extends AbstractDiscreteDistribution
*/
public void setProbabilityOfSuccess(double p) {
if (p < 0.0 || p > 1.0) {
throw new IllegalArgumentException(
"probability of success must be between 0.0 and 1.0, inclusive.");
throw new IllegalArgumentException("probability of success must be between 0.0 and 1.0, inclusive.");
}
probabilityOfSuccess = p;
}
/**
* Access the domain value lower bound, based on <code>p</code>, used to
* bracket a PDF root.
@ -152,19 +153,19 @@ public class BinomialDistributionImpl extends AbstractDiscreteDistribution
* @param x the value at which the PDF is evaluated.
* @return PDF for this distribution.
*/
public double cummulativeProbability(int x) {
public double cummulativeProbability(int x) throws MathException {
double ret;
if (x < 0) {
ret = 0.0;
} else if (x >= getNumberOfTrials()) {
ret = 1.0;
} else {
try {
ret = 1.0 - Beta.regularizedBeta(getProbabilityOfSuccess(),
x + 1.0, getNumberOfTrials() - x);
} catch (MathException ex) {
ret = Double.NaN;
}
ret =
1.0
- Beta.regularizedBeta(
getProbabilityOfSuccess(),
x + 1.0,
getNumberOfTrials() - x);
}
return ret;
}
@ -179,10 +180,12 @@ public class BinomialDistributionImpl extends AbstractDiscreteDistribution
if (x < 0 || x > getNumberOfTrials()) {
ret = 0.0;
} else {
ret = MathUtils.binomialCoefficientDouble(getNumberOfTrials(), x) *
Math.pow(getProbabilityOfSuccess(), x) *
Math.pow(1.0 - getProbabilityOfSuccess(),
getNumberOfTrials() - x);
ret =
MathUtils.binomialCoefficientDouble(getNumberOfTrials(), x)
* Math.pow(getProbabilityOfSuccess(), x)
* Math.pow(
1.0 - getProbabilityOfSuccess(),
getNumberOfTrials() - x);
}
return ret;
}

View File

@ -53,14 +53,18 @@
*/
package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
/**
* The default implementation of {@link ChiSquaredDistribution}
*
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:35 $
* @version $Revision: 1.11 $ $Date: 2003/11/19 03:22:53 $
*/
public class ChiSquaredDistributionImpl
extends AbstractContinuousDistribution
implements ChiSquaredDistribution {
implements ChiSquaredDistribution, Serializable {
/** Internal Gamma distribution. */
private GammaDistribution gamma;
@ -96,7 +100,7 @@ public class ChiSquaredDistributionImpl
* @param x the value at which the CDF is evaluated.
* @return CDF for this distribution.
*/
public double cummulativeProbability(double x) {
public double cummulativeProbability(double x) throws MathException {
return getGamma().cummulativeProbability(x);
}

View File

@ -53,10 +53,12 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
/**
* Base interface for various continuous distributions.
*
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:35 $
* @version $Revision: 1.9 $ $Date: 2003/11/19 03:22:53 $
*/
public interface ContinuousDistribution {
/**
@ -64,7 +66,7 @@ public interface ContinuousDistribution {
* @param x the value at which the CDF is evaluated.
* @return CDF for this distribution.
*/
double cummulativeProbability(double x);
double cummulativeProbability(double x) throws MathException;
/**
* For this disbution, X, this method returns P(x0 &lt; X &lt; x1).
@ -72,12 +74,12 @@ public interface ContinuousDistribution {
* @param x1 the upper bound
* @return the cummulative probability.
*/
double cummulativeProbability(double x0, double x1);
double cummulativeProbability(double x0, double x1) throws MathException;
/**
* For this disbution, X, this method returns x such that P(X &lt; x) = p.
* @param p the cummulative probability.
* @return x.
*/
double inverseCummulativeProbability(double p);
double inverseCummulativeProbability(double p) throws MathException;
}

View File

@ -53,10 +53,12 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
/**
* Base interface for various discrete distributions.
*
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:35 $
* @version $Revision: 1.7 $ $Date: 2003/11/19 03:22:53 $
*/
public interface DiscreteDistribution {
/**
@ -71,7 +73,7 @@ public interface DiscreteDistribution {
* @param x the value at which the PDF is evaluated.
* @return PDF for this distribution.
*/
double cummulativeProbability(int x);
double cummulativeProbability(int x) throws MathException;
/**
* For this disbution, X, this method returns P(x0 &le; X &le; x1).
@ -79,12 +81,12 @@ public interface DiscreteDistribution {
* @param x1 the inclusive, upper bound
* @return the cummulative probability.
*/
double cummulativeProbability(int x0, int x1);
double cummulativeProbability(int x0, int x1) throws MathException;
/**
* For this disbution, X, this method returns x such that P(X &le; x) <= p.
* @param p the cummulative probability.
* @return x.
*/
int inverseCummulativeProbability(double p);
int inverseCummulativeProbability(double p) throws MathException;
}

View File

@ -53,14 +53,16 @@
*/
package org.apache.commons.math.distribution;
import java.io.Serializable;
/**
* A concrete distribution factory. This is the default factory used by
* Commons-Math.
*
* @version $Revision: 1.15 $ $Date: 2003/11/15 16:01:36 $
* @version $Revision: 1.16 $ $Date: 2003/11/19 03:22:53 $
*/
public class DistributionFactoryImpl extends DistributionFactory {
public class DistributionFactoryImpl extends DistributionFactory implements Serializable {
/**
* Default constructor. Package scope to prevent unwanted instantiation.
*/

View File

@ -53,13 +53,17 @@
*/
package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
/**
* The default implementation of {@link ExponentialDistribution}
*
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:36 $
* @version $Revision: 1.10 $ $Date: 2003/11/19 03:22:53 $
*/
public class ExponentialDistributionImpl
implements ExponentialDistribution {
implements ExponentialDistribution, Serializable {
/** The mean of this distribution. */
private double mean;
@ -105,7 +109,7 @@ public class ExponentialDistributionImpl
* @param x the value at which the CDF is evaluated.
* @return CDF for this distribution.
*/
public double cummulativeProbability(double x) {
public double cummulativeProbability(double x) throws MathException{
double ret;
if (x <= 0.0) {
ret = 0.0;
@ -122,7 +126,7 @@ public class ExponentialDistributionImpl
* @param p the desired probability
* @return x, such that P(X &lt; x) = <code>p</code>
*/
public double inverseCummulativeProbability(double p) {
public double inverseCummulativeProbability(double p) throws MathException{
double ret;
if (p < 0.0 || p > 1.0) {
@ -142,7 +146,7 @@ public class ExponentialDistributionImpl
* @param x1 the upper bound
* @return the cummulative probability.
*/
public double cummulativeProbability(double x0, double x1) {
public double cummulativeProbability(double x0, double x1) throws MathException{
return cummulativeProbability(x1) - cummulativeProbability(x0);
}
}

View File

@ -53,6 +53,8 @@
*/
package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.special.Beta;
@ -60,11 +62,11 @@ import org.apache.commons.math.special.Beta;
* Default implementation of
* {@link org.apache.commons.math.distribution.FDistribution}.
*
* @version $Revision: 1.10 $ $Date: 2003/11/15 18:59:10 $
* @version $Revision: 1.11 $ $Date: 2003/11/19 03:22:53 $
*/
public class FDistributionImpl
extends AbstractContinuousDistribution
implements FDistribution {
implements FDistribution, Serializable {
/** The numerator degrees of freedom*/
private double numeratorDegreesOfFreedom;
@ -97,7 +99,7 @@ public class FDistributionImpl
* @param x the value at which the CDF is evaluated.
* @return CDF for this distribution.
*/
public double cummulativeProbability(double x) {
public double cummulativeProbability(double x) throws MathException {
double ret;
if (x <= 0.0) {
ret = 0.0;
@ -105,13 +107,9 @@ public class FDistributionImpl
double n = getNumeratorDegreesOfFreedom();
double m = getDenominatorDegreesOfFreedom();
try {
ret = Beta.regularizedBeta((n * x) / (m + n * x),
0.5 * n,
0.5 * m);
} catch (MathException ex) {
ret = Double.NaN;
}
ret = Beta.regularizedBeta((n * x) / (m + n * x),
0.5 * n,
0.5 * m);
}
return ret;
}

View File

@ -53,16 +53,18 @@
*/
package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.special.Gamma;
/**
* The default implementation of {@link GammaDistribution}
*
* @version $Revision: 1.13 $ $Date: 2003/11/15 18:59:10 $
* @version $Revision: 1.14 $ $Date: 2003/11/19 03:22:53 $
*/
public class GammaDistributionImpl extends AbstractContinuousDistribution
implements GammaDistribution {
implements GammaDistribution, Serializable {
/** The shape parameter. */
private double alpha;
@ -96,17 +98,13 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
* @param x the value at which the CDF is evaluated.
* @return CDF for this distribution.
*/
public double cummulativeProbability(double x) {
public double cummulativeProbability(double x) throws MathException{
double ret;
if (x <= 0.0) {
ret = 0.0;
} else {
try {
ret = Gamma.regularizedGammaP(getAlpha(), x / getBeta());
} catch(MathException ex){
ret = Double.NaN;
}
ret = Gamma.regularizedGammaP(getAlpha(), x / getBeta());
}
return ret;

View File

@ -54,15 +54,18 @@
package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.util.MathUtils;
/**
* The default implementation of {@link HypergeometricDistribution}.
*
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:36 $
* @version $Revision: 1.7 $ $Date: 2003/11/19 03:22:53 $
*/
public class HypergeometricDistributionImpl extends AbstractDiscreteDistribution
implements HypergeometricDistribution
implements HypergeometricDistribution, Serializable
{
/** The number of successes in the population. */
@ -95,7 +98,7 @@ public class HypergeometricDistributionImpl extends AbstractDiscreteDistribution
* @param x the value at which the PDF is evaluated.
* @return PDF for this distribution.
*/
public double cummulativeProbability(int x) {
public double cummulativeProbability(int x) throws MathException{
double ret;
int n = getPopulationSize();

View File

@ -53,6 +53,8 @@
*/
package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.special.Beta;
@ -60,15 +62,15 @@ import org.apache.commons.math.special.Beta;
* Default implementation of
* {@link org.apache.commons.math.distribution.TDistribution}.
*
* @version $Revision: 1.10 $ $Date: 2003/11/15 18:59:10 $
* @version $Revision: 1.11 $ $Date: 2003/11/19 03:22:53 $
*/
public class TDistributionImpl
extends AbstractContinuousDistribution
implements TDistribution {
implements TDistribution, Serializable {
/** The degrees of freedom*/
private double degreesOfFreedom;
/**
* Create a t distribution using the given degrees of freedom.
* @param degreesOfFreedom the degrees of freedom.
@ -84,8 +86,7 @@ public class TDistributionImpl
*/
public void setDegreesOfFreedom(double degreesOfFreedom) {
if (degreesOfFreedom <= 0.0) {
throw new IllegalArgumentException(
"degrees of freedom must be positive.");
throw new IllegalArgumentException("degrees of freedom must be positive.");
}
this.degreesOfFreedom = degreesOfFreedom;
}
@ -103,30 +104,26 @@ public class TDistributionImpl
* @param x the value at which the CDF is evaluated.
* @return CDF evaluted at <code>x</code>.
*/
public double cummulativeProbability(double x) {
public double cummulativeProbability(double x) throws MathException{
double ret;
if (x == 0.0) {
ret = 0.5;
} else {
double t;
try {
t = Beta.regularizedBeta(
double t =
Beta.regularizedBeta(
getDegreesOfFreedom() / (getDegreesOfFreedom() + (x * x)),
0.5 * getDegreesOfFreedom(),
0.5);
if (x < 0.0) {
ret = 0.5 * t;
} else {
ret = 1.0 - 0.5 * t;
}
} catch (MathException ex) {
ret = Double.NaN;
if (x < 0.0) {
ret = 0.5 * t;
} else {
ret = 1.0 - 0.5 * t;
}
}
return ret;
}
/**
* Access the domain value lower bound, based on <code>p</code>, used to
* bracket a CDF root. This method is used by

View File

@ -53,6 +53,9 @@
*/
package org.apache.commons.math.stat;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.DistributionFactory;
import org.apache.commons.math.distribution.TDistribution;
@ -84,37 +87,38 @@ import org.apache.commons.math.distribution.TDistribution;
* the necessary computations to return the requested statistic.</li>
* </ul>
*
* @version $Revision: 1.10 $ $Date: 2003/11/18 15:07:12 $
* @version $Revision: 1.11 $ $Date: 2003/11/19 03:22:54 $
*/
public class BivariateRegression {
public class BivariateRegression implements Serializable {
static final long serialVersionUID = -3004689053607543335L;
/** sum of x values */
private double sumX = 0d;
/** total variation in x (sum of squared deviations from xbar) */
private double sumXX = 0d;
/** sum of y values */
private double sumY = 0d;
/** total variation in y (sum of squared deviations from ybar) */
private double sumYY = 0d;
/** sum of products */
private double sumXY = 0d;
/** number of observations */
private long n = 0;
/** mean of accumulated x values, used in updating formulas */
private double xbar = 0;
/** mean of accumulated y values, used in updating formulas */
private double ybar = 0;
// ---------------------Public methods--------------------------------------
/**
* Adds the observation (x,y) to the regression data set.
* <p>
@ -144,8 +148,8 @@ public class BivariateRegression {
sumX += x;
sumY += y;
n++;
}
}
/**
* Adds the observations represented by the elements in
* <code>data</code>.
@ -160,11 +164,11 @@ public class BivariateRegression {
* @param data array of observations to be added
*/
public void addData(double[][] data) {
for (int i = 0; i < data.length; i++) {
for (int i = 0; i < data.length; i++) {
addData(data[i][0], data[i][1]);
}
}
}
/**
* Clears all data from the model.
*/
@ -176,7 +180,7 @@ public class BivariateRegression {
sumXY = 0d;
n = 0;
}
/**
* Returns the number of observations that have been added to the model.
*
@ -185,7 +189,7 @@ public class BivariateRegression {
public long getN() {
return n;
}
/**
* Returns the "predicted" <code>y</code> value associated with the
* supplied <code>x</code> value.
@ -206,7 +210,7 @@ public class BivariateRegression {
double b1 = getSlope();
return getIntercept(b1) + b1 * x;
}
/**
* Returns the intercept of the estimated regression line.
* <p>
@ -223,326 +227,327 @@ public class BivariateRegression {
*
* @return the intercept of the regression line
*/
public double getIntercept() {
return getIntercept(getSlope());
}
/**
* Returns the slope of the estimated regression line.
* <p>
* The least squares estimate of the slope is computed using the
* <a href="http://www.xycoon.com/estimation4.htm">normal equations</a>.
* The slope is sometimes denoted b1.
public double getIntercept() {
return getIntercept(getSlope());
}
/**
* Returns the slope of the estimated regression line.
* <p>
* The least squares estimate of the slope is computed using the
* <a href="http://www.xycoon.com/estimation4.htm">normal equations</a>.
* The slope is sometimes denoted b1.
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>At least two observations (with at least two different x values)
* must have been added before invoking this method. If this method is
* invoked before a model can be estimated, <code>Double.NaN</code> is
* returned.
* </li></ul>
*
* @return the slope of the regression line
*/
public double getSlope() {
if (n < 2) {
return Double.NaN; //not enough data
}
if (Math.abs(sumXX) < 10 * Double.MIN_VALUE) {
return Double.NaN; //not enough variation in x
}
return sumXY / sumXX;
}
/**
* Returns the <a href="http://www.xycoon.com/SumOfSquares.htm">
* sum of squared errors</a> (SSE) associated with the regression
* model.
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>At least two observations (with at least two different x values)
* must have been added before invoking this method. If this method is
* invoked before a model can be estimated, <code>Double.NaN</code> is
* invoked before a model can be estimated, <code>Double,NaN</code> is
* returned.
* </li></ul>
*
* @return the slope of the regression line
* @return sum of squared errors associated with the regression model
*/
public double getSlope() {
if (n < 2) {
return Double.NaN; //not enough data
}
if (Math.abs(sumXX) < 10 * Double.MIN_VALUE) {
return Double.NaN; //not enough variation in x
}
return sumXY / sumXX;
}
/**
* Returns the <a href="http://www.xycoon.com/SumOfSquares.htm">
* sum of squared errors</a> (SSE) associated with the regression
* model.
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>At least two observations (with at least two different x values)
* must have been added before invoking this method. If this method is
* invoked before a model can be estimated, <code>Double,NaN</code> is
* returned.
* </li></ul>
*
* @return sum of squared errors associated with the regression model
*/
public double getSumSquaredErrors() {
return getSumSquaredErrors(getSlope());
}
/**
* Returns the sum of squared deviations of the y values about their mean.
* <p>
* This is defined as SSTO
* <a href="http://www.xycoon.com/SumOfSquares.htm">here</a>.
* <p>
* If <code>n < 2</code>, this returns <code>Double.NaN</code>.
*
* @return sum of squared deviations of y values
*/
public double getTotalSumSquares() {
if (n < 2) {
return Double.NaN;
}
return sumYY;
}
/**
* Returns the sum of squared deviations of the predicted y values about
* their mean (which equals the mean of y).
* <p>
* This is usually abbreviated SSR or SSM. It is defined as SSM
* <a href="http://www.xycoon.com/SumOfSquares.htm">here</a>
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>At least two observations (with at least two different x values)
* must have been added before invoking this method. If this method is
* invoked before a model can be estimated, <code>Double,NaN</code> is
* returned.
* </li></ul>
*
* @return sum of squared deviations of predicted y values
*/
public double getRegressionSumSquares() {
return getRegressionSumSquares(getSlope());
}
/**
* Returns the sum of squared errors divided by the degrees of freedom,
* usually abbreviated MSE.
* <p>
* If there are fewer than <strong>three</strong> data pairs in the model,
* or if there is no variation in <code>x</code>, this returns
* <code>Double.NaN</code>.
*
* @return sum of squared deviations of y values
*/
public double getMeanSquareError() {
if (n < 3) {
return Double.NaN;
}
return getSumSquaredErrors() / (double) (n - 2);
}
/**
* Returns <a href="http://www.stt.msu.edu/~xiaoyimi/STT200/Lecture5.pdf">
* Pearson's product moment correlation coefficient</a>,
* usually denoted r.
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>At least two observations (with at least two different x values)
* must have been added before invoking this method. If this method is
* invoked before a model can be estimated, <code>Double,NaN</code> is
* returned.
* </li></ul>
*
* @return Pearson's r
*/
public double getR() {
double b1 = getSlope();
double result = Math.sqrt(getRSquare(b1));
if (b1 < 0) {
result = -result;
}
return result;
}
/**
* Returns the <a href="http://www.xycoon.com/coefficient1.htm">
* coefficient of determination</a>,
* usually denoted r-square.
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>At least two observations (with at least two different x values)
* must have been added before invoking this method. If this method is
* invoked before a model can be estimated, <code>Double,NaN</code> is
* returned.
* </li></ul>
*
* @return r-square
*/
public double getRSquare() {
return getRSquare(getSlope());
}
/**
* Returns the <a href="http://www.xycoon.com/standarderrorb0.htm">
* standard error of the intercept estimate</a>,
* usually denoted s(b0).
* <p>
* If there are fewer that <strong>three</strong> observations in the
* model, or if there is no variation in x, this returns
* <code>Double.NaN</code>.
*
* @return standard error associated with intercept estimate
*/
public double getInterceptStdErr() {
return Math.sqrt(getMeanSquareError() * ((1d / (double) n) +
(xbar * xbar) / sumXX));
}
/**
* Returns the <a href="http://www.xycoon.com/standerrorb(1).htm">standard
* error of the slope estimate</a>,
* usually denoted s(b1).
* <p>
* If there are fewer that <strong>three</strong> data pairs in the model,
* or if there is no variation in x, this returns <code>Double.NaN</code>.
*
* @return standard error associated with slope estimate
*/
public double getSlopeStdErr() {
return Math.sqrt(getMeanSquareError() / sumXX);
}
/**
* Returns the half-width of a 95% confidence interval for the slope
* estimate.
* <p>
* The 95% confidence interval is
* <p>
* <code>(getSlope() - getSlopeConfidenceInterval(),
* getSlope() + getSlopeConfidenceInterval())</code>
* <p>
* If there are fewer that <strong>three</strong> observations in the
* model, or if there is no variation in x, this returns
* <code>Double.NaN</code>.
* <p>
* <strong>Usage Note</strong>:<br>
* The validity of this statistic depends on the assumption that the
* observations included in the model are drawn from a
* <a href="http://mathworld.wolfram.com/
* BivariateNormalDistribution.html">Bivariate Normal Distribution</a>.
*
* @return half-width of 95% confidence interval for the slope estimate
*/
public double getSlopeConfidenceInterval() {
return getSlopeConfidenceInterval(0.05d);
}
/**
* Returns the half-width of a (100-100*alpha)% confidence interval for
* the slope estimate.
* <p>
* The (100-100*alpha)% confidence interval is
* <p>
* <code>(getSlope() - getSlopeConfidenceInterval(),
* getSlope() + getSlopeConfidenceInterval())</code>
* <p>
* To request, for example, a 99% confidence interval, use
* <code>alpha = .01</code>
* <p>
* <strong>Usage Note</strong>:<br>
* The validity of this statistic depends on the assumption that the
* observations included in the model are drawn from a
* <a href="http://mathworld.wolfram.com/
* BivariateNormalDistribution.html">Bivariate Normal Distribution</a>.
* <p>
* <strong> Preconditions:</strong><ul>
* <li>If there are fewer that <strong>three</strong> observations in the
* model, or if there is no variation in x, this returns
* <code>Double.NaN</code>.
* </li>
* <li><code>(0 < alpha < 1)</code>; otherwise an
* <code>IllegalArgumentException</code> is thrown.
* </li></ul>
*
* @param alpha the desired significance level
* @return half-width of 95% confidence interval for the slope estimate
*/
public double getSlopeConfidenceInterval(double alpha) {
if (alpha >= 1 || alpha <= 0) {
throw new IllegalArgumentException();
}
return getSlopeStdErr() *
getTDistribution().inverseCummulativeProbability(1d - alpha / 2d);
}
/**
* Returns the significance level of the slope (equiv) correlation.
* <p>
* Specifically, the returned value is the smallest <code>alpha</code>
* such that the slope confidence interval with significance level
* equal to <code>alpha</code> does not include <code>0</code>.
* On regression output, this is often denoted <code>Prob(|t| > 0)</code>
* <p>
* <strong>Usage Note</strong>:<br>
* The validity of this statistic depends on the assumption that the
* observations included in the model are drawn from a
* <a href="http://mathworld.wolfram.com/
* BivariateNormalDistribution.html">Bivariate Normal Distribution</a>.
* <p>
* If there are fewer that <strong>three</strong> observations in the
* model, or if there is no variation in x, this returns
* <code>Double.NaN</code>.
*
* @return significance level for slope/correlation
*/
public double getSignificance() {
return (1d - getTDistribution().cummulativeProbability(
Math.abs(getSlope()) / getSlopeStdErr()));
}
// ---------------------Private methods-----------------------------------
/**
* Returns the intercept of the estimated regression line, given the slope.
* <p>
* Will return <code>NaN</code> if slope is <code>NaN</code>.
*
* @param slope current slope
* @return the intercept of the regression line
*/
private double getIntercept(double slope) {
return (sumY - slope * sumX) / ((double) n);
}
/**
* Returns the sum of squared errors associated with the regression
* model, using the slope of the regression line.
* <p>
* Returns NaN if the slope is NaN.
*
* @param b1 current slope
* @return sum of squared errors associated with the regression model
*/
private double getSumSquaredErrors(double b1) {
return sumYY - sumXY * sumXY / sumXX;
}
/**
* Computes r-square from the slope.
* <p>
* will return NaN if slope is Nan.
*
* @param b1 current slope
* @return r-square
*/
private double getRSquare(double b1) {
double ssto = getTotalSumSquares();
return (ssto - getSumSquaredErrors(b1)) / ssto;
}
/**
* Computes SSR from b1.
*
* @param slope regression slope estimate
* @return sum of squared deviations of predicted y values
*/
private double getRegressionSumSquares(double slope) {
return slope * slope * sumXX;
}
/**
* Uses distribution framework to get a t distribution instance
* with df = n - 2
*
* @return t distribution with df = n - 2
*/
private TDistribution getTDistribution() {
return DistributionFactory.newInstance().createTDistribution(n - 2);
}
}
public double getSumSquaredErrors() {
return getSumSquaredErrors(getSlope());
}
/**
* Returns the sum of squared deviations of the y values about their mean.
* <p>
* This is defined as SSTO
* <a href="http://www.xycoon.com/SumOfSquares.htm">here</a>.
* <p>
* If <code>n < 2</code>, this returns <code>Double.NaN</code>.
*
* @return sum of squared deviations of y values
*/
public double getTotalSumSquares() {
if (n < 2) {
return Double.NaN;
}
return sumYY;
}
/**
* Returns the sum of squared deviations of the predicted y values about
* their mean (which equals the mean of y).
* <p>
* This is usually abbreviated SSR or SSM. It is defined as SSM
* <a href="http://www.xycoon.com/SumOfSquares.htm">here</a>
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>At least two observations (with at least two different x values)
* must have been added before invoking this method. If this method is
* invoked before a model can be estimated, <code>Double,NaN</code> is
* returned.
* </li></ul>
*
* @return sum of squared deviations of predicted y values
*/
public double getRegressionSumSquares() {
return getRegressionSumSquares(getSlope());
}
/**
* Returns the sum of squared errors divided by the degrees of freedom,
* usually abbreviated MSE.
* <p>
* If there are fewer than <strong>three</strong> data pairs in the model,
* or if there is no variation in <code>x</code>, this returns
* <code>Double.NaN</code>.
*
* @return sum of squared deviations of y values
*/
public double getMeanSquareError() {
if (n < 3) {
return Double.NaN;
}
return getSumSquaredErrors() / (double) (n - 2);
}
/**
* Returns <a href="http://www.stt.msu.edu/~xiaoyimi/STT200/Lecture5.pdf">
* Pearson's product moment correlation coefficient</a>,
* usually denoted r.
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>At least two observations (with at least two different x values)
* must have been added before invoking this method. If this method is
* invoked before a model can be estimated, <code>Double,NaN</code> is
* returned.
* </li></ul>
*
* @return Pearson's r
*/
public double getR() {
double b1 = getSlope();
double result = Math.sqrt(getRSquare(b1));
if (b1 < 0) {
result = -result;
}
return result;
}
/**
* Returns the <a href="http://www.xycoon.com/coefficient1.htm">
* coefficient of determination</a>,
* usually denoted r-square.
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>At least two observations (with at least two different x values)
* must have been added before invoking this method. If this method is
* invoked before a model can be estimated, <code>Double,NaN</code> is
* returned.
* </li></ul>
*
* @return r-square
*/
public double getRSquare() {
return getRSquare(getSlope());
}
/**
* Returns the <a href="http://www.xycoon.com/standarderrorb0.htm">
* standard error of the intercept estimate</a>,
* usually denoted s(b0).
* <p>
* If there are fewer that <strong>three</strong> observations in the
* model, or if there is no variation in x, this returns
* <code>Double.NaN</code>.
*
* @return standard error associated with intercept estimate
*/
public double getInterceptStdErr() {
return Math.sqrt(
getMeanSquareError() * ((1d / (double) n) + (xbar * xbar) / sumXX));
}
/**
* Returns the <a href="http://www.xycoon.com/standerrorb(1).htm">standard
* error of the slope estimate</a>,
* usually denoted s(b1).
* <p>
* If there are fewer that <strong>three</strong> data pairs in the model,
* or if there is no variation in x, this returns <code>Double.NaN</code>.
*
* @return standard error associated with slope estimate
*/
public double getSlopeStdErr() {
return Math.sqrt(getMeanSquareError() / sumXX);
}
/**
* Returns the half-width of a 95% confidence interval for the slope
* estimate.
* <p>
* The 95% confidence interval is
* <p>
* <code>(getSlope() - getSlopeConfidenceInterval(),
* getSlope() + getSlopeConfidenceInterval())</code>
* <p>
* If there are fewer that <strong>three</strong> observations in the
* model, or if there is no variation in x, this returns
* <code>Double.NaN</code>.
* <p>
* <strong>Usage Note</strong>:<br>
* The validity of this statistic depends on the assumption that the
* observations included in the model are drawn from a
* <a href="http://mathworld.wolfram.com/
* BivariateNormalDistribution.html">Bivariate Normal Distribution</a>.
*
* @return half-width of 95% confidence interval for the slope estimate
*/
public double getSlopeConfidenceInterval() throws MathException {
return getSlopeConfidenceInterval(0.05d);
}
/**
* Returns the half-width of a (100-100*alpha)% confidence interval for
* the slope estimate.
* <p>
* The (100-100*alpha)% confidence interval is
* <p>
* <code>(getSlope() - getSlopeConfidenceInterval(),
* getSlope() + getSlopeConfidenceInterval())</code>
* <p>
* To request, for example, a 99% confidence interval, use
* <code>alpha = .01</code>
* <p>
* <strong>Usage Note</strong>:<br>
* The validity of this statistic depends on the assumption that the
* observations included in the model are drawn from a
* <a href="http://mathworld.wolfram.com/
* BivariateNormalDistribution.html">Bivariate Normal Distribution</a>.
* <p>
* <strong> Preconditions:</strong><ul>
* <li>If there are fewer that <strong>three</strong> observations in the
* model, or if there is no variation in x, this returns
* <code>Double.NaN</code>.
* </li>
* <li><code>(0 < alpha < 1)</code>; otherwise an
* <code>IllegalArgumentException</code> is thrown.
* </li></ul>
*
* @param alpha the desired significance level
* @return half-width of 95% confidence interval for the slope estimate
*/
public double getSlopeConfidenceInterval(double alpha)
throws MathException {
if (alpha >= 1 || alpha <= 0) {
throw new IllegalArgumentException();
}
return getSlopeStdErr()
* getTDistribution().inverseCummulativeProbability(1d - alpha / 2d);
}
/**
* Returns the significance level of the slope (equiv) correlation.
* <p>
* Specifically, the returned value is the smallest <code>alpha</code>
* such that the slope confidence interval with significance level
* equal to <code>alpha</code> does not include <code>0</code>.
* On regression output, this is often denoted <code>Prob(|t| > 0)</code>
* <p>
* <strong>Usage Note</strong>:<br>
* The validity of this statistic depends on the assumption that the
* observations included in the model are drawn from a
* <a href="http://mathworld.wolfram.com/
* BivariateNormalDistribution.html">Bivariate Normal Distribution</a>.
* <p>
* If there are fewer that <strong>three</strong> observations in the
* model, or if there is no variation in x, this returns
* <code>Double.NaN</code>.
*
* @return significance level for slope/correlation
*/
public double getSignificance() throws MathException {
return (
1d
- getTDistribution().cummulativeProbability(
Math.abs(getSlope()) / getSlopeStdErr()));
}
// ---------------------Private methods-----------------------------------
/**
* Returns the intercept of the estimated regression line, given the slope.
* <p>
* Will return <code>NaN</code> if slope is <code>NaN</code>.
*
* @param slope current slope
* @return the intercept of the regression line
*/
private double getIntercept(double slope) {
return (sumY - slope * sumX) / ((double) n);
}
/**
* Returns the sum of squared errors associated with the regression
* model, using the slope of the regression line.
* <p>
* Returns NaN if the slope is NaN.
*
* @param b1 current slope
* @return sum of squared errors associated with the regression model
*/
private double getSumSquaredErrors(double b1) {
return sumYY - sumXY * sumXY / sumXX;
}
/**
* Computes r-square from the slope.
* <p>
* will return NaN if slope is Nan.
*
* @param b1 current slope
* @return r-square
*/
private double getRSquare(double b1) {
double ssto = getTotalSumSquares();
return (ssto - getSumSquaredErrors(b1)) / ssto;
}
/**
* Computes SSR from b1.
*
* @param slope regression slope estimate
* @return sum of squared deviations of predicted y values
*/
private double getRegressionSumSquares(double slope) {
return slope * slope * sumXX;
}
/**
* Uses distribution framework to get a t distribution instance
* with df = n - 2
*
* @return t distribution with df = n - 2
*/
private TDistribution getTDistribution() {
return DistributionFactory.newInstance().createTDistribution(n - 2);
}
}

View File

@ -52,10 +52,13 @@
* <http://www.apache.org/>.
*/
package org.apache.commons.math.stat;
import org.apache.commons.math.MathException;
/**
* A collection of commonly used test statistics and statistical tests.
*
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:38 $
* @version $Revision: 1.10 $ $Date: 2003/11/19 03:22:54 $
*/
public interface TestStatistic {
@ -83,7 +86,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if preconditions are not met
*/
double chiSquare(double[] expected, double[] observed)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Returns the <i>observed significance level</i>, or <a href=
@ -114,7 +117,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if preconditions are not met
*/
double chiSquareTest(double[] expected, double[] observed)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/
@ -148,7 +151,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if preconditions are not met
*/
boolean chiSquareTest(double[] expected, double[] observed, double alpha)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/
@ -167,7 +170,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if input array length is less than 2
*/
double t(double mu, double[] observed)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section3
@ -187,7 +190,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
double t(double[] sample1, double[] sample2)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Returns the <i>observed significance level</i>, or <a href=
@ -220,7 +223,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
double tTest(double[] sample1, double[] sample2)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/
@ -267,7 +270,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the preconditions are not met
*/
boolean tTest(double[] sample1, double[] sample2, double alpha)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/
@ -306,7 +309,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
boolean tTest(double mu, double[] sample, double alpha)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Returns the <i>observed significance level</i>, or <a href=
@ -335,7 +338,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
double tTest(double mu, double[] sample)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/
@ -354,7 +357,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
double t(double mu, DescriptiveStatistics sampleStats)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section3
@ -375,7 +378,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
double t(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Returns the <i>observed significance level</i>, or <a href=
@ -409,7 +412,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
double tTest(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/
@ -459,7 +462,7 @@ public interface TestStatistic {
*/
boolean tTest(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2,
double alpha)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/
@ -498,7 +501,7 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
boolean tTest(double mu, DescriptiveStatistics sampleStats, double alpha)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
/**
* Returns the <i>observed significance level</i>, or <a href=
@ -528,6 +531,6 @@ public interface TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
double tTest(double mu, DescriptiveStatistics sampleStats)
throws IllegalArgumentException;
throws IllegalArgumentException, MathException;
}

View File

@ -54,6 +54,9 @@
package org.apache.commons.math.stat;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.DistributionFactory;
import org.apache.commons.math.distribution.TDistribution;
import org.apache.commons.math.distribution.ChiSquaredDistribution;
@ -61,16 +64,18 @@ import org.apache.commons.math.distribution.ChiSquaredDistribution;
/**
* Implements test statistics defined in the TestStatistic interface.
*
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:39 $
* @version $Revision: 1.10 $ $Date: 2003/11/19 03:22:54 $
*/
public class TestStatisticImpl implements TestStatistic {
public class TestStatisticImpl implements TestStatistic, Serializable {
static final long serialVersionUID = 3357444126133491679L;
/**
* Default constructor
*/
public TestStatisticImpl() {
}
/**
* @param observed array of observed frequency counts
* @param expected array of expected frequency counts
@ -83,37 +88,37 @@ public class TestStatisticImpl implements TestStatistic {
double sumSq = 0.0d;
double dev = 0.0d;
if ((expected.length < 2) || (expected.length != observed.length)) {
throw new IllegalArgumentException
("observed, expected array lengths incorrect");
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]);
sumSq += dev * dev / expected[i];
}
return sumSq;
}
/**
* @param observed array of observed frequency counts
* @param expected array of exptected frequency counts
* @return p-value
* @throws IllegalArgumentException if preconditions are not met
*/
public double chiSquareTest(double[] expected, double[] observed)
throws IllegalArgumentException {
ChiSquaredDistribution chiSquaredDistribution =
DistributionFactory.newInstance().createChiSquareDistribution
((double) expected.length - 1);
return 1 - chiSquaredDistribution.cummulativeProbability(
chiSquare(expected, observed));
public double chiSquareTest(double[] expected, double[] observed)
throws IllegalArgumentException, MathException {
ChiSquaredDistribution chiSquaredDistribution =
DistributionFactory.newInstance().createChiSquareDistribution(
(double) expected.length - 1);
return 1
- chiSquaredDistribution.cummulativeProbability(
chiSquare(expected, observed));
}
/**
* @param observed array of observed frequency counts
* @param expected array of exptected frequency counts
@ -122,12 +127,14 @@ public class TestStatisticImpl implements TestStatistic {
* 1 - alpha
* @throws IllegalArgumentException if preconditions are not met
*/
public boolean chiSquareTest(double[] expected, double[] observed,
double alpha)
throws IllegalArgumentException {
public boolean chiSquareTest(
double[] expected,
double[] observed,
double alpha)
throws IllegalArgumentException, MathException {
if ((alpha <= 0) || (alpha > 0.5)) {
throw new IllegalArgumentException
("bad significance level: " + alpha);
throw new IllegalArgumentException(
"bad significance level: " + alpha);
}
return (chiSquareTest(expected, observed) < alpha);
}
@ -138,16 +145,18 @@ public class TestStatisticImpl implements TestStatistic {
* @return t statistic
* @throws IllegalArgumentException if input array length is less than 5
*/
public double t(double mu, double[] observed)
throws IllegalArgumentException {
public double t(double mu, double[] observed)
throws IllegalArgumentException {
if ((observed == null) || (observed.length < 5)) {
throw new IllegalArgumentException
("insufficient data for t statistic");
throw new IllegalArgumentException("insufficient data for t statistic");
}
return t(StatUtils.mean(observed), mu, StatUtils.variance(observed),
return t(
StatUtils.mean(observed),
mu,
StatUtils.variance(observed),
observed.length);
}
/**
* @param mu constant value to compare sample mean against
* @param sample array of sample data values
@ -156,32 +165,36 @@ public class TestStatisticImpl implements TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
public boolean tTest(double mu, double[] sample, double alpha)
throws IllegalArgumentException {
throws IllegalArgumentException, MathException {
if ((alpha <= 0) || (alpha > 0.5)) {
throw new IllegalArgumentException
("bad significance level: " + alpha);
}
throw new IllegalArgumentException(
"bad significance level: " + alpha);
}
return (tTest(mu, sample) < alpha);
}
/**
* @param sample1 array of sample data values
* @param sample2 array of sample data values
* @return t-statistic
* @throws IllegalArgumentException if the precondition is not met
*/
public double t(double[] sample1, double[] sample2)
public double t(double[] sample1, double[] sample2)
throws IllegalArgumentException {
if ((sample1 == null) || (sample2 == null ||
Math.min(sample1.length, sample2.length) < 5)) {
throw new IllegalArgumentException
("insufficient data for t statistic");
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), StatUtils.variance(sample2),
(double) sample1.length, (double) sample2.length);
return t(
StatUtils.mean(sample1),
StatUtils.mean(sample2),
StatUtils.variance(sample1),
StatUtils.variance(sample2),
(double) sample1.length,
(double) sample2.length);
}
/**
*
* @param sample1 array of sample data values
@ -190,17 +203,21 @@ public class TestStatisticImpl implements TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
public double tTest(double[] sample1, double[] sample2)
throws IllegalArgumentException {
if ((sample1 == null) || (sample2 == null ||
Math.min(sample1.length, sample2.length) < 5)) {
throw new IllegalArgumentException
("insufficient data");
throws IllegalArgumentException, MathException {
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), StatUtils.variance(sample2),
(double) sample1.length, (double) sample2.length);
return tTest(
StatUtils.mean(sample1),
StatUtils.mean(sample2),
StatUtils.variance(sample1),
StatUtils.variance(sample2),
(double) sample1.length,
(double) sample2.length);
}
/**
* @param sample1 array of sample data values
* @param sample2 array of sample data values
@ -210,82 +227,98 @@ public class TestStatisticImpl implements TestStatistic {
* @throws IllegalArgumentException if the preconditions are not met
*/
public boolean tTest(double[] sample1, double[] sample2, double alpha)
throws IllegalArgumentException {
if ((alpha <= 0) || (alpha > 0.5)) {
throw new IllegalArgumentException
("bad significance level: " + alpha);
}
return (tTest(sample1, sample2) < alpha);
throws IllegalArgumentException, MathException {
if ((alpha <= 0) || (alpha > 0.5)) {
throw new IllegalArgumentException(
"bad significance level: " + alpha);
}
return (tTest(sample1, sample2) < alpha);
}
/**
* @param mu constant value to compare sample mean against
* @param sample array of sample data values
* @return p-value
* @throws IllegalArgumentException if the precondition is not met
*/
public double tTest(double mu, double[] sample)
throws IllegalArgumentException {
public double tTest(double mu, double[] sample)
throws IllegalArgumentException, MathException {
if ((sample == null) || (sample.length < 5)) {
throw new IllegalArgumentException
("insufficient data for t statistic");
throw new IllegalArgumentException("insufficient data for t statistic");
}
return tTest(StatUtils.mean(sample), mu, StatUtils.variance(sample),
return tTest(
StatUtils.mean(sample),
mu,
StatUtils.variance(sample),
sample.length);
}
/**
* @param mu comparison constant
* @param sampleStats DescriptiveStatistics holding sample summary statitstics
* @return t statistic
* @throws IllegalArgumentException if the precondition is not met
*/
public double t(double mu, DescriptiveStatistics sampleStats)
public double t(double mu, DescriptiveStatistics sampleStats)
throws IllegalArgumentException {
if ((sampleStats == null) || (sampleStats.getN() < 5)) {
throw new IllegalArgumentException
("insufficient data for t statistic");
throw new IllegalArgumentException("insufficient data for t statistic");
}
return t(sampleStats.getMean(), mu, sampleStats.getVariance(),
return t(
sampleStats.getMean(),
mu,
sampleStats.getVariance(),
sampleStats.getN());
}
/**
* @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(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
public double t(
DescriptiveStatistics sampleStats1,
DescriptiveStatistics sampleStats2)
throws IllegalArgumentException {
if ((sampleStats1 == null) || (sampleStats2 == null ||
Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) {
throw new IllegalArgumentException
("insufficient data for t statistic");
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(), sampleStats2.getVariance(),
(double) sampleStats1.getN(), (double) sampleStats2.getN());
return t(
sampleStats1.getMean(),
sampleStats2.getMean(),
sampleStats1.getVariance(),
sampleStats2.getVariance(),
(double) sampleStats1.getN(),
(double) sampleStats2.getN());
}
/**
* @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(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2)
throws IllegalArgumentException {
if ((sampleStats1 == null) || (sampleStats2 == null ||
Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) {
throw new IllegalArgumentException
("insufficient data for t statistic");
public double tTest(
DescriptiveStatistics sampleStats1,
DescriptiveStatistics sampleStats2)
throws IllegalArgumentException, MathException {
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(), sampleStats2.getVariance(),
(double) sampleStats1.getN(), (double) sampleStats2.getN());
return tTest(
sampleStats1.getMean(),
sampleStats2.getMean(),
sampleStats1.getVariance(),
sampleStats2.getVariance(),
(double) sampleStats1.getN(),
(double) sampleStats2.getN());
}
/**
* @param sampleStats1 DescriptiveStatistics describing sample data values
* @param sampleStats2 DescriptiveStatistics describing sample data values
@ -294,15 +327,18 @@ public class TestStatisticImpl implements TestStatistic {
* confidence 1 - alpha
* @throws IllegalArgumentException if the preconditions are not met
*/
public boolean tTest(DescriptiveStatistics sampleStats1, DescriptiveStatistics sampleStats2,
double alpha) throws IllegalArgumentException {
public boolean tTest(
DescriptiveStatistics sampleStats1,
DescriptiveStatistics sampleStats2,
double alpha)
throws IllegalArgumentException, MathException {
if ((alpha <= 0) || (alpha > 0.5)) {
throw new IllegalArgumentException
("bad significance level: " + alpha);
throw new IllegalArgumentException(
"bad significance level: " + alpha);
}
return (tTest(sampleStats1, sampleStats2) < alpha);
}
/**
* @param mu constant value to compare sample mean against
* @param sampleStats DescriptiveStatistics describing sample data values
@ -310,15 +346,18 @@ public class TestStatisticImpl implements TestStatistic {
* @return p-value
* @throws IllegalArgumentException if the precondition is not met
*/
public boolean tTest(double mu, DescriptiveStatistics sampleStats, double alpha)
throws IllegalArgumentException {
public boolean tTest(
double mu,
DescriptiveStatistics sampleStats,
double alpha)
throws IllegalArgumentException, MathException {
if ((alpha <= 0) || (alpha > 0.5)) {
throw new IllegalArgumentException
("bad significance level: " + alpha);
}
throw new IllegalArgumentException(
"bad significance level: " + alpha);
}
return (tTest(mu, sampleStats) < alpha);
}
/**
* @param mu constant value to compare sample mean against
* @param sampleStats DescriptiveStatistics describing sample data
@ -326,17 +365,19 @@ public class TestStatisticImpl implements TestStatistic {
* @throws IllegalArgumentException if the precondition is not met
*/
public double tTest(double mu, DescriptiveStatistics sampleStats)
throws IllegalArgumentException {
throws IllegalArgumentException, MathException {
if ((sampleStats == null) || (sampleStats.getN() < 5)) {
throw new IllegalArgumentException
("insufficient data for t statistic");
throw new IllegalArgumentException("insufficient data for t statistic");
}
return tTest(sampleStats.getMean(), mu, sampleStats.getVariance(),
return tTest(
sampleStats.getMean(),
mu,
sampleStats.getVariance(),
sampleStats.getN());
}
//----------------------------------------------- Private methods
/**
* Computes approximate degrees of freedom for 2-sample t-test.
*
@ -347,27 +388,32 @@ public class TestStatisticImpl implements TestStatistic {
* @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)));
}
/**
* Computes t test statistic for 2-sample t-test.
*
* @param m1 first sample mean
* @param m2 second sample mean
* @param v1 first sample variance
* @param v2 second sample variance
* @param n1 first sample n
* @param n2 second sample n
* @return t test statistic
*/
private double t(double m1, double m2, double v1, double v2, double n1,
/**
* Computes t test statistic for 2-sample t-test.
*
* @param m1 first sample mean
* @param m2 second sample mean
* @param v1 first sample variance
* @param v2 second sample variance
* @param n1 first sample n
* @param n2 second sample n
* @return t test statistic
*/
private double t(
double m1,
double m2,
double v1,
double v2,
double n1,
double n2) {
return (m1 - m2) / Math.sqrt((v1 / n1) + (v2 / n2));
}
/**
* Computes t test statistic for 1-sample t-test.
*
@ -380,7 +426,7 @@ public class TestStatisticImpl implements TestStatistic {
private double t(double m, double mu, double v, double n) {
return (m - mu) / Math.sqrt(v / n);
}
/**
* Computes p-value for 2-sided, 2-sample t-test.
*
@ -392,15 +438,21 @@ public class TestStatisticImpl implements TestStatistic {
* @param n2 second sample n
* @return p-value
*/
private double tTest(double m1, double m2, double v1, double v2, double n1,
double n2) {
private double tTest(
double m1,
double m2,
double v1,
double v2,
double n1,
double n2)
throws MathException {
double t = Math.abs(t(m1, m2, v1, v2, n1, n2));
TDistribution tDistribution =
DistributionFactory.newInstance().createTDistribution
(df(v1, v2, n1, n2));
return 1.0 - tDistribution.cummulativeProbability(-t, t);
TDistribution tDistribution =
DistributionFactory.newInstance().createTDistribution(
df(v1, v2, n1, n2));
return 1.0 - tDistribution.cummulativeProbability(-t, t);
}
/**
* Computes p-value for 2-sided, 1-sample t-test.
*
@ -410,11 +462,11 @@ public class TestStatisticImpl implements TestStatistic {
* @param n sample n
* @return p-value
*/
private double tTest(double m, double mu, double v, double n) {
double t = Math.abs(t(m, mu, v, n));
TDistribution tDistribution =
DistributionFactory.newInstance().createTDistribution
(n - 1);
private double tTest(double m, double mu, double v, double n)
throws MathException {
double t = Math.abs(t(m, mu, v, n));
TDistribution tDistribution =
DistributionFactory.newInstance().createTDistribution(n - 1);
return 1.0 - tDistribution.cummulativeProbability(-t, t);
}
}
}

View File

@ -53,14 +53,16 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:39 $
* @version $Revision: 1.9 $ $Date: 2003/11/19 03:22:54 $
*/
public class BinomialDistributionTest extends TestCase {
private BinomialDistribution b;
/**
* Constructor for ChiSquareDistributionTest.
* @param name
@ -74,7 +76,10 @@ public class BinomialDistributionTest extends TestCase {
*/
protected void setUp() throws Exception {
super.setUp();
b = DistributionFactory.newInstance().createBinomialDistribution(10, 0.70);
b =
DistributionFactory.newInstance().createBinomialDistribution(
10,
0.70);
}
/*
@ -88,11 +93,11 @@ public class BinomialDistributionTest extends TestCase {
public void testInverseCummulativeProbability001() {
testValue(1, .001);
}
public void testInverseCumulativeProbability010() {
testValue(2, .010);
}
public void testInverseCumulativeProbability025() {
testValue(3, .025);
}
@ -100,7 +105,7 @@ public class BinomialDistributionTest extends TestCase {
public void testInverseCumulativeProbability050() {
testValue(4, .050);
}
public void testInverseCumulativeProbability100() {
testValue(4, .100);
}
@ -108,11 +113,11 @@ public class BinomialDistributionTest extends TestCase {
public void testInverseCummulativeProbability999() {
testValue(9, .999);
}
public void testInverseCumulativeProbability990() {
testValue(9, .990);
}
public void testInverseCumulativeProbability975() {
testValue(9, .975);
}
@ -120,7 +125,7 @@ public class BinomialDistributionTest extends TestCase {
public void testInverseCumulativeProbability950() {
testValue(8, .950);
}
public void testInverseCumulativeProbability900() {
testValue(8, .900);
}
@ -128,11 +133,11 @@ public class BinomialDistributionTest extends TestCase {
public void testCummulativeProbability1() {
testProbability(1, .00014);
}
public void testCumulativeProbability2() {
testProbability(2, .00159);
}
public void testCumulativeProbability3() {
testProbability(3, .01059);
}
@ -140,7 +145,7 @@ public class BinomialDistributionTest extends TestCase {
public void testCumulativeProbability4() {
testProbability(4, .04735);
}
public void testCumulativeProbability9() {
testProbability(9, .97175);
}
@ -148,16 +153,27 @@ public class BinomialDistributionTest extends TestCase {
public void testCummulativeProbability8() {
testProbability(8, .85069);
}
private void testProbability(int x, double expected){
double actual = b.cummulativeProbability(x);
assertEquals(expected, actual, 10e-4);
private void testProbability(int x, double expected) {
try {
double actual = b.cummulativeProbability(x);
assertEquals(expected, actual, 10e-4);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void testValue(int expected, double p){
int actual = b.inverseCummulativeProbability(p);
assertEquals(expected, actual);
assertTrue(b.cummulativeProbability(actual) <= p);
assertTrue(b.cummulativeProbability(actual + 1) >= p);
private void testValue(int expected, double p) {
try {
int actual = b.inverseCummulativeProbability(p);
assertEquals(expected, actual);
assertTrue(b.cummulativeProbability(actual) <= p);
assertTrue(b.cummulativeProbability(actual + 1) >= p);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -54,10 +54,12 @@
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:39 $
* @version $Revision: 1.10 $ $Date: 2003/11/19 03:22:54 $
*/
public class ChiSquareDistributionTest extends TestCase {
private ChiSquaredDistribution chiSquare;
@ -119,12 +121,22 @@ public class ChiSquareDistributionTest extends TestCase {
}
private void testProbability(double x, double expected){
double actual = chiSquare.cummulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
try {
double actual = chiSquare.cummulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
} catch (MathException e) {
e.printStackTrace();
}
}
private void testValue(double p, double expected){
double actual = chiSquare.inverseCummulativeProbability(p);
assertEquals("value for " + p, expected, actual, 10e-4);
try {
double actual = chiSquare.inverseCummulativeProbability(p);
assertEquals("value for " + p, expected, actual, 10e-4);
} catch (MathException e) {
e.printStackTrace();
}
}
}

View File

@ -53,16 +53,17 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import org.apache.commons.math.TestUtils;
import junit.framework.TestCase;
/**
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:39 $
* @version $Revision: 1.9 $ $Date: 2003/11/19 03:22:54 $
*/
public class ExponentialDistributionTest extends TestCase {
private ExponentialDistribution exp;
/**
* Constructor for ChiSquareDistributionTest.
* @param name
@ -76,7 +77,9 @@ public class ExponentialDistributionTest extends TestCase {
*/
protected void setUp() throws Exception {
super.setUp();
exp = DistributionFactory.newInstance().createExponentialDistribution(5.0);
exp =
DistributionFactory.newInstance().createExponentialDistribution(
5.0);
}
/*
@ -90,11 +93,11 @@ public class ExponentialDistributionTest extends TestCase {
public void testInverseCummulativeProbability001() {
testValue(.005003, .001);
}
public void testInverseCummulativeProbability010() {
testValue(0.050252, .010);
}
public void testInverseCummulativeProbability025() {
testValue(0.126589, .025);
}
@ -102,7 +105,7 @@ public class ExponentialDistributionTest extends TestCase {
public void testInverseCummulativeProbability050() {
testValue(0.256566, .050);
}
public void testInverseCummulativeProbability100() {
testValue(0.526803, .100);
}
@ -110,11 +113,11 @@ public class ExponentialDistributionTest extends TestCase {
public void testInverseCummulativeProbability999() {
testValue(34.5388, .999);
}
public void testInverseCummulativeProbability990() {
testValue(23.0259, .990);
}
public void testInverseCummulativeProbability975() {
testValue(18.4444, .975);
}
@ -122,7 +125,7 @@ public class ExponentialDistributionTest extends TestCase {
public void testInverseCummulativeProbability950() {
testValue(14.9787, .950);
}
public void testInverseCummulativeProbability900() {
testValue(11.5129, .900);
}
@ -130,11 +133,11 @@ public class ExponentialDistributionTest extends TestCase {
public void testCummulativeProbability001() {
testProbability(0.005003, .001);
}
public void testCummulativeProbability010() {
testProbability(0.050252, .010);
}
public void testCummulativeProbability025() {
testProbability(0.126589, .025);
}
@ -142,7 +145,7 @@ public class ExponentialDistributionTest extends TestCase {
public void testCummulativeProbability050() {
testProbability(0.256566, .050);
}
public void testCummulativeProbability100() {
testProbability(0.526803, .100);
}
@ -150,11 +153,11 @@ public class ExponentialDistributionTest extends TestCase {
public void testCummulativeProbability999() {
testProbability(34.5388, .999);
}
public void testCummulativeProbability990() {
testProbability(23.0259, .990);
}
public void testCummulativeProbability975() {
testProbability(18.4444, .975);
}
@ -162,7 +165,7 @@ public class ExponentialDistributionTest extends TestCase {
public void testCummulativeProbability950() {
testProbability(14.9787, .950);
}
public void testCummulativeProbability900() {
testProbability(11.5129, .900);
}
@ -190,19 +193,35 @@ public class ExponentialDistributionTest extends TestCase {
public void testInverseCummulativeProbabilityPositive() {
testValue(Double.NaN, 2.0);
}
public void testCummulativeProbability2() {
double actual = exp.cummulativeProbability(0.25, 0.75);
assertEquals(0.0905214, actual, 10e-4);
try {
double actual = exp.cummulativeProbability(0.25, 0.75);
assertEquals(0.0905214, actual, 10e-4);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void testProbability(double x, double expected){
double actual = exp.cummulativeProbability(x);
TestUtils.assertEquals(expected, actual, 10e-4);
private void testProbability(double x, double expected) {
try {
double actual = exp.cummulativeProbability(x);
TestUtils.assertEquals(expected, actual, 10e-4);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void testValue(double expected, double p){
double actual = exp.inverseCummulativeProbability(p);
TestUtils.assertEquals(expected, actual, 10e-4);
private void testValue(double expected, double p) {
try {
double actual = exp.inverseCummulativeProbability(p);
TestUtils.assertEquals(expected, actual, 10e-4);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -53,14 +53,16 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.7 $ $Date: 2003/11/15 16:01:40 $
* @version $Revision: 1.8 $ $Date: 2003/11/19 03:22:54 $
*/
public class FDistributionTest extends TestCase {
private FDistribution f;
/**
* Constructor for ChiSquareDistributionTest.
* @param name
@ -85,41 +87,51 @@ public class FDistributionTest extends TestCase {
super.tearDown();
}
public void testLowerTailProbability(){
public void testLowerTailProbability() {
testProbability(1.0 / 10.67, .010);
testProbability(1.0 / 6.98, .025);
testProbability(1.0 / 4.95, .050);
testProbability(1.0 / 3.40, .100);
testProbability(1.0 / 6.98, .025);
testProbability(1.0 / 4.95, .050);
testProbability(1.0 / 3.40, .100);
}
public void testUpperTailProbability(){
public void testUpperTailProbability() {
testProbability(8.75, .990);
testProbability(5.99, .975);
testProbability(4.39, .950);
testProbability(3.11, .900);
}
public void testLowerTailValues(){
public void testLowerTailValues() {
testValue(1.0 / 10.67, .010);
testValue(1.0 / 6.98, .025);
testValue(1.0 / 4.95, .050);
testValue(1.0 / 3.40, .100);
testValue(1.0 / 6.98, .025);
testValue(1.0 / 4.95, .050);
testValue(1.0 / 3.40, .100);
}
public void testUpperTailValues(){
public void testUpperTailValues() {
testValue(8.75, .990);
testValue(5.99, .975);
testValue(4.39, .950);
testValue(3.11, .900);
}
private void testProbability(double x, double expected){
double actual = f.cummulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 1e-3);
private void testProbability(double x, double expected) {
try {
double actual = f.cummulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 1e-3);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void testValue(double expected, double p){
double actual = f.inverseCummulativeProbability(p);
assertEquals("value for " + p, expected, actual, 1e-2);
private void testValue(double expected, double p) {
try {
double actual = f.inverseCummulativeProbability(p);
assertEquals("value for " + p, expected, actual, 1e-2);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -54,34 +54,58 @@
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.10 $ $Date: 2003/11/15 16:01:40 $
* @version $Revision: 1.11 $ $Date: 2003/11/19 03:22:54 $
*/
public class GammaDistributionTest extends TestCase {
public void testProbabilities(){
public void testProbabilities() {
testProbability(-1.000, 4.0, 2.0, .0000);
testProbability(15.501, 4.0, 2.0, .9499);
testProbability( 0.504, 4.0, 1.0, .0018);
testProbability(0.504, 4.0, 1.0, .0018);
testProbability(10.011, 1.0, 2.0, .9933);
testProbability( 5.000, 2.0, 2.0, .7127);
}
public void testValues(){
testValue(15.501, 4.0, 2.0, .9499);
testValue( 0.504, 4.0, 1.0, .0018);
testValue(10.011, 1.0, 2.0, .9933);
testValue( 5.000, 2.0, 2.0, .7127);
}
private void testProbability(double x, double a, double b, double expected){
double actual = DistributionFactory.newInstance().createGammaDistribution(a, b).cummulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
testProbability(5.000, 2.0, 2.0, .7127);
}
private void testValue(double expected, double a, double b, double p){
double actual = DistributionFactory.newInstance().createGammaDistribution(a, b).inverseCummulativeProbability(p);
assertEquals("critical value for " + p, expected, actual, 10e-4);
public void testValues() {
testValue(15.501, 4.0, 2.0, .9499);
testValue(0.504, 4.0, 1.0, .0018);
testValue(10.011, 1.0, 2.0, .9933);
testValue(5.000, 2.0, 2.0, .7127);
}
private void testProbability(
double x,
double a,
double b,
double expected) {
try {
double actual =
DistributionFactory
.newInstance()
.createGammaDistribution(a, b)
.cummulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void testValue(double expected, double a, double b, double p) {
try {
double actual =
DistributionFactory
.newInstance()
.createGammaDistribution(a, b)
.inverseCummulativeProbability(p);
assertEquals("critical value for " + p, expected, actual, 10e-4);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -54,14 +54,16 @@
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.6 $ $Date: 2003/11/15 16:01:40 $
* @version $Revision: 1.7 $ $Date: 2003/11/19 03:22:54 $
*/
public class HypergeometricDistributionTest extends TestCase {
private HypergeometricDistribution h;
/**
* Constructor for ChiSquareDistributionTest.
* @param name
@ -75,7 +77,11 @@ public class HypergeometricDistributionTest extends TestCase {
*/
protected void setUp() throws Exception {
super.setUp();
h = DistributionFactory.newInstance().createHypergeometricDistribution(10, 5, 5);
h =
DistributionFactory.newInstance().createHypergeometricDistribution(
10,
5,
5);
}
/*
@ -89,11 +95,11 @@ public class HypergeometricDistributionTest extends TestCase {
public void testInverseCummulativeProbability001() {
testValue(-1, .001);
}
public void testInverseCumulativeProbability010() {
testValue(0, .010);
}
public void testInverseCumulativeProbability025() {
testValue(0, .025);
}
@ -101,7 +107,7 @@ public class HypergeometricDistributionTest extends TestCase {
public void testInverseCumulativeProbability050() {
testValue(0, .050);
}
public void testInverseCumulativeProbability100() {
testValue(0, .100);
}
@ -109,11 +115,11 @@ public class HypergeometricDistributionTest extends TestCase {
public void testInverseCummulativeProbability999() {
testValue(4, .999);
}
public void testInverseCumulativeProbability990() {
testValue(3, .990);
}
public void testInverseCumulativeProbability975() {
testValue(3, .975);
}
@ -121,7 +127,7 @@ public class HypergeometricDistributionTest extends TestCase {
public void testInverseCumulativeProbability950() {
testValue(3, .950);
}
public void testInverseCumulativeProbability900() {
testValue(3, .900);
}
@ -133,11 +139,11 @@ public class HypergeometricDistributionTest extends TestCase {
public void testCummulativeProbability1() {
testProbability(1, .10318);
}
public void testCumulativeProbability2() {
testProbability(2, .50000);
}
public void testCumulativeProbability3() {
testProbability(3, .89683);
}
@ -145,20 +151,30 @@ public class HypergeometricDistributionTest extends TestCase {
public void testCumulativeProbability4() {
testProbability(4, .99603);
}
public void testCumulativeProbability5() {
testProbability(5, 1.00000);
}
private void testProbability(int x, double expected){
double actual = h.cummulativeProbability(x);
assertEquals(expected, actual, 10e-4);
private void testProbability(int x, double expected) {
try {
double actual = h.cummulativeProbability(x);
assertEquals(expected, actual, 10e-4);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void testValue(int expected, double p){
int actual = h.inverseCummulativeProbability(p);
assertEquals(expected, actual);
assertTrue(h.cummulativeProbability(actual) <= p);
assertTrue(h.cummulativeProbability(actual + 1) >= p);
private void testValue(int expected, double p) {
try {
int actual = h.inverseCummulativeProbability(p);
assertEquals(expected, actual);
assertTrue(h.cummulativeProbability(actual) <= p);
assertTrue(h.cummulativeProbability(actual + 1) >= p);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -53,14 +53,16 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:40 $
* @version $Revision: 1.9 $ $Date: 2003/11/19 03:22:54 $
*/
public class TDistributionTest extends TestCase {
private TDistribution t;
/**
* Constructor for ChiSquareDistributionTest.
* @param name
@ -88,11 +90,11 @@ public class TDistributionTest extends TestCase {
public void testInverseCummulativeProbability001() {
testValue(-5.893, .001);
}
public void testInverseCumulativeProbability010() {
testValue(-3.365, .010);
}
public void testInverseCumulativeProbability025() {
testValue(-2.571, .025);
}
@ -100,7 +102,7 @@ public class TDistributionTest extends TestCase {
public void testInverseCumulativeProbability050() {
testValue(-2.015, .050);
}
public void testInverseCumulativeProbability100() {
testValue(-1.476, .100);
}
@ -108,11 +110,11 @@ public class TDistributionTest extends TestCase {
public void testInverseCummulativeProbability999() {
testValue(5.893, .999);
}
public void testInverseCumulativeProbability990() {
testValue(3.365, .990);
}
public void testInverseCumulativeProbability975() {
testValue(2.571, .975);
}
@ -120,7 +122,7 @@ public class TDistributionTest extends TestCase {
public void testInverseCumulativeProbability950() {
testValue(2.015, .950);
}
public void testInverseCumulativeProbability900() {
testValue(1.476, .900);
}
@ -128,11 +130,11 @@ public class TDistributionTest extends TestCase {
public void testCummulativeProbability001() {
testProbability(-5.893, .001);
}
public void testCumulativeProbability010() {
testProbability(-3.365, .010);
}
public void testCumulativeProbability025() {
testProbability(-2.571, .025);
}
@ -140,7 +142,7 @@ public class TDistributionTest extends TestCase {
public void testCumulativeProbability050() {
testProbability(-2.015, .050);
}
public void testCumulativeProbability100() {
testProbability(-1.476, .100);
}
@ -148,11 +150,11 @@ public class TDistributionTest extends TestCase {
public void testCummulativeProbability999() {
testProbability(5.893, .999);
}
public void testCumulativeProbability990() {
testProbability(3.365, .990);
}
public void testCumulativeProbability975() {
testProbability(2.571, .975);
}
@ -160,18 +162,28 @@ public class TDistributionTest extends TestCase {
public void testCumulativeProbability950() {
testProbability(2.015, .950);
}
public void testCumulativeProbability900() {
testProbability(1.476, .900);
}
private void testProbability(double x, double expected){
double actual = t.cummulativeProbability(x);
assertEquals(expected, actual, 10e-4);
private void testProbability(double x, double expected) {
try {
double actual = t.cummulativeProbability(x);
assertEquals(expected, actual, 10e-4);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void testValue(double expected, double p){
double actual = t.inverseCummulativeProbability(p);
assertEquals(expected, actual, 10e-4);
private void testValue(double expected, double p) {
try {
double actual = t.inverseCummulativeProbability(p);
assertEquals(expected, actual, 10e-4);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -55,13 +55,15 @@ package org.apache.commons.math.stat;
import java.util.Random;
import org.apache.commons.math.MathException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the TestStatistic class.
*
* @version $Revision: 1.9 $ $Date: 2003/11/18 15:07:12 $
* @version $Revision: 1.10 $ $Date: 2003/11/19 03:22:54 $
*/
public final class BivariateRegressionTest extends TestCase {
@ -71,41 +73,97 @@ public final class BivariateRegressionTest extends TestCase {
* http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Norris.dat
* Strangely, order is {y,x}
*/
private double[][] data = {{0.1,0.2},{338.8,337.4},{118.1,118.2},
{888.0,884.6},{9.2,10.1},{228.1,226.5},{668.5,666.3},{998.5,996.3},
{449.1,448.6},{778.9,777.0},{559.2,558.2},{0.3,0.4},{0.1,0.6},
{778.1,775.5},{668.8,666.9},{339.3,338.0},{448.9,447.5},{10.8,11.6},
{557.7,556.0},{228.3,228.1},{998.0,995.8},{888.8,887.6},{119.6,120.2},
{0.3,0.3},{0.6,0.3},{557.6,556.8},{339.3,339.1},{888.0,887.2},
{998.5,999.0},{778.9,779.0},{10.2,11.1},{117.6,118.3},{228.9,229.2},
{668.4,669.1},{449.2,448.9},{0.2,0.5}};
private double[][] data = { { 0.1, 0.2 }, {
338.8, 337.4 }, {
118.1, 118.2 }, {
888.0, 884.6 }, {
9.2, 10.1 }, {
228.1, 226.5 }, {
668.5, 666.3 }, {
998.5, 996.3 }, {
449.1, 448.6 }, {
778.9, 777.0 }, {
559.2, 558.2 }, {
0.3, 0.4 }, {
0.1, 0.6 }, {
778.1, 775.5 }, {
668.8, 666.9 }, {
339.3, 338.0 }, {
448.9, 447.5 }, {
10.8, 11.6 }, {
557.7, 556.0 }, {
228.3, 228.1 }, {
998.0, 995.8 }, {
888.8, 887.6 }, {
119.6, 120.2 }, {
0.3, 0.3 }, {
0.6, 0.3 }, {
557.6, 556.8 }, {
339.3, 339.1 }, {
888.0, 887.2 }, {
998.5, 999.0 }, {
778.9, 779.0 }, {
10.2, 11.1 }, {
117.6, 118.3 }, {
228.9, 229.2 }, {
668.4, 669.1 }, {
449.2, 448.9 }, {
0.2, 0.5 }
};
/*
* Correlation example from
* http://www.xycoon.com/correlation.htm
*/
private double[][] corrData = {{101.0,99.2},{100.1,99.0},{100.0,100.0},
{90.6,111.6},{86.5,122.2},{89.7,117.6},{90.6,121.1},{82.8,136.0},
{70.1,154.2},{65.4,153.6},{61.3,158.5},{62.5,140.6},{63.6,136.2},
{52.6,168.0},{59.7,154.3},{59.5,149.0},{61.3,165.5}};
private double[][] corrData = { { 101.0, 99.2 }, {
100.1, 99.0 }, {
100.0, 100.0 }, {
90.6, 111.6 }, {
86.5, 122.2 }, {
89.7, 117.6 }, {
90.6, 121.1 }, {
82.8, 136.0 }, {
70.1, 154.2 }, {
65.4, 153.6 }, {
61.3, 158.5 }, {
62.5, 140.6 }, {
63.6, 136.2 }, {
52.6, 168.0 }, {
59.7, 154.3 }, {
59.5, 149.0 }, {
61.3, 165.5 }
};
/*
* From Moore and Mcabe, "Introduction to the Practice of Statistics"
* Example 10.3
*/
private double[][] infData = {{15.6,5.2},{26.8,6.1},{37.8,8.7},{36.4,8.5},
{35.5,8.8},{18.6,4.9},{15.3,4.5},{7.9,2.5},{0.0,1.1}};
private double[][] infData = { { 15.6, 5.2 }, {
26.8, 6.1 }, {
37.8, 8.7 }, {
36.4, 8.5 }, {
35.5, 8.8 }, {
18.6, 4.9 }, {
15.3, 4.5 }, {
7.9, 2.5 }, {
0.0, 1.1 }
};
/*
* From http://www.xycoon.com/simple_linear_regression.htm
*/
private double[][] infData2 = {{1,3},{2,5},{3,7},{4,14},{5,11}};
private double[][] infData2 = { { 1, 3 }, {
2, 5 }, {
3, 7 }, {
4, 14 }, {
5, 11 }
};
public BivariateRegressionTest(String name) {
super(name);
}
public void setUp() {
public void setUp() {
}
public static Test suite() {
@ -113,175 +171,258 @@ public final class BivariateRegressionTest extends TestCase {
suite.setName("BivariateRegression Tests");
return suite;
}
public void testNorris() {
BivariateRegression regression = new BivariateRegression();
for (int i = 0; i < data.length; i++) {
regression.addData(data[i][1],data[i][0]);
}
assertEquals("slope",1.00211681802045,
regression.getSlope(),10E-12);
assertEquals("slope std err",0.429796848199937E-03,
regression.getSlopeStdErr(),10E-12);
assertEquals("number of observations",36,regression.getN());
assertEquals("intercept", -0.262323073774029,
regression.getIntercept(),10E-12);
assertEquals("std err intercept", 0.232818234301152,
regression.getInterceptStdErr(),10E-12);
assertEquals("r-square",0.999993745883712,
regression.getRSquare(),10E-12);
assertEquals("SSR",4255954.13232369,
regression.getRegressionSumSquares(),10E-9);
assertEquals("MSE",0.782864662630069,
regression.getMeanSquareError(),10E-10);
assertEquals("SSE",26.6173985294224,
regression.getSumSquaredErrors(),10E-9);
assertEquals("predict(0)",-0.262323073774029,
regression.predict(0),10E-12);
assertEquals("predict(1)",1.00211681802045-0.262323073774029,
regression.predict(1),10E-12);
}
public void testCorr() {
BivariateRegression regression = new BivariateRegression();
regression.addData(corrData);
assertEquals("number of observations",17,regression.getN());
assertEquals("r-square",.896123,
regression.getRSquare(),10E-6);
assertEquals("r",-.946638,
regression.getR(),10E-6);
}
public void testNaNs() {
BivariateRegression regression = new BivariateRegression();
assertTrue("intercept not NaN",Double.isNaN(regression.getIntercept()));
assertTrue("slope not NaN",Double.isNaN(regression.getSlope()));
assertTrue("slope std err not NaN",
for (int i = 0; i < data.length; i++) {
regression.addData(data[i][1], data[i][0]);
}
assertEquals("slope", 1.00211681802045, regression.getSlope(), 10E-12);
assertEquals(
"slope std err",
0.429796848199937E-03,
regression.getSlopeStdErr(),
10E-12);
assertEquals("number of observations", 36, regression.getN());
assertEquals(
"intercept",
-0.262323073774029,
regression.getIntercept(),
10E-12);
assertEquals(
"std err intercept",
0.232818234301152,
regression.getInterceptStdErr(),
10E-12);
assertEquals(
"r-square",
0.999993745883712,
regression.getRSquare(),
10E-12);
assertEquals(
"SSR",
4255954.13232369,
regression.getRegressionSumSquares(),
10E-9);
assertEquals(
"MSE",
0.782864662630069,
regression.getMeanSquareError(),
10E-10);
assertEquals(
"SSE",
26.6173985294224,
regression.getSumSquaredErrors(),
10E-9);
assertEquals(
"predict(0)",
-0.262323073774029,
regression.predict(0),
10E-12);
assertEquals(
"predict(1)",
1.00211681802045 - 0.262323073774029,
regression.predict(1),
10E-12);
}
public void testCorr() {
BivariateRegression regression = new BivariateRegression();
regression.addData(corrData);
assertEquals("number of observations", 17, regression.getN());
assertEquals("r-square", .896123, regression.getRSquare(), 10E-6);
assertEquals("r", -.946638, regression.getR(), 10E-6);
}
public void testNaNs() {
BivariateRegression regression = new BivariateRegression();
assertTrue(
"intercept not NaN",
Double.isNaN(regression.getIntercept()));
assertTrue("slope not NaN", Double.isNaN(regression.getSlope()));
assertTrue(
"slope std err not NaN",
Double.isNaN(regression.getSlopeStdErr()));
assertTrue("intercept std err not NaN",
assertTrue(
"intercept std err not NaN",
Double.isNaN(regression.getInterceptStdErr()));
assertTrue("MSE not NaN",Double.isNaN(regression.getMeanSquareError()));
assertTrue("e not NaN",Double.isNaN(regression.getR()));
assertTrue("r-square not NaN",Double.isNaN(regression.getRSquare()));
assertTrue("RSS not NaN",
assertTrue(
"MSE not NaN",
Double.isNaN(regression.getMeanSquareError()));
assertTrue("e not NaN", Double.isNaN(regression.getR()));
assertTrue("r-square not NaN", Double.isNaN(regression.getRSquare()));
assertTrue(
"RSS not NaN",
Double.isNaN(regression.getRegressionSumSquares()));
assertTrue("SSE not NaN",Double.isNaN(regression.getSumSquaredErrors()));
assertTrue("SSTO not NaN",Double.isNaN(regression.getTotalSumSquares()));
assertTrue("predict not NaN",Double.isNaN(regression.predict(0)));
regression.addData(1,2);
regression.addData(1,3);
assertTrue(
"SSE not NaN",
Double.isNaN(regression.getSumSquaredErrors()));
assertTrue(
"SSTO not NaN",
Double.isNaN(regression.getTotalSumSquares()));
assertTrue("predict not NaN", Double.isNaN(regression.predict(0)));
regression.addData(1, 2);
regression.addData(1, 3);
// No x variation, so these should still blow...
assertTrue("intercept not NaN",Double.isNaN(regression.getIntercept()));
assertTrue("slope not NaN",Double.isNaN(regression.getSlope()));
assertTrue("slope std err not NaN",
assertTrue(
"intercept not NaN",
Double.isNaN(regression.getIntercept()));
assertTrue("slope not NaN", Double.isNaN(regression.getSlope()));
assertTrue(
"slope std err not NaN",
Double.isNaN(regression.getSlopeStdErr()));
assertTrue("intercept std err not NaN",
assertTrue(
"intercept std err not NaN",
Double.isNaN(regression.getInterceptStdErr()));
assertTrue("MSE not NaN",Double.isNaN(regression.getMeanSquareError()));
assertTrue("e not NaN",Double.isNaN(regression.getR()));
assertTrue("r-square not NaN",Double.isNaN(regression.getRSquare()));
assertTrue("RSS not NaN",
assertTrue(
"MSE not NaN",
Double.isNaN(regression.getMeanSquareError()));
assertTrue("e not NaN", Double.isNaN(regression.getR()));
assertTrue("r-square not NaN", Double.isNaN(regression.getRSquare()));
assertTrue(
"RSS not NaN",
Double.isNaN(regression.getRegressionSumSquares()));
assertTrue("SSE not NaN",Double.isNaN(regression.getSumSquaredErrors()));
assertTrue("predict not NaN",Double.isNaN(regression.predict(0)));
assertTrue(
"SSE not NaN",
Double.isNaN(regression.getSumSquaredErrors()));
assertTrue("predict not NaN", Double.isNaN(regression.predict(0)));
// but SSTO should be OK
assertTrue("SSTO NaN",!Double.isNaN(regression.getTotalSumSquares()));
assertTrue("SSTO NaN", !Double.isNaN(regression.getTotalSumSquares()));
regression = new BivariateRegression();
regression.addData(1,2);
regression.addData(3,3);
regression.addData(1, 2);
regression.addData(3, 3);
// All should be OK except MSE, s(b0), s(b1) which need one more df
assertTrue("interceptNaN",!Double.isNaN(regression.getIntercept()));
assertTrue("slope NaN",!Double.isNaN(regression.getSlope()));
assertTrue("slope std err not NaN",
assertTrue("interceptNaN", !Double.isNaN(regression.getIntercept()));
assertTrue("slope NaN", !Double.isNaN(regression.getSlope()));
assertTrue(
"slope std err not NaN",
Double.isNaN(regression.getSlopeStdErr()));
assertTrue("intercept std err not NaN",
assertTrue(
"intercept std err not NaN",
Double.isNaN(regression.getInterceptStdErr()));
assertTrue("MSE not NaN",Double.isNaN(regression.getMeanSquareError()));
assertTrue("r NaN",!Double.isNaN(regression.getR()));
assertTrue("r-square NaN",!Double.isNaN(regression.getRSquare()));
assertTrue("RSS NaN",
assertTrue(
"MSE not NaN",
Double.isNaN(regression.getMeanSquareError()));
assertTrue("r NaN", !Double.isNaN(regression.getR()));
assertTrue("r-square NaN", !Double.isNaN(regression.getRSquare()));
assertTrue(
"RSS NaN",
!Double.isNaN(regression.getRegressionSumSquares()));
assertTrue("SSE NaN",!Double.isNaN(regression.getSumSquaredErrors()));
assertTrue("SSTO NaN",!Double.isNaN(regression.getTotalSumSquares()));
assertTrue("predict NaN",!Double.isNaN(regression.predict(0)));
regression.addData(1,4);
assertTrue("SSE NaN", !Double.isNaN(regression.getSumSquaredErrors()));
assertTrue("SSTO NaN", !Double.isNaN(regression.getTotalSumSquares()));
assertTrue("predict NaN", !Double.isNaN(regression.predict(0)));
regression.addData(1, 4);
// MSE, MSE, s(b0), s(b1) should all be OK now
assertTrue("MSE NaN",!Double.isNaN(regression.getMeanSquareError()));
assertTrue("slope std err NaN",
assertTrue("MSE NaN", !Double.isNaN(regression.getMeanSquareError()));
assertTrue(
"slope std err NaN",
!Double.isNaN(regression.getSlopeStdErr()));
assertTrue("intercept std err NaN",
assertTrue(
"intercept std err NaN",
!Double.isNaN(regression.getInterceptStdErr()));
}
public void testClear() {
BivariateRegression regression = new BivariateRegression();
regression.addData(corrData);
assertEquals("number of observations",17,regression.getN());
regression.clear();
assertEquals("number of observations",0,regression.getN());
regression.addData(corrData);
assertEquals("r-square",.896123,regression.getRSquare(),10E-6);
regression.addData(data);
assertEquals("number of observations",53,regression.getN());
BivariateRegression regression = new BivariateRegression();
regression.addData(corrData);
assertEquals("number of observations", 17, regression.getN());
regression.clear();
assertEquals("number of observations", 0, regression.getN());
regression.addData(corrData);
assertEquals("r-square", .896123, regression.getRSquare(), 10E-6);
regression.addData(data);
assertEquals("number of observations", 53, regression.getN());
}
public void testInference() {
BivariateRegression regression = new BivariateRegression();
regression.addData(infData);
assertEquals("slope confidence interval", 0.0271,
regression.getSlopeConfidenceInterval(),0.0001);
assertEquals("slope std err",0.01146,
regression.getSlopeStdErr(),0.0001);
regression = new BivariateRegression();
regression.addData(infData2);
assertEquals("significance", 0.023331,
regression.getSignificance(),0.0001);
//FIXME: get a real example to test against with alpha = .01
assertTrue("tighter means wider",
regression.getSlopeConfidenceInterval() <
regression.getSlopeConfidenceInterval(0.01));
try {
double x = regression.getSlopeConfidenceInterval(1);
fail("expecting IllegalArgumentException for alpha = 1");
} catch (IllegalArgumentException ex) {
;
}
BivariateRegression regression = new BivariateRegression();
regression.addData(infData);
try {
assertEquals(
"slope confidence interval",
0.0271,
regression.getSlopeConfidenceInterval(),
0.0001);
assertEquals(
"slope std err",
0.01146,
regression.getSlopeStdErr(),
0.0001);
regression = new BivariateRegression();
regression.addData(infData2);
assertEquals(
"significance",
0.023331,
regression.getSignificance(),
0.0001);
//FIXME: get a real example to test against with alpha = .01
assertTrue(
"tighter means wider",
regression.getSlopeConfidenceInterval()
< regression.getSlopeConfidenceInterval(0.01));
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
double x = regression.getSlopeConfidenceInterval(1);
fail("expecting IllegalArgumentException for alpha = 1");
} catch (IllegalArgumentException ex) {
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void testPerfect() {
BivariateRegression regression = new BivariateRegression();
int n = 100;
for (int i = 0; i < n; i++) {
regression.addData(((double) i) / (n - 1), i);
}
assertEquals(0.0, regression.getSignificance(), 1.0e-5);
assertTrue(regression.getSlope() > 0.0);
try {
assertEquals(0.0, regression.getSignificance(), 1.0e-5);
assertTrue(regression.getSlope() > 0.0);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void testPerfectNegative() {
BivariateRegression regression = new BivariateRegression();
int n = 100;
for (int i = 0; i < n; i++) {
regression.addData(-((double) i) / (n - 1), i);
regression.addData(- ((double) i) / (n - 1), i);
}
try {
assertEquals(0.0, regression.getSignificance(), 1.0e-5);
assertTrue(regression.getSlope() < 0.0);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertEquals(0.0, regression.getSignificance(), 1.0e-5);
assertTrue(regression.getSlope() < 0.0);
}
public void testRandom() {
BivariateRegression regression = new BivariateRegression();
Random random = new Random(1);
@ -289,6 +430,14 @@ public final class BivariateRegressionTest extends TestCase {
for (int i = 0; i < n; i++) {
regression.addData(((double) i) / (n - 1), random.nextDouble());
}
assertTrue(0.0 < regression.getSignificance() && regression.getSignificance() < 1.0);
try {
assertTrue(
0.0 < regression.getSignificance()
&& regression.getSignificance() < 1.0);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -53,6 +53,7 @@
*/
package org.apache.commons.math.stat;
import org.apache.commons.math.MathException;
import junit.framework.Test;
import junit.framework.TestCase;
@ -60,19 +61,18 @@ import junit.framework.TestSuite;
/**
* Test cases for the TestStatistic class.
*
* @version $Revision: 1.8 $ $Date: 2003/11/15 16:01:41 $
* @version $Revision: 1.9 $ $Date: 2003/11/19 03:22:54 $
*/
public final class TestStatisticTest extends TestCase {
private TestStatisticImpl testStatistic = new TestStatisticImpl();
public TestStatisticTest(String name) {
super(name);
}
public void setUp() {
public void setUp() {
}
public static Test suite() {
@ -82,85 +82,137 @@ public final class TestStatisticTest extends TestCase {
}
public void testChiSquare() {
double[] observed = {11,24,69,96};
double[] expected = {8.2,25.2,65.8,100.8};
assertEquals("chi-square statistic",
1.39743495,testStatistic.chiSquare(expected,observed),10E-5);
double[] tooShortObs = {0};
double[] tooShortEx = {1};
try {
testStatistic.chiSquare(tooShortObs,tooShortEx);
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
try {
testStatistic.chiSquareTest(tooShortObs,tooShortEx);
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
double[] unMatchedObs = {0,1,2,3};
double[] unMatchedEx = {1,1,2};
try {
testStatistic.chiSquare(unMatchedEx,unMatchedObs);
fail("arrays have different lengths," +
" IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
expected[0] = 0;
try {
testStatistic.chiSquareTest(expected, observed, .01);
fail("bad expected count, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
/** from http://www.vsenvirginia.org/stat/classpractice/Voter_Preferences_CP.pdf */
double[] observed1 = {504, 523, 72, 70, 31};
double[] expected1 = {480, 540, 84, 60, 36};
assertEquals("chi-square test statistic", 5.81,
testStatistic.chiSquare(expected1,observed1),10E-2);
assertEquals("chi-square p-value", 0.21,
testStatistic.chiSquareTest(expected1, observed1),10E-2);
assertTrue("chi-square test reject",
testStatistic.chiSquareTest(expected1, observed1, 0.3));
assertTrue("chi-square test accept",
!testStatistic.chiSquareTest(expected1, observed1, 0.1));
try {
testStatistic.chiSquareTest(expected1, observed1, 95);
fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
double[] observed = { 11, 24, 69, 96 };
double[] expected = { 8.2, 25.2, 65.8, 100.8 };
assertEquals(
"chi-square statistic",
1.39743495,
testStatistic.chiSquare(expected, observed),
10E-5);
double[] tooShortObs = { 0 };
double[] tooShortEx = { 1 };
try {
testStatistic.chiSquare(tooShortObs, tooShortEx);
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
try {
testStatistic.chiSquareTest(tooShortObs, tooShortEx);
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double[] unMatchedObs = { 0, 1, 2, 3 };
double[] unMatchedEx = { 1, 1, 2 };
try {
testStatistic.chiSquare(unMatchedEx, unMatchedObs);
fail(
"arrays have different lengths,"
+ " IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
expected[0] = 0;
try {
testStatistic.chiSquareTest(expected, observed, .01);
fail("bad expected count, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/** from http://www.vsenvirginia.org/stat/classpractice/Voter_Preferences_CP.pdf */
double[] observed1 = { 504, 523, 72, 70, 31 };
double[] expected1 = { 480, 540, 84, 60, 36 };
try {
assertEquals(
"chi-square test statistic",
5.81,
testStatistic.chiSquare(expected1, observed1),
10E-2);
assertEquals(
"chi-square p-value",
0.21,
testStatistic.chiSquareTest(expected1, observed1),
10E-2);
assertTrue(
"chi-square test reject",
testStatistic.chiSquareTest(expected1, observed1, 0.3));
assertTrue(
"chi-square test accept",
!testStatistic.chiSquareTest(expected1, observed1, 0.1));
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.chiSquareTest(expected1, observed1, 95);
fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void testT(){
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};
public void testT() {
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;
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++) {
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]);
}
assertEquals("t statistic", -2.82, testStatistic.t(mu, observed),
assertEquals(
"t statistic",
-2.82,
testStatistic.t(mu, observed),
10E-3);
assertEquals("t statistic", -2.82, testStatistic.t(mu, sampleStats),
assertEquals(
"t statistic",
-2.82,
testStatistic.t(mu, sampleStats),
10E-3);
double[] nullObserved = null;
try {
testStatistic.t(mu, nullObserved);
@ -168,25 +220,28 @@ public final class TestStatisticTest extends TestCase {
} catch (IllegalArgumentException ex) {
;
}
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 {
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) {
;
}
double[] emptyObs = {};
double[] emptyObs = {
};
try {
testStatistic.t(mu, emptyObs);
fail("arguments too short, IllegalArgumentException expected");
@ -194,24 +249,26 @@ public final class TestStatisticTest extends TestCase {
;
}
DescriptiveStatistics emptyStats = null;
try {
emptyStats = DescriptiveStatistics.newInstance(StorelessDescriptiveStatisticsImpl.class);
} catch (InstantiationException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (IllegalAccessException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
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) {
;
}
double[] tooShortObs = {1.0};
double[] tooShortObs = { 1.0 };
try {
testStatistic.t(mu, tooShortObs);
fail("arguments too short, IllegalArgumentException expected");
@ -223,19 +280,24 @@ public final class TestStatisticTest extends TestCase {
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DescriptiveStatistics tooShortStats = null;
try {
tooShortStats = DescriptiveStatistics.newInstance(StorelessDescriptiveStatisticsImpl.class);
} catch (InstantiationException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
} catch (IllegalAccessException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
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);
@ -248,153 +310,251 @@ public final class TestStatisticTest extends TestCase {
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/** 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};
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++) {
/** 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 };
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,
testStatistic.t(0d,oneSidedP),0.01);
assertEquals("one sample t stat",3.86,
testStatistic.t(0d,oneSidedPStats),0.01);
assertEquals("one sample p value",0.00052,
testStatistic.tTest(0d,oneSidedP)/2d,10E-5);
assertEquals("one sample p value",0.00052,
testStatistic.tTest(0d,oneSidedPStats)/2d,10E-5);
assertTrue("one sample t-test reject",
testStatistic.tTest(0d,oneSidedP,0.01));
assertTrue("one sample t-test reject",
testStatistic.tTest(0d,oneSidedPStats,0.01));
assertTrue("one sample t-test accept",
!testStatistic.tTest(0d,oneSidedP,0.0001));
assertTrue("one sample t-test accept",
!testStatistic.tTest(0d,oneSidedPStats,0.0001));
try {
testStatistic.tTest(0d,oneSidedP, 95);
fail("alpha out of range, IllegalArgumentException expected");
assertEquals(
"one sample t stat",
3.86,
testStatistic.t(0d, oneSidedP),
0.01);
assertEquals(
"one sample t stat",
3.86,
testStatistic.t(0d, oneSidedPStats),
0.01);
assertEquals(
"one sample p value",
0.00052,
testStatistic.tTest(0d, oneSidedP) / 2d,
10E-5);
assertEquals(
"one sample p value",
0.00052,
testStatistic.tTest(0d, oneSidedPStats) / 2d,
10E-5);
assertTrue(
"one sample t-test reject",
testStatistic.tTest(0d, oneSidedP, 0.01));
assertTrue(
"one sample t-test reject",
testStatistic.tTest(0d, oneSidedPStats, 0.01));
assertTrue(
"one sample t-test accept",
!testStatistic.tTest(0d, oneSidedP, 0.0001));
assertTrue(
"one sample t-test accept",
!testStatistic.tTest(0d, oneSidedPStats, 0.0001));
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.tTest(0d, oneSidedP, 95);
fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.tTest(0d,oneSidedPStats, 95);
fail("alpha out of range, IllegalArgumentException expected");
testStatistic.tTest(0d, oneSidedPStats, 95);
fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
/** 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};
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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++) {
/** 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 };
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]);
}
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++) {
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
// should replace with R-verified example
assertEquals("two sample t stat",1.634,
testStatistic.t(sample1, sample2), 0.1);
assertEquals("two sample t stat",1.634,
testStatistic.t(sampleStats1, sampleStats2), 0.1);
// This test is OK, since book reports non-pooled exact p-value
assertEquals("two sample p value",0.059,
testStatistic.tTest(sample1, sample2)/2d, 10E-3);
assertEquals("two sample p value",0.059,
testStatistic.tTest(sampleStats1, sampleStats2)/2d, 10E-3);
assertTrue("two sample t-test reject",
testStatistic.tTest(sample1, sample2, 0.2));
assertTrue("two sample t-test reject",
testStatistic.tTest(sampleStats1, sampleStats2, 0.2));
assertTrue("two sample t-test accept",
!testStatistic.tTest(sample1, sample2,0.1));
assertTrue("two sample t-test accept",
!testStatistic.tTest(sampleStats1, sampleStats2,0.1));
try {
testStatistic.tTest(sample1, sample2, 95);
fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
//FIXME: textbook example reported t stat uses pooled variance
// should replace with R-verified example
assertEquals(
"two sample t stat",
1.634,
testStatistic.t(sample1, sample2),
0.1);
assertEquals(
"two sample t stat",
1.634,
testStatistic.t(sampleStats1, sampleStats2),
0.1);
// This test is OK, since book reports non-pooled exact p-value
assertEquals(
"two sample p value",
0.059,
testStatistic.tTest(sample1, sample2) / 2d,
10E-3);
assertEquals(
"two sample p value",
0.059,
testStatistic.tTest(sampleStats1, sampleStats2) / 2d,
10E-3);
assertTrue(
"two sample t-test reject",
testStatistic.tTest(sample1, sample2, 0.2));
assertTrue(
"two sample t-test reject",
testStatistic.tTest(sampleStats1, sampleStats2, 0.2));
assertTrue(
"two sample t-test accept",
!testStatistic.tTest(sample1, sample2, 0.1));
assertTrue(
"two sample t-test accept",
!testStatistic.tTest(sampleStats1, sampleStats2, 0.1));
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.tTest(sampleStats1, sampleStats2, 95);
fail("alpha out of range, IllegalArgumentException expected");
testStatistic.tTest(sample1, sample2, 95);
fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.tTest(sample1, tooShortObs, .01);
fail("insufficient data, IllegalArgumentException expected");
testStatistic.tTest(sampleStats1, sampleStats2, 95);
fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.tTest(sampleStats1, tooShortStats, .01);
fail("insufficient data, IllegalArgumentException expected");
testStatistic.tTest(sample1, tooShortObs, .01);
fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.tTest(sample1, tooShortObs);
fail("insufficient data, IllegalArgumentException expected");
testStatistic.tTest(sampleStats1, tooShortStats, .01);
fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.tTest(sampleStats1, tooShortStats);
fail("insufficient data, IllegalArgumentException expected");
testStatistic.tTest(sample1, tooShortObs);
fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.t(sample1, tooShortObs);
fail("insufficient data, IllegalArgumentException expected");
testStatistic.tTest(sampleStats1, tooShortStats);
fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
;
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testStatistic.t(sampleStats1, tooShortStats);
fail("insufficient data, IllegalArgumentException expected");
testStatistic.t(sample1, tooShortObs);
fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
;
}
try {
testStatistic.t(sampleStats1, tooShortStats);
fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
}
}