Class TTest
- java.lang.Object
-
- org.apache.commons.math4.legacy.stat.inference.TTest
-
public class TTest extends Object
An implementation for Student's t-tests.Tests can be:
- One-sample or two-sample
- One-sided or two-sided
- Paired or unpaired (for two-sample tests)
- Homoscedastic (equal variance assumption) or heteroscedastic (for two sample tests)
- Fixed significance level (boolean-valued) or returning p-values.
Test statistics are available for all tests. Methods including "Test" in in their names perform tests, all other methods return t-statistics. Among the "Test" methods,
double-valued methods return p-values;boolean-valued methods perform fixed significance level tests. Significance levels are always specified as numbers between 0 and 0.5 (e.g. tests at the 95% level usealpha=0.05).Input to tests can be either
double[]arrays orStatisticalSummaryinstances.Uses commons-math
TDistributionimplementation to estimate exact p-values.
-
-
Constructor Summary
Constructors Constructor Description TTest()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected doubledf(double v1, double v2, double n1, double n2)Computes approximate degrees of freedom for 2-sample t-test.doublehomoscedasticT(double[] sample1, double[] sample2)Computes a 2-sample t statistic, under the hypothesis of equal subpopulation variances.protected doublehomoscedasticT(double m1, double m2, double v1, double v2, double n1, double n2)Computes t test statistic for 2-sample t-test under the hypothesis of equal subpopulation variances.doublehomoscedasticT(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)Computes a 2-sample t statistic, comparing the means of the datasets described by twoStatisticalSummaryinstances, under the assumption of equal subpopulation variances.doublehomoscedasticTTest(double[] sample1, double[] sample2)Returns the observed significance level, or p-value, associated with a two-sample, two-tailed t-test comparing the means of the input arrays, under the assumption that the two samples are drawn from subpopulations with equal variances.booleanhomoscedasticTTest(double[] sample1, double[] sample2, double alpha)Performs a two-sided t-test evaluating the null hypothesis thatsample1andsample2are drawn from populations with the same mean, with significance levelalpha, assuming that the subpopulation variances are equal.protected doublehomoscedasticTTest(double m1, double m2, double v1, double v2, double n1, double n2)Computes p-value for 2-sided, 2-sample t-test, under the assumption of equal subpopulation variances.doublehomoscedasticTTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)Returns the observed significance level, or p-value, associated with a two-sample, two-tailed t-test comparing the means of the datasets described by two StatisticalSummary instances, under the hypothesis of equal subpopulation variances.doublepairedT(double[] sample1, double[] sample2)Computes a paired, 2-sample t-statistic based on the data in the input arrays.doublepairedTTest(double[] sample1, double[] sample2)Returns the observed significance level, or p-value, associated with a paired, two-sample, two-tailed t-test based on the data in the input arrays.booleanpairedTTest(double[] sample1, double[] sample2, double alpha)Performs a paired t-test evaluating the null hypothesis that the mean of the paired differences betweensample1andsample2is 0 in favor of the two-sided alternative that the mean paired difference is not equal to 0, with significance levelalpha.doublet(double[] sample1, double[] sample2)Computes a 2-sample t statistic, without the hypothesis of equal subpopulation variances.doublet(double mu, double[] observed)Computes a t statistic given observed values and a comparison constant.protected doublet(double m, double mu, double v, double n)Computes t test statistic for 1-sample t-test.protected doublet(double m1, double m2, double v1, double v2, double n1, double n2)Computes t test statistic for 2-sample t-test.doublet(double mu, StatisticalSummary sampleStats)doublet(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)Computes a 2-sample t statistic, comparing the means of the datasets described by twoStatisticalSummaryinstances, without the assumption of equal subpopulation variances.doubletTest(double[] sample1, double[] sample2)Returns the observed significance level, or p-value, associated with a two-sample, two-tailed t-test comparing the means of the input arrays.booleantTest(double[] sample1, double[] sample2, double alpha)Performs a two-sided t-test evaluating the null hypothesis thatsample1andsample2are drawn from populations with the same mean, with significance levelalpha.doubletTest(double mu, double[] sample)Returns the observed significance level, or p-value, associated with a one-sample, two-tailed t-test comparing the mean of the input array with the constantmu.booleantTest(double mu, double[] sample, double alpha)Performs a two-sided t-test evaluating the null hypothesis that the mean of the population from whichsampleis drawn equalsmu.protected doubletTest(double m, double mu, double v, double n)Computes p-value for 2-sided, 1-sample t-test.protected doubletTest(double m1, double m2, double v1, double v2, double n1, double n2)Computes p-value for 2-sided, 2-sample t-test.doubletTest(double mu, StatisticalSummary sampleStats)Returns the observed significance level, or p-value, associated with a one-sample, two-tailed t-test comparing the mean of the dataset described bysampleStatswith the constantmu.booleantTest(double mu, StatisticalSummary sampleStats, double alpha)Performs a two-sided t-test evaluating the null hypothesis that the mean of the population from which the dataset described bystatsis drawn equalsmu.doubletTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)Returns the observed significance level, or p-value, associated with a two-sample, two-tailed t-test comparing the means of the datasets described by two StatisticalSummary instances.booleantTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2, double alpha)Performs a two-sided t-test evaluating the null hypothesis thatsampleStats1andsampleStats2describe datasets drawn from populations with the same mean, with significance levelalpha.
-
-
-
Constructor Detail
-
TTest
public TTest()
-
-
Method Detail
-
pairedT
public double pairedT(double[] sample1, double[] sample2) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException
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 computing the one-sample t-statistict(double, double[]), withmu = 0and the sample array consisting of the (signed) differences between corresponding entries insample1andsample2.Preconditions:
- The input arrays must have the same length and their common length must be at least 2.
- Parameters:
sample1- array of sample data valuessample2- array of sample data values- Returns:
- t statistic
- Throws:
NullArgumentException- if the arrays arenullNoDataException- if the arrays are emptyDimensionMismatchException- if the length of the arrays is not equalNumberIsTooSmallException- if the length of the arrays is < 2
-
pairedTTest
public double pairedTTest(double[] sample1, double[] sample2) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException
Returns the observed significance level, or p-value, associated with a paired, two-sample, two-tailed t-test based on the data in the input arrays.The number returned is the smallest significance level at which one can reject the null hypothesis that the mean of the paired differences is 0 in favor of the two-sided alternative that the mean paired difference is not equal to 0. For a one-sided test, divide the returned value by 2.
This test is equivalent to a one-sample t-test computed using
tTest(double, double[])withmu = 0and the sample array consisting of the signed differences between corresponding elements ofsample1andsample2.Usage Note:
The validity of the p-value depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The input array lengths must be the same and their common length must be at least 2.
- Parameters:
sample1- array of sample data valuessample2- array of sample data values- Returns:
- p-value for t-test
- Throws:
NullArgumentException- if the arrays arenullNoDataException- if the arrays are emptyDimensionMismatchException- if the length of the arrays is not equalNumberIsTooSmallException- if the length of the arrays is < 2MaxCountExceededException- if an error occurs computing the p-value
-
pairedTTest
public boolean pairedTTest(double[] sample1, double[] sample2, double alpha) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException
Performs a paired t-test evaluating the null hypothesis that the mean of the paired differences betweensample1andsample2is 0 in favor of the two-sided alternative that the mean paired difference is not equal to 0, with significance levelalpha.Returns
trueiff the null hypothesis can be rejected with confidence1 - alpha. To perform a 1-sided test, usealpha * 2Usage Note:
The validity of the test depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The input array lengths must be the same and their common length must be at least 2.
-
0 < alpha < 0.5
- Parameters:
sample1- array of sample data valuessample2- array of sample data valuesalpha- significance level of the test- Returns:
- true if the null hypothesis can be rejected with confidence 1 - alpha
- Throws:
NullArgumentException- if the arrays arenullNoDataException- if the arrays are emptyDimensionMismatchException- if the length of the arrays is not equalNumberIsTooSmallException- if the length of the arrays is < 2OutOfRangeException- ifalphais not in the range (0, 0.5]MaxCountExceededException- if an error occurs computing the p-value
-
t
public double t(double mu, double[] observed) throws NullArgumentException, NumberIsTooSmallException
Computes a t statistic given observed values and a comparison constant.This statistic can be used to perform a one sample t-test for the mean.
Preconditions:
- The observed array length must be at least 2.
- Parameters:
mu- comparison constantobserved- array of values- Returns:
- t statistic
- Throws:
NullArgumentException- ifobservedisnullNumberIsTooSmallException- if the length ofobservedis < 2
-
t
public double t(double mu, StatisticalSummary sampleStats) throws NullArgumentException, NumberIsTooSmallException
Computes a t statistic to use in comparing the mean of the dataset described bysampleStatstomu.This statistic can be used to perform a one sample t-test for the mean.
Preconditions:
observed.getN() ≥ 2.
- Parameters:
mu- comparison constantsampleStats- DescriptiveStatistics holding sample summary statitstics- Returns:
- t statistic
- Throws:
NullArgumentException- ifsampleStatsisnullNumberIsTooSmallException- if the number of samples is < 2
-
homoscedasticT
public double homoscedasticT(double[] sample1, double[] sample2) throws NullArgumentException, NumberIsTooSmallException
Computes a 2-sample t statistic, under the hypothesis of equal subpopulation variances. To compute a t-statistic without the equal variances hypothesis, uset(double[], double[]).This statistic can be used to perform a (homoscedastic) two-sample t-test to compare sample means.
The t-statistic is
t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))where
n1is the size of first sample;n2is the size of second sample;m1is the mean of first sample;m2is the mean of second sample andvaris the pooled variance estimate:var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1)))with
var1the variance of the first sample andvar2the variance of the second sample.Preconditions:
- The observed array lengths must both be at least 2.
- Parameters:
sample1- array of sample data valuessample2- array of sample data values- Returns:
- t statistic
- Throws:
NullArgumentException- if the arrays arenullNumberIsTooSmallException- if the length of the arrays is < 2
-
t
public double t(double[] sample1, double[] sample2) throws NullArgumentException, NumberIsTooSmallException
Computes a 2-sample t statistic, without the hypothesis of equal subpopulation variances. To compute a t-statistic assuming equal variances, usehomoscedasticT(double[], double[]).This statistic can be used to perform a two-sample t-test to compare sample means.
The t-statistic is
t = (m1 - m2) / sqrt(var1/n1 + var2/n2)where
n1is the size of the first samplen2is the size of the second sample;m1is the mean of the first sample;m2is the mean of the second sample;var1is the variance of the first sample;var2is the variance of the second sample;Preconditions:
- The observed array lengths must both be at least 2.
- Parameters:
sample1- array of sample data valuessample2- array of sample data values- Returns:
- t statistic
- Throws:
NullArgumentException- if the arrays arenullNumberIsTooSmallException- if the length of the arrays is < 2
-
t
public double t(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException
Computes a 2-sample t statistic, comparing the means of the datasets described by twoStatisticalSummaryinstances, without the assumption of equal subpopulation variances. UsehomoscedasticT(StatisticalSummary, StatisticalSummary)to compute a t-statistic under the equal variances assumption.This statistic can be used to perform a two-sample t-test to compare sample means.
The returned t-statistic is
t = (m1 - m2) / sqrt(var1/n1 + var2/n2)where
n1is the size of the first sample;n2is the size of the second sample;m1is the mean of the first sample;m2is the mean of the second samplevar1is the variance of the first sample;var2is the variance of the second samplePreconditions:
- The datasets described by the two Univariates must each contain at least 2 observations.
- Parameters:
sampleStats1- StatisticalSummary describing data from the first samplesampleStats2- StatisticalSummary describing data from the second sample- Returns:
- t statistic
- Throws:
NullArgumentException- if the sample statistics arenullNumberIsTooSmallException- if the number of samples is < 2
-
homoscedasticT
public double homoscedasticT(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException
Computes a 2-sample t statistic, comparing the means of the datasets described by twoStatisticalSummaryinstances, under the assumption of equal subpopulation variances. To compute a t-statistic without the equal variances assumption, uset(StatisticalSummary, StatisticalSummary).This statistic can be used to perform a (homoscedastic) two-sample t-test to compare sample means.
The t-statistic returned is
t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))where
n1is the size of first sample;n2is the size of second sample;m1is the mean of first sample;m2is the mean of second sample andvaris the pooled variance estimate:var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1)))with
var1the variance of the first sample andvar2the variance of the second sample.Preconditions:
- The datasets described by the two Univariates must each contain at least 2 observations.
- Parameters:
sampleStats1- StatisticalSummary describing data from the first samplesampleStats2- StatisticalSummary describing data from the second sample- Returns:
- t statistic
- Throws:
NullArgumentException- if the sample statistics arenullNumberIsTooSmallException- if the number of samples is < 2
-
tTest
public double tTest(double mu, double[] sample) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException
Returns the observed significance level, or p-value, associated with a one-sample, two-tailed t-test comparing the mean of the input array with the constantmu.The number returned is the smallest significance level at which one can reject the null hypothesis that the mean equals
muin favor of the two-sided alternative that the mean is different frommu. For a one-sided test, divide the returned value by 2.Usage Note:
The validity of the test depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The observed array length must be at least 2.
- Parameters:
mu- constant value to compare sample mean againstsample- array of sample data values- Returns:
- p-value
- Throws:
NullArgumentException- if the sample array isnullNumberIsTooSmallException- if the length of the array is < 2MaxCountExceededException- if an error occurs computing the p-value
-
tTest
public boolean tTest(double mu, double[] sample, double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException
Performs a two-sided t-test evaluating the null hypothesis that the mean of the population from whichsampleis drawn equalsmu.Returns
trueiff the null hypothesis can be rejected with confidence1 - alpha. To perform a 1-sided test, usealpha * 2Examples:
- To test the (2-sided) hypothesis
sample mean = muat the 95% level, usetTest(mu, sample, 0.05) - To test the (one-sided) hypothesis
sample mean < muat the 99% level, first verify that the measured sample mean is less thanmuand then usetTest(mu, sample, 0.02)
Usage Note:
The validity of the test depends on the assumptions of the one-sample parametric t-test procedure, as discussed herePreconditions:
- The observed array length must be at least 2.
- Parameters:
mu- constant value to compare sample mean againstsample- array of sample data valuesalpha- significance level of the test- Returns:
- p-value
- Throws:
NullArgumentException- if the sample array isnullNumberIsTooSmallException- if the length of the array is < 2OutOfRangeException- ifalphais not in the range (0, 0.5]MaxCountExceededException- if an error computing the p-value
- To test the (2-sided) hypothesis
-
tTest
public double tTest(double mu, StatisticalSummary sampleStats) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException
Returns the observed significance level, or p-value, associated with a one-sample, two-tailed t-test comparing the mean of the dataset described bysampleStatswith the constantmu.The number returned is the smallest significance level at which one can reject the null hypothesis that the mean equals
muin favor of the two-sided alternative that the mean is different frommu. For a one-sided test, divide the returned value by 2.Usage Note:
The validity of the test depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The sample must contain at least 2 observations.
- Parameters:
mu- constant value to compare sample mean againstsampleStats- StatisticalSummary describing sample data- Returns:
- p-value
- Throws:
NullArgumentException- ifsampleStatsisnullNumberIsTooSmallException- if the number of samples is < 2MaxCountExceededException- if an error occurs computing the p-value
-
tTest
public boolean tTest(double mu, StatisticalSummary sampleStats, double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException
Performs a two-sided t-test evaluating the null hypothesis that the mean of the population from which the dataset described bystatsis drawn equalsmu.Returns
trueiff the null hypothesis can be rejected with confidence1 - alpha. To perform a 1-sided test, usealpha * 2.Examples:
- To test the (2-sided) hypothesis
sample mean = muat the 95% level, usetTest(mu, sampleStats, 0.05) - To test the (one-sided) hypothesis
sample mean < muat the 99% level, first verify that the measured sample mean is less thanmuand then usetTest(mu, sampleStats, 0.02)
Usage Note:
The validity of the test depends on the assumptions of the one-sample parametric t-test procedure, as discussed herePreconditions:
- The sample must include at least 2 observations.
- Parameters:
mu- constant value to compare sample mean againstsampleStats- StatisticalSummary describing sample data valuesalpha- significance level of the test- Returns:
- p-value
- Throws:
NullArgumentException- ifsampleStatsisnullNumberIsTooSmallException- if the number of samples is < 2OutOfRangeException- ifalphais not in the range (0, 0.5]MaxCountExceededException- if an error occurs computing the p-value
- To test the (2-sided) hypothesis
-
tTest
public double tTest(double[] sample1, double[] sample2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException
Returns the observed significance level, or p-value, associated with a two-sample, two-tailed t-test comparing the means of the input arrays.The number returned is the smallest significance level at which one can reject the null hypothesis that the two means are equal in favor of the two-sided alternative that they are different. For a one-sided test, divide the returned value by 2.
The test does not assume that the underlying popuation variances are equal and it uses approximated degrees of freedom computed from the sample data to compute the p-value. The t-statistic used is as defined in
t(double[], double[])and the Welch-Satterthwaite approximation to the degrees of freedom is used, as described here. To perform the test under the assumption of equal subpopulation variances, usehomoscedasticTTest(double[], double[]).Usage Note:
The validity of the p-value depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The observed array lengths must both be at least 2.
- Parameters:
sample1- array of sample data valuessample2- array of sample data values- Returns:
- p-value for t-test
- Throws:
NullArgumentException- if the arrays arenullNumberIsTooSmallException- if the length of the arrays is < 2MaxCountExceededException- if an error occurs computing the p-value
-
homoscedasticTTest
public double homoscedasticTTest(double[] sample1, double[] sample2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException
Returns the observed significance level, or p-value, associated with a two-sample, two-tailed t-test comparing the means of the input arrays, under the assumption that the two samples are drawn from subpopulations with equal variances. To perform the test without the equal variances assumption, usetTest(double[], double[]).The number returned is the smallest significance level at which one can reject the null hypothesis that the two means are equal in favor of the two-sided alternative that they are different. For a one-sided test, divide the returned value by 2.
A pooled variance estimate is used to compute the t-statistic. See
homoscedasticT(double[], double[]). The sum of the sample sizes minus 2 is used as the degrees of freedom.Usage Note:
The validity of the p-value depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The observed array lengths must both be at least 2.
- Parameters:
sample1- array of sample data valuessample2- array of sample data values- Returns:
- p-value for t-test
- Throws:
NullArgumentException- if the arrays arenullNumberIsTooSmallException- if the length of the arrays is < 2MaxCountExceededException- if an error occurs computing the p-value
-
tTest
public boolean tTest(double[] sample1, double[] sample2, double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException
Performs a two-sided t-test evaluating the null hypothesis thatsample1andsample2are drawn from populations with the same mean, with significance levelalpha. This test does not assume that the subpopulation variances are equal. To perform the test assuming equal variances, usehomoscedasticTTest(double[], double[], double).Returns
trueiff the null hypothesis that the means are equal can be rejected with confidence1 - alpha. To perform a 1-sided test, usealpha * 2See
t(double[], double[])for the formula used to compute the t-statistic. Degrees of freedom are approximated using the Welch-Satterthwaite approximation.Examples:
- To test the (2-sided) hypothesis
mean 1 = mean 2at the 95% level, usetTest(sample1, sample2, 0.05). - To test the (one-sided) hypothesis
mean 1 < mean 2, at the 99% level, first verify that the measured mean ofsample 1is less than the mean ofsample 2and then usetTest(sample1, sample2, 0.02)
Usage Note:
The validity of the test depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The observed array lengths must both be at least 2.
-
0 < alpha < 0.5
- Parameters:
sample1- array of sample data valuessample2- array of sample data valuesalpha- significance level of the test- Returns:
- true if the null hypothesis can be rejected with confidence 1 - alpha
- Throws:
NullArgumentException- if the arrays arenullNumberIsTooSmallException- if the length of the arrays is < 2OutOfRangeException- ifalphais not in the range (0, 0.5]MaxCountExceededException- if an error occurs computing the p-value
- To test the (2-sided) hypothesis
-
homoscedasticTTest
public boolean homoscedasticTTest(double[] sample1, double[] sample2, double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException
Performs a two-sided t-test evaluating the null hypothesis thatsample1andsample2are drawn from populations with the same mean, with significance levelalpha, assuming that the subpopulation variances are equal. UsetTest(double[], double[], double)to perform the test without the assumption of equal variances.Returns
trueiff the null hypothesis that the means are equal can be rejected with confidence1 - alpha. To perform a 1-sided test, usealpha * 2.To perform the test without the assumption of equal subpopulation variances, usetTest(double[], double[], double).A pooled variance estimate is used to compute the t-statistic. See
t(double[], double[])for the formula. The sum of the sample sizes minus 2 is used as the degrees of freedom.Examples:
- To test the (2-sided) hypothesis
mean 1 = mean 2at the 95% level, usetTest(sample1, sample2, 0.05). - To test the (one-sided) hypothesis
mean 1 < mean 2,at the 99% level, first verify that the measured mean ofsample 1is less than the mean ofsample 2and then usetTest(sample1, sample2, 0.02)
Usage Note:
The validity of the test depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The observed array lengths must both be at least 2.
-
0 < alpha < 0.5
- Parameters:
sample1- array of sample data valuessample2- array of sample data valuesalpha- significance level of the test- Returns:
- true if the null hypothesis can be rejected with confidence 1 - alpha
- Throws:
NullArgumentException- if the arrays arenullNumberIsTooSmallException- if the length of the arrays is < 2OutOfRangeException- ifalphais not in the range (0, 0.5]MaxCountExceededException- if an error occurs computing the p-value
- To test the (2-sided) hypothesis
-
tTest
public double tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException
Returns the observed significance level, or p-value, associated with a two-sample, two-tailed t-test comparing the means of the datasets described by two StatisticalSummary instances.The number returned is the smallest significance level at which one can reject the null hypothesis that the two means are equal in favor of the two-sided alternative that they are different. For a one-sided test, divide the returned value by 2.
The test does not assume that the underlying population variances are equal and it uses approximated degrees of freedom computed from the sample data to compute the p-value. To perform the test assuming equal variances, use
homoscedasticTTest(StatisticalSummary, StatisticalSummary).Usage Note:
The validity of the p-value depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The datasets described by the two Univariates must each contain at least 2 observations.
- Parameters:
sampleStats1- StatisticalSummary describing data from the first samplesampleStats2- StatisticalSummary describing data from the second sample- Returns:
- p-value for t-test
- Throws:
NullArgumentException- if the sample statistics arenullNumberIsTooSmallException- if the number of samples is < 2MaxCountExceededException- if an error occurs computing the p-value
-
homoscedasticTTest
public double homoscedasticTTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException
Returns the observed significance level, or p-value, associated with a two-sample, two-tailed t-test comparing the means of the datasets described by two StatisticalSummary instances, under the hypothesis of equal subpopulation variances. To perform a test without the equal variances assumption, usetTest(StatisticalSummary, StatisticalSummary).The number returned is the smallest significance level at which one can reject the null hypothesis that the two means are equal in favor of the two-sided alternative that they are different. For a one-sided test, divide the returned value by 2.
See
homoscedasticT(double[], double[])for the formula used to compute the t-statistic. The sum of the sample sizes minus 2 is used as the degrees of freedom.Usage Note:
The validity of the p-value depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The datasets described by the two Univariates must each contain at least 2 observations.
- Parameters:
sampleStats1- StatisticalSummary describing data from the first samplesampleStats2- StatisticalSummary describing data from the second sample- Returns:
- p-value for t-test
- Throws:
NullArgumentException- if the sample statistics arenullNumberIsTooSmallException- if the number of samples is < 2MaxCountExceededException- if an error occurs computing the p-value
-
tTest
public boolean tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2, double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException
Performs a two-sided t-test evaluating the null hypothesis thatsampleStats1andsampleStats2describe datasets drawn from populations with the same mean, with significance levelalpha. This test does not assume that the subpopulation variances are equal. To perform the test under the equal variances assumption, usehomoscedasticTTest(StatisticalSummary, StatisticalSummary).Returns
trueiff the null hypothesis that the means are equal can be rejected with confidence1 - alpha. To perform a 1-sided test, usealpha * 2See
t(double[], double[])for the formula used to compute the t-statistic. Degrees of freedom are approximated using the Welch-Satterthwaite approximation.Examples:
- To test the (2-sided) hypothesis
mean 1 = mean 2at the 95%, usetTest(sampleStats1, sampleStats2, 0.05) - To test the (one-sided) hypothesis
mean 1 < mean 2at the 99% level, first verify that the measured mean ofsample 1is less than the mean ofsample 2and then usetTest(sampleStats1, sampleStats2, 0.02)
Usage Note:
The validity of the test depends on the assumptions of the parametric t-test procedure, as discussed herePreconditions:
- The datasets described by the two Univariates must each contain at least 2 observations.
-
0 < alpha < 0.5
- Parameters:
sampleStats1- StatisticalSummary describing sample data valuessampleStats2- StatisticalSummary describing sample data valuesalpha- significance level of the test- Returns:
- true if the null hypothesis can be rejected with confidence 1 - alpha
- Throws:
NullArgumentException- if the sample statistics arenullNumberIsTooSmallException- if the number of samples is < 2OutOfRangeException- ifalphais not in the range (0, 0.5]MaxCountExceededException- if an error occurs computing the p-value
- To test the (2-sided) hypothesis
-
df
protected double df(double v1, double v2, double n1, double n2)
Computes approximate degrees of freedom for 2-sample t-test.- Parameters:
v1- first sample variancev2- second sample variancen1- first sample nn2- second sample n- Returns:
- approximate degrees of freedom
-
t
protected double t(double m, double mu, double v, double n)
Computes t test statistic for 1-sample t-test.- Parameters:
m- sample meanmu- constant to test againstv- sample variancen- sample n- Returns:
- t test statistic
-
t
protected double t(double m1, double m2, double v1, double v2, double n1, double n2)
Computes t test statistic for 2-sample t-test.Does not assume that subpopulation variances are equal.
- Parameters:
m1- first sample meanm2- second sample meanv1- first sample variancev2- second sample variancen1- first sample nn2- second sample n- Returns:
- t test statistic
-
homoscedasticT
protected double homoscedasticT(double m1, double m2, double v1, double v2, double n1, double n2)
Computes t test statistic for 2-sample t-test under the hypothesis of equal subpopulation variances.- Parameters:
m1- first sample meanm2- second sample meanv1- first sample variancev2- second sample variancen1- first sample nn2- second sample n- Returns:
- t test statistic
-
tTest
protected double tTest(double m, double mu, double v, double n) throws MaxCountExceededException, MathIllegalArgumentException
Computes p-value for 2-sided, 1-sample t-test.- Parameters:
m- sample meanmu- constant to test againstv- sample variancen- sample n- Returns:
- p-value
- Throws:
MaxCountExceededException- if an error occurs computing the p-valueMathIllegalArgumentException- if n is not greater than 1
-
tTest
protected double tTest(double m1, double m2, double v1, double v2, double n1, double n2) throws MaxCountExceededException, NotStrictlyPositiveException
Computes p-value for 2-sided, 2-sample t-test.Does not assume subpopulation variances are equal. Degrees of freedom are estimated from the data.
- Parameters:
m1- first sample meanm2- second sample meanv1- first sample variancev2- second sample variancen1- first sample nn2- second sample n- Returns:
- p-value
- Throws:
MaxCountExceededException- if an error occurs computing the p-valueNotStrictlyPositiveException- if the estimated degrees of freedom is not strictly positive
-
homoscedasticTTest
protected double homoscedasticTTest(double m1, double m2, double v1, double v2, double n1, double n2) throws MaxCountExceededException, NotStrictlyPositiveException
Computes p-value for 2-sided, 2-sample t-test, under the assumption of equal subpopulation variances.The sum of the sample sizes minus 2 is used as degrees of freedom.
- Parameters:
m1- first sample meanm2- second sample meanv1- first sample variancev2- second sample variancen1- first sample nn2- second sample n- Returns:
- p-value
- Throws:
MaxCountExceededException- if an error occurs computing the p-valueNotStrictlyPositiveException- if the estimated degrees of freedom is not strictly positive
-
-