Class InferenceTestUtils

Method Summary
Modifier and TypeMethodDescriptionstatic double
approximateP
(double d, int n, int m) Uses the KolmogorovSmirnov distribution to approximate \(P(D_{n,m} > d)\) where \(D_{n,m}\) is the 2sample KolmogorovSmirnov statistic.static double
chiSquare
(double[] expected, long[] observed) static double
chiSquare
(long[][] counts) Computes the ChiSquare statistic associated with a chisquare test of independence based on the inputcounts
array, viewed as a twoway table.static double
chiSquareDataSetsComparison
(long[] observed1, long[] observed2) Computes a ChiSquare two sample test statistic comparing bin frequency counts inobserved1
andobserved2
.static double
chiSquareTest
(double[] expected, long[] observed) Returns the observed significance level, or pvalue, associated with a Chisquare goodness of fit test comparing theobserved
frequency counts to those in theexpected
array.static boolean
chiSquareTest
(double[] expected, long[] observed, double alpha) Performs a Chisquare 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 pvalue, associated with a chisquare test of independence based on the inputcounts
array, viewed as a twoway table.static boolean
chiSquareTest
(long[][] counts, double alpha) Performs a chisquare test of independence evaluating the null hypothesis that the classifications represented by the counts in the columns of the input 2way table are independent of the rows, with significance levelalpha
.static double
chiSquareTestDataSetsComparison
(long[] observed1, long[] observed2) Returns the observed significance level, or pvalue, associated with a ChiSquare two sample test comparing bin frequency counts inobserved1
andobserved2
.static boolean
chiSquareTestDataSetsComparison
(long[] observed1, long[] observed2, double alpha) Performs a ChiSquare 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 2sample KolmogorovSmirnov statistic.static double
g
(double[] expected, long[] observed) static double
gDataSetsComparison
(long[] observed1, long[] observed2) Computes a G (LogLikelihood 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 pvalue, associated with a GTest for goodness of fit comparing theobserved
frequency counts to those in theexpected
array.static boolean
gTest
(double[] expected, long[] observed, double alpha) Performs a GTest (LogLikelihood 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 pvalue, associated with a GValue (LogLikelihood Ratio) for two sample test comparing bin frequency counts inobserved1
andobserved2
.static boolean
gTestDataSetsComparison
(long[] observed1, long[] observed2, double alpha) Performs a GTest (LogLikelihood Ratio Test) comparing two binned data sets.static double
gTestIntrinsic
(double[] expected, long[] observed) Returns the intrinsic (HardyWeinberg proportions) pValue, as described in p6469 of McDonald, J.H.static double
homoscedasticT
(double[] sample1, double[] sample2) Computes a 2sample t statistic, under the hypothesis of equal subpopulation variances.static double
homoscedasticT
(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) Computes a 2sample 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 pvalue, associated with a twosample, twotailed ttest 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 twosided ttest 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 pvalue, associated with a twosample, twotailed ttest 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 twosample KolmogorovSmirnov 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 onesample KolmogorovSmirnov 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 pvalue, or observed significance level, of a twosample KolmogorovSmirnov 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 pvalue, or observed significance level, of a twosample KolmogorovSmirnov test evaluating the null hypothesis thatx
andy
are samples drawn from the same probability distribution.static double
kolmogorovSmirnovTest
(RealDistribution dist, double[] data) Computes the pvalue, or observed significance level, of a onesample KolmogorovSmirnov test evaluating the null hypothesis thatdata
conforms todistribution
.static double
kolmogorovSmirnovTest
(RealDistribution dist, double[] data, boolean strict) Computes the pvalue, or observed significance level, of a onesample KolmogorovSmirnov test evaluating the null hypothesis thatdata
conforms todistribution
.static boolean
kolmogorovSmirnovTest
(RealDistribution dist, double[] data, double alpha) Performs a KolmogorovSmirnov test evaluating the null hypothesis thatdata
conforms todistribution
.static double
oneWayAnovaFValue
(Collection<double[]> categoryData) Computes the ANOVA Fvalue for a collection ofdouble[]
arrays.static double
oneWayAnovaPValue
(Collection<double[]> categoryData) Computes the ANOVA Pvalue 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, 2sample tstatistic based on the data in the input arrays.static double
pairedTTest
(double[] sample1, double[] sample2) Returns the observed significance level, or pvalue, associated with a paired, twosample, twotailed ttest based on the data in the input arrays.static boolean
pairedTTest
(double[] sample1, double[] sample2, double alpha) Performs a paired ttest evaluating the null hypothesis that the mean of the paired differences betweensample1
andsample2
is 0 in favor of the twosided 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 loglikelihood ratio for 2 state Datasets.static double
t
(double[] sample1, double[] sample2) Computes a 2sample 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 2sample 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 pvalue, associated with a twosample, twotailed ttest comparing the means of the input arrays.static boolean
tTest
(double[] sample1, double[] sample2, double alpha) Performs a twosided ttest 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 pvalue, associated with a onesample, twotailed ttest comparing the mean of the input array with the constantmu
.static boolean
tTest
(double mu, double[] sample, double alpha) Performs a twosided ttest 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 pvalue, associated with a onesample, twotailed ttest comparing the mean of the dataset described bysampleStats
with the constantmu
.static boolean
tTest
(double mu, StatisticalSummary sampleStats, double alpha) Performs a twosided ttest 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 pvalue, associated with a twosample, twotailed ttest comparing the means of the datasets described by two StatisticalSummary instances.static boolean
tTest
(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2, double alpha) Performs a twosided ttest evaluating the null hypothesis thatsampleStats1
andsampleStats2
describe datasets drawn from populations with the same mean, with significance levelalpha
.

Method Details

homoscedasticT
public static double homoscedasticT(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException Computes a 2sample t statistic, under the hypothesis of equal subpopulation variances. To compute a tstatistic without the equal variances hypothesis, uset(double[], double[])
.This statistic can be used to perform a (homoscedastic) twosample ttest to compare sample means.
The tstatistic 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) / ((n11) + (n21)))
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 2sample t statistic, comparing the means of the datasets described by twoStatisticalSummary
instances, under the assumption of equal subpopulation variances. To compute a tstatistic without the equal variances assumption, uset(StatisticalSummary, StatisticalSummary)
.This statistic can be used to perform a (homoscedastic) twosample ttest to compare sample means.
The tstatistic 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) / ((n11) + (n21)))
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 twosided ttest 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 1sided 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 tstatistic. 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 (2sided) hypothesis
mean 1 = mean 2
at the 95% level, usetTest(sample1, sample2, 0.05).
 To test the (onesided) 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 ttest 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 pvalue
 To test the (2sided) hypothesis

