Merged TTest implementation and interface, removed use of MathException.

JIRA: MATH-488, MATH-739

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1243304 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2012-02-12 19:44:21 +00:00
parent 36a3920924
commit a436c93ce3
5 changed files with 653 additions and 1279 deletions

View File

@ -16,11 +16,20 @@
*/ */
package org.apache.commons.math.stat.inference; package org.apache.commons.math.stat.inference;
import org.apache.commons.math.MathException; import org.apache.commons.math.distribution.TDistribution;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MaxCountExceededException;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.stat.StatUtils;
import org.apache.commons.math.stat.descriptive.StatisticalSummary; import org.apache.commons.math.stat.descriptive.StatisticalSummary;
import org.apache.commons.math.util.FastMath;
/** /**
* An interface for Student's t-tests. * An implementation for Student's t-tests.
* <p> * <p>
* Tests can be:<ul> * Tests can be:<ul>
* <li>One-sample or two-sample</li> * <li>One-sample or two-sample</li>
@ -39,12 +48,13 @@ import org.apache.commons.math.stat.descriptive.StatisticalSummary;
* (e.g. tests at the 95% level use <code>alpha=0.05</code>).</p> * (e.g. tests at the 95% level use <code>alpha=0.05</code>).</p>
* <p> * <p>
* Input to tests can be either <code>double[]</code> arrays or * Input to tests can be either <code>double[]</code> arrays or
* {@link StatisticalSummary} instances.</p> * {@link StatisticalSummary} instances.</p><p>
* * Uses commons-math {@link org.apache.commons.math.distribution.TDistribution}
* implementation to estimate exact p-values.</p>
* *
* @version $Id$ * @version $Id$
*/ */
public interface TTest { public class TTest {
/** /**
* Computes a paired, 2-sample t-statistic based on the data in the input * Computes a paired, 2-sample t-statistic based on the data in the input
* arrays. The t-statistic returned is equivalent to what would be returned by * arrays. The t-statistic returned is equivalent to what would be returned by
@ -61,12 +71,24 @@ public interface TTest {
* @param sample1 array of sample data values * @param sample1 array of sample data values
* @param sample2 array of sample data values * @param sample2 array of sample data values
* @return t statistic * @return t statistic
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the arrays are <code>null</code>
* @throws MathException if the statistic can not be computed do to a * @throws NoDataException if the arrays are empty
* convergence or other numerical error. * @throws DimensionMismatchException if the length of the arrays is not equal
* @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
*/ */
double pairedT(double[] sample1, double[] sample2) public double pairedT(final double[] sample1, final double[] sample2)
throws IllegalArgumentException, MathException; throws NullArgumentException, NoDataException,
DimensionMismatchException, NumberIsTooSmallException {
checkSampleData(sample1);
checkSampleData(sample2);
double meanDifference = StatUtils.meanDifference(sample1, sample2);
return t(meanDifference, 0,
StatUtils.varianceDifference(sample1, sample2, meanDifference),
sample1.length);
}
/** /**
* Returns the <i>observed significance level</i>, or * Returns the <i>observed significance level</i>, or
* <i> p-value</i>, associated with a paired, two-sample, two-tailed t-test * <i> p-value</i>, associated with a paired, two-sample, two-tailed t-test
@ -97,11 +119,23 @@ public interface TTest {
* @param sample1 array of sample data values * @param sample1 array of sample data values
* @param sample2 array of sample data values * @param sample2 array of sample data values
* @return p-value for t-test * @return p-value for t-test
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the arrays are <code>null</code>
* @throws MathException if an error occurs computing the p-value * @throws NoDataException if the arrays are empty
* @throws DimensionMismatchException if the length of the arrays is not equal
* @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
double pairedTTest(double[] sample1, double[] sample2) public double pairedTTest(final double[] sample1, final double[] sample2)
throws IllegalArgumentException, MathException; throws NullArgumentException, NoDataException, DimensionMismatchException,
NumberIsTooSmallException, MaxCountExceededException {
double meanDifference = StatUtils.meanDifference(sample1, sample2);
return tTest(meanDifference, 0,
StatUtils.varianceDifference(sample1, sample2, meanDifference),
sample1.length);
}
/** /**
* Performs a paired t-test evaluating the null hypothesis that the * Performs a paired t-test evaluating the null hypothesis that the
* mean of the paired differences between <code>sample1</code> and * mean of the paired differences between <code>sample1</code> and
@ -123,7 +157,7 @@ public interface TTest {
* <li>The input array lengths must be the same and their common length * <li>The input array lengths must be the same and their common length
* must be at least 2. * must be at least 2.
* </li> * </li>
* <li> <code> 0 < alpha < 0.5 </code> * <li> <code> 0 &lt; alpha &lt; 0.5 </code>
* </li></ul></p> * </li></ul></p>
* *
* @param sample1 array of sample data values * @param sample1 array of sample data values
@ -131,14 +165,23 @@ public interface TTest {
* @param alpha significance level of the test * @param alpha significance level of the test
* @return true if the null hypothesis can be rejected with * @return true if the null hypothesis can be rejected with
* confidence 1 - alpha * confidence 1 - alpha
* @throws IllegalArgumentException if the preconditions are not met * @throws NullArgumentException if the arrays are <code>null</code>
* @throws MathException if an error occurs performing the test * @throws NoDataException if the arrays are empty
* @throws DimensionMismatchException if the length of the arrays is not equal
* @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
* @throws OutOfRangeException if <code>alpha</code> is not in the range (0, 0.5]
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
boolean pairedTTest( public boolean pairedTTest(final double[] sample1, final double[] sample2,
double[] sample1, final double alpha)
double[] sample2, throws NullArgumentException, NoDataException, DimensionMismatchException,
double alpha) NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException {
throws IllegalArgumentException, MathException;
checkSignificanceLevel(alpha);
return pairedTTest(sample1, sample2) < alpha;
}
/** /**
* Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm#formula"> * Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm#formula">
* t statistic </a> given observed values and a comparison constant. * t statistic </a> given observed values and a comparison constant.
@ -152,10 +195,18 @@ public interface TTest {
* @param mu comparison constant * @param mu comparison constant
* @param observed array of values * @param observed array of values
* @return t statistic * @return t statistic
* @throws IllegalArgumentException if input array length is less than 2 * @throws NullArgumentException if <code>observed</code> is <code>null</code>
* @throws NumberIsTooSmallException if the length of <code>observed</code> is &lt; 2
*/ */
double t(double mu, double[] observed) public double t(final double mu, final double[] observed)
throws IllegalArgumentException; throws NullArgumentException, NumberIsTooSmallException {
checkSampleData(observed);
return t(StatUtils.mean(observed), mu, StatUtils.variance(observed),
observed.length);
}
/** /**
* Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm#formula"> * Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm#formula">
* t statistic </a> to use in comparing the mean of the dataset described by * t statistic </a> to use in comparing the mean of the dataset described by
@ -164,16 +215,24 @@ public interface TTest {
* This statistic can be used to perform a one sample t-test for the mean. * This statistic can be used to perform a one sample t-test for the mean.
* </p><p> * </p><p>
* <strong>Preconditions</strong>: <ul> * <strong>Preconditions</strong>: <ul>
* <li><code>observed.getN() > = 2</code>. * <li><code>observed.getN() &ge; 2</code>.
* </li></ul></p> * </li></ul></p>
* *
* @param mu comparison constant * @param mu comparison constant
* @param sampleStats DescriptiveStatistics holding sample summary statitstics * @param sampleStats DescriptiveStatistics holding sample summary statitstics
* @return t statistic * @return t statistic
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if <code>sampleStats</code> is <code>null</code>
* @throws NumberIsTooSmallException if the number of samples is &lt; 2
*/ */
double t(double mu, StatisticalSummary sampleStats) public double t(final double mu, final StatisticalSummary sampleStats)
throws IllegalArgumentException; throws NullArgumentException, NumberIsTooSmallException {
checkSampleData(sampleStats);
return t(sampleStats.getMean(), mu, sampleStats.getVariance(),
sampleStats.getN());
}
/** /**
* Computes a 2-sample t statistic, under the hypothesis of equal * Computes a 2-sample t statistic, under the hypothesis of equal
* subpopulation variances. To compute a t-statistic without the * subpopulation variances. To compute a t-statistic without the
@ -182,7 +241,7 @@ public interface TTest {
* This statistic can be used to perform a (homoscedastic) two-sample * This statistic can be used to perform a (homoscedastic) two-sample
* t-test to compare sample means.</p> * t-test to compare sample means.</p>
* <p> * <p>
* The t-statisitc is</p> * The t-statistic is</p>
* <p> * <p>
* &nbsp;&nbsp;<code> t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))</code> * &nbsp;&nbsp;<code> t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))</code>
* </p><p> * </p><p>
@ -205,10 +264,20 @@ public interface TTest {
* @param sample1 array of sample data values * @param sample1 array of sample data values
* @param sample2 array of sample data values * @param sample2 array of sample data values
* @return t statistic * @return t statistic
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the arrays are <code>null</code>
* @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
*/ */
double homoscedasticT(double[] sample1, double[] sample2) public double homoscedasticT(final double[] sample1, final double[] sample2)
throws IllegalArgumentException; throws NullArgumentException, NumberIsTooSmallException {
checkSampleData(sample1);
checkSampleData(sample2);
return homoscedasticT(StatUtils.mean(sample1), StatUtils.mean(sample2),
StatUtils.variance(sample1), StatUtils.variance(sample2),
sample1.length, sample2.length);
}
/** /**
* Computes a 2-sample t statistic, without the hypothesis of equal * Computes a 2-sample t statistic, without the hypothesis of equal
* subpopulation variances. To compute a t-statistic assuming equal * subpopulation variances. To compute a t-statistic assuming equal
@ -217,7 +286,7 @@ public interface TTest {
* This statistic can be used to perform a two-sample t-test to compare * This statistic can be used to perform a two-sample t-test to compare
* sample means.</p> * sample means.</p>
* <p> * <p>
* The t-statisitc is</p> * The t-statistic is</p>
* <p> * <p>
* &nbsp;&nbsp; <code> t = (m1 - m2) / sqrt(var1/n1 + var2/n2)</code> * &nbsp;&nbsp; <code> t = (m1 - m2) / sqrt(var1/n1 + var2/n2)</code>
* </p><p> * </p><p>
@ -235,10 +304,20 @@ public interface TTest {
* @param sample1 array of sample data values * @param sample1 array of sample data values
* @param sample2 array of sample data values * @param sample2 array of sample data values
* @return t statistic * @return t statistic
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the arrays are <code>null</code>
* @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
*/ */
double t(double[] sample1, double[] sample2) public double t(final double[] sample1, final double[] sample2)
throws IllegalArgumentException; throws NullArgumentException, NumberIsTooSmallException {
checkSampleData(sample1);
checkSampleData(sample2);
return t(StatUtils.mean(sample1), StatUtils.mean(sample2),
StatUtils.variance(sample1), StatUtils.variance(sample2),
sample1.length, sample2.length);
}
/** /**
* Computes a 2-sample t statistic </a>, comparing the means of the datasets * Computes a 2-sample t statistic </a>, comparing the means of the datasets
* described by two {@link StatisticalSummary} instances, without the * described by two {@link StatisticalSummary} instances, without the
@ -249,7 +328,7 @@ public interface TTest {
* This statistic can be used to perform a two-sample t-test to compare * This statistic can be used to perform a two-sample t-test to compare
* sample means.</p> * sample means.</p>
* <p> * <p>
* The returned t-statisitc is</p> * The returned t-statistic is</p>
* <p> * <p>
* &nbsp;&nbsp; <code> t = (m1 - m2) / sqrt(var1/n1 + var2/n2)</code> * &nbsp;&nbsp; <code> t = (m1 - m2) / sqrt(var1/n1 + var2/n2)</code>
* </p><p> * </p><p>
@ -268,12 +347,21 @@ public interface TTest {
* @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats1 StatisticalSummary describing data from the first sample
* @param sampleStats2 StatisticalSummary describing data from the second sample * @param sampleStats2 StatisticalSummary describing data from the second sample
* @return t statistic * @return t statistic
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the sample statistics are <code>null</code>
* @throws NumberIsTooSmallException if the number of samples is &lt; 2
*/ */
double t( public double t(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2)
StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException {
throws IllegalArgumentException;
checkSampleData(sampleStats1);
checkSampleData(sampleStats2);
return t(sampleStats1.getMean(), sampleStats2.getMean(),
sampleStats1.getVariance(), sampleStats2.getVariance(),
sampleStats1.getN(), sampleStats2.getN());
}
/** /**
* Computes a 2-sample t statistic, comparing the means of the datasets * Computes a 2-sample t statistic, comparing the means of the datasets
* described by two {@link StatisticalSummary} instances, under the * described by two {@link StatisticalSummary} instances, under the
@ -284,7 +372,7 @@ public interface TTest {
* This statistic can be used to perform a (homoscedastic) two-sample * This statistic can be used to perform a (homoscedastic) two-sample
* t-test to compare sample means.</p> * t-test to compare sample means.</p>
* <p> * <p>
* The t-statisitc returned is</p> * The t-statistic returned is</p>
* <p> * <p>
* &nbsp;&nbsp;<code> t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))</code> * &nbsp;&nbsp;<code> t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))</code>
* </p><p> * </p><p>
@ -307,12 +395,21 @@ public interface TTest {
* @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats1 StatisticalSummary describing data from the first sample
* @param sampleStats2 StatisticalSummary describing data from the second sample * @param sampleStats2 StatisticalSummary describing data from the second sample
* @return t statistic * @return t statistic
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the sample statistics are <code>null</code>
* @throws NumberIsTooSmallException if the number of samples is &lt; 2
*/ */
double homoscedasticT( public double homoscedasticT(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2)
StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException {
throws IllegalArgumentException;
checkSampleData(sampleStats1);
checkSampleData(sampleStats2);
return homoscedasticT(sampleStats1.getMean(), sampleStats2.getMean(),
sampleStats1.getVariance(), sampleStats2.getVariance(),
sampleStats1.getN(), sampleStats2.getN());
}
/** /**
* Returns the <i>observed significance level</i>, or * Returns the <i>observed significance level</i>, or
* <i>p-value</i>, associated with a one-sample, two-tailed t-test * <i>p-value</i>, associated with a one-sample, two-tailed t-test
@ -336,11 +433,20 @@ public interface TTest {
* @param mu constant value to compare sample mean against * @param mu constant value to compare sample mean against
* @param sample array of sample data values * @param sample array of sample data values
* @return p-value * @return p-value
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the sample array is <code>null</code>
* @throws MathException if an error occurs computing the p-value * @throws NumberIsTooSmallException if the length of the array is &lt; 2
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
double tTest(double mu, double[] sample) public double tTest(final double mu, final double[] sample)
throws IllegalArgumentException, MathException; throws NullArgumentException, NumberIsTooSmallException,
MaxCountExceededException {
checkSampleData(sample);
return tTest(StatUtils.mean(sample), mu, StatUtils.variance(sample),
sample.length);
}
/** /**
* Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm"> * Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
* two-sided t-test</a> evaluating the null hypothesis that the mean of the population from * two-sided t-test</a> evaluating the null hypothesis that the mean of the population from
@ -373,11 +479,20 @@ public interface TTest {
* @param sample array of sample data values * @param sample array of sample data values
* @param alpha significance level of the test * @param alpha significance level of the test
* @return p-value * @return p-value
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the sample array is <code>null</code>
* @throws MathException if an error computing the p-value * @throws NumberIsTooSmallException if the length of the array is &lt; 2
* @throws OutOfRangeException if <code>alpha</code> is not in the range (0, 0.5]
* @throws MaxCountExceededException if an error computing the p-value
*/ */
boolean tTest(double mu, double[] sample, double alpha) public boolean tTest(final double mu, final double[] sample, final double alpha)
throws IllegalArgumentException, MathException; throws NullArgumentException, NumberIsTooSmallException,
OutOfRangeException, MaxCountExceededException {
checkSignificanceLevel(alpha);
return tTest(mu, sample) < alpha;
}
/** /**
* Returns the <i>observed significance level</i>, or * Returns the <i>observed significance level</i>, or
* <i>p-value</i>, associated with a one-sample, two-tailed t-test * <i>p-value</i>, associated with a one-sample, two-tailed t-test
@ -403,11 +518,20 @@ public interface TTest {
* @param mu constant value to compare sample mean against * @param mu constant value to compare sample mean against
* @param sampleStats StatisticalSummary describing sample data * @param sampleStats StatisticalSummary describing sample data
* @return p-value * @return p-value
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if <code>sampleStats</code> is <code>null</code>
* @throws MathException if an error occurs computing the p-value * @throws NumberIsTooSmallException if the number of samples is &lt; 2
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
double tTest(double mu, StatisticalSummary sampleStats) public double tTest(final double mu, final StatisticalSummary sampleStats)
throws IllegalArgumentException, MathException; throws NullArgumentException, NumberIsTooSmallException,
MaxCountExceededException {
checkSampleData(sampleStats);
return tTest(sampleStats.getMean(), mu, sampleStats.getVariance(),
sampleStats.getN());
}
/** /**
* Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm"> * Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
* two-sided t-test</a> evaluating the null hypothesis that the mean of the * two-sided t-test</a> evaluating the null hypothesis that the mean of the
@ -441,14 +565,21 @@ public interface TTest {
* @param sampleStats StatisticalSummary describing sample data values * @param sampleStats StatisticalSummary describing sample data values
* @param alpha significance level of the test * @param alpha significance level of the test
* @return p-value * @return p-value
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if <code>sampleStats</code> is <code>null</code>
* @throws MathException if an error occurs computing the p-value * @throws NumberIsTooSmallException if the number of samples is &lt; 2
* @throws OutOfRangeException if <code>alpha</code> is not in the range (0, 0.5]
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
boolean tTest( public boolean tTest(final double mu, final StatisticalSummary sampleStats,
double mu, final double alpha)
StatisticalSummary sampleStats, throws NullArgumentException, NumberIsTooSmallException,
double alpha) OutOfRangeException, MaxCountExceededException {
throws IllegalArgumentException, MathException;
checkSignificanceLevel(alpha);
return tTest(mu, sampleStats) < alpha;
}
/** /**
* Returns the <i>observed significance level</i>, or * Returns the <i>observed significance level</i>, or
* <i>p-value</i>, associated with a two-sample, two-tailed t-test * <i>p-value</i>, associated with a two-sample, two-tailed t-test
@ -482,11 +613,22 @@ public interface TTest {
* @param sample1 array of sample data values * @param sample1 array of sample data values
* @param sample2 array of sample data values * @param sample2 array of sample data values
* @return p-value for t-test * @return p-value for t-test
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the arrays are <code>null</code>
* @throws MathException if an error occurs computing the p-value * @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
double tTest(double[] sample1, double[] sample2) public double tTest(final double[] sample1, final double[] sample2)
throws IllegalArgumentException, MathException; throws NullArgumentException, NumberIsTooSmallException,
MaxCountExceededException {
checkSampleData(sample1);
checkSampleData(sample2);
return tTest(StatUtils.mean(sample1), StatUtils.mean(sample2),
StatUtils.variance(sample1), StatUtils.variance(sample2),
sample1.length, sample2.length);
}
/** /**
* Returns the <i>observed significance level</i>, or * Returns the <i>observed significance level</i>, or
* <i>p-value</i>, associated with a two-sample, two-tailed t-test * <i>p-value</i>, associated with a two-sample, two-tailed t-test
@ -517,13 +659,24 @@ public interface TTest {
* @param sample1 array of sample data values * @param sample1 array of sample data values
* @param sample2 array of sample data values * @param sample2 array of sample data values
* @return p-value for t-test * @return p-value for t-test
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the arrays are <code>null</code>
* @throws MathException if an error occurs computing the p-value * @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
double homoscedasticTTest( public double homoscedasticTTest(final double[] sample1, final double[] sample2)
double[] sample1, throws NullArgumentException, NumberIsTooSmallException,
double[] sample2) MaxCountExceededException {
throws IllegalArgumentException, MathException;
checkSampleData(sample1);
checkSampleData(sample2);
return homoscedasticTTest(StatUtils.mean(sample1),
StatUtils.mean(sample2),
StatUtils.variance(sample1),
StatUtils.variance(sample2),
sample1.length, sample2.length);
}
/** /**
* Performs a * Performs a
* <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm"> * <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
@ -571,14 +724,21 @@ public interface TTest {
* @param alpha significance level of the test * @param alpha significance level of the test
* @return true if the null hypothesis can be rejected with * @return true if the null hypothesis can be rejected with
* confidence 1 - alpha * confidence 1 - alpha
* @throws IllegalArgumentException if the preconditions are not met * @throws NullArgumentException if the arrays are <code>null</code>
* @throws MathException if an error occurs performing the test * @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
* @throws OutOfRangeException if <code>alpha</code> is not in the range (0, 0.5]
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
boolean tTest( public boolean tTest(final double[] sample1, final double[] sample2,
double[] sample1, final double alpha)
double[] sample2, throws NullArgumentException, NumberIsTooSmallException,
double alpha) OutOfRangeException, MaxCountExceededException {
throws IllegalArgumentException, MathException;
checkSignificanceLevel(alpha);
return tTest(sample1, sample2) < alpha;
}
/** /**
* Performs a * Performs a
* <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm"> * <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
@ -627,14 +787,21 @@ public interface TTest {
* @param alpha significance level of the test * @param alpha significance level of the test
* @return true if the null hypothesis can be rejected with * @return true if the null hypothesis can be rejected with
* confidence 1 - alpha * confidence 1 - alpha
* @throws IllegalArgumentException if the preconditions are not met * @throws NullArgumentException if the arrays are <code>null</code>
* @throws MathException if an error occurs performing the test * @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
* @throws OutOfRangeException if <code>alpha</code> is not in the range (0, 0.5]
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
boolean homoscedasticTTest( public boolean homoscedasticTTest(final double[] sample1, final double[] sample2,
double[] sample1, final double alpha)
double[] sample2, throws NullArgumentException, NumberIsTooSmallException,
double alpha) OutOfRangeException, MaxCountExceededException {
throws IllegalArgumentException, MathException;
checkSignificanceLevel(alpha);
return homoscedasticTTest(sample1, sample2) < alpha;
}
/** /**
* Returns the <i>observed significance level</i>, or * Returns the <i>observed significance level</i>, or
* <i>p-value</i>, associated with a two-sample, two-tailed t-test * <i>p-value</i>, associated with a two-sample, two-tailed t-test
@ -646,7 +813,7 @@ public interface TTest {
* equal in favor of the two-sided alternative that they are different. * equal in favor of the two-sided alternative that they are different.
* For a one-sided test, divide the returned value by 2.</p> * For a one-sided test, divide the returned value by 2.</p>
* <p> * <p>
* The test does not assume that the underlying popuation variances are * The test does not assume that the underlying population variances are
* equal and it uses approximated degrees of freedom computed from the * equal and it uses approximated degrees of freedom computed from the
* sample data to compute the p-value. To perform the test assuming * sample data to compute the p-value. To perform the test assuming
* equal variances, use * equal variances, use
@ -666,13 +833,23 @@ public interface TTest {
* @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats1 StatisticalSummary describing data from the first sample
* @param sampleStats2 StatisticalSummary describing data from the second sample * @param sampleStats2 StatisticalSummary describing data from the second sample
* @return p-value for t-test * @return p-value for t-test
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the sample statistics are <code>null</code>
* @throws MathException if an error occurs computing the p-value * @throws NumberIsTooSmallException if the number of samples is &lt; 2
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
double tTest( public double tTest(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2)
StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException,
throws IllegalArgumentException, MathException; MaxCountExceededException {
checkSampleData(sampleStats1);
checkSampleData(sampleStats2);
return tTest(sampleStats1.getMean(), sampleStats2.getMean(),
sampleStats1.getVariance(), sampleStats2.getVariance(),
sampleStats1.getN(), sampleStats2.getN());
}
/** /**
* Returns the <i>observed significance level</i>, or * Returns the <i>observed significance level</i>, or
* <i>p-value</i>, associated with a two-sample, two-tailed t-test * <i>p-value</i>, associated with a two-sample, two-tailed t-test
@ -703,13 +880,25 @@ public interface TTest {
* @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats1 StatisticalSummary describing data from the first sample
* @param sampleStats2 StatisticalSummary describing data from the second sample * @param sampleStats2 StatisticalSummary describing data from the second sample
* @return p-value for t-test * @return p-value for t-test
* @throws IllegalArgumentException if the precondition is not met * @throws NullArgumentException if the sample statistics are <code>null</code>
* @throws MathException if an error occurs computing the p-value * @throws NumberIsTooSmallException if the number of samples is &lt; 2
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
double homoscedasticTTest( public double homoscedasticTTest(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2)
StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException,
throws IllegalArgumentException, MathException; MaxCountExceededException {
checkSampleData(sampleStats1);
checkSampleData(sampleStats2);
return homoscedasticTTest(sampleStats1.getMean(),
sampleStats2.getMean(),
sampleStats1.getVariance(),
sampleStats2.getVariance(),
sampleStats1.getN(), sampleStats2.getN());
}
/** /**
* Performs a * Performs a
* <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm"> * <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
@ -760,12 +949,221 @@ public interface TTest {
* @param alpha significance level of the test * @param alpha significance level of the test
* @return true if the null hypothesis can be rejected with * @return true if the null hypothesis can be rejected with
* confidence 1 - alpha * confidence 1 - alpha
* @throws IllegalArgumentException if the preconditions are not met * @throws NullArgumentException if the sample statistics are <code>null</code>
* @throws MathException if an error occurs performing the test * @throws NumberIsTooSmallException if the number of samples is &lt; 2
* @throws OutOfRangeException if <code>alpha</code> is not in the range (0, 0.5]
* @throws MaxCountExceededException if an error occurs computing the p-value
*/ */
boolean tTest( public boolean tTest(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2,
StatisticalSummary sampleStats2, final double alpha)
double alpha) throws NullArgumentException, NumberIsTooSmallException,
throws IllegalArgumentException, MathException; OutOfRangeException, MaxCountExceededException {
checkSignificanceLevel(alpha);
return tTest(sampleStats1, sampleStats2) < alpha;
}
//----------------------------------------------- Protected methods
/**
* Computes approximate degrees of freedom for 2-sample t-test.
*
* @param v1 first sample variance
* @param v2 second sample variance
* @param n1 first sample n
* @param n2 second sample n
* @return approximate degrees of freedom
*/
protected 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)));
}
/**
* Computes t test statistic for 1-sample t-test.
*
* @param m sample mean
* @param mu constant to test against
* @param v sample variance
* @param n sample n
* @return t test statistic
*/
protected double t(final double m, final double mu,
final double v, final double n) {
return (m - mu) / FastMath.sqrt(v / n);
}
/**
* Computes t test statistic for 2-sample t-test.
* <p>
* Does not assume that subpopulation variances are equal.</p>
*
* @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
*/
protected double t(final double m1, final double m2,
final double v1, final double v2,
final double n1, final double n2) {
return (m1 - m2) / FastMath.sqrt((v1 / n1) + (v2 / n2));
}
/**
* Computes t test statistic for 2-sample t-test under the hypothesis
* of equal subpopulation variances.
*
* @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
*/
protected double homoscedasticT(final double m1, final double m2,
final double v1, final double v2,
final double n1, final double n2) {
final double pooledVariance = ((n1 - 1) * v1 + (n2 -1) * v2 ) / (n1 + n2 - 2);
return (m1 - m2) / FastMath.sqrt(pooledVariance * (1d / n1 + 1d / n2));
}
/**
* Computes p-value for 2-sided, 1-sample t-test.
*
* @param m sample mean
* @param mu constant to test against
* @param v sample variance
* @param n sample n
* @return p-value
* @throws MaxCountExceededException if an error occurs computing the p-value
*/
protected double tTest(final double m, final double mu,
final double v, final double n)
throws MaxCountExceededException {
double t = FastMath.abs(t(m, mu, v, n));
TDistribution distribution = new TDistribution(n - 1);
return 2.0 * distribution.cumulativeProbability(-t);
}
/**
* Computes p-value for 2-sided, 2-sample t-test.
* <p>
* Does not assume subpopulation variances are equal. Degrees of freedom
* are estimated from the data.</p>
*
* @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 p-value
* @throws MaxCountExceededException if an error occurs computing the p-value
*/
protected double tTest(final double m1, final double m2,
final double v1, final double v2,
final double n1, final double n2)
throws MaxCountExceededException {
final double t = FastMath.abs(t(m1, m2, v1, v2, n1, n2));
final double degreesOfFreedom = df(v1, v2, n1, n2);
TDistribution distribution = new TDistribution(degreesOfFreedom);
return 2.0 * distribution.cumulativeProbability(-t);
}
/**
* Computes p-value for 2-sided, 2-sample t-test, under the assumption
* of equal subpopulation variances.
* <p>
* The sum of the sample sizes minus 2 is used as degrees of freedom.</p>
*
* @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 p-value
* @throws MaxCountExceededException if an error occurs computing the p-value
*/
protected double homoscedasticTTest(double m1, double m2,
double v1, double v2,
double n1, double n2)
throws MaxCountExceededException {
final double t = FastMath.abs(homoscedasticT(m1, m2, v1, v2, n1, n2));
final double degreesOfFreedom = n1 + n2 - 2;
TDistribution distribution = new TDistribution(degreesOfFreedom);
return 2.0 * distribution.cumulativeProbability(-t);
}
/**
* Check significance level.
*
* @param alpha significance level
* @throws OutOfRangeException if the significance level is out of bounds.
*/
private void checkSignificanceLevel(final double alpha)
throws OutOfRangeException {
if (alpha <= 0 || alpha > 0.5) {
throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
alpha, 0.0, 0.5);
}
}
/**
* Check sample data.
*
* @param data Sample data.
* @throws NullArgumentException if {@code data} is {@code null}.
* @throws NumberIsTooSmallException if there is not enough sample data.
*/
private void checkSampleData(final double[] data)
throws NullArgumentException, NumberIsTooSmallException {
if (data == null) {
throw new NullArgumentException();
}
if (data.length < 2) {
throw new NumberIsTooSmallException(
LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC,
data.length, 2, true);
}
}
/**
* Check sample data.
*
* @param stat Statistical summary.
* @throws NullArgumentException if {@code data} is {@code null}.
* @throws NumberIsTooSmallException if there is not enough sample data.
*/
private void checkSampleData(final StatisticalSummary stat)
throws NullArgumentException, NumberIsTooSmallException {
if (stat == null) {
throw new NullArgumentException();
}
if (stat.getN() < 2) {
throw new NumberIsTooSmallException(
LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC,
stat.getN(), 2, true);
}
}
} }

