Renamed o.h.stat.inference.TestUtils to InferenceTestUtils
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/5c341d9d Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/5c341d9d Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/5c341d9d Branch: refs/heads/develop Commit: 5c341d9ded098fafcbed3ce88643e96286928011 Parents: dccc125 Author: Phil Steitz <[email protected]> Authored: Fri Aug 5 14:25:35 2016 +0200 Committer: Emmanuel Bourg <[email protected]> Committed: Fri Aug 5 14:25:35 2016 +0200 ---------------------------------------------------------------------- .../stat/inference/InferenceTestUtils.java | 547 ++++++++++++++++++ .../commons/math4/stat/inference/TestUtils.java | 547 ------------------ .../distribution/BetaDistributionTest.java | 7 +- .../stat/inference/InferenceTestUtilsTest.java | 557 ++++++++++++++++++ .../math4/stat/inference/TestUtilsTest.java | 559 ------------------- 5 files changed, 1107 insertions(+), 1110 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/5c341d9d/src/main/java/org/apache/commons/math4/stat/inference/InferenceTestUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/inference/InferenceTestUtils.java b/src/main/java/org/apache/commons/math4/stat/inference/InferenceTestUtils.java new file mode 100644 index 0000000..a0da22f --- /dev/null +++ b/src/main/java/org/apache/commons/math4/stat/inference/InferenceTestUtils.java @@ -0,0 +1,547 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math4.stat.inference; + +import java.util.Collection; + +import org.apache.commons.math4.distribution.RealDistribution; +import org.apache.commons.math4.exception.ConvergenceException; +import org.apache.commons.math4.exception.DimensionMismatchException; +import org.apache.commons.math4.exception.InsufficientDataException; +import org.apache.commons.math4.exception.MaxCountExceededException; +import org.apache.commons.math4.exception.NoDataException; +import org.apache.commons.math4.exception.NotPositiveException; +import org.apache.commons.math4.exception.NotStrictlyPositiveException; +import org.apache.commons.math4.exception.NullArgumentException; +import org.apache.commons.math4.exception.NumberIsTooSmallException; +import org.apache.commons.math4.exception.OutOfRangeException; +import org.apache.commons.math4.exception.ZeroException; +import org.apache.commons.math4.stat.descriptive.StatisticalSummary; + +/** + * A collection of static methods to create inference test instances or to + * perform inference tests. + * + * @since 1.1 + */ +public class InferenceTestUtils { + + /** Singleton TTest instance. */ + private static final TTest T_TEST = new TTest(); + + /** Singleton ChiSquareTest instance. */ + private static final ChiSquareTest CHI_SQUARE_TEST = new ChiSquareTest(); + + /** Singleton OneWayAnova instance. */ + private static final OneWayAnova ONE_WAY_ANANOVA = new OneWayAnova(); + + /** Singleton G-Test instance. */ + private static final GTest G_TEST = new GTest(); + + /** Singleton K-S test instance */ + private static final KolmogorovSmirnovTest KS_TEST = new KolmogorovSmirnovTest(); + + /** + * Prevent instantiation. + */ + private InferenceTestUtils() { + super(); + } + + // CHECKSTYLE: stop JavadocMethodCheck + + /** + * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticT(double[], double[]) + */ + public static double homoscedasticT(final double[] sample1, final double[] sample2) + throws NullArgumentException, NumberIsTooSmallException { + return T_TEST.homoscedasticT(sample1, sample2); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticT(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary) + */ + public static double homoscedasticT(final StatisticalSummary sampleStats1, + final StatisticalSummary sampleStats2) + throws NullArgumentException, NumberIsTooSmallException { + return T_TEST.homoscedasticT(sampleStats1, sampleStats2); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticTTest(double[], double[], double) + */ + public static boolean homoscedasticTTest(final double[] sample1, final double[] sample2, + final double alpha) + throws NullArgumentException, NumberIsTooSmallException, + OutOfRangeException, MaxCountExceededException { + return T_TEST.homoscedasticTTest(sample1, sample2, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticTTest(double[], double[]) + */ + public static double homoscedasticTTest(final double[] sample1, final double[] sample2) + throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { + return T_TEST.homoscedasticTTest(sample1, sample2); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticTTest(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary) + */ + public static double homoscedasticTTest(final StatisticalSummary sampleStats1, + final StatisticalSummary sampleStats2) + throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { + return T_TEST.homoscedasticTTest(sampleStats1, sampleStats2); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#pairedT(double[], double[]) + */ + public static double pairedT(final double[] sample1, final double[] sample2) + throws NullArgumentException, NoDataException, + DimensionMismatchException, NumberIsTooSmallException { + return T_TEST.pairedT(sample1, sample2); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#pairedTTest(double[], double[], double) + */ + public static boolean pairedTTest(final double[] sample1, final double[] sample2, + final double alpha) + throws NullArgumentException, NoDataException, DimensionMismatchException, + NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { + return T_TEST.pairedTTest(sample1, sample2, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#pairedTTest(double[], double[]) + */ + public static double pairedTTest(final double[] sample1, final double[] sample2) + throws NullArgumentException, NoDataException, DimensionMismatchException, + NumberIsTooSmallException, MaxCountExceededException { + return T_TEST.pairedTTest(sample1, sample2); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#t(double, double[]) + */ + public static double t(final double mu, final double[] observed) + throws NullArgumentException, NumberIsTooSmallException { + return T_TEST.t(mu, observed); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#t(double, org.apache.commons.math4.stat.descriptive.StatisticalSummary) + */ + public static double t(final double mu, final StatisticalSummary sampleStats) + throws NullArgumentException, NumberIsTooSmallException { + return T_TEST.t(mu, sampleStats); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#t(double[], double[]) + */ + public static double t(final double[] sample1, final double[] sample2) + throws NullArgumentException, NumberIsTooSmallException { + return T_TEST.t(sample1, sample2); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#t(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary) + */ + public static double t(final StatisticalSummary sampleStats1, + final StatisticalSummary sampleStats2) + throws NullArgumentException, NumberIsTooSmallException { + return T_TEST.t(sampleStats1, sampleStats2); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#tTest(double, double[], double) + */ + public static boolean tTest(final double mu, final double[] sample, final double alpha) + throws NullArgumentException, NumberIsTooSmallException, + OutOfRangeException, MaxCountExceededException { + return T_TEST.tTest(mu, sample, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#tTest(double, double[]) + */ + public static double tTest(final double mu, final double[] sample) + throws NullArgumentException, NumberIsTooSmallException, + MaxCountExceededException { + return T_TEST.tTest(mu, sample); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#tTest(double, org.apache.commons.math4.stat.descriptive.StatisticalSummary, double) + */ + public static boolean tTest(final double mu, final StatisticalSummary sampleStats, + final double alpha) + throws NullArgumentException, NumberIsTooSmallException, + OutOfRangeException, MaxCountExceededException { + return T_TEST.tTest(mu, sampleStats, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#tTest(double, org.apache.commons.math4.stat.descriptive.StatisticalSummary) + */ + public static double tTest(final double mu, final StatisticalSummary sampleStats) + throws NullArgumentException, NumberIsTooSmallException, + MaxCountExceededException { + return T_TEST.tTest(mu, sampleStats); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#tTest(double[], double[], double) + */ + public static boolean tTest(final double[] sample1, final double[] sample2, + final double alpha) + throws NullArgumentException, NumberIsTooSmallException, + OutOfRangeException, MaxCountExceededException { + return T_TEST.tTest(sample1, sample2, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#tTest(double[], double[]) + */ + public static double tTest(final double[] sample1, final double[] sample2) + throws NullArgumentException, NumberIsTooSmallException, + MaxCountExceededException { + return T_TEST.tTest(sample1, sample2); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#tTest(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary, double) + */ + public static boolean tTest(final StatisticalSummary sampleStats1, + final StatisticalSummary sampleStats2, + final double alpha) + throws NullArgumentException, NumberIsTooSmallException, + OutOfRangeException, MaxCountExceededException { + return T_TEST.tTest(sampleStats1, sampleStats2, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.TTest#tTest(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary) + */ + public static double tTest(final StatisticalSummary sampleStats1, + final StatisticalSummary sampleStats2) + throws NullArgumentException, NumberIsTooSmallException, + MaxCountExceededException { + return T_TEST.tTest(sampleStats1, sampleStats2); + } + + /** + * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquare(double[], long[]) + */ + public static double chiSquare(final double[] expected, final long[] observed) + throws NotPositiveException, NotStrictlyPositiveException, + DimensionMismatchException { + return CHI_SQUARE_TEST.chiSquare(expected, observed); + } + + /** + * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquare(long[][]) + */ + public static double chiSquare(final long[][] counts) + throws NullArgumentException, NotPositiveException, + DimensionMismatchException { + return CHI_SQUARE_TEST.chiSquare(counts); + } + + /** + * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTest(double[], long[], double) + */ + public static boolean chiSquareTest(final double[] expected, final long[] observed, + final double alpha) + throws NotPositiveException, NotStrictlyPositiveException, + DimensionMismatchException, OutOfRangeException, MaxCountExceededException { + return CHI_SQUARE_TEST.chiSquareTest(expected, observed, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTest(double[], long[]) + */ + public static double chiSquareTest(final double[] expected, final long[] observed) + throws NotPositiveException, NotStrictlyPositiveException, + DimensionMismatchException, MaxCountExceededException { + return CHI_SQUARE_TEST.chiSquareTest(expected, observed); + } + + /** + * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTest(long[][], double) + */ + public static boolean chiSquareTest(final long[][] counts, final double alpha) + throws NullArgumentException, DimensionMismatchException, + NotPositiveException, OutOfRangeException, MaxCountExceededException { + return CHI_SQUARE_TEST.chiSquareTest(counts, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTest(long[][]) + */ + public static double chiSquareTest(final long[][] counts) + throws NullArgumentException, DimensionMismatchException, + NotPositiveException, MaxCountExceededException { + return CHI_SQUARE_TEST.chiSquareTest(counts); + } + + /** + * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareDataSetsComparison(long[], long[]) + * + * @since 1.2 + */ + public static double chiSquareDataSetsComparison(final long[] observed1, + final long[] observed2) + throws DimensionMismatchException, NotPositiveException, ZeroException { + return CHI_SQUARE_TEST.chiSquareDataSetsComparison(observed1, observed2); + } + + /** + * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[]) + * + * @since 1.2 + */ + public static double chiSquareTestDataSetsComparison(final long[] observed1, + final long[] observed2) + throws DimensionMismatchException, NotPositiveException, ZeroException, + MaxCountExceededException { + return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2); + } + + /** + * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[], double) + * + * @since 1.2 + */ + public static boolean chiSquareTestDataSetsComparison(final long[] observed1, + final long[] observed2, + final double alpha) + throws DimensionMismatchException, NotPositiveException, + ZeroException, OutOfRangeException, MaxCountExceededException { + return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.OneWayAnova#anovaFValue(Collection) + * + * @since 1.2 + */ + public static double oneWayAnovaFValue(final Collection<double[]> categoryData) + throws NullArgumentException, DimensionMismatchException { + return ONE_WAY_ANANOVA.anovaFValue(categoryData); + } + + /** + * @see org.apache.commons.math4.stat.inference.OneWayAnova#anovaPValue(Collection) + * + * @since 1.2 + */ + public static double oneWayAnovaPValue(final Collection<double[]> categoryData) + throws NullArgumentException, DimensionMismatchException, + ConvergenceException, MaxCountExceededException { + return ONE_WAY_ANANOVA.anovaPValue(categoryData); + } + + /** + * @see org.apache.commons.math4.stat.inference.OneWayAnova#anovaTest(Collection,double) + * + * @since 1.2 + */ + public static boolean oneWayAnovaTest(final Collection<double[]> categoryData, + final double alpha) + throws NullArgumentException, DimensionMismatchException, + OutOfRangeException, ConvergenceException, MaxCountExceededException { + return ONE_WAY_ANANOVA.anovaTest(categoryData, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.GTest#g(double[], long[]) + * @since 3.1 + */ + public static double g(final double[] expected, final long[] observed) + throws NotPositiveException, NotStrictlyPositiveException, + DimensionMismatchException { + return G_TEST.g(expected, observed); + } + + /** + * @see org.apache.commons.math4.stat.inference.GTest#gTest( double[], long[] ) + * @since 3.1 + */ + public static double gTest(final double[] expected, final long[] observed) + throws NotPositiveException, NotStrictlyPositiveException, + DimensionMismatchException, MaxCountExceededException { + return G_TEST.gTest(expected, observed); + } + + /** + * @see org.apache.commons.math4.stat.inference.GTest#gTestIntrinsic(double[], long[] ) + * @since 3.1 + */ + public static double gTestIntrinsic(final double[] expected, final long[] observed) + throws NotPositiveException, NotStrictlyPositiveException, + DimensionMismatchException, MaxCountExceededException { + return G_TEST.gTestIntrinsic(expected, observed); + } + + /** + * @see org.apache.commons.math4.stat.inference.GTest#gTest( double[],long[],double) + * @since 3.1 + */ + public static boolean gTest(final double[] expected, final long[] observed, + final double alpha) + throws NotPositiveException, NotStrictlyPositiveException, + DimensionMismatchException, OutOfRangeException, MaxCountExceededException { + return G_TEST.gTest(expected, observed, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.GTest#gDataSetsComparison(long[], long[]) + * @since 3.1 + */ + public static double gDataSetsComparison(final long[] observed1, + final long[] observed2) + throws DimensionMismatchException, NotPositiveException, ZeroException { + return G_TEST.gDataSetsComparison(observed1, observed2); + } + + /** + * @see org.apache.commons.math4.stat.inference.GTest#rootLogLikelihoodRatio(long, long, long, long) + * @since 3.1 + */ + public static double rootLogLikelihoodRatio(final long k11, final long k12, final long k21, final long k22) + throws DimensionMismatchException, NotPositiveException, ZeroException { + return G_TEST.rootLogLikelihoodRatio(k11, k12, k21, k22); + } + + + /** + * @see org.apache.commons.math4.stat.inference.GTest#gTestDataSetsComparison(long[], long[]) + * @since 3.1 + */ + public static double gTestDataSetsComparison(final long[] observed1, + final long[] observed2) + throws DimensionMismatchException, NotPositiveException, ZeroException, + MaxCountExceededException { + return G_TEST.gTestDataSetsComparison(observed1, observed2); + } + + /** + * @see org.apache.commons.math4.stat.inference.GTest#gTestDataSetsComparison(long[],long[],double) + * @since 3.1 + */ + public static boolean gTestDataSetsComparison(final long[] observed1, + final long[] observed2, + final double alpha) + throws DimensionMismatchException, NotPositiveException, + ZeroException, OutOfRangeException, MaxCountExceededException { + return G_TEST.gTestDataSetsComparison(observed1, observed2, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovStatistic(RealDistribution, double[]) + * @since 3.3 + */ + public static double kolmogorovSmirnovStatistic(RealDistribution dist, double[] data) + throws InsufficientDataException, NullArgumentException { + return KS_TEST.kolmogorovSmirnovStatistic(dist, data); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(RealDistribution, double[]) + * @since 3.3 + */ + public static double kolmogorovSmirnovTest(RealDistribution dist, double[] data) + throws InsufficientDataException, NullArgumentException { + return KS_TEST.kolmogorovSmirnovTest(dist, data); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(RealDistribution, double[], boolean) + * @since 3.3 + */ + public static double kolmogorovSmirnovTest(RealDistribution dist, double[] data, boolean strict) + throws InsufficientDataException, NullArgumentException { + return KS_TEST.kolmogorovSmirnovTest(dist, data, strict); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(RealDistribution, double[], double) + * @since 3.3 + */ + public static boolean kolmogorovSmirnovTest(RealDistribution dist, double[] data, double alpha) + throws InsufficientDataException, NullArgumentException { + return KS_TEST.kolmogorovSmirnovTest(dist, data, alpha); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovStatistic(double[], double[]) + * @since 3.3 + */ + public static double kolmogorovSmirnovStatistic(double[] x, double[] y) + throws InsufficientDataException, NullArgumentException { + return KS_TEST.kolmogorovSmirnovStatistic(x, y); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(double[], double[]) + * @since 3.3 + */ + public static double kolmogorovSmirnovTest(double[] x, double[] y) + throws InsufficientDataException, NullArgumentException { + return KS_TEST.kolmogorovSmirnovTest(x, y); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(double[], double[], boolean) + * @since 3.3 + */ + public static double kolmogorovSmirnovTest(double[] x, double[] y, boolean strict) + throws InsufficientDataException, NullArgumentException { + return KS_TEST.kolmogorovSmirnovTest(x, y, strict); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#exactP(double, int, int, boolean) + * @since 3.3 + */ + public static double exactP(double d, int m, int n, boolean strict) { + return KS_TEST.exactP(d, n, m, strict); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#approximateP(double, int, int) + * @since 3.3 + */ + public static double approximateP(double d, int n, int m) { + return KS_TEST.approximateP(d, n, m); + } + + /** + * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#monteCarloP(double, int, int, boolean, int) + * @since 3.3 + */ + public static double monteCarloP(double d, int n, int m, boolean strict, int iterations) { + return KS_TEST.monteCarloP(d, n, m, strict, iterations); + } + + + // CHECKSTYLE: resume JavadocMethodCheck + +} http://git-wip-us.apache.org/repos/asf/commons-math/blob/5c341d9d/src/main/java/org/apache/commons/math4/stat/inference/TestUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/inference/TestUtils.java b/src/main/java/org/apache/commons/math4/stat/inference/TestUtils.java deleted file mode 100644 index a2bcb3f..0000000 --- a/src/main/java/org/apache/commons/math4/stat/inference/TestUtils.java +++ /dev/null @@ -1,547 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.stat.inference; - -import java.util.Collection; - -import org.apache.commons.math4.distribution.RealDistribution; -import org.apache.commons.math4.exception.ConvergenceException; -import org.apache.commons.math4.exception.DimensionMismatchException; -import org.apache.commons.math4.exception.InsufficientDataException; -import org.apache.commons.math4.exception.MaxCountExceededException; -import org.apache.commons.math4.exception.NoDataException; -import org.apache.commons.math4.exception.NotPositiveException; -import org.apache.commons.math4.exception.NotStrictlyPositiveException; -import org.apache.commons.math4.exception.NullArgumentException; -import org.apache.commons.math4.exception.NumberIsTooSmallException; -import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.exception.ZeroException; -import org.apache.commons.math4.stat.descriptive.StatisticalSummary; - -/** - * A collection of static methods to create inference test instances or to - * perform inference tests. - * - * @since 1.1 - */ -public class TestUtils { - - /** Singleton TTest instance. */ - private static final TTest T_TEST = new TTest(); - - /** Singleton ChiSquareTest instance. */ - private static final ChiSquareTest CHI_SQUARE_TEST = new ChiSquareTest(); - - /** Singleton OneWayAnova instance. */ - private static final OneWayAnova ONE_WAY_ANANOVA = new OneWayAnova(); - - /** Singleton G-Test instance. */ - private static final GTest G_TEST = new GTest(); - - /** Singleton K-S test instance */ - private static final KolmogorovSmirnovTest KS_TEST = new KolmogorovSmirnovTest(); - - /** - * Prevent instantiation. - */ - private TestUtils() { - super(); - } - - // CHECKSTYLE: stop JavadocMethodCheck - - /** - * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticT(double[], double[]) - */ - public static double homoscedasticT(final double[] sample1, final double[] sample2) - throws NullArgumentException, NumberIsTooSmallException { - return T_TEST.homoscedasticT(sample1, sample2); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticT(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary) - */ - public static double homoscedasticT(final StatisticalSummary sampleStats1, - final StatisticalSummary sampleStats2) - throws NullArgumentException, NumberIsTooSmallException { - return T_TEST.homoscedasticT(sampleStats1, sampleStats2); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticTTest(double[], double[], double) - */ - public static boolean homoscedasticTTest(final double[] sample1, final double[] sample2, - final double alpha) - throws NullArgumentException, NumberIsTooSmallException, - OutOfRangeException, MaxCountExceededException { - return T_TEST.homoscedasticTTest(sample1, sample2, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticTTest(double[], double[]) - */ - public static double homoscedasticTTest(final double[] sample1, final double[] sample2) - throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { - return T_TEST.homoscedasticTTest(sample1, sample2); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#homoscedasticTTest(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary) - */ - public static double homoscedasticTTest(final StatisticalSummary sampleStats1, - final StatisticalSummary sampleStats2) - throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { - return T_TEST.homoscedasticTTest(sampleStats1, sampleStats2); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#pairedT(double[], double[]) - */ - public static double pairedT(final double[] sample1, final double[] sample2) - throws NullArgumentException, NoDataException, - DimensionMismatchException, NumberIsTooSmallException { - return T_TEST.pairedT(sample1, sample2); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#pairedTTest(double[], double[], double) - */ - public static boolean pairedTTest(final double[] sample1, final double[] sample2, - final double alpha) - throws NullArgumentException, NoDataException, DimensionMismatchException, - NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { - return T_TEST.pairedTTest(sample1, sample2, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#pairedTTest(double[], double[]) - */ - public static double pairedTTest(final double[] sample1, final double[] sample2) - throws NullArgumentException, NoDataException, DimensionMismatchException, - NumberIsTooSmallException, MaxCountExceededException { - return T_TEST.pairedTTest(sample1, sample2); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#t(double, double[]) - */ - public static double t(final double mu, final double[] observed) - throws NullArgumentException, NumberIsTooSmallException { - return T_TEST.t(mu, observed); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#t(double, org.apache.commons.math4.stat.descriptive.StatisticalSummary) - */ - public static double t(final double mu, final StatisticalSummary sampleStats) - throws NullArgumentException, NumberIsTooSmallException { - return T_TEST.t(mu, sampleStats); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#t(double[], double[]) - */ - public static double t(final double[] sample1, final double[] sample2) - throws NullArgumentException, NumberIsTooSmallException { - return T_TEST.t(sample1, sample2); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#t(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary) - */ - public static double t(final StatisticalSummary sampleStats1, - final StatisticalSummary sampleStats2) - throws NullArgumentException, NumberIsTooSmallException { - return T_TEST.t(sampleStats1, sampleStats2); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#tTest(double, double[], double) - */ - public static boolean tTest(final double mu, final double[] sample, final double alpha) - throws NullArgumentException, NumberIsTooSmallException, - OutOfRangeException, MaxCountExceededException { - return T_TEST.tTest(mu, sample, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#tTest(double, double[]) - */ - public static double tTest(final double mu, final double[] sample) - throws NullArgumentException, NumberIsTooSmallException, - MaxCountExceededException { - return T_TEST.tTest(mu, sample); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#tTest(double, org.apache.commons.math4.stat.descriptive.StatisticalSummary, double) - */ - public static boolean tTest(final double mu, final StatisticalSummary sampleStats, - final double alpha) - throws NullArgumentException, NumberIsTooSmallException, - OutOfRangeException, MaxCountExceededException { - return T_TEST.tTest(mu, sampleStats, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#tTest(double, org.apache.commons.math4.stat.descriptive.StatisticalSummary) - */ - public static double tTest(final double mu, final StatisticalSummary sampleStats) - throws NullArgumentException, NumberIsTooSmallException, - MaxCountExceededException { - return T_TEST.tTest(mu, sampleStats); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#tTest(double[], double[], double) - */ - public static boolean tTest(final double[] sample1, final double[] sample2, - final double alpha) - throws NullArgumentException, NumberIsTooSmallException, - OutOfRangeException, MaxCountExceededException { - return T_TEST.tTest(sample1, sample2, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#tTest(double[], double[]) - */ - public static double tTest(final double[] sample1, final double[] sample2) - throws NullArgumentException, NumberIsTooSmallException, - MaxCountExceededException { - return T_TEST.tTest(sample1, sample2); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#tTest(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary, double) - */ - public static boolean tTest(final StatisticalSummary sampleStats1, - final StatisticalSummary sampleStats2, - final double alpha) - throws NullArgumentException, NumberIsTooSmallException, - OutOfRangeException, MaxCountExceededException { - return T_TEST.tTest(sampleStats1, sampleStats2, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.TTest#tTest(org.apache.commons.math4.stat.descriptive.StatisticalSummary, org.apache.commons.math4.stat.descriptive.StatisticalSummary) - */ - public static double tTest(final StatisticalSummary sampleStats1, - final StatisticalSummary sampleStats2) - throws NullArgumentException, NumberIsTooSmallException, - MaxCountExceededException { - return T_TEST.tTest(sampleStats1, sampleStats2); - } - - /** - * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquare(double[], long[]) - */ - public static double chiSquare(final double[] expected, final long[] observed) - throws NotPositiveException, NotStrictlyPositiveException, - DimensionMismatchException { - return CHI_SQUARE_TEST.chiSquare(expected, observed); - } - - /** - * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquare(long[][]) - */ - public static double chiSquare(final long[][] counts) - throws NullArgumentException, NotPositiveException, - DimensionMismatchException { - return CHI_SQUARE_TEST.chiSquare(counts); - } - - /** - * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTest(double[], long[], double) - */ - public static boolean chiSquareTest(final double[] expected, final long[] observed, - final double alpha) - throws NotPositiveException, NotStrictlyPositiveException, - DimensionMismatchException, OutOfRangeException, MaxCountExceededException { - return CHI_SQUARE_TEST.chiSquareTest(expected, observed, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTest(double[], long[]) - */ - public static double chiSquareTest(final double[] expected, final long[] observed) - throws NotPositiveException, NotStrictlyPositiveException, - DimensionMismatchException, MaxCountExceededException { - return CHI_SQUARE_TEST.chiSquareTest(expected, observed); - } - - /** - * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTest(long[][], double) - */ - public static boolean chiSquareTest(final long[][] counts, final double alpha) - throws NullArgumentException, DimensionMismatchException, - NotPositiveException, OutOfRangeException, MaxCountExceededException { - return CHI_SQUARE_TEST.chiSquareTest(counts, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTest(long[][]) - */ - public static double chiSquareTest(final long[][] counts) - throws NullArgumentException, DimensionMismatchException, - NotPositiveException, MaxCountExceededException { - return CHI_SQUARE_TEST.chiSquareTest(counts); - } - - /** - * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareDataSetsComparison(long[], long[]) - * - * @since 1.2 - */ - public static double chiSquareDataSetsComparison(final long[] observed1, - final long[] observed2) - throws DimensionMismatchException, NotPositiveException, ZeroException { - return CHI_SQUARE_TEST.chiSquareDataSetsComparison(observed1, observed2); - } - - /** - * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[]) - * - * @since 1.2 - */ - public static double chiSquareTestDataSetsComparison(final long[] observed1, - final long[] observed2) - throws DimensionMismatchException, NotPositiveException, ZeroException, - MaxCountExceededException { - return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2); - } - - /** - * @see org.apache.commons.math4.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[], double) - * - * @since 1.2 - */ - public static boolean chiSquareTestDataSetsComparison(final long[] observed1, - final long[] observed2, - final double alpha) - throws DimensionMismatchException, NotPositiveException, - ZeroException, OutOfRangeException, MaxCountExceededException { - return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.OneWayAnova#anovaFValue(Collection) - * - * @since 1.2 - */ - public static double oneWayAnovaFValue(final Collection<double[]> categoryData) - throws NullArgumentException, DimensionMismatchException { - return ONE_WAY_ANANOVA.anovaFValue(categoryData); - } - - /** - * @see org.apache.commons.math4.stat.inference.OneWayAnova#anovaPValue(Collection) - * - * @since 1.2 - */ - public static double oneWayAnovaPValue(final Collection<double[]> categoryData) - throws NullArgumentException, DimensionMismatchException, - ConvergenceException, MaxCountExceededException { - return ONE_WAY_ANANOVA.anovaPValue(categoryData); - } - - /** - * @see org.apache.commons.math4.stat.inference.OneWayAnova#anovaTest(Collection,double) - * - * @since 1.2 - */ - public static boolean oneWayAnovaTest(final Collection<double[]> categoryData, - final double alpha) - throws NullArgumentException, DimensionMismatchException, - OutOfRangeException, ConvergenceException, MaxCountExceededException { - return ONE_WAY_ANANOVA.anovaTest(categoryData, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.GTest#g(double[], long[]) - * @since 3.1 - */ - public static double g(final double[] expected, final long[] observed) - throws NotPositiveException, NotStrictlyPositiveException, - DimensionMismatchException { - return G_TEST.g(expected, observed); - } - - /** - * @see org.apache.commons.math4.stat.inference.GTest#gTest( double[], long[] ) - * @since 3.1 - */ - public static double gTest(final double[] expected, final long[] observed) - throws NotPositiveException, NotStrictlyPositiveException, - DimensionMismatchException, MaxCountExceededException { - return G_TEST.gTest(expected, observed); - } - - /** - * @see org.apache.commons.math4.stat.inference.GTest#gTestIntrinsic(double[], long[] ) - * @since 3.1 - */ - public static double gTestIntrinsic(final double[] expected, final long[] observed) - throws NotPositiveException, NotStrictlyPositiveException, - DimensionMismatchException, MaxCountExceededException { - return G_TEST.gTestIntrinsic(expected, observed); - } - - /** - * @see org.apache.commons.math4.stat.inference.GTest#gTest( double[],long[],double) - * @since 3.1 - */ - public static boolean gTest(final double[] expected, final long[] observed, - final double alpha) - throws NotPositiveException, NotStrictlyPositiveException, - DimensionMismatchException, OutOfRangeException, MaxCountExceededException { - return G_TEST.gTest(expected, observed, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.GTest#gDataSetsComparison(long[], long[]) - * @since 3.1 - */ - public static double gDataSetsComparison(final long[] observed1, - final long[] observed2) - throws DimensionMismatchException, NotPositiveException, ZeroException { - return G_TEST.gDataSetsComparison(observed1, observed2); - } - - /** - * @see org.apache.commons.math4.stat.inference.GTest#rootLogLikelihoodRatio(long, long, long, long) - * @since 3.1 - */ - public static double rootLogLikelihoodRatio(final long k11, final long k12, final long k21, final long k22) - throws DimensionMismatchException, NotPositiveException, ZeroException { - return G_TEST.rootLogLikelihoodRatio(k11, k12, k21, k22); - } - - - /** - * @see org.apache.commons.math4.stat.inference.GTest#gTestDataSetsComparison(long[], long[]) - * @since 3.1 - */ - public static double gTestDataSetsComparison(final long[] observed1, - final long[] observed2) - throws DimensionMismatchException, NotPositiveException, ZeroException, - MaxCountExceededException { - return G_TEST.gTestDataSetsComparison(observed1, observed2); - } - - /** - * @see org.apache.commons.math4.stat.inference.GTest#gTestDataSetsComparison(long[],long[],double) - * @since 3.1 - */ - public static boolean gTestDataSetsComparison(final long[] observed1, - final long[] observed2, - final double alpha) - throws DimensionMismatchException, NotPositiveException, - ZeroException, OutOfRangeException, MaxCountExceededException { - return G_TEST.gTestDataSetsComparison(observed1, observed2, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovStatistic(RealDistribution, double[]) - * @since 3.3 - */ - public static double kolmogorovSmirnovStatistic(RealDistribution dist, double[] data) - throws InsufficientDataException, NullArgumentException { - return KS_TEST.kolmogorovSmirnovStatistic(dist, data); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(RealDistribution, double[]) - * @since 3.3 - */ - public static double kolmogorovSmirnovTest(RealDistribution dist, double[] data) - throws InsufficientDataException, NullArgumentException { - return KS_TEST.kolmogorovSmirnovTest(dist, data); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(RealDistribution, double[], boolean) - * @since 3.3 - */ - public static double kolmogorovSmirnovTest(RealDistribution dist, double[] data, boolean strict) - throws InsufficientDataException, NullArgumentException { - return KS_TEST.kolmogorovSmirnovTest(dist, data, strict); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(RealDistribution, double[], double) - * @since 3.3 - */ - public static boolean kolmogorovSmirnovTest(RealDistribution dist, double[] data, double alpha) - throws InsufficientDataException, NullArgumentException { - return KS_TEST.kolmogorovSmirnovTest(dist, data, alpha); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovStatistic(double[], double[]) - * @since 3.3 - */ - public static double kolmogorovSmirnovStatistic(double[] x, double[] y) - throws InsufficientDataException, NullArgumentException { - return KS_TEST.kolmogorovSmirnovStatistic(x, y); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(double[], double[]) - * @since 3.3 - */ - public static double kolmogorovSmirnovTest(double[] x, double[] y) - throws InsufficientDataException, NullArgumentException { - return KS_TEST.kolmogorovSmirnovTest(x, y); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#kolmogorovSmirnovTest(double[], double[], boolean) - * @since 3.3 - */ - public static double kolmogorovSmirnovTest(double[] x, double[] y, boolean strict) - throws InsufficientDataException, NullArgumentException { - return KS_TEST.kolmogorovSmirnovTest(x, y, strict); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#exactP(double, int, int, boolean) - * @since 3.3 - */ - public static double exactP(double d, int m, int n, boolean strict) { - return KS_TEST.exactP(d, n, m, strict); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#approximateP(double, int, int) - * @since 3.3 - */ - public static double approximateP(double d, int n, int m) { - return KS_TEST.approximateP(d, n, m); - } - - /** - * @see org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest#monteCarloP(double, int, int, boolean, int) - * @since 3.3 - */ - public static double monteCarloP(double d, int n, int m, boolean strict, int iterations) { - return KS_TEST.monteCarloP(d, n, m, strict, iterations); - } - - - // CHECKSTYLE: resume JavadocMethodCheck - -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/5c341d9d/src/test/java/org/apache/commons/math4/distribution/BetaDistributionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/distribution/BetaDistributionTest.java b/src/test/java/org/apache/commons/math4/distribution/BetaDistributionTest.java index 03054b8..b7859ba 100644 --- a/src/test/java/org/apache/commons/math4/distribution/BetaDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/distribution/BetaDistributionTest.java @@ -18,12 +18,11 @@ package org.apache.commons.math4.distribution; import java.util.Arrays; -import org.apache.commons.math4.distribution.BetaDistribution; import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.stat.StatUtils; import org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest; -import org.apache.commons.math4.stat.inference.TestUtils; +import org.apache.commons.math4.stat.inference.InferenceTestUtils; import org.junit.Assert; import org.junit.Test; @@ -325,7 +324,7 @@ public class BetaDistributionTest { for (final double beta : alphaBetas) { final BetaDistribution betaDistribution = new BetaDistribution(alpha, beta); final double[] observed = AbstractRealDistribution.sample(numSamples, - betaDistribution.createSampler(rng)); + betaDistribution.createSampler(rng)); Arrays.sort(observed); final String distribution = String.format("Beta(%.2f, %.2f)", alpha, beta); @@ -380,6 +379,6 @@ public class BetaDistributionTest { final double[] expected = new double[numBins]; Arrays.fill(expected, (double) values.length / numBins); - return TestUtils.gTest(expected, observed); + return InferenceTestUtils.gTest(expected, observed); } } http://git-wip-us.apache.org/repos/asf/commons-math/blob/5c341d9d/src/test/java/org/apache/commons/math4/stat/inference/InferenceTestUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/stat/inference/InferenceTestUtilsTest.java b/src/test/java/org/apache/commons/math4/stat/inference/InferenceTestUtilsTest.java new file mode 100644 index 0000000..e2d5ee1 --- /dev/null +++ b/src/test/java/org/apache/commons/math4/stat/inference/InferenceTestUtilsTest.java @@ -0,0 +1,557 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math4.stat.inference; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.math4.distribution.NormalDistribution; +import org.apache.commons.math4.exception.DimensionMismatchException; +import org.apache.commons.math4.exception.NotPositiveException; +import org.apache.commons.math4.exception.NotStrictlyPositiveException; +import org.apache.commons.math4.exception.NullArgumentException; +import org.apache.commons.math4.exception.NumberIsTooSmallException; +import org.apache.commons.math4.exception.OutOfRangeException; +import org.apache.commons.math4.stat.descriptive.SummaryStatistics; +import org.apache.commons.math4.util.FastMath; +import org.junit.Assert; +import org.junit.Test; + + +/** + * Test cases for the TestUtils class. + * + */ +public class InferenceTestUtilsTest { + + @Test + public void testChiSquare() { + + // Target values computed using R version 1.8.1 + // Some assembly required ;-) + // Use sum((obs - exp)^2/exp) for the chi-square statistic and + // 1 - pchisq(sum((obs - exp)^2/exp), length(obs) - 1) for the p-value + + long[] observed = {10, 9, 11}; + double[] expected = {10, 10, 10}; + Assert.assertEquals("chi-square statistic", 0.2, InferenceTestUtils.chiSquare(expected, observed), 10E-12); + Assert.assertEquals("chi-square p-value", 0.904837418036, InferenceTestUtils.chiSquareTest(expected, observed), 1E-10); + + long[] observed1 = { 500, 623, 72, 70, 31 }; + double[] expected1 = { 485, 541, 82, 61, 37 }; + Assert.assertEquals( "chi-square test statistic", 9.023307936427388, InferenceTestUtils.chiSquare(expected1, observed1), 1E-10); + Assert.assertEquals("chi-square p-value", 0.06051952647453607, InferenceTestUtils.chiSquareTest(expected1, observed1), 1E-9); + Assert.assertTrue("chi-square test reject", InferenceTestUtils.chiSquareTest(expected1, observed1, 0.07)); + Assert.assertTrue("chi-square test accept", !InferenceTestUtils.chiSquareTest(expected1, observed1, 0.05)); + + try { + InferenceTestUtils.chiSquareTest(expected1, observed1, 95); + Assert.fail("alpha out of range, OutOfRangeException expected"); + } catch (OutOfRangeException ex) { + // expected + } + + long[] tooShortObs = { 0 }; + double[] tooShortEx = { 1 }; + try { + InferenceTestUtils.chiSquare(tooShortEx, tooShortObs); + Assert.fail("arguments too short, DimensionMismatchException expected"); + } catch (DimensionMismatchException ex) { + // expected + } + + // unmatched arrays + long[] unMatchedObs = { 0, 1, 2, 3 }; + double[] unMatchedEx = { 1, 1, 2 }; + try { + InferenceTestUtils.chiSquare(unMatchedEx, unMatchedObs); + Assert.fail("arrays have different lengths, DimensionMismatchException expected"); + } catch (DimensionMismatchException ex) { + // expected + } + + // 0 expected count + expected[0] = 0; + try { + InferenceTestUtils.chiSquareTest(expected, observed, .01); + Assert.fail("bad expected count, NotStrictlyPositiveException expected"); + } catch (NotStrictlyPositiveException ex) { + // expected + } + + // negative observed count + expected[0] = 1; + observed[0] = -1; + try { + InferenceTestUtils.chiSquareTest(expected, observed, .01); + Assert.fail("bad expected count, NotPositiveException expected"); + } catch (NotPositiveException ex) { + // expected + } + + } + + @Test + public void testChiSquareIndependence() { + + // Target values computed using R version 1.8.1 + + long[][] counts = { {40, 22, 43}, {91, 21, 28}, {60, 10, 22}}; + Assert.assertEquals( "chi-square test statistic", 22.709027688, InferenceTestUtils.chiSquare(counts), 1E-9); + Assert.assertEquals("chi-square p-value", 0.000144751460134, InferenceTestUtils.chiSquareTest(counts), 1E-9); + Assert.assertTrue("chi-square test reject", InferenceTestUtils.chiSquareTest(counts, 0.0002)); + Assert.assertTrue("chi-square test accept", !InferenceTestUtils.chiSquareTest(counts, 0.0001)); + + long[][] counts2 = {{10, 15}, {30, 40}, {60, 90} }; + Assert.assertEquals( "chi-square test statistic", 0.168965517241, InferenceTestUtils.chiSquare(counts2), 1E-9); + Assert.assertEquals("chi-square p-value",0.918987499852, InferenceTestUtils.chiSquareTest(counts2), 1E-9); + Assert.assertTrue("chi-square test accept", !InferenceTestUtils.chiSquareTest(counts2, 0.1)); + + // ragged input array + long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}}; + try { + InferenceTestUtils.chiSquare(counts3); + Assert.fail("Expecting DimensionMismatchException"); + } catch (DimensionMismatchException ex) { + // expected + } + + // insufficient data + long[][] counts4 = {{40, 22, 43}}; + try { + InferenceTestUtils.chiSquare(counts4); + Assert.fail("Expecting DimensionMismatchException"); + } catch (DimensionMismatchException ex) { + // expected + } + long[][] counts5 = {{40}, {40}, {30}, {10}}; + try { + InferenceTestUtils.chiSquare(counts5); + Assert.fail("Expecting DimensionMismatchException"); + } catch (DimensionMismatchException ex) { + // expected + } + + // negative counts + long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} }; + try { + InferenceTestUtils.chiSquare(counts6); + Assert.fail("Expecting NotPositiveException"); + } catch (NotPositiveException ex) { + // expected + } + + // bad alpha + try { + InferenceTestUtils.chiSquareTest(counts, 0); + Assert.fail("Expecting OutOfRangeException"); + } catch (OutOfRangeException ex) { + // expected + } + } + + @Test + public void testChiSquareLargeTestStatistic() { + double[] exp = new double[] { + 3389119.5, 649136.6, 285745.4, 25357364.76, 11291189.78, 543628.0, + 232921.0, 437665.75 + }; + + long[] obs = new long[] { + 2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899 + }; + org.apache.commons.math4.stat.inference.ChiSquareTest csti = + new org.apache.commons.math4.stat.inference.ChiSquareTest(); + double cst = csti.chiSquareTest(exp, obs); + Assert.assertEquals("chi-square p-value", 0.0, cst, 1E-3); + Assert.assertEquals( "chi-square test statistic", + 114875.90421929007, InferenceTestUtils.chiSquare(exp, obs), 1E-9); + } + + /** Contingency table containing zeros - PR # 32531 */ + @Test + public void testChiSquareZeroCount() { + // Target values computed using R version 1.8.1 + long[][] counts = { {40, 0, 4}, {91, 1, 2}, {60, 2, 0}}; + Assert.assertEquals( "chi-square test statistic", 9.67444662263, + InferenceTestUtils.chiSquare(counts), 1E-9); + Assert.assertEquals("chi-square p-value", 0.0462835770603, + InferenceTestUtils.chiSquareTest(counts), 1E-9); + } + + private double[] tooShortObs = { 1.0 }; + private double[] emptyObs = {}; + private SummaryStatistics emptyStats = new SummaryStatistics(); + + @Test + public void testOneSampleT() { + double[] observed = + {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0 }; + double mu = 100.0; + SummaryStatistics sampleStats = null; + sampleStats = new SummaryStatistics(); + for (int i = 0; i < observed.length; i++) { + sampleStats.addValue(observed[i]); + } + + // Target comparison values computed using R version 1.8.1 (Linux version) + Assert.assertEquals("t statistic", -2.81976445346, + InferenceTestUtils.t(mu, observed), 10E-10); + Assert.assertEquals("t statistic", -2.81976445346, + InferenceTestUtils.t(mu, sampleStats), 10E-10); + Assert.assertEquals("p value", 0.0136390585873, + InferenceTestUtils.tTest(mu, observed), 10E-10); + Assert.assertEquals("p value", 0.0136390585873, + InferenceTestUtils.tTest(mu, sampleStats), 10E-10); + + try { + InferenceTestUtils.t(mu, (double[]) null); + Assert.fail("arguments too short, NullArgumentException expected"); + } catch (NullArgumentException ex) { + // expected + } + + try { + InferenceTestUtils.t(mu, (SummaryStatistics) null); + Assert.fail("arguments too short, NullArgumentException expected"); + } catch (NullArgumentException ex) { + // expected + } + + try { + InferenceTestUtils.t(mu, emptyObs); + Assert.fail("arguments too short, NumberIsTooSmallException expected"); + } catch (NumberIsTooSmallException ex) { + // expected + } + + try { + InferenceTestUtils.t(mu, emptyStats); + Assert.fail("arguments too short, NumberIsTooSmallException expected"); + } catch (NumberIsTooSmallException ex) { + // expected + } + + try { + InferenceTestUtils.t(mu, tooShortObs); + Assert.fail("insufficient data to compute t statistic, NumberIsTooSmallException expected"); + } catch (NumberIsTooSmallException ex) { + // expected + } + try { + InferenceTestUtils.tTest(mu, tooShortObs); + Assert.fail("insufficient data to perform t test, NumberIsTooSmallException expected"); + } catch (NumberIsTooSmallException ex) { + // expected + } + + try { + InferenceTestUtils.t(mu, (SummaryStatistics) null); + Assert.fail("insufficient data to compute t statistic, NullArgumentException expected"); + } catch (NullArgumentException ex) { + // expected + } + try { + InferenceTestUtils.tTest(mu, (SummaryStatistics) null); + Assert.fail("insufficient data to perform t test, NullArgumentException expected"); + } catch (NullArgumentException ex) { + // expected + } + } + + @Test + public void testOneSampleTTest() { + double[] oneSidedP = + {2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d, 6d, 6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d }; + SummaryStatistics oneSidedPStats = new SummaryStatistics(); + for (int i = 0; i < oneSidedP.length; i++) { + oneSidedPStats.addValue(oneSidedP[i]); + } + // Target comparison values computed using R version 1.8.1 (Linux version) + Assert.assertEquals("one sample t stat", 3.86485535541, + InferenceTestUtils.t(0d, oneSidedP), 10E-10); + Assert.assertEquals("one sample t stat", 3.86485535541, + InferenceTestUtils.t(0d, oneSidedPStats),1E-10); + Assert.assertEquals("one sample p value", 0.000521637019637, + InferenceTestUtils.tTest(0d, oneSidedP) / 2d, 10E-10); + Assert.assertEquals("one sample p value", 0.000521637019637, + InferenceTestUtils.tTest(0d, oneSidedPStats) / 2d, 10E-5); + Assert.assertTrue("one sample t-test reject", InferenceTestUtils.tTest(0d, oneSidedP, 0.01)); + Assert.assertTrue("one sample t-test reject", InferenceTestUtils.tTest(0d, oneSidedPStats, 0.01)); + Assert.assertTrue("one sample t-test accept", !InferenceTestUtils.tTest(0d, oneSidedP, 0.0001)); + Assert.assertTrue("one sample t-test accept", !InferenceTestUtils.tTest(0d, oneSidedPStats, 0.0001)); + + try { + InferenceTestUtils.tTest(0d, oneSidedP, 95); + Assert.fail("alpha out of range, OutOfRangeException expected"); + } catch (OutOfRangeException ex) { + // expected + } + + try { + InferenceTestUtils.tTest(0d, oneSidedPStats, 95); + Assert.fail("alpha out of range, OutOfRangeException expected"); + } catch (OutOfRangeException ex) { + // expected + } + + } + + @Test + public void testTwoSampleTHeterscedastic() { + double[] sample1 = { 7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d }; + double[] sample2 = { -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d }; + SummaryStatistics sampleStats1 = new SummaryStatistics(); + for (int i = 0; i < sample1.length; i++) { + sampleStats1.addValue(sample1[i]); + } + SummaryStatistics sampleStats2 = new SummaryStatistics(); + for (int i = 0; i < sample2.length; i++) { + sampleStats2.addValue(sample2[i]); + } + + // Target comparison values computed using R version 1.8.1 (Linux version) + Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, + InferenceTestUtils.t(sample1, sample2), 1E-10); + Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, + InferenceTestUtils.t(sampleStats1, sampleStats2), 1E-10); + Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, + InferenceTestUtils.tTest(sample1, sample2), 1E-10); + Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, + InferenceTestUtils.tTest(sampleStats1, sampleStats2), 1E-10); + Assert.assertTrue("two sample heteroscedastic t-test reject", + InferenceTestUtils.tTest(sample1, sample2, 0.2)); + Assert.assertTrue("two sample heteroscedastic t-test reject", + InferenceTestUtils.tTest(sampleStats1, sampleStats2, 0.2)); + Assert.assertTrue("two sample heteroscedastic t-test accept", + !InferenceTestUtils.tTest(sample1, sample2, 0.1)); + Assert.assertTrue("two sample heteroscedastic t-test accept", + !InferenceTestUtils.tTest(sampleStats1, sampleStats2, 0.1)); + + try { + InferenceTestUtils.tTest(sample1, sample2, .95); + Assert.fail("alpha out of range, OutOfRangeException expected"); + } catch (OutOfRangeException ex) { + // expected + } + + try { + InferenceTestUtils.tTest(sampleStats1, sampleStats2, .95); + Assert.fail("alpha out of range, OutOfRangeException expected"); + } catch (OutOfRangeException ex) { + // expected + } + + try { + InferenceTestUtils.tTest(sample1, tooShortObs, .01); + Assert.fail("insufficient data, NumberIsTooSmallException expected"); + } catch (NumberIsTooSmallException ex) { + // expected + } + + try { + InferenceTestUtils.tTest(sampleStats1, (SummaryStatistics) null, .01); + Assert.fail("insufficient data, NullArgumentException expected"); + } catch (NullArgumentException ex) { + // expected + } + + try { + InferenceTestUtils.tTest(sample1, tooShortObs); + Assert.fail("insufficient data, NumberIsTooSmallException expected"); + } catch (NumberIsTooSmallException ex) { + // expected + } + + try { + InferenceTestUtils.tTest(sampleStats1, (SummaryStatistics) null); + Assert.fail("insufficient data, NullArgumentException expected"); + } catch (NullArgumentException ex) { + // expected + } + + try { + InferenceTestUtils.t(sample1, tooShortObs); + Assert.fail("insufficient data, NumberIsTooSmallException expected"); + } catch (NumberIsTooSmallException ex) { + // expected + } + + try { + InferenceTestUtils.t(sampleStats1, (SummaryStatistics) null); + Assert.fail("insufficient data, NullArgumentException expected"); + } catch (NullArgumentException ex) { + // expected + } + } + @Test + public void testTwoSampleTHomoscedastic() { + double[] sample1 ={2, 4, 6, 8, 10, 97}; + double[] sample2 = {4, 6, 8, 10, 16}; + SummaryStatistics sampleStats1 = new SummaryStatistics(); + for (int i = 0; i < sample1.length; i++) { + sampleStats1.addValue(sample1[i]); + } + SummaryStatistics sampleStats2 = new SummaryStatistics(); + for (int i = 0; i < sample2.length; i++) { + sampleStats2.addValue(sample2[i]); + } + + // Target comparison values computed using R version 1.8.1 (Linux version) + Assert.assertEquals("two sample homoscedastic t stat", 0.73096310086, + InferenceTestUtils.homoscedasticT(sample1, sample2), 10E-11); + Assert.assertEquals("two sample homoscedastic p value", 0.4833963785, + InferenceTestUtils.homoscedasticTTest(sampleStats1, sampleStats2), 1E-10); + Assert.assertTrue("two sample homoscedastic t-test reject", + InferenceTestUtils.homoscedasticTTest(sample1, sample2, 0.49)); + Assert.assertTrue("two sample homoscedastic t-test accept", + !InferenceTestUtils.homoscedasticTTest(sample1, sample2, 0.48)); + } + + @Test + public void testSmallSamples() { + double[] sample1 = {1d, 3d}; + double[] sample2 = {4d, 5d}; + + // Target values computed using R, version 1.8.1 (linux version) + Assert.assertEquals(-2.2360679775, InferenceTestUtils.t(sample1, sample2), + 1E-10); + Assert.assertEquals(0.198727388935, InferenceTestUtils.tTest(sample1, sample2), + 1E-10); + } + + @Test + public void testPaired() { + double[] sample1 = {1d, 3d, 5d, 7d}; + double[] sample2 = {0d, 6d, 11d, 2d}; + double[] sample3 = {5d, 7d, 8d, 10d}; + + // Target values computed using R, version 1.8.1 (linux version) + Assert.assertEquals(-0.3133, InferenceTestUtils.pairedT(sample1, sample2), 1E-4); + Assert.assertEquals(0.774544295819, InferenceTestUtils.pairedTTest(sample1, sample2), 1E-10); + Assert.assertEquals(0.001208, InferenceTestUtils.pairedTTest(sample1, sample3), 1E-6); + Assert.assertFalse(InferenceTestUtils.pairedTTest(sample1, sample3, .001)); + Assert.assertTrue(InferenceTestUtils.pairedTTest(sample1, sample3, .002)); + } + + private double[] classA = + {93.0, 103.0, 95.0, 101.0}; + private double[] classB = + {99.0, 92.0, 102.0, 100.0, 102.0}; + private double[] classC = + {110.0, 115.0, 111.0, 117.0, 128.0}; + + private List<double[]> classes = new ArrayList<double[]>(); + private OneWayAnova oneWayAnova = new OneWayAnova(); + + @Test + public void testOneWayAnovaUtils() { + classes.add(classA); + classes.add(classB); + classes.add(classC); + Assert.assertEquals(oneWayAnova.anovaFValue(classes), + InferenceTestUtils.oneWayAnovaFValue(classes), 10E-12); + Assert.assertEquals(oneWayAnova.anovaPValue(classes), + InferenceTestUtils.oneWayAnovaPValue(classes), 10E-12); + Assert.assertEquals(oneWayAnova.anovaTest(classes, 0.01), + InferenceTestUtils.oneWayAnovaTest(classes, 0.01)); + } + @Test + public void testGTestGoodnesOfFit() throws Exception { + double[] exp = new double[]{ + 0.54d, 0.40d, 0.05d, 0.01d + }; + + long[] obs = new long[]{ + 70, 79, 3, 4 + }; + Assert.assertEquals("G test statistic", + 13.144799, InferenceTestUtils.g(exp, obs), 1E-5); + double p_gtgf = InferenceTestUtils.gTest(exp, obs); + Assert.assertEquals("g-Test p-value", 0.004333, p_gtgf, 1E-5); + + Assert.assertTrue(InferenceTestUtils.gTest(exp, obs, 0.05)); +} + + @Test + public void testGTestIndependance() throws Exception { + long[] obs1 = new long[]{ + 268, 199, 42 + }; + + long[] obs2 = new long[]{ + 807, 759, 184 + }; + + double g = InferenceTestUtils.gDataSetsComparison(obs1, obs2); + + Assert.assertEquals("G test statistic", + 7.3008170, g, 1E-4); + double p_gti = InferenceTestUtils.gTestDataSetsComparison(obs1, obs2); + + Assert.assertEquals("g-Test p-value", 0.0259805, p_gti, 1E-4); + Assert.assertTrue(InferenceTestUtils.gTestDataSetsComparison(obs1, obs2, 0.05)); + } + + @Test + public void testRootLogLikelihood() { + // positive where k11 is bigger than expected. + Assert.assertTrue(InferenceTestUtils.rootLogLikelihoodRatio(904, 21060, 1144, 283012) > 0.0); + + // negative because k11 is lower than expected + Assert.assertTrue(InferenceTestUtils.rootLogLikelihoodRatio(36, 21928, 60280, 623876) < 0.0); + + Assert.assertEquals(FastMath.sqrt(2.772589), InferenceTestUtils.rootLogLikelihoodRatio(1, 0, 0, 1), 0.000001); + Assert.assertEquals(-FastMath.sqrt(2.772589), InferenceTestUtils.rootLogLikelihoodRatio(0, 1, 1, 0), 0.000001); + Assert.assertEquals(FastMath.sqrt(27.72589), InferenceTestUtils.rootLogLikelihoodRatio(10, 0, 0, 10), 0.00001); + + Assert.assertEquals(FastMath.sqrt(39.33052), InferenceTestUtils.rootLogLikelihoodRatio(5, 1995, 0, 100000), 0.00001); + Assert.assertEquals(-FastMath.sqrt(39.33052), InferenceTestUtils.rootLogLikelihoodRatio(0, 100000, 5, 1995), 0.00001); + + Assert.assertEquals(FastMath.sqrt(4730.737), InferenceTestUtils.rootLogLikelihoodRatio(1000, 1995, 1000, 100000), 0.001); + Assert.assertEquals(-FastMath.sqrt(4730.737), InferenceTestUtils.rootLogLikelihoodRatio(1000, 100000, 1000, 1995), 0.001); + + Assert.assertEquals(FastMath.sqrt(5734.343), InferenceTestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 100000), 0.001); + Assert.assertEquals(FastMath.sqrt(5714.932), InferenceTestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 99000), 0.001); + } + + @Test + public void testKSOneSample() throws Exception { + final NormalDistribution unitNormal = new NormalDistribution(0d, 1d); + final double[] sample = KolmogorovSmirnovTestTest.gaussian; + final double tol = KolmogorovSmirnovTestTest.TOLERANCE; + Assert.assertEquals(0.3172069207622391, InferenceTestUtils.kolmogorovSmirnovTest(unitNormal, sample), tol); + Assert.assertEquals(0.0932947561266756, InferenceTestUtils.kolmogorovSmirnovStatistic(unitNormal, sample), tol); + } + + @Test + public void testKSTwoSample() throws Exception { + final double tol = KolmogorovSmirnovTestTest.TOLERANCE; + final double[] smallSample1 = { + 6, 7, 9, 13, 19, 21, 22, 23, 24 + }; + final double[] smallSample2 = { + 10, 11, 12, 16, 20, 27, 28, 32, 44, 54 + }; + Assert + .assertEquals(0.105577085453247, InferenceTestUtils.kolmogorovSmirnovTest(smallSample1, smallSample2, false), tol); + final double d = InferenceTestUtils.kolmogorovSmirnovStatistic(smallSample1, smallSample2); + Assert.assertEquals(0.5, d, tol); + Assert + .assertEquals(0.105577085453247, InferenceTestUtils.exactP(d, smallSample1.length, smallSample2.length, false), tol); + } +}