homoscedasticTTest
public static double homoscedasticTTest(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a twosample, twotailed ttest 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 twosided alternative that they are different. For a onesided test, divide the returned value by 2.
A pooled variance estimate is used to compute the tstatistic. 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 pvalue depends on the assumptions of the parametric ttest 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:
 pvalue for ttest
 Throws:
NullArgumentException
 if the arrays arenull
MathIllegalArgumentException
 if the length of the arrays is < 2MathIllegalStateException
 if an error occurs computing the pvalue

homoscedasticTTest
public static double homoscedasticTTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a twosample, twotailed ttest 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 twosided alternative that they are different. For a onesided test, divide the returned value by 2.
See
homoscedasticT(double[], double[])
for the formula used to compute the tstatistic. The sum of the sample sizes minus 2 is used as the degrees of freedom.Usage Note:
The validity of the pvalue depends on the assumptions of the parametric ttest 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:
 pvalue for ttest
 Throws:
NullArgumentException
 if the sample statistics arenull
MathIllegalArgumentException
 if the number of samples is < 2MathIllegalStateException
 if an error occurs computing the pvalue

pairedT
public static double pairedT(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException Computes a paired, 2sample tstatistic based on the data in the input arrays. The tstatistic returned is equivalent to what would be returned by computing the onesample tstatistict(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 ttest evaluating the null hypothesis that the mean of the paired differences betweensample1
andsample2
is 0 in favor of the twosided 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 1sided test, usealpha * 2
Usage Note:
The validity of the test depends on the assumptions of the parametric ttest 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 pvalue

pairedTTest
public static double pairedTTest(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a paired, twosample, twotailed ttest 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 twosided alternative that the mean paired difference is not equal to 0. For a onesided test, divide the returned value by 2.
This test is equivalent to a onesample ttest 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 pvalue depends on the assumptions of the parametric ttest 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:
 pvalue for ttest
 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 pvalue

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 ttest 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 ttest 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 2sample t statistic, without the hypothesis of equal subpopulation variances. To compute a tstatistic assuming equal variances, usehomoscedasticT(double[], double[])
.This statistic can be used to perform a twosample ttest to compare sample means.
The tstatistic 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 2sample 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 tstatistic under the equal variances assumption.This statistic can be used to perform a twosample ttest to compare sample means.
The returned tstatistic 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 twosided ttest 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 1sided test, usealpha * 2
Examples:
 To test the (2sided) hypothesis
sample mean = mu
at the 95% level, usetTest(mu, sample, 0.05)
 To test the (onesided) 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 onesample parametric ttest 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:
 pvalue
 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 pvalue
 To test the (2sided) hypothesis

tTest
public static double tTest(double mu, double[] sample) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a onesample, twotailed ttest 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 twosided alternative that the mean is different frommu
. For a onesided test, divide the returned value by 2.Usage Note:
The validity of the test depends on the assumptions of the parametric ttest 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:
 pvalue
 Throws:
NullArgumentException
 if the sample array isnull
MathIllegalArgumentException
 if the length of the array is < 2MathIllegalStateException
 if an error occurs computing the pvalue

tTest
public static boolean tTest(double mu, StatisticalSummary sampleStats, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Performs a twosided ttest 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 1sided test, usealpha * 2.
Examples:
 To test the (2sided) hypothesis
sample mean = mu
at the 95% level, usetTest(mu, sampleStats, 0.05)
 To test the (onesided) 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 onesample parametric ttest 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:
 pvalue
 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 pvalue
 To test the (2sided) hypothesis

tTest
public static double tTest(double mu, StatisticalSummary sampleStats) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a onesample, twotailed ttest 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 twosided alternative that the mean is different frommu
. For a onesided test, divide the returned value by 2.Usage Note:
The validity of the test depends on the assumptions of the parametric ttest 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:
 pvalue
 Throws:
NullArgumentException
 ifsampleStats
isnull
MathIllegalArgumentException
 if the number of samples is < 2MathIllegalStateException
 if an error occurs computing the pvalue

tTest
public static boolean tTest(double[] sample1, double[] sample2, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Performs a twosided ttest 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 1sided test, usealpha * 2
See
t(double[], double[])
for the formula used to compute the tstatistic. Degrees of freedom are approximated using the WelchSatterthwaite approximation.Examples:
 To test the (2sided) hypothesis
mean 1 = mean 2
at the 95% level, usetTest(sample1, sample2, 0.05).
 To test the (onesided) 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 ttest 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 pvalue
 To test the (2sided) hypothesis

tTest
public static double tTest(double[] sample1, double[] sample2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a twosample, twotailed ttest 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 twosided alternative that they are different. For a onesided 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 pvalue. The tstatistic used is as defined in
t(double[], double[])
and the WelchSatterthwaite 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 pvalue depends on the assumptions of the parametric ttest 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:
 pvalue for ttest
 Throws:
NullArgumentException
 if the arrays arenull
MathIllegalArgumentException
 if the length of the arrays is < 2MathIllegalStateException
 if an error occurs computing the pvalue

tTest
public static boolean tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Performs a twosided ttest 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 1sided test, usealpha * 2
See
t(double[], double[])
for the formula used to compute the tstatistic. Degrees of freedom are approximated using the WelchSatterthwaite approximation.Examples:
 To test the (2sided) hypothesis
mean 1 = mean 2
at the 95%, usetTest(sampleStats1, sampleStats2, 0.05)
 To test the (onesided) 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 ttest 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 pvalue
 To test the (2sided) hypothesis

tTest
public static double tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a twosample, twotailed ttest 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 twosided alternative that they are different. For a onesided 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 pvalue. To perform the test assuming equal variances, use
homoscedasticTTest(StatisticalSummary, StatisticalSummary)
.Usage Note:
The validity of the pvalue depends on the assumptions of the parametric ttest 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:
 pvalue for ttest
 Throws:
NullArgumentException
 if the sample statistics arenull
MathIllegalArgumentException
 if the number of samples is < 2MathIllegalStateException
 if an error occurs computing the pvalue

chiSquare
public static double chiSquare(double[] expected, long[] observed) throws MathIllegalArgumentException Computes the ChiSquare statistic comparingobserved
andexpected
frequency counts.This statistic can be used to perform a ChiSquare 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 ChiSquare statistic associated with a chisquare test of independence based on the inputcounts
array, viewed as a twoway table.The rows of the 2way 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 2way 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 2way 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 Chisquare 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 pvalue

chiSquareTest
public static double chiSquareTest(double[] expected, long[] observed) throws MathIllegalArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a Chisquare 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:
 pvalue
 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 pvalue

chiSquareTest
public static boolean chiSquareTest(long[][] counts, double alpha) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Performs a chisquare test of independence evaluating the null hypothesis that the classifications represented by the counts in the columns of the input 2way 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 2way 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 2way 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 2way 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 pvalue

chiSquareTest
public static double chiSquareTest(long[][] counts) throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a chisquare test of independence based on the inputcounts
array, viewed as a twoway table.The rows of the 2way 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 2way 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 2way table Returns:
 pvalue
 Throws:
NullArgumentException
 if the array is nullMathIllegalArgumentException
 if the array is not rectangularMathIllegalArgumentException
 ifcounts
has negative entriesMathIllegalStateException
 if an error occurs computing the pvalue

chiSquareDataSetsComparison
public static double chiSquareDataSetsComparison(long[] observed1, long[] observed2) throws MathIllegalArgumentException Computes a ChiSquare 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 ChiSquare test evaluating the null hypothesis that both observed counts follow the same distribution.
Preconditions:
 Observed counts must be nonnegative.
 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 pvalue, associated with a ChiSquare 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 nonnegative.
 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:
 pvalue
 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 pvalue

chiSquareTestDataSetsComparison
public static boolean chiSquareTestDataSetsComparison(long[] observed1, long[] observed2, double alpha) throws MathIllegalArgumentException, MathIllegalStateException Performs a ChiSquare 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 nonnegative.
 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 Fvalue 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 Pvalue 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 pvalue, 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 pvalue 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 pvalue, 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 pvalue 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 pvalue can not be computed due to a convergence errorMathIllegalStateException
 if the maximum number of iterations is exceeded
 The categoryData

g
Computes the G statistic for Goodness of Fit comparingobserved
andexpected
frequency counts.This statistic can be used to perform a G test (LogLikelihood 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:
 GTest 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 pvalue, associated with a GTest 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:
 pvalue
 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 pvalue.

gTestIntrinsic
public static double gTestIntrinsic(double[] expected, long[] observed) throws MathIllegalArgumentException, MathIllegalStateException Returns the intrinsic (HardyWeinberg proportions) pValue, as described in p6469 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:
 pvalue
 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 pvalue.

gTest
public static boolean gTest(double[] expected, long[] observed, double alpha) throws MathIllegalArgumentException, MathIllegalStateException Performs a GTest (LogLikelihood 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 pvalue.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 (LogLikelihood 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 nonnegative.
 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:
 GTest 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 loglikelihood 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 loglikelihood ratio
 Throws:
MathIllegalArgumentException

gTestDataSetsComparison
public static double gTestDataSetsComparison(long[] observed1, long[] observed2) throws MathIllegalArgumentException, MathIllegalStateException Returns the observed significance level, or pvalue, associated with a GValue (LogLikelihood 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 pvalue 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 nonnegative.
 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:
 pvalue
 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 pvalue.

gTestDataSetsComparison
public static boolean gTestDataSetsComparison(long[] observed1, long[] observed2, double alpha) throws MathIllegalArgumentException, MathIllegalStateException Performs a GTest (LogLikelihood 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 nonnegative.
 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 onesample KolmogorovSmirnov 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:
 KolmogorovSmirnov 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 pvalue, or observed significance level, of a onesample KolmogorovSmirnov test evaluating the null hypothesis thatdata
conforms todistribution
. Parameters:
dist
 reference distributiondata
 sample being being evaluated Returns:
 the pvalue 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 pvalue, or observed significance level, of a onesample KolmogorovSmirnov test evaluating the null hypothesis thatdata
conforms todistribution
. Ifexact
is true, the distribution used to compute the pvalue 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 pvalue Returns:
 the pvalue 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 KolmogorovSmirnov 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 twosample KolmogorovSmirnov 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 pvalue, or observed significance level, of a twosample KolmogorovSmirnov 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 pvalue. SeeKolmogorovSmirnovTest.kolmogorovSmirnovTest(RealDistribution, double[], boolean)
. Parameters:
x
 first sample datasety
 second sample dataset Returns:
 pvalue 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 pvalue, or observed significance level, of a twosample KolmogorovSmirnov 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 pvalue 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 pvalue. 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 pvalue. 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:
 pvalue 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:
 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 2sample KolmogorovSmirnov 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
 Dstatistic 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 mn 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 KolmogorovSmirnov distribution to approximate \(P(D_{n,m} > d)\) where \(D_{n,m}\) is the 2sample KolmogorovSmirnov 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
 Dstatistic valuen
 first sample sizem
 second sample size Returns:
 approximate probability that a randomly selected mn partition of m + n generates
\(D_{n,m}\) greater than
d
