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 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.static double
chiSquare(double[] expected, long[] observed)
static double
chiSquare(long[][] counts)
Computes the Chi-Square statistic associated with a chi-square test of independence based on the inputcounts
array, viewed as a two-way table.static double
chiSquareDataSetsComparison(long[] observed1, long[] observed2)
Computes a Chi-Square two sample test statistic comparing bin frequency counts inobserved1
andobserved2
.static double
chiSquareTest(double[] expected, long[] observed)
Returns the observed significance level, or p-value, associated with a Chi-square goodness of fit test comparing theobserved
frequency counts to those in theexpected
array.static boolean
chiSquareTest(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 double
chiSquareTest(long[][] counts)
Returns the observed significance level, or p-value, associated with a chi-square test of independence based on the inputcounts
array, viewed as a two-way table.static boolean
chiSquareTest(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 double
chiSquareTestDataSetsComparison(long[] observed1, long[] observed2)
Returns the observed significance level, or p-value, associated with a Chi-Square two sample test comparing bin frequency counts inobserved1
andobserved2
.static boolean
chiSquareTestDataSetsComparison(long[] observed1, long[] observed2, double alpha)
Performs a Chi-Square two sample test comparing two binned data sets.static double
exactP(double d, int m, int n, boolean strict)
Computes \(P(D_{n,m} > d)\) ifstrict
istrue
; otherwise \(P(D_{n,m} \ge d)\), where \(D_{n,m}\) is the 2-sample Kolmogorov-Smirnov statistic.static double
g(double[] expected, long[] observed)
static double
gDataSetsComparison(long[] observed1, long[] observed2)
Computes a G (Log-Likelihood Ratio) two sample test statistic for independence comparing frequency counts inobserved1
andobserved2
.static double
gTest(double[] expected, long[] observed)
Returns the observed significance level, or p-value, associated with a G-Test for goodness of fit comparing theobserved
frequency counts to those in theexpected
array.static boolean
gTest(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 double
gTestDataSetsComparison(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 inobserved1
andobserved2
.static boolean
gTestDataSetsComparison(long[] observed1, long[] observed2, double alpha)
Performs a G-Test (Log-Likelihood Ratio Test) comparing two binned data sets.static double
gTestIntrinsic(double[] expected, long[] observed)
Returns the intrinsic (Hardy-Weinberg proportions) p-Value, as described in p64-69 of McDonald, J.H. 2009.static double
homoscedasticT(double[] sample1, double[] sample2)
Computes a 2-sample t statistic, under the hypothesis of equal subpopulation variances.static double
homoscedasticT(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)
Computes a 2-sample t statistic, comparing the means of the datasets described by twoStatisticalSummary
instances, under the assumption of equal subpopulation variances.static double
homoscedasticTTest(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 boolean
homoscedasticTTest(double[] sample1, double[] sample2, double alpha)
Performs a two-sided t-test evaluating the null hypothesis thatsample1
andsample2
are drawn from populations with the same mean, with significance levelalpha
, assuming that the subpopulation variances are equal.static double
homoscedasticTTest(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 double
kolmogorovSmirnovStatistic(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 inx
and \(F_m\) is the empirical distribution of they
values.static double
kolmogorovSmirnovStatistic(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 ofdata
and \(F_n\) is the empirical distribution that puts mass \(1/n\) at each of the values indata
.static double
kolmogorovSmirnovTest(double[] x, double[] y)
Computes the p-value, or observed significance level, of a two-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatx
andy
are samples drawn from the same probability distribution.static double
kolmogorovSmirnovTest(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 thatx
andy
are samples drawn from the same probability distribution.static double
kolmogorovSmirnovTest(RealDistribution dist, double[] data)
Computes the p-value, or observed significance level, of a one-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatdata
conforms todistribution
.static double
kolmogorovSmirnovTest(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 thatdata
conforms todistribution
.static boolean
kolmogorovSmirnovTest(RealDistribution dist, double[] data, double alpha)
Performs a Kolmogorov-Smirnov test evaluating the null hypothesis thatdata
conforms todistribution
.static double
oneWayAnovaFValue(Collection<double[]> categoryData)
Computes the ANOVA F-value for a collection ofdouble[]
arrays.static double
oneWayAnovaPValue(Collection<double[]> categoryData)
Computes the ANOVA P-value for a collection ofdouble[]
arrays.static boolean
oneWayAnovaTest(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 double
pairedT(double[] sample1, double[] sample2)
Computes a paired, 2-sample t-statistic based on the data in the input arrays.static double
pairedTTest(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 boolean
pairedTTest(double[] sample1, double[] sample2, double alpha)
Performs a paired t-test evaluating the null hypothesis that the mean of the paired differences betweensample1
andsample2
is 0 in favor of the two-sided alternative that the mean paired difference is not equal to 0, with significance levelalpha
.static double
rootLogLikelihoodRatio(long k11, long k12, long k21, long k22)
Calculates the root log-likelihood ratio for 2 state Datasets.static double
t(double[] sample1, double[] sample2)
Computes a 2-sample t statistic, without the hypothesis of equal subpopulation variances.static double
t(double mu, double[] observed)
Computes a t statistic given observed values and a comparison constant.static double
t(double mu, StatisticalSummary sampleStats)
static double
t(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)
Computes a 2-sample t statistic, comparing the means of the datasets described by twoStatisticalSummary
instances, without the assumption of equal subpopulation variances.static double
tTest(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 boolean
tTest(double[] sample1, double[] sample2, double alpha)
Performs a two-sided t-test evaluating the null hypothesis thatsample1
andsample2
are drawn from populations with the same mean, with significance levelalpha
.static double
tTest(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 boolean
tTest(double mu, double[] sample, double alpha)
Performs a two-sided t-test evaluating the null hypothesis that the mean of the population from whichsample
is drawn equalsmu
.static double
tTest(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 bysampleStats
with the constantmu
.static boolean
tTest(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 bystats
is drawn equalsmu
.static double
tTest(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 boolean
tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2, double alpha)
Performs a two-sided t-test evaluating the null hypothesis thatsampleStats1
andsampleStats2
describe datasets drawn from populations with the same mean, with significance levelalpha
.
-
-
-
Method Detail
-
homoscedasticT
public static double homoscedasticT(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException
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
n1
is the size of first sample;n2
is the size of second sample;m1
is the mean of first sample;m2
is the mean of second sample andvar
is the pooled variance estimate:var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1)))
with
var1
the variance of the first sample andvar2
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 arenull
MathIllegalArgumentException
- 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 twoStatisticalSummary
instances, 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
n1
is the size of first sample;n2
is the size of second sample;m1
is the mean of first sample;m2
is the mean of second sample andvar
is the pooled variance estimate:var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1)))
with
var1
the variance of the first sample andvar2
the 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 arenull
MathIllegalArgumentException
- if the number of samples is < 2
-
homoscedasticTTest
public static boolean homoscedasticTTest(double[] sample1, double[] sample2, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException
Performs a two-sided t-test evaluating the null hypothesis thatsample1
andsample2
are 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
true
iff 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 2
at 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 1
is less than the mean ofsample 2
and 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 arenull
MathIllegalArgumentException
- if the length of the arrays is < 2MathIllegalArgumentException
- ifalpha
is 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, MathIllegalStateException
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 arenull
MathIllegalArgumentException
- 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 arenull
MathIllegalArgumentException
- 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, NullArgumentException
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 = 0
and the sample array consisting of the (signed) differences between corresponding entries insample1
andsample2.
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 arenull
MathIllegalArgumentException
- 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, MathIllegalStateException
Performs a paired t-test evaluating the null hypothesis that the mean of the paired differences betweensample1
andsample2
is 0 in favor of the two-sided alternative that the mean paired difference is not equal to 0, with significance levelalpha
.Returns
true
iff the null hypothesis can be rejected with confidence1 - alpha
. To perform a 1-sided test, usealpha * 2
Usage 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 arenull
MathIllegalArgumentException
- if the arrays are emptyMathIllegalArgumentException
- if the length of the arrays is not equalMathIllegalArgumentException
- if the length of the arrays is < 2MathIllegalArgumentException
- ifalpha
is 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, MathIllegalStateException
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 = 0
and the sample array consisting of the signed differences between corresponding elements ofsample1
andsample2.
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 arenull
MathIllegalArgumentException
- 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, NullArgumentException
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
- ifobserved
isnull
MathIllegalArgumentException
- if the length ofobserved
is < 2
-
t
public static double t(double mu, StatisticalSummary sampleStats) throws MathIllegalArgumentException, NullArgumentException
Computes a t statistic to use in comparing the mean of the dataset described bysampleStats
tomu
.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
- ifsampleStats
isnull
MathIllegalArgumentException
- if the number of samples is < 2
-
t
public static double t(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException
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
n1
is the size of the first samplen2
is the size of the second sample;m1
is the mean of the first sample;m2
is the mean of the second sample;var1
is the variance of the first sample;var2
is 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 arenull
MathIllegalArgumentException
- 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 twoStatisticalSummary
instances, 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
n1
is the size of the first sample;n2
is the size of the second sample;m1
is the mean of the first sample;m2
is the mean of the second samplevar1
is the variance of the first sample;var2
is 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 arenull
MathIllegalArgumentException
- if the number of samples is < 2
-
tTest
public static boolean tTest(double mu, double[] sample, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException
Performs a two-sided t-test evaluating the null hypothesis that the mean of the population from whichsample
is drawn equalsmu
.Returns
true
iff 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 = mu
at the 95% level, usetTest(mu, sample, 0.05)
- To test the (one-sided) hypothesis
sample mean < mu
at the 99% level, first verify that the measured sample mean is less thanmu
and 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 isnull
MathIllegalArgumentException
- if the length of the array is < 2MathIllegalArgumentException
- ifalpha
is 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, MathIllegalStateException
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
mu
in 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 isnull
MathIllegalArgumentException
- 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, MathIllegalStateException
Performs a two-sided t-test evaluating the null hypothesis that the mean of the population from which the dataset described bystats
is drawn equalsmu
.Returns
true
iff 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 = mu
at the 95% level, usetTest(mu, sampleStats, 0.05)
- To test the (one-sided) hypothesis
sample mean < mu
at the 99% level, first verify that the measured sample mean is less thanmu
and 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
- ifsampleStats
isnull
MathIllegalArgumentException
- if the number of samples is < 2MathIllegalArgumentException
- ifalpha
is 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, MathIllegalStateException
Returns the observed significance level, or p-value, associated with a one-sample, two-tailed t-test comparing the mean of the dataset described bysampleStats
with the constantmu
.The number returned is the smallest significance level at which one can reject the null hypothesis that the mean equals
mu
in 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
- ifsampleStats
isnull
MathIllegalArgumentException
- 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, MathIllegalStateException
Performs a two-sided t-test evaluating the null hypothesis thatsample1
andsample2
are 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
true
iff the null hypothesis that the means are equal can be rejected with confidence1 - alpha
. To perform a 1-sided test, usealpha * 2
See
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 2
at 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 1
is less than the mean ofsample 2
and 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 arenull
MathIllegalArgumentException
- if the length of the arrays is < 2MathIllegalArgumentException
- ifalpha
is 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, MathIllegalStateException
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 arenull
MathIllegalArgumentException
- 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 thatsampleStats1
andsampleStats2
describe 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
true
iff the null hypothesis that the means are equal can be rejected with confidence1 - alpha
. To perform a 1-sided test, usealpha * 2
See
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 2
at the 95%, usetTest(sampleStats1, sampleStats2, 0.05)
- To test the (one-sided) hypothesis
mean 1 < mean 2
at the 99% level, first verify that the measured mean ofsample 1
is less than the mean ofsample 2
and 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 arenull
MathIllegalArgumentException
- if the number of samples is < 2MathIllegalArgumentException
- ifalpha
is 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 arenull
MathIllegalArgumentException
- 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 MathIllegalArgumentException
Computes the Chi-Square statistic comparingobserved
andexpected
frequency 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
IllegalArgumentException
is thrown.Note: This implementation rescales the
expected
array 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
- ifobserved
has negative entriesMathIllegalArgumentException
- ifexpected
has entries that are not strictly positiveMathIllegalArgumentException
- if the arrays length is less than 2
-
chiSquare
public static double chiSquare(long[][] counts) throws MathIllegalArgumentException, NullArgumentException
Computes the Chi-Square statistic associated with a chi-square test of independence based on the inputcounts
array, 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
counts
must have at least 2 columns and at least 2 rows.
If any of the preconditions are not met, an
IllegalArgumentException
is 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
- ifcounts
has negative entries
-
chiSquareTest
public static boolean chiSquareTest(double[] expected, long[] observed, double alpha) throws MathIllegalArgumentException, MathIllegalStateException
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
. Returns true iff the null hypothesis can be rejected with 100 * (1 - alpha) percent confidence.Example:
To test the hypothesis thatobserved
followsexpected
at 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
IllegalArgumentException
is thrown.Note: This implementation rescales the
expected
array 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
- ifobserved
has negative entriesMathIllegalArgumentException
- ifexpected
has entries that are not strictly positiveMathIllegalArgumentException
- if the arrays length is less than 2MathIllegalArgumentException
- ifalpha
is 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, MathIllegalStateException
Returns the observed significance level, or p-value, associated with a Chi-square goodness of fit test comparing theobserved
frequency counts to those in theexpected
array.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
IllegalArgumentException
is thrown.Note: This implementation rescales the
expected
array 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
- ifobserved
has negative entriesMathIllegalArgumentException
- ifexpected
has 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, MathIllegalStateException
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
. 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
counts
must have at least 2 columns and at least 2 rows.
If any of the preconditions are not met, an
IllegalArgumentException
is 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
- ifcounts
has any negative entriesMathIllegalArgumentException
- ifalpha
is 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, MathIllegalStateException
Returns the observed significance level, or p-value, associated with a chi-square test of independence based on the inputcounts
array, 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
counts
must have at least 2 columns and at least 2 rows.
If any of the preconditions are not met, an
IllegalArgumentException
is 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
- ifcounts
has negative entriesMathIllegalStateException
- if an error occurs computing the p-value
-
chiSquareDataSetsComparison
public static double chiSquareDataSetsComparison(long[] observed1, long[] observed2) throws MathIllegalArgumentException
Computes a Chi-Square two sample test statistic comparing bin frequency counts inobserved1
andobserved2
.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
observed1
andobserved2
must have the same length and their common length must be at least 2.
If any of the preconditions are not met, an
IllegalArgumentException
is 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 inobserved1
orobserved2
are negativeMathIllegalArgumentException
- if either all counts ofobserved1
orobserved2
are zero, or if the count at some index is zero for both arrays
-
chiSquareTestDataSetsComparison
public static double chiSquareTestDataSetsComparison(long[] observed1, long[] observed2) throws MathIllegalArgumentException, MathIllegalStateException
Returns the observed significance level, or p-value, associated with a Chi-Square two sample test comparing bin frequency counts inobserved1
andobserved2
.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
observed1
andobserved2
must have the same length and their common length must be at least 2.
If any of the preconditions are not met, an
IllegalArgumentException
is 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 inobserved1
orobserved2
are negativeMathIllegalArgumentException
- if either all counts ofobserved1
orobserved2
are 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, MathIllegalStateException
Performs 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
observed1
andobserved2
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
IllegalArgumentException
is 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 inobserved1
orobserved2
are negativeMathIllegalArgumentException
- if either all counts ofobserved1
orobserved2
are zero, or if the count at the same index is zero for both arraysMathIllegalArgumentException
- ifalpha
is 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
Collection
must containdouble[]
arrays. - There must be at least two
double[]
arrays in thecategoryData
collection 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
-Collection
ofdouble[]
arrays each containing data for one category- Returns:
- Fvalue
- Throws:
NullArgumentException
- ifcategoryData
isnull
MathIllegalArgumentException
- if the length of thecategoryData
array 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
Collection
must containdouble[]
arrays. - There must be at least two
double[]
arrays in thecategoryData
collection and each of these arrays must contain at least two values.
This implementation uses the
Hipparchus F Distribution implementation
to estimate the exact p-value, using the formulap = 1 - cumulativeProbability(F)
where
F
is the F value andcumulativeProbability
is the Hipparchus implementation of the F distribution.- Parameters:
categoryData
-Collection
ofdouble[]
arrays each containing data for one category- Returns:
- Pvalue
- Throws:
NullArgumentException
- ifcategoryData
isnull
MathIllegalArgumentException
- if the length of thecategoryData
array 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
Collection
must containdouble[]
arrays. - There must be at least two
double[]
arrays in thecategoryData
collection 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 implementation
to estimate the exact p-value, using the formulap = 1 - cumulativeProbability(F)
where
F
is the F value andcumulativeProbability
is the Hipparchus implementation of the F distribution.True is returned iff the estimated p-value is less than alpha.
- Parameters:
categoryData
-Collection
ofdouble[]
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
- ifcategoryData
isnull
MathIllegalArgumentException
- if the length of thecategoryData
array is less than 2 or a containeddouble[]
array does not have at least two valuesMathIllegalArgumentException
- ifalpha
is 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 MathIllegalArgumentException
Computes the G statistic for Goodness of Fit comparingobserved
andexpected
frequency 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
MathIllegalArgumentException
is thrown.Note:This implementation rescales the
expected
array 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
- ifobserved
has negative entriesMathIllegalArgumentException
- ifexpected
has 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, MathIllegalStateException
Returns the observed significance level, or p-value, associated with a G-Test for goodness of fit comparing theobserved
frequency counts to those in theexpected
array.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 ofexpected
andobserved
.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
MathIllegalArgumentException
is thrown.Note:This implementation rescales the
expected
array 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
- ifobserved
has negative entriesMathIllegalArgumentException
- ifexpected
has 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, MathIllegalStateException
Returns 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 ofexpected
andobserved
.- Parameters:
observed
- array of observed frequency countsexpected
- array of expected frequency counts- Returns:
- p-value
- Throws:
MathIllegalArgumentException
- ifobserved
has negative entriesMathIllegalArgumentException
-expected
has 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, MathIllegalStateException
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
. Returns true iff the null hypothesis can be rejected with100 * (1 - alpha)
percent confidence.Example:
To test the hypothesis thatobserved
followsexpected
at 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
MathIllegalArgumentException
is thrown.Note:This implementation rescales the
expected
array 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
- ifobserved
has negative entriesMathIllegalArgumentException
- ifexpected
has 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 MathIllegalArgumentException
Computes a G (Log-Likelihood Ratio) two sample test statistic for independence comparing frequency counts in
observed1
andobserved2
. 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
H
is the Shannon Entropy of the random variable formed by viewing the elements of the argument array as incidence counts;
k
is a matrix with rows[observed1, observed2]
;
rowSums, colSums
are the row/col sums ofk
;
andtotalSum
is 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
observed1
andobserved2
must have the same length and their common length must be at least 2.
If any of the preconditions are not met, a
MathIllegalArgumentException
is 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 inobserved1
orobserved2
is negativeMathIllegalArgumentException
- if either all counts ofobserved1
orobserved2
are 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 MathIllegalArgumentException
Calculates 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
sgn
is -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, MathIllegalStateException
Returns the observed significance level, or p-value, associated with a G-Value (Log-Likelihood Ratio) for two sample test comparing bin frequency counts in
observed1
andobserved2
.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
observed1
andobserved2
must have the same length and their common length must be at least 2.
If any of the preconditions are not met, a
MathIllegalArgumentException
is 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 inobserved1
orobserved2
are negativeMathIllegalArgumentException
- if either all counts ofobserved1
orobserved2
are 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, MathIllegalStateException
Performs 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
observed1
andobserved2
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
MathIllegalArgumentException
is 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 inobserved1
orobserved2
are negativeMathIllegalArgumentException
- if either all counts ofobserved1
orobserved2
are zero, or if the count at some index is zero for both arraysMathIllegalArgumentException
- ifalpha
is 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 ofdata
and \(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
- ifdata
does not have length at least 2NullArgumentException
- ifdata
is 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 thatdata
conforms todistribution
.- Parameters:
dist
- reference distributiondata
- sample being being evaluated- Returns:
- the p-value associated with the null hypothesis that
data
is a sample fromdistribution
- Throws:
MathIllegalArgumentException
- ifdata
does not have length at least 2NullArgumentException
- ifdata
is 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 thatdata
conforms todistribution
. Ifexact
is 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
data
is a sample fromdistribution
- Throws:
MathIllegalArgumentException
- ifdata
does not have length at least 2NullArgumentException
- ifdata
is null
-
kolmogorovSmirnovTest
public static boolean kolmogorovSmirnovTest(RealDistribution dist, double[] data, double alpha) throws MathIllegalArgumentException, NullArgumentException
Performs a Kolmogorov-Smirnov test evaluating the null hypothesis thatdata
conforms todistribution
.- Parameters:
dist
- reference distributiondata
- sample being being evaluatedalpha
- significance level of the test- Returns:
- true iff the null hypothesis that
data
is a sample fromdistribution
can be rejected with confidence 1 -alpha
- Throws:
MathIllegalArgumentException
- ifdata
does not have length at least 2NullArgumentException
- ifdata
is null
-
kolmogorovSmirnovStatistic
public static double kolmogorovSmirnovStatistic(double[] x, double[] y) throws MathIllegalArgumentException, NullArgumentException
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 inx
and \(F_m\) is the empirical distribution of they
values.- Parameters:
x
- first sampley
- second sample- Returns:
- test statistic \(D_{n,m}\) used to evaluate the null hypothesis that
x
andy
represent samples from the same underlying distribution - Throws:
MathIllegalArgumentException
- if eitherx
ory
does not have length at least 2NullArgumentException
- if eitherx
ory
is null
-
kolmogorovSmirnovTest
public static double kolmogorovSmirnovTest(double[] x, double[] y) throws MathIllegalArgumentException, NullArgumentException
Computes the p-value, or observed significance level, of a two-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatx
andy
are 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
x
andy
represent samples from the same distribution - Throws:
MathIllegalArgumentException
- if eitherx
ory
does not have length at least 2NullArgumentException
- if eitherx
ory
is null
-
kolmogorovSmirnovTest
public static double kolmogorovSmirnovTest(double[] x, double[] y, boolean strict) throws MathIllegalArgumentException, NullArgumentException
Computes the p-value, or observed significance level, of a two-sample Kolmogorov-Smirnov test evaluating the null hypothesis thatx
andy
are 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.length
andy.length
will strictly exceed (ifstrict
istrue
) 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_PRODUCT
and the combined set of values inx
andy
contains ties, random jitter is added tox
andy
to 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
x
andy
represent samples from the same distribution - Throws:
MathIllegalArgumentException
- if eitherx
ory
does not have length at least 2NullArgumentException
- if eitherx
ory
is 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)\) ifstrict
istrue
; 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 passesksSum
KolmogorovSmirnovTest.KS_SUM_CAUCHY_CRITERION
astolerance
andKolmogorovSmirnovTest.MAXIMUM_PARTIAL_SUM_COUNT
asmaxIterations
.- 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
-
-