View File

@ -17,13 +17,14 @@
package org.apache.commons.math.stat.inference; package org.apache.commons.math.stat.inference;
import java.util.Collection; import java.util.Collection;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.ConvergenceException; import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MaxCountExceededException; import org.apache.commons.math.exception.MaxCountExceededException;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.NotPositiveException; import org.apache.commons.math.exception.NotPositiveException;
import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.NullArgumentException; import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.OutOfRangeException; import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.ZeroException; import org.apache.commons.math.exception.ZeroException;
import org.apache.commons.math.stat.descriptive.StatisticalSummary; import org.apache.commons.math.stat.descriptive.StatisticalSummary;
@ -38,7 +39,7 @@ import org.apache.commons.math.stat.descriptive.StatisticalSummary;
public class TestUtils { public class TestUtils {
/** Singleton TTest instance. */ /** Singleton TTest instance. */
private static final TTest T_TEST = new TTestImpl(); private static final TTest T_TEST = new TTest();
/** Singleton ChiSquareTest instance. */ /** Singleton ChiSquareTest instance. */
private static final ChiSquareTest CHI_SQUARE_TEST = new ChiSquareTest(); private static final ChiSquareTest CHI_SQUARE_TEST = new ChiSquareTest();
@ -58,168 +59,182 @@ public class TestUtils {
/** /**
* @see org.apache.commons.math.stat.inference.TTest#homoscedasticT(double[], double[]) * @see org.apache.commons.math.stat.inference.TTest#homoscedasticT(double[], double[])
*/ */
public static double homoscedasticT(double[] sample1, double[] sample2) public static double homoscedasticT(final double[] sample1, final double[] sample2)
throws IllegalArgumentException { throws NullArgumentException, NumberIsTooSmallException {
return T_TEST.homoscedasticT(sample1, sample2); return T_TEST.homoscedasticT(sample1, sample2);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#homoscedasticT(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary) * @see org.apache.commons.math.stat.inference.TTest#homoscedasticT(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary)
*/ */
public static double homoscedasticT(StatisticalSummary sampleStats1, public static double homoscedasticT(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats2) final StatisticalSummary sampleStats2)
throws IllegalArgumentException { throws NullArgumentException, NumberIsTooSmallException {
return T_TEST.homoscedasticT(sampleStats1, sampleStats2); return T_TEST.homoscedasticT(sampleStats1, sampleStats2);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(double[], double[], double) * @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(double[], double[], double)
*/ */
public static boolean homoscedasticTTest(double[] sample1, double[] sample2, public static boolean homoscedasticTTest(final double[] sample1, final double[] sample2,
double alpha) final double alpha)
throws IllegalArgumentException, MathException { throws NullArgumentException, NumberIsTooSmallException,
OutOfRangeException, MaxCountExceededException {
return T_TEST.homoscedasticTTest(sample1, sample2, alpha); return T_TEST.homoscedasticTTest(sample1, sample2, alpha);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(double[], double[]) * @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(double[], double[])
*/ */
public static double homoscedasticTTest(double[] sample1, double[] sample2) public static double homoscedasticTTest(final double[] sample1, final double[] sample2)
throws IllegalArgumentException, MathException { throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException {
return T_TEST.homoscedasticTTest(sample1, sample2); return T_TEST.homoscedasticTTest(sample1, sample2);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary) * @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary)
*/ */
public static double homoscedasticTTest(StatisticalSummary sampleStats1, public static double homoscedasticTTest(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats2) final StatisticalSummary sampleStats2)
throws IllegalArgumentException, MathException { throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException {
return T_TEST.homoscedasticTTest(sampleStats1, sampleStats2); return T_TEST.homoscedasticTTest(sampleStats1, sampleStats2);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#pairedT(double[], double[]) * @see org.apache.commons.math.stat.inference.TTest#pairedT(double[], double[])
*/ */
public static double pairedT(double[] sample1, double[] sample2) public static double pairedT(final double[] sample1, final double[] sample2)
throws IllegalArgumentException, MathException { throws NullArgumentException, NoDataException,
DimensionMismatchException, NumberIsTooSmallException {
return T_TEST.pairedT(sample1, sample2); return T_TEST.pairedT(sample1, sample2);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#pairedTTest(double[], double[], double) * @see org.apache.commons.math.stat.inference.TTest#pairedTTest(double[], double[], double)
*/ */
public static boolean pairedTTest(double[] sample1, double[] sample2, public static boolean pairedTTest(final double[] sample1, final double[] sample2,
double alpha) final double alpha)
throws IllegalArgumentException, MathException { throws NullArgumentException, NoDataException, DimensionMismatchException,
NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException {
return T_TEST.pairedTTest(sample1, sample2, alpha); return T_TEST.pairedTTest(sample1, sample2, alpha);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#pairedTTest(double[], double[]) * @see org.apache.commons.math.stat.inference.TTest#pairedTTest(double[], double[])
*/ */
public static double pairedTTest(double[] sample1, double[] sample2) public static double pairedTTest(final double[] sample1, final double[] sample2)
throws IllegalArgumentException, MathException { throws NullArgumentException, NoDataException, DimensionMismatchException,
NumberIsTooSmallException, MaxCountExceededException {
return T_TEST.pairedTTest(sample1, sample2); return T_TEST.pairedTTest(sample1, sample2);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#t(double, double[]) * @see org.apache.commons.math.stat.inference.TTest#t(double, double[])
*/ */
public static double t(double mu, double[] observed) public static double t(final double mu, final double[] observed)
throws IllegalArgumentException { throws NullArgumentException, NumberIsTooSmallException {
return T_TEST.t(mu, observed); return T_TEST.t(mu, observed);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#t(double, org.apache.commons.math.stat.descriptive.StatisticalSummary) * @see org.apache.commons.math.stat.inference.TTest#t(double, org.apache.commons.math.stat.descriptive.StatisticalSummary)
*/ */
public static double t(double mu, StatisticalSummary sampleStats) public static double t(final double mu, final StatisticalSummary sampleStats)
throws IllegalArgumentException { throws NullArgumentException, NumberIsTooSmallException {
return T_TEST.t(mu, sampleStats); return T_TEST.t(mu, sampleStats);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#t(double[], double[]) * @see org.apache.commons.math.stat.inference.TTest#t(double[], double[])
*/ */
public static double t(double[] sample1, double[] sample2) public static double t(final double[] sample1, final double[] sample2)
throws IllegalArgumentException { throws NullArgumentException, NumberIsTooSmallException {
return T_TEST.t(sample1, sample2); return T_TEST.t(sample1, sample2);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#t(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary) * @see org.apache.commons.math.stat.inference.TTest#t(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary)
*/ */
public static double t(StatisticalSummary sampleStats1, public static double t(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats2) final StatisticalSummary sampleStats2)
throws IllegalArgumentException { throws NullArgumentException, NumberIsTooSmallException {
return T_TEST.t(sampleStats1, sampleStats2); return T_TEST.t(sampleStats1, sampleStats2);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#tTest(double, double[], double) * @see org.apache.commons.math.stat.inference.TTest#tTest(double, double[], double)
*/ */
public static boolean tTest(double mu, double[] sample, double alpha) public static boolean tTest(final double mu, final double[] sample, final double alpha)
throws IllegalArgumentException, MathException { throws NullArgumentException, NumberIsTooSmallException,
OutOfRangeException, MaxCountExceededException {
return T_TEST.tTest(mu, sample, alpha); return T_TEST.tTest(mu, sample, alpha);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#tTest(double, double[]) * @see org.apache.commons.math.stat.inference.TTest#tTest(double, double[])
*/ */
public static double tTest(double mu, double[] sample) public static double tTest(final double mu, final double[] sample)
throws IllegalArgumentException, MathException { throws NullArgumentException, NumberIsTooSmallException,
MaxCountExceededException {
return T_TEST.tTest(mu, sample); return T_TEST.tTest(mu, sample);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#tTest(double, org.apache.commons.math.stat.descriptive.StatisticalSummary, double) * @see org.apache.commons.math.stat.inference.TTest#tTest(double, org.apache.commons.math.stat.descriptive.StatisticalSummary, double)
*/ */
public static boolean tTest(double mu, StatisticalSummary sampleStats, public static boolean tTest(final double mu, final StatisticalSummary sampleStats,
double alpha) final double alpha)
throws IllegalArgumentException, MathException { throws NullArgumentException, NumberIsTooSmallException,
OutOfRangeException, MaxCountExceededException {
return T_TEST.tTest(mu, sampleStats, alpha); return T_TEST.tTest(mu, sampleStats, alpha);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#tTest(double, org.apache.commons.math.stat.descriptive.StatisticalSummary) * @see org.apache.commons.math.stat.inference.TTest#tTest(double, org.apache.commons.math.stat.descriptive.StatisticalSummary)
*/ */
public static double tTest(double mu, StatisticalSummary sampleStats) public static double tTest(final double mu, final StatisticalSummary sampleStats)
throws IllegalArgumentException, MathException { throws NullArgumentException, NumberIsTooSmallException,
MaxCountExceededException {
return T_TEST.tTest(mu, sampleStats); return T_TEST.tTest(mu, sampleStats);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#tTest(double[], double[], double) * @see org.apache.commons.math.stat.inference.TTest#tTest(double[], double[], double)
*/ */
public static boolean tTest(double[] sample1, double[] sample2, double alpha) public static boolean tTest(final double[] sample1, final double[] sample2,
throws IllegalArgumentException, MathException { final double alpha)
throws NullArgumentException, NumberIsTooSmallException,
OutOfRangeException, MaxCountExceededException {
return T_TEST.tTest(sample1, sample2, alpha); return T_TEST.tTest(sample1, sample2, alpha);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#tTest(double[], double[]) * @see org.apache.commons.math.stat.inference.TTest#tTest(double[], double[])
*/ */
public static double tTest(double[] sample1, double[] sample2) public static double tTest(final double[] sample1, final double[] sample2)
throws IllegalArgumentException, MathException { throws NullArgumentException, NumberIsTooSmallException,
MaxCountExceededException {
return T_TEST.tTest(sample1, sample2); return T_TEST.tTest(sample1, sample2);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#tTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary, double) * @see org.apache.commons.math.stat.inference.TTest#tTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary, double)
*/ */
public static boolean tTest(StatisticalSummary sampleStats1, public static boolean tTest(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats2, double alpha) final StatisticalSummary sampleStats2,
throws IllegalArgumentException, MathException { final double alpha)
throws NullArgumentException, NumberIsTooSmallException,
OutOfRangeException, MaxCountExceededException {
return T_TEST.tTest(sampleStats1, sampleStats2, alpha); return T_TEST.tTest(sampleStats1, sampleStats2, alpha);
} }
/** /**
* @see org.apache.commons.math.stat.inference.TTest#tTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary) * @see org.apache.commons.math.stat.inference.TTest#tTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary)
*/ */
public static double tTest(StatisticalSummary sampleStats1, public static double tTest(final StatisticalSummary sampleStats1,
StatisticalSummary sampleStats2) final StatisticalSummary sampleStats2)
throws IllegalArgumentException, MathException { throws NullArgumentException, NumberIsTooSmallException,
MaxCountExceededException {
return T_TEST.tTest(sampleStats1, sampleStats2); return T_TEST.tTest(sampleStats1, sampleStats2);
} }

View File

@ -17,7 +17,9 @@
package org.apache.commons.math.stat.inference; package org.apache.commons.math.stat.inference;
import org.apache.commons.math.exception.MathIllegalArgumentException; import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.stat.descriptive.SummaryStatistics; import org.apache.commons.math.stat.descriptive.SummaryStatistics;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -30,7 +32,7 @@ import org.junit.Test;
*/ */
public class TTestTest { public class TTestTest {
protected TTest testStatistic = new TTestImpl(); protected TTest testStatistic = new TTest();
private double[] tooShortObs = { 1.0 }; private double[] tooShortObs = { 1.0 };
private double[] emptyObs = {}; private double[] emptyObs = {};
@ -66,55 +68,55 @@ public class TTestTest {
try { try {
testStatistic.t(mu, (double[]) null); testStatistic.t(mu, (double[]) null);
Assert.fail("arguments too short, MathIllegalArgumentException expected"); Assert.fail("arguments too short, NullArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (NullArgumentException ex) {
// expected // expected
} }
try { try {
testStatistic.t(mu, (SummaryStatistics) null); testStatistic.t(mu, (SummaryStatistics) null);
Assert.fail("arguments too short, MathIllegalArgumentException expected"); Assert.fail("arguments too short, NullArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (NullArgumentException ex) {
// expected // expected
} }
try { try {
testStatistic.t(mu, emptyObs); testStatistic.t(mu, emptyObs);
Assert.fail("arguments too short, MathIllegalArgumentException expected"); Assert.fail("arguments too short, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.t(mu, emptyStats); testStatistic.t(mu, emptyStats);
Assert.fail("arguments too short, MathIllegalArgumentException expected"); Assert.fail("arguments too short, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.t(mu, tooShortObs); testStatistic.t(mu, tooShortObs);
Assert.fail("insufficient data to compute t statistic, MathIllegalArgumentException expected"); Assert.fail("insufficient data to compute t statistic, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.tTest(mu, tooShortObs); testStatistic.tTest(mu, tooShortObs);
Assert.fail("insufficient data to perform t test, MathIllegalArgumentException expected"); Assert.fail("insufficient data to perform t test, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.t(mu, tooShortStats); testStatistic.t(mu, tooShortStats);
Assert.fail("insufficient data to compute t statistic, MathIllegalArgumentException expected"); Assert.fail("insufficient data to compute t statistic, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.tTest(mu, tooShortStats); testStatistic.tTest(mu, tooShortStats);
Assert.fail("insufficient data to perform t test, MathIllegalArgumentException expected"); Assert.fail("insufficient data to perform t test, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
} }
@ -143,15 +145,15 @@ public class TTestTest {
try { try {
testStatistic.tTest(0d, oneSidedP, 95); testStatistic.tTest(0d, oneSidedP, 95);
Assert.fail("alpha out of range, MathIllegalArgumentException expected"); Assert.fail("alpha out of range, OutOfRangeException expected");
} catch (MathIllegalArgumentException ex) { } catch (OutOfRangeException ex) {
// expected // expected
} }
try { try {
testStatistic.tTest(0d, oneSidedPStats, 95); testStatistic.tTest(0d, oneSidedPStats, 95);
Assert.fail("alpha out of range, MathIllegalArgumentException expected"); Assert.fail("alpha out of range, OutOfRangeException expected");
} catch (MathIllegalArgumentException ex) { } catch (OutOfRangeException ex) {
// expected // expected
} }
@ -190,57 +192,57 @@ public class TTestTest {
try { try {
testStatistic.tTest(sample1, sample2, .95); testStatistic.tTest(sample1, sample2, .95);
Assert.fail("alpha out of range, MathIllegalArgumentException expected"); Assert.fail("alpha out of range, OutOfRangeException expected");
} catch (MathIllegalArgumentException ex) { } catch (OutOfRangeException ex) {
// expected // expected
} }
try { try {
testStatistic.tTest(sampleStats1, sampleStats2, .95); testStatistic.tTest(sampleStats1, sampleStats2, .95);
Assert.fail("alpha out of range, MathIllegalArgumentException expected"); Assert.fail("alpha out of range, OutOfRangeException expected");
} catch (MathIllegalArgumentException ex) { } catch (OutOfRangeException ex) {
// expected // expected
} }
try { try {
testStatistic.tTest(sample1, tooShortObs, .01); testStatistic.tTest(sample1, tooShortObs, .01);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.tTest(sampleStats1, tooShortStats, .01); testStatistic.tTest(sampleStats1, tooShortStats, .01);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.tTest(sample1, tooShortObs); testStatistic.tTest(sample1, tooShortObs);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.tTest(sampleStats1, tooShortStats); testStatistic.tTest(sampleStats1, tooShortStats);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.t(sample1, tooShortObs); testStatistic.t(sample1, tooShortObs);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
testStatistic.t(sampleStats1, tooShortStats); testStatistic.t(sampleStats1, tooShortStats);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
} }

View File

@ -20,9 +20,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.NotPositiveException; import org.apache.commons.math.exception.NotPositiveException;
import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.OutOfRangeException; import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.stat.descriptive.SummaryStatistics; import org.apache.commons.math.stat.descriptive.SummaryStatistics;
import org.junit.Assert; import org.junit.Assert;
@ -218,55 +219,55 @@ public class TestUtilsTest {
try { try {
TestUtils.t(mu, (double[]) null); TestUtils.t(mu, (double[]) null);
Assert.fail("arguments too short, MathIllegalArgumentException expected"); Assert.fail("arguments too short, NullArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (NullArgumentException ex) {
// expected // expected
} }
try { try {
TestUtils.t(mu, (SummaryStatistics) null); TestUtils.t(mu, (SummaryStatistics) null);
Assert.fail("arguments too short, MathIllegalArgumentException expected"); Assert.fail("arguments too short, NullArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (NullArgumentException ex) {
// expected // expected
} }
try { try {
TestUtils.t(mu, emptyObs); TestUtils.t(mu, emptyObs);
Assert.fail("arguments too short, MathIllegalArgumentException expected"); Assert.fail("arguments too short, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
TestUtils.t(mu, emptyStats); TestUtils.t(mu, emptyStats);
Assert.fail("arguments too short, MathIllegalArgumentException expected"); Assert.fail("arguments too short, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
TestUtils.t(mu, tooShortObs); TestUtils.t(mu, tooShortObs);
Assert.fail("insufficient data to compute t statistic, MathIllegalArgumentException expected"); Assert.fail("insufficient data to compute t statistic, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
TestUtils.tTest(mu, tooShortObs); TestUtils.tTest(mu, tooShortObs);
Assert.fail("insufficient data to perform t test, MathIllegalArgumentException expected"); Assert.fail("insufficient data to perform t test, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
TestUtils.t(mu, (SummaryStatistics) null); TestUtils.t(mu, (SummaryStatistics) null);
Assert.fail("insufficient data to compute t statistic, MathIllegalArgumentException expected"); Assert.fail("insufficient data to compute t statistic, NullArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (NullArgumentException ex) {
// expected // expected
} }
try { try {
TestUtils.tTest(mu, (SummaryStatistics) null); TestUtils.tTest(mu, (SummaryStatistics) null);
Assert.fail("insufficient data to perform t test, MathIllegalArgumentException expected"); Assert.fail("insufficient data to perform t test, NullArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (NullArgumentException ex) {
// expected // expected
} }
} }
@ -295,15 +296,15 @@ public class TestUtilsTest {
try { try {
TestUtils.tTest(0d, oneSidedP, 95); TestUtils.tTest(0d, oneSidedP, 95);
Assert.fail("alpha out of range, MathIllegalArgumentException expected"); Assert.fail("alpha out of range, OutOfRangeException expected");
} catch (MathIllegalArgumentException ex) { } catch (OutOfRangeException ex) {
// expected // expected
} }
try { try {
TestUtils.tTest(0d, oneSidedPStats, 95); TestUtils.tTest(0d, oneSidedPStats, 95);
Assert.fail("alpha out of range, MathIllegalArgumentException expected"); Assert.fail("alpha out of range, OutOfRangeException expected");
} catch (MathIllegalArgumentException ex) { } catch (OutOfRangeException ex) {
// expected // expected
} }
@ -342,57 +343,57 @@ public class TestUtilsTest {
try { try {
TestUtils.tTest(sample1, sample2, .95); TestUtils.tTest(sample1, sample2, .95);
Assert.fail("alpha out of range, MathIllegalArgumentException expected"); Assert.fail("alpha out of range, OutOfRangeException expected");
} catch (MathIllegalArgumentException ex) { } catch (OutOfRangeException ex) {
// expected // expected
} }
try { try {
TestUtils.tTest(sampleStats1, sampleStats2, .95); TestUtils.tTest(sampleStats1, sampleStats2, .95);
Assert.fail("alpha out of range, MathIllegalArgumentException expected"); Assert.fail("alpha out of range, OutOfRangeException expected");
} catch (MathIllegalArgumentException ex) { } catch (OutOfRangeException ex) {
// expected // expected
} }
try { try {
TestUtils.tTest(sample1, tooShortObs, .01); TestUtils.tTest(sample1, tooShortObs, .01);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
TestUtils.tTest(sampleStats1, (SummaryStatistics) null, .01); TestUtils.tTest(sampleStats1, (SummaryStatistics) null, .01);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NullArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (NullArgumentException ex) {
// expected // expected
} }
try { try {
TestUtils.tTest(sample1, tooShortObs); TestUtils.tTest(sample1, tooShortObs);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
TestUtils.tTest(sampleStats1, (SummaryStatistics) null); TestUtils.tTest(sampleStats1, (SummaryStatistics) null);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NullArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (NullArgumentException ex) {
// expected // expected
} }
try { try {
TestUtils.t(sample1, tooShortObs); TestUtils.t(sample1, tooShortObs);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NumberIsTooSmallException expected");
} catch (MathIllegalArgumentException ex) { } catch (NumberIsTooSmallException ex) {
// expected // expected
} }
try { try {
TestUtils.t(sampleStats1, (SummaryStatistics) null); TestUtils.t(sampleStats1, (SummaryStatistics) null);
Assert.fail("insufficient data, MathIllegalArgumentException expected"); Assert.fail("insufficient data, NullArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (NullArgumentException ex) {
// expected // expected
} }
} }