Class InferenceTestUtils
- java.lang.Object
-
- org.hipparchus.stat.inference.InferenceTestUtils
-
public class InferenceTestUtils extends Object
A collection of static methods to create inference test instances or to perform inference tests.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doubleapproximateP(double d, int n, int m)Uses the Kolmogorov-Smirnov distribution to approximate \(P(D_{n,m} > d)\) where \(D_{n,m}\) is the 2-sample Kolmogorov-Smirnov statistic.static doublechiSquare(double[] expected, long[] observed)static doublechiSquare(long[][] counts)Computes the Chi-Square statistic associated with a chi-square test of independence based on the inputcountsarray, viewed as a two-way table.static doublechiSquareDataSetsComparison(long[] observed1, long[] observed2)Computes a Chi-Square two sample test statistic comparing bin frequency counts inobserved1andobserved2.static doublechiSquareTest(double[] expected, long[] observed)Returns the observed significance level, or p-value, associated with a Chi-square goodness of fit test comparing theobservedfrequency counts to those in theexpectedarray.static booleanchiSquareTest(double[] expected, long[] observed, double alpha)Performs a Chi-square goodness of fit test evaluating the null hypothesis that the observed counts conform to the frequency distribution described by the expected counts, with significance levelalpha.static doublechiSquareTest(long[][] counts)Returns the observed significance level, or p-value, associated with a chi-square test of independence based on the inputcountsarray, viewed as a two-way table.static booleanchiSquareTest(long[][] counts, double alpha)Performs a chi-square test of independence evaluating the null hypothesis that the classifications represented by the counts in the columns of the input 2-way table are independent of the rows, with significance levelalpha.static doublechiSquareTestDataSetsComparison(long[] observed1, long[] observed2)Returns the observed significance level, or p-value, associated with a Chi-Square two sample test comparing bin frequency counts inobserved1andobserved2.static booleanchiSquareTestDataSetsComparison(long[] observed1, long[] observed2, double alpha)Performs a Chi-Square two sample test comparing two binned data sets.static doubleexactP(double d, int m, int n, boolean strict)Computes \(P(D_{n,m} > d)\) ifstrictistrue; otherwise \(P(D_{n,m} \ge d)\), where \(D_{n,m}\) is the 2-sample Kolmogorov-Smirnov statistic.static doubleg(double[] expected, long[] observed)static doublegDataSetsComparison(long[] observed1, long[] observed2)Computes a G (Log-Likelihood Ratio) two sample test statistic for independence comparing frequency counts inobserved1andobserved2.static doublegTest(double[] expected, long[] observed)Returns the observed significance level, or p-value, associated with a G-Test for goodness of fit comparing theobservedfrequency counts to those in theexpectedarray.static booleangTest(double[] expected, long[] observed, double alpha)Performs a G-Test (Log-Likelihood Ratio Test) for goodness of fit evaluating the null hypothesis that the observed counts conform to the frequency distribution described by the expected counts, with significance levelalpha.static doublegTestDataSetsComparison(long[] observed1, long[] observed2)Returns the observed significance level, or p-value, associated with a G-Value (Log-Likelihood Ratio) for two sample test comparing bin frequency counts inobserved1andobserved2.static booleangTestDataSetsComparison(long[] observed1, long[] observed2, double alpha)Performs a G-Test (Log-Likelihood Ratio Test) comparing two binned data sets.static doublegTestIntrinsic(double[] expected, long[] observed)Returns the intrinsic (Hardy-Weinberg proportions) p-Value, as described in p64-69 of McDonald, J.H. 2009.static doublehomoscedasticT(double[] sample1, double[] sample2)Computes a 2-sample t statistic, under the hypothesis of equal subpopulation variances.static 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.static 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.static 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.static 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.static doublekolmogorovSmirnovStatistic(double[] x, double[] y)Computes the two-sample Kolmogorov-Smirnov test statistic, \(D_{n,m}=\sup_x |F_n(x)-F_m(x)|\) where \(n\) is the length ofx, \(m\) is the length ofy, \(F_n\) is the empirical distribution that puts mass \(1/n\) at each of the values inxand \(F_m\) is the empirical distribution of theyvalues.static doublekolmogorovSmirnovStatistic(RealDistribution dist, double[] data)Computes the one-sample Kolmogorov-Smirnov test statistic, \(D_n=\sup_x |F_n(x)-F(x)|\) where \(F\) is the distribution (cdf) function associated withdistribution, \(n\) is the length ofdataand \(F_n\) is the empirical distribution that puts mass \(1/n\) at each of the values indata.static doublekolmogorovSmirnovTest(double[] x, double[] y)Computes the p-value, or observed significance level, of a two-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatxandyare samples drawn from the same probability distribution.static doublekolmogorovSmirnovTest(double[] x, double[] y, boolean strict)Computes the p-value, or observed significance level, of a two-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatxandyare samples drawn from the same probability distribution.static doublekolmogorovSmirnovTest(RealDistribution dist, double[] data)Computes the p-value, or observed significance level, of a one-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatdataconforms todistribution.static doublekolmogorovSmirnovTest(RealDistribution dist, double[] data, boolean strict)Computes the p-value, or observed significance level, of a one-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatdataconforms todistribution.static booleankolmogorovSmirnovTest(RealDistribution dist, double[] data, double alpha)Performs a Kolmogorov-Smirnov test evaluating the null hypothesis thatdataconforms todistribution.static doubleoneWayAnovaFValue(Collection<double[]> categoryData)Computes the ANOVA F-value for a collection ofdouble[]arrays.static doubleoneWayAnovaPValue(Collection<double[]> categoryData)Computes the ANOVA P-value for a collection ofdouble[]arrays.static booleanoneWayAnovaTest(Collection<double[]> categoryData, double alpha)Performs an ANOVA test, evaluating the null hypothesis that there is no difference among the means of the data categories.static doublepairedT(double[] sample1, double[] sample2)Computes a paired, 2-sample t-statistic based on the data in the input arrays.static 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.static 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.static doublerootLogLikelihoodRatio(long k11, long k12, long k21, long k22)Calculates the root log-likelihood ratio for 2 state Datasets.static doublet(double[] sample1, double[] sample2)Computes a 2-sample t statistic, without the hypothesis of equal subpopulation variances.static doublet(double mu, double[] observed)Computes a t statistic given observed values and a comparison constant.static doublet(double mu, StatisticalSummary sampleStats)static 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.static 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.static 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.static 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.static 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.static 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.static 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.static 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.static 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.
-
-
-
Method Detail
-
homoscedasticT
public static double homoscedasticT(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentExceptionComputes 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 arenullMathIllegalArgumentException- if the length of the arrays is < 2
-
homoscedasticT
public static double homoscedasticT(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws MathIllegalArgumentException, NullArgumentException
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 arenullMathIllegalArgumentException- if the number of samples is < 2
-
homoscedasticTTest
public static boolean homoscedasticTTest(double[] sample1, double[] sample2, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionPerforms 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 arenullMathIllegalArgumentException- if the length of the arrays is < 2MathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error occurs computing the p-value
- To test the (2-sided) hypothesis
-
homoscedasticTTest
public static double homoscedasticTTest(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionReturns 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 arenullMathIllegalArgumentException- if the length of the arrays is < 2MathIllegalStateException- if an error occurs computing the p-value
-
homoscedasticTTest
public static double homoscedasticTTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException
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 arenullMathIllegalArgumentException- if the number of samples is < 2MathIllegalStateException- if an error occurs computing the p-value
-
pairedT
public static double pairedT(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentExceptionComputes 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 arenullMathIllegalArgumentException- if the arrays are emptyMathIllegalArgumentException- if the length of the arrays is not equalMathIllegalArgumentException- if the length of the arrays is < 2
-
pairedTTest
public static boolean pairedTTest(double[] sample1, double[] sample2, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionPerforms 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 arenullMathIllegalArgumentException- if the arrays are emptyMathIllegalArgumentException- if the length of the arrays is not equalMathIllegalArgumentException- if the length of the arrays is < 2MathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error occurs computing the p-value
-
pairedTTest
public static double pairedTTest(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionReturns 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 arenullMathIllegalArgumentException- if the arrays are emptyMathIllegalArgumentException- if the length of the arrays is not equalMathIllegalArgumentException- if the length of the arrays is < 2MathIllegalStateException- if an error occurs computing the p-value
-
t
public static double t(double mu, double[] observed) throws MathIllegalArgumentException, NullArgumentExceptionComputes 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- ifobservedisnullMathIllegalArgumentException- if the length ofobservedis < 2
-
t
public static double t(double mu, StatisticalSummary sampleStats) throws MathIllegalArgumentException, NullArgumentExceptionComputes 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- ifsampleStatsisnullMathIllegalArgumentException- if the number of samples is < 2
-
t
public static double t(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentExceptionComputes 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 arenullMathIllegalArgumentException- if the length of the arrays is < 2
-
t
public static double t(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws MathIllegalArgumentException, NullArgumentException
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 arenullMathIllegalArgumentException- if the number of samples is < 2
-
tTest
public static boolean tTest(double mu, double[] sample, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionPerforms 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 isnullMathIllegalArgumentException- if the length of the array is < 2MathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error computing the p-value
- To test the (2-sided) hypothesis
-
tTest
public static double tTest(double mu, double[] sample) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionReturns 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 isnullMathIllegalArgumentException- if the length of the array is < 2MathIllegalStateException- if an error occurs computing the p-value
-
tTest
public static boolean tTest(double mu, StatisticalSummary sampleStats, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionPerforms 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- ifsampleStatsisnullMathIllegalArgumentException- if the number of samples is < 2MathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error occurs computing the p-value
- To test the (2-sided) hypothesis
-
tTest
public static double tTest(double mu, StatisticalSummary sampleStats) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionReturns 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- ifsampleStatsisnullMathIllegalArgumentException- if the number of samples is < 2MathIllegalStateException- if an error occurs computing the p-value
-
tTest
public static boolean tTest(double[] sample1, double[] sample2, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionPerforms 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 arenullMathIllegalArgumentException- if the length of the arrays is < 2MathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error occurs computing the p-value
- To test the (2-sided) hypothesis
-
tTest
public static double tTest(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionReturns 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 arenullMathIllegalArgumentException- if the length of the arrays is < 2MathIllegalStateException- if an error occurs computing the p-value
-
tTest
public static boolean tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException
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 arenullMathIllegalArgumentException- if the number of samples is < 2MathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error occurs computing the p-value
- To test the (2-sided) hypothesis
-
tTest
public static double tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException
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 arenullMathIllegalArgumentException- if the number of samples is < 2MathIllegalStateException- if an error occurs computing the p-value
-
chiSquare
public static double chiSquare(double[] expected, long[] observed) throws MathIllegalArgumentExceptionComputes the Chi-Square statistic comparingobservedandexpectedfrequency counts.This statistic can be used to perform a Chi-Square test evaluating the null hypothesis that the observed counts follow the expected distribution.
Preconditions:
- Expected counts must all be positive.
- Observed counts must all be ≥ 0.
- The observed and expected arrays must have the same length and their common length must be at least 2.
If any of the preconditions are not met, an
IllegalArgumentExceptionis thrown.Note: This implementation rescales the
expectedarray if necessary to ensure that the sum of the expected and observed counts are equal.- Parameters:
observed- array of observed frequency countsexpected- array of expected frequency counts- Returns:
- chiSquare test statistic
- Throws:
MathIllegalArgumentException- ifobservedhas negative entriesMathIllegalArgumentException- ifexpectedhas entries that are not strictly positiveMathIllegalArgumentException- if the arrays length is less than 2
-
chiSquare
public static double chiSquare(long[][] counts) throws MathIllegalArgumentException, NullArgumentExceptionComputes the Chi-Square statistic associated with a chi-square test of independence based on the inputcountsarray, viewed as a two-way table.The rows of the 2-way table are
count[0], ... , count[count.length - 1]Preconditions:
- All counts must be ≥ 0.
- The count array must be rectangular (i.e. all count[i] subarrays must have the same length).
- The 2-way table represented by
countsmust have at least 2 columns and at least 2 rows.
If any of the preconditions are not met, an
IllegalArgumentExceptionis thrown.- Parameters:
counts- array representation of 2-way table- Returns:
- chiSquare test statistic
- Throws:
NullArgumentException- if the array is nullMathIllegalArgumentException- if the array is not rectangularMathIllegalArgumentException- ifcountshas negative entries
-
chiSquareTest
public static boolean chiSquareTest(double[] expected, long[] observed, double alpha) throws MathIllegalArgumentException, MathIllegalStateExceptionPerforms a Chi-square goodness of fit test evaluating the null hypothesis that the observed counts conform to the frequency distribution described by the expected counts, with significance levelalpha. Returns true iff the null hypothesis can be rejected with 100 * (1 - alpha) percent confidence.Example:
To test the hypothesis thatobservedfollowsexpectedat the 99% level, usechiSquareTest(expected, observed, 0.01)Preconditions:
- Expected counts must all be positive.
- Observed counts must all be ≥ 0.
- The observed and expected arrays must have the same length and their common length must be at least 2.
0 < alpha < 0.5
If any of the preconditions are not met, an
IllegalArgumentExceptionis thrown.Note: This implementation rescales the
expectedarray if necessary to ensure that the sum of the expected and observed counts are equal.- Parameters:
observed- array of observed frequency countsexpected- array of expected frequency countsalpha- significance level of the test- Returns:
- true iff null hypothesis can be rejected with confidence 1 - alpha
- Throws:
MathIllegalArgumentException- ifobservedhas negative entriesMathIllegalArgumentException- ifexpectedhas entries that are not strictly positiveMathIllegalArgumentException- if the arrays length is less than 2MathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error occurs computing the p-value
-
chiSquareTest
public static double chiSquareTest(double[] expected, long[] observed) throws MathIllegalArgumentException, MathIllegalStateExceptionReturns the observed significance level, or p-value, associated with a Chi-square goodness of fit test comparing theobservedfrequency counts to those in theexpectedarray.The number returned is the smallest significance level at which one can reject the null hypothesis that the observed counts conform to the frequency distribution described by the expected counts.
Preconditions:
- Expected counts must all be positive.
- Observed counts must all be ≥ 0.
- The observed and expected arrays must have the same length and their common length must be at least 2.
If any of the preconditions are not met, an
IllegalArgumentExceptionis thrown.Note: This implementation rescales the
expectedarray if necessary to ensure that the sum of the expected and observed counts are equal.- Parameters:
observed- array of observed frequency countsexpected- array of expected frequency counts- Returns:
- p-value
- Throws:
MathIllegalArgumentException- ifobservedhas negative entriesMathIllegalArgumentException- ifexpectedhas entries that are not strictly positiveMathIllegalArgumentException- if the arrays length is less than 2MathIllegalStateException- if an error occurs computing the p-value
-
chiSquareTest
public static boolean chiSquareTest(long[][] counts, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionPerforms a chi-square test of independence evaluating the null hypothesis that the classifications represented by the counts in the columns of the input 2-way table are independent of the rows, with significance levelalpha. Returns true iff the null hypothesis can be rejected with 100 * (1 - alpha) percent confidence.The rows of the 2-way table are
count[0], ... , count[count.length - 1]Example:
To test the null hypothesis that the counts incount[0], ... , count[count.length - 1]all correspond to the same underlying probability distribution at the 99% level, usechiSquareTest(counts, 0.01).Preconditions:
- All counts must be ≥ 0.
- The count array must be rectangular (i.e. all count[i] subarrays must have the same length).
- The 2-way table represented by
countsmust have at least 2 columns and at least 2 rows.
If any of the preconditions are not met, an
IllegalArgumentExceptionis thrown.- Parameters:
counts- array representation of 2-way tablealpha- significance level of the test- Returns:
- true iff null hypothesis can be rejected with confidence 1 - alpha
- Throws:
NullArgumentException- if the array is nullMathIllegalArgumentException- if the array is not rectangularMathIllegalArgumentException- ifcountshas any negative entriesMathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error occurs computing the p-value
-
chiSquareTest
public static double chiSquareTest(long[][] counts) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateExceptionReturns the observed significance level, or p-value, associated with a chi-square test of independence based on the inputcountsarray, viewed as a two-way table.The rows of the 2-way table are
count[0], ... , count[count.length - 1]Preconditions:
- All counts must be ≥ 0.
- The count array must be rectangular (i.e. all count[i] subarrays must have the same length).
- The 2-way table represented by
countsmust have at least 2 columns and at least 2 rows.
If any of the preconditions are not met, an
IllegalArgumentExceptionis thrown.- Parameters:
counts- array representation of 2-way table- Returns:
- p-value
- Throws:
NullArgumentException- if the array is nullMathIllegalArgumentException- if the array is not rectangularMathIllegalArgumentException- ifcountshas negative entriesMathIllegalStateException- if an error occurs computing the p-value
-
chiSquareDataSetsComparison
public static double chiSquareDataSetsComparison(long[] observed1, long[] observed2) throws MathIllegalArgumentExceptionComputes a Chi-Square two sample test statistic comparing bin frequency counts inobserved1andobserved2.The sums of frequency counts in the two samples are not required to be the same. The formula used to compute the test statistic is
∑[(K * observed1[i] - observed2[i]/K)2 / (observed1[i] + observed2[i])]where
K = √[∑(observed2 / ∑(observed1)]This statistic can be used to perform a Chi-Square test evaluating the null hypothesis that both observed counts follow the same distribution.
Preconditions:
- Observed counts must be non-negative.
- Observed counts for a specific bin must not both be zero.
- Observed counts for a specific sample must not all be 0.
- The arrays
observed1andobserved2must have the same length and their common length must be at least 2.
If any of the preconditions are not met, an
IllegalArgumentExceptionis thrown.- Parameters:
observed1- array of observed frequency counts of the first data setobserved2- array of observed frequency counts of the second data set- Returns:
- chiSquare test statistic
- Throws:
MathIllegalArgumentException- the the length of the arrays does not matchMathIllegalArgumentException- if any entries inobserved1orobserved2are negativeMathIllegalArgumentException- if either all counts ofobserved1orobserved2are zero, or if the count at some index is zero for both arrays
-
chiSquareTestDataSetsComparison
public static double chiSquareTestDataSetsComparison(long[] observed1, long[] observed2) throws MathIllegalArgumentException, MathIllegalStateExceptionReturns the observed significance level, or p-value, associated with a Chi-Square two sample test comparing bin frequency counts inobserved1andobserved2.The number returned is the smallest significance level at which one can reject the null hypothesis that the observed counts conform to the same distribution.
See
chiSquareDataSetsComparison(long[], long[])for details on the formula used to compute the test statistic. The degrees of of freedom used to perform the test is one less than the common length of the input observed count arrays.Preconditions:
- Observed counts must be non-negative.
- Observed counts for a specific bin must not both be zero.
- Observed counts for a specific sample must not all be 0.
- The arrays
observed1andobserved2must have the same length and their common length must be at least 2.
If any of the preconditions are not met, an
IllegalArgumentExceptionis thrown.- Parameters:
observed1- array of observed frequency counts of the first data setobserved2- array of observed frequency counts of the second data set- Returns:
- p-value
- Throws:
MathIllegalArgumentException- the the length of the arrays does not matchMathIllegalArgumentException- if any entries inobserved1orobserved2are negativeMathIllegalArgumentException- if either all counts ofobserved1orobserved2are zero, or if the count at the same index is zero for both arraysMathIllegalStateException- if an error occurs computing the p-value
-
chiSquareTestDataSetsComparison
public static boolean chiSquareTestDataSetsComparison(long[] observed1, long[] observed2, double alpha) throws MathIllegalArgumentException, MathIllegalStateExceptionPerforms a Chi-Square two sample test comparing two binned data sets. The test evaluates the null hypothesis that the two lists of observed counts conform to the same frequency distribution, with significance levelalpha. Returns true iff the null hypothesis can be rejected with 100 * (1 - alpha) percent confidence.See
chiSquareDataSetsComparison(long[], long[])for details on the formula used to compute the Chisquare statistic used in the test. The degrees of of freedom used to perform the test is one less than the common length of the input observed count arrays.Preconditions:
- Observed counts must be non-negative.
- Observed counts for a specific bin must not both be zero.
- Observed counts for a specific sample must not all be 0.
- The arrays
observed1andobserved2must have the same length and their common length must be at least 2. 0 < alpha < 0.5
If any of the preconditions are not met, an
IllegalArgumentExceptionis thrown.- Parameters:
observed1- array of observed frequency counts of the first data setobserved2- array of observed frequency counts of the second data setalpha- significance level of the test- Returns:
- true iff null hypothesis can be rejected with confidence 1 - alpha
- Throws:
MathIllegalArgumentException- the the length of the arrays does not matchMathIllegalArgumentException- if any entries inobserved1orobserved2are negativeMathIllegalArgumentException- if either all counts ofobserved1orobserved2are zero, or if the count at the same index is zero for both arraysMathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error occurs performing the test
-
oneWayAnovaFValue
public static double oneWayAnovaFValue(Collection<double[]> categoryData) throws MathIllegalArgumentException, NullArgumentException
Computes the ANOVA F-value for a collection ofdouble[]arrays.Preconditions:
- The categoryData
Collectionmust containdouble[]arrays. - There must be at least two
double[]arrays in thecategoryDatacollection and each of these arrays must contain at least two values.
This implementation computes the F statistic using the definitional formula
F = msbg/mswg
where
msbg = between group mean square mswg = within group mean square
are as defined here
- Parameters:
categoryData-Collectionofdouble[]arrays each containing data for one category- Returns:
- Fvalue
- Throws:
NullArgumentException- ifcategoryDataisnullMathIllegalArgumentException- if the length of thecategoryDataarray is less than 2 or a containeddouble[]array does not have at least two values
- The categoryData
-
oneWayAnovaPValue
public static double oneWayAnovaPValue(Collection<double[]> categoryData) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException
Computes the ANOVA P-value for a collection ofdouble[]arrays.Preconditions:
- The categoryData
Collectionmust containdouble[]arrays. - There must be at least two
double[]arrays in thecategoryDatacollection and each of these arrays must contain at least two values.
This implementation uses the
Hipparchus F Distribution implementationto estimate the exact p-value, using the formulap = 1 - cumulativeProbability(F)
where
Fis the F value andcumulativeProbabilityis the Hipparchus implementation of the F distribution.- Parameters:
categoryData-Collectionofdouble[]arrays each containing data for one category- Returns:
- Pvalue
- Throws:
NullArgumentException- ifcategoryDataisnullMathIllegalArgumentException- if the length of thecategoryDataarray is less than 2 or a containeddouble[]array does not have at least two valuesMathIllegalStateException- if the p-value can not be computed due to a convergence errorMathIllegalStateException- if the maximum number of iterations is exceeded
- The categoryData
-
oneWayAnovaTest
public static boolean oneWayAnovaTest(Collection<double[]> categoryData, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException
Performs an ANOVA test, evaluating the null hypothesis that there is no difference among the means of the data categories.Preconditions:
- The categoryData
Collectionmust containdouble[]arrays. - There must be at least two
double[]arrays in thecategoryDatacollection and each of these arrays must contain at least two values. - alpha must be strictly greater than 0 and less than or equal to 0.5.
This implementation uses the
Hipparchus F Distribution implementationto estimate the exact p-value, using the formulap = 1 - cumulativeProbability(F)
where
Fis the F value andcumulativeProbabilityis the Hipparchus implementation of the F distribution.True is returned iff the estimated p-value is less than alpha.
- Parameters:
categoryData-Collectionofdouble[]arrays each containing data for one categoryalpha- significance level of the test- Returns:
- true if the null hypothesis can be rejected with confidence 1 - alpha
- Throws:
NullArgumentException- ifcategoryDataisnullMathIllegalArgumentException- if the length of thecategoryDataarray is less than 2 or a containeddouble[]array does not have at least two valuesMathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if the p-value can not be computed due to a convergence errorMathIllegalStateException- if the maximum number of iterations is exceeded
- The categoryData
-
g
public static double g(double[] expected, long[] observed) throws MathIllegalArgumentExceptionComputes the G statistic for Goodness of Fit comparingobservedandexpectedfrequency counts.This statistic can be used to perform a G test (Log-Likelihood Ratio Test) evaluating the null hypothesis that the observed counts follow the expected distribution.
Preconditions:
- Expected counts must all be positive.
- Observed counts must all be ≥ 0.
- The observed and expected arrays must have the same length and their common length must be at least 2.
If any of the preconditions are not met, a
MathIllegalArgumentExceptionis thrown.Note:This implementation rescales the
expectedarray if necessary to ensure that the sum of the expected and observed counts are equal.- Parameters:
observed- array of observed frequency countsexpected- array of expected frequency counts- Returns:
- G-Test statistic
- Throws:
MathIllegalArgumentException- ifobservedhas negative entriesMathIllegalArgumentException- ifexpectedhas entries that are not strictly positiveMathIllegalArgumentException- if the array lengths do not match or are less than 2.
-
gTest
public static double gTest(double[] expected, long[] observed) throws MathIllegalArgumentException, MathIllegalStateExceptionReturns the observed significance level, or p-value, associated with a G-Test for goodness of fit comparing theobservedfrequency counts to those in theexpectedarray.The number returned is the smallest significance level at which one can reject the null hypothesis that the observed counts conform to the frequency distribution described by the expected counts.
The probability returned is the tail probability beyond
g(expected, observed)in the ChiSquare distribution with degrees of freedom one less than the common length ofexpectedandobserved.Preconditions:
- Expected counts must all be positive.
- Observed counts must all be ≥ 0.
- The observed and expected arrays must have the same length and their common length must be at least 2.
If any of the preconditions are not met, a
MathIllegalArgumentExceptionis thrown.Note:This implementation rescales the
expectedarray if necessary to ensure that the sum of the expected and observed counts are equal.- Parameters:
observed- array of observed frequency countsexpected- array of expected frequency counts- Returns:
- p-value
- Throws:
MathIllegalArgumentException- ifobservedhas negative entriesMathIllegalArgumentException- ifexpectedhas entries that are not strictly positiveMathIllegalArgumentException- if the array lengths do not match or are less than 2.MathIllegalStateException- if an error occurs computing the p-value.
-
gTestIntrinsic
public static double gTestIntrinsic(double[] expected, long[] observed) throws MathIllegalArgumentException, MathIllegalStateExceptionReturns the intrinsic (Hardy-Weinberg proportions) p-Value, as described in p64-69 of McDonald, J.H. 2009. Handbook of Biological Statistics (2nd ed.). Sparky House Publishing, Baltimore, Maryland.The probability returned is the tail probability beyond
g(expected, observed)in the ChiSquare distribution with degrees of freedom two less than the common length ofexpectedandobserved.- Parameters:
observed- array of observed frequency countsexpected- array of expected frequency counts- Returns:
- p-value
- Throws:
MathIllegalArgumentException- ifobservedhas negative entriesMathIllegalArgumentException-expectedhas entries that are not strictly positiveMathIllegalArgumentException- if the array lengths do not match or are less than 2.MathIllegalStateException- if an error occurs computing the p-value.
-
gTest
public static boolean gTest(double[] expected, long[] observed, double alpha) throws MathIllegalArgumentException, MathIllegalStateExceptionPerforms a G-Test (Log-Likelihood Ratio Test) for goodness of fit evaluating the null hypothesis that the observed counts conform to the frequency distribution described by the expected counts, with significance levelalpha. Returns true iff the null hypothesis can be rejected with100 * (1 - alpha)percent confidence.Example:
To test the hypothesis thatobservedfollowsexpectedat the 99% level, usegTest(expected, observed, 0.01)Returns true iff
gTestGoodnessOfFitPValue(expected, observed)> alphaPreconditions:
- Expected counts must all be positive.
- Observed counts must all be ≥ 0.
- The observed and expected arrays must have the same length and their common length must be at least 2.
-
0 < alpha < 0.5
If any of the preconditions are not met, a
MathIllegalArgumentExceptionis thrown.Note:This implementation rescales the
expectedarray if necessary to ensure that the sum of the expected and observed counts are equal.- Parameters:
observed- array of observed frequency countsexpected- array of expected frequency countsalpha- significance level of the test- Returns:
- true iff null hypothesis can be rejected with confidence 1 - alpha
- Throws:
MathIllegalArgumentException- ifobservedhas negative entriesMathIllegalArgumentException- ifexpectedhas entries that are not strictly positiveMathIllegalArgumentException- if the array lengths do not match or are less than 2.MathIllegalStateException- if an error occurs computing the p-value.MathIllegalArgumentException- if alpha is not strictly greater than zero and less than or equal to 0.5
-
gDataSetsComparison
public static double gDataSetsComparison(long[] observed1, long[] observed2) throws MathIllegalArgumentExceptionComputes a G (Log-Likelihood Ratio) two sample test statistic for independence comparing frequency counts in
observed1andobserved2. The sums of frequency counts in the two samples are not required to be the same. The formula used to compute the test statistic is2 * totalSum * [H(rowSums) + H(colSums) - H(k)]where
His the Shannon Entropy of the random variable formed by viewing the elements of the argument array as incidence counts;
kis a matrix with rows[observed1, observed2];
rowSums, colSumsare the row/col sums ofk;
andtotalSumis the overall sum of all entries ink.This statistic can be used to perform a G test evaluating the null hypothesis that both observed counts are independent
Preconditions:
- Observed counts must be non-negative.
- Observed counts for a specific bin must not both be zero.
- Observed counts for a specific sample must not all be 0.
- The arrays
observed1andobserved2must have the same length and their common length must be at least 2.
If any of the preconditions are not met, a
MathIllegalArgumentExceptionis thrown.- Parameters:
observed1- array of observed frequency counts of the first data setobserved2- array of observed frequency counts of the second data set- Returns:
- G-Test statistic
- Throws:
MathIllegalArgumentException- the the lengths of the arrays do not match or their common length is less than 2MathIllegalArgumentException- if any entry inobserved1orobserved2is negativeMathIllegalArgumentException- if either all counts ofobserved1orobserved2are zero, or if the count at the same index is zero for both arrays.
-
rootLogLikelihoodRatio
public static double rootLogLikelihoodRatio(long k11, long k12, long k21, long k22) throws MathIllegalArgumentExceptionCalculates the root log-likelihood ratio for 2 state Datasets. SeegDataSetsComparison(long[], long[] ).Given two events A and B, let k11 be the number of times both events occur, k12 the incidence of B without A, k21 the count of A without B, and k22 the number of times neither A nor B occurs. What is returned by this method is
(sgn) sqrt(gValueDataSetsComparison({k11, k12}, {k21, k22})where
sgnis -1 ifk11 / (k11 + k12) < k21 / (k21 + k22));
1 otherwise.Signed root LLR has two advantages over the basic LLR: a) it is positive where k11 is bigger than expected, negative where it is lower b) if there is no difference it is asymptotically normally distributed. This allows one to talk about "number of standard deviations" which is a more common frame of reference than the chi^2 distribution.
- Parameters:
k11- number of times the two events occurred together (AB)k12- number of times the second event occurred WITHOUT the first event (notA,B)k21- number of times the first event occurred WITHOUT the second event (A, notB)k22- number of times something else occurred (i.e. was neither of these events (notA, notB)- Returns:
- root log-likelihood ratio
- Throws:
MathIllegalArgumentException
-
gTestDataSetsComparison
public static double gTestDataSetsComparison(long[] observed1, long[] observed2) throws MathIllegalArgumentException, MathIllegalStateExceptionReturns the observed significance level, or p-value, associated with a G-Value (Log-Likelihood Ratio) for two sample test comparing bin frequency counts in
observed1andobserved2.The number returned is the smallest significance level at which one can reject the null hypothesis that the observed counts conform to the same distribution.
See
gTest(double[], long[])for details on how the p-value is computed. The degrees of of freedom used to perform the test is one less than the common length of the input observed count arrays.Preconditions:
- Observed counts must be non-negative.
- Observed counts for a specific bin must not both be zero.
- Observed counts for a specific sample must not all be 0.
- The arrays
observed1andobserved2must have the same length and their common length must be at least 2.
If any of the preconditions are not met, a
MathIllegalArgumentExceptionis thrown.- Parameters:
observed1- array of observed frequency counts of the first data setobserved2- array of observed frequency counts of the second data set- Returns:
- p-value
- Throws:
MathIllegalArgumentException- the the length of the arrays does not match or their common length is less than 2MathIllegalArgumentException- if any of the entries inobserved1orobserved2are negativeMathIllegalArgumentException- if either all counts ofobserved1orobserved2are zero, or if the count at some index is zero for both arraysMathIllegalStateException- if an error occurs computing the p-value.
-
gTestDataSetsComparison
public static boolean gTestDataSetsComparison(long[] observed1, long[] observed2, double alpha) throws MathIllegalArgumentException, MathIllegalStateExceptionPerforms a G-Test (Log-Likelihood Ratio Test) comparing two binned data sets. The test evaluates the null hypothesis that the two lists of observed counts conform to the same frequency distribution, with significance level
alpha. Returns true iff the null hypothesis can be rejected with 100 * (1 - alpha) percent confidence.See
gDataSetsComparison(long[], long[])for details on the formula used to compute the G (LLR) statistic used in the test andgTest(double[], long[])for information on how the observed significance level is computed. The degrees of of freedom used to perform the test is one less than the common length of the input observed count arrays.Preconditions:
- Observed counts must be non-negative.
- Observed counts for a specific bin must not both be zero.
- Observed counts for a specific sample must not all be 0.
- The arrays
observed1andobserved2must have the same length and their common length must be at least 2. 0 < alpha < 0.5
If any of the preconditions are not met, a
MathIllegalArgumentExceptionis thrown.- Parameters:
observed1- array of observed frequency counts of the first data setobserved2- array of observed frequency counts of the second data setalpha- significance level of the test- Returns:
- true iff null hypothesis can be rejected with confidence 1 - alpha
- Throws:
MathIllegalArgumentException- the the length of the arrays does not matchMathIllegalArgumentException- if any of the entries inobserved1orobserved2are negativeMathIllegalArgumentException- if either all counts ofobserved1orobserved2are zero, or if the count at some index is zero for both arraysMathIllegalArgumentException- ifalphais not in the range (0, 0.5]MathIllegalStateException- if an error occurs performing the test
-
kolmogorovSmirnovStatistic
public static double kolmogorovSmirnovStatistic(RealDistribution dist, double[] data) throws MathIllegalArgumentException, NullArgumentException
Computes the one-sample Kolmogorov-Smirnov test statistic, \(D_n=\sup_x |F_n(x)-F(x)|\) where \(F\) is the distribution (cdf) function associated withdistribution, \(n\) is the length ofdataand \(F_n\) is the empirical distribution that puts mass \(1/n\) at each of the values indata.- Parameters:
dist- reference distributiondata- sample being evaluated- Returns:
- Kolmogorov-Smirnov statistic \(D_n\)
- Throws:
MathIllegalArgumentException- ifdatadoes not have length at least 2NullArgumentException- ifdatais null
-
kolmogorovSmirnovTest
public static double kolmogorovSmirnovTest(RealDistribution dist, double[] data) throws MathIllegalArgumentException, NullArgumentException
Computes the p-value, or observed significance level, of a one-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatdataconforms todistribution.- Parameters:
dist- reference distributiondata- sample being being evaluated- Returns:
- the p-value associated with the null hypothesis that
datais a sample fromdistribution - Throws:
MathIllegalArgumentException- ifdatadoes not have length at least 2NullArgumentException- ifdatais null
-
kolmogorovSmirnovTest
public static double kolmogorovSmirnovTest(RealDistribution dist, double[] data, boolean strict) throws MathIllegalArgumentException, NullArgumentException
Computes the p-value, or observed significance level, of a one-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatdataconforms todistribution. Ifexactis true, the distribution used to compute the p-value is computed using extended precision. SeeKolmogorovSmirnovTest.cdfExact(double, int).- Parameters:
dist- reference distributiondata- sample being being evaluatedstrict- whether or not to force exact computation of the p-value- Returns:
- the p-value associated with the null hypothesis that
datais a sample fromdistribution - Throws:
MathIllegalArgumentException- ifdatadoes not have length at least 2NullArgumentException- ifdatais null
-
kolmogorovSmirnovTest
public static boolean kolmogorovSmirnovTest(RealDistribution dist, double[] data, double alpha) throws MathIllegalArgumentException, NullArgumentException
Performs a Kolmogorov-Smirnov test evaluating the null hypothesis thatdataconforms todistribution.- Parameters:
dist- reference distributiondata- sample being being evaluatedalpha- significance level of the test- Returns:
- true iff the null hypothesis that
datais a sample fromdistributioncan be rejected with confidence 1 -alpha - Throws:
MathIllegalArgumentException- ifdatadoes not have length at least 2NullArgumentException- ifdatais null
-
kolmogorovSmirnovStatistic
public static double kolmogorovSmirnovStatistic(double[] x, double[] y) throws MathIllegalArgumentException, NullArgumentExceptionComputes the two-sample Kolmogorov-Smirnov test statistic, \(D_{n,m}=\sup_x |F_n(x)-F_m(x)|\) where \(n\) is the length ofx, \(m\) is the length ofy, \(F_n\) is the empirical distribution that puts mass \(1/n\) at each of the values inxand \(F_m\) is the empirical distribution of theyvalues.- Parameters:
x- first sampley- second sample- Returns:
- test statistic \(D_{n,m}\) used to evaluate the null hypothesis that
xandyrepresent samples from the same underlying distribution - Throws:
MathIllegalArgumentException- if eitherxorydoes not have length at least 2NullArgumentException- if eitherxoryis null
-
kolmogorovSmirnovTest
public static double kolmogorovSmirnovTest(double[] x, double[] y) throws MathIllegalArgumentException, NullArgumentExceptionComputes the p-value, or observed significance level, of a two-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatxandyare samples drawn from the same probability distribution. Assumes the strict form of the inequality used to compute the p-value. SeeKolmogorovSmirnovTest.kolmogorovSmirnovTest(RealDistribution, double[], boolean).- Parameters:
x- first sample datasety- second sample dataset- Returns:
- p-value associated with the null hypothesis that
xandyrepresent samples from the same distribution - Throws:
MathIllegalArgumentException- if eitherxorydoes not have length at least 2NullArgumentException- if eitherxoryis null
-
kolmogorovSmirnovTest
public static double kolmogorovSmirnovTest(double[] x, double[] y, boolean strict) throws MathIllegalArgumentException, NullArgumentExceptionComputes the p-value, or observed significance level, of a two-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatxandyare samples drawn from the same probability distribution. Specifically, what is returned is an estimate of the probability that theKolmogorovSmirnovTest.kolmogorovSmirnovStatistic(double[], double[])associated with a randomly selected partition of the combined sample into subsamples of sizesx.lengthandy.lengthwill strictly exceed (ifstrictistrue) or be at least as large asstrict = false) askolmogorovSmirnovStatistic(x, y).- For small samples (where the product of the sample sizes is less than
KolmogorovSmirnovTest.LARGE_SAMPLE_PRODUCT), the exact p-value is computed using the method presented in [4], implemented inexactP(double, int, int, boolean). - When the product of the sample sizes exceeds
KolmogorovSmirnovTest.LARGE_SAMPLE_PRODUCT, the asymptotic distribution of \(D_{n,m}\) is used. SeeapproximateP(double, int, int)for details on the approximation.
If
x.length * y.length<KolmogorovSmirnovTest.LARGE_SAMPLE_PRODUCTand the combined set of values inxandycontains ties, random jitter is added toxandyto break ties before computing \(D_{n,m}\) and the p-value. The jitter is uniformly distributed on (-minDelta / 2, minDelta / 2) where minDelta is the smallest pairwise difference between values in the combined sample.If ties are known to be present in the data,
KolmogorovSmirnovTest.bootstrap(double[], double[], int, boolean)may be used as an alternative method for estimating the p-value.- Parameters:
x- first sample datasety- second sample datasetstrict- whether or not the probability to compute is expressed as a strict inequality (ignored for large samples)- Returns:
- p-value associated with the null hypothesis that
xandyrepresent samples from the same distribution - Throws:
MathIllegalArgumentException- if eitherxorydoes not have length at least 2NullArgumentException- if eitherxoryis null- See Also:
KolmogorovSmirnovTest.bootstrap(double[], double[], int, boolean)
- For small samples (where the product of the sample sizes is less than
-
exactP
public static double exactP(double d, int m, int n, boolean strict)Computes \(P(D_{n,m} > d)\) ifstrictistrue; otherwise \(P(D_{n,m} \ge d)\), where \(D_{n,m}\) is the 2-sample Kolmogorov-Smirnov statistic. SeeKolmogorovSmirnovTest.kolmogorovSmirnovStatistic(double[], double[])for the definition of \(D_{n,m}\).The returned probability is exact, implemented by unwinding the recursive function definitions presented in [4] from the class javadoc.
- Parameters:
d- D-statistic valuen- first sample sizem- second sample sizestrict- whether or not the probability to compute is expressed as a strict inequality- Returns:
- probability that a randomly selected m-n partition of m + n generates \(D_{n,m}\)
greater than (resp. greater than or equal to)
d
-
approximateP
public static double approximateP(double d, int n, int m)Uses the Kolmogorov-Smirnov distribution to approximate \(P(D_{n,m} > d)\) where \(D_{n,m}\) is the 2-sample Kolmogorov-Smirnov statistic. SeeKolmogorovSmirnovTest.kolmogorovSmirnovStatistic(double[], double[])for the definition of \(D_{n,m}\).Specifically, what is returned is \(1 - k(d \sqrt{mn / (m + n)})\) where \(k(t) = 1 + 2 \sum_{i=1}^\infty (-1)^i e^{-2 i^2 t^2}\). See
KolmogorovSmirnovTest.ksSum(double, double, int)for details on how convergence of the sum is determined. This implementation passesksSumKolmogorovSmirnovTest.KS_SUM_CAUCHY_CRITERIONastoleranceandKolmogorovSmirnovTest.MAXIMUM_PARTIAL_SUM_COUNTasmaxIterations.- Parameters:
d- D-statistic valuen- first sample sizem- second sample size- Returns:
- approximate probability that a randomly selected m-n partition of m + n generates
\(D_{n,m}\) greater than
d
-
-