Changed G-test method names to follow conventions in the inference package. JIRA: MATH-878.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1408172 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
16acabd53f
commit
f1dac3079a
|
@ -74,7 +74,7 @@ public class GTest {
|
|||
* @throws DimensionMismatchException if the array lengths do not match or
|
||||
* are less than 2.
|
||||
*/
|
||||
public double gValueGoodnessOfFit(final double[] expected, final long[] observed)
|
||||
public double g(final double[] expected, final long[] observed)
|
||||
throws NotPositiveException, NotStrictlyPositiveException,
|
||||
DimensionMismatchException {
|
||||
|
||||
|
@ -120,7 +120,7 @@ public class GTest {
|
|||
* frequency distribution described by the expected counts.</p>
|
||||
*
|
||||
* <p>The probability returned is the tail probability beyond
|
||||
* {@link #gValueGoodnessOfFit(double[], long[]) gValueGoodnessOfFit(expected, observed)}
|
||||
* {@link #g(double[], long[]) gValueGoodnessOfFit(expected, observed)}
|
||||
* in the ChiSquare distribution with degrees of freedom one less than the
|
||||
* common length of {@code expected} and {@code observed}.</p>
|
||||
*
|
||||
|
@ -149,14 +149,14 @@ public class GTest {
|
|||
* @throws MaxCountExceededException if an error occurs computing the
|
||||
* p-value.
|
||||
*/
|
||||
public double gTestGoodnessOfFitPValue(final double[] expected, final long[] observed)
|
||||
public double gTest(final double[] expected, final long[] observed)
|
||||
throws NotPositiveException, NotStrictlyPositiveException,
|
||||
DimensionMismatchException, MaxCountExceededException {
|
||||
|
||||
final ChiSquaredDistribution distribution =
|
||||
new ChiSquaredDistribution(expected.length - 1.0);
|
||||
return 1.0 - distribution.cumulativeProbability(
|
||||
gValueGoodnessOfFit(expected, observed));
|
||||
g(expected, observed));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -165,7 +165,7 @@ public class GTest {
|
|||
* (2nd ed.). Sparky House Publishing, Baltimore, Maryland.
|
||||
*
|
||||
* <p> The probability returned is the tail probability beyond
|
||||
* {@link #gValueGoodnessOfFit(double[], long[]) gValueGoodnessOfFit(expected, observed)}
|
||||
* {@link #g(double[], long[]) gValueGoodnessOfFit(expected, observed)}
|
||||
* in the ChiSquare distribution with degrees of freedom two less than the
|
||||
* common length of {@code expected} and {@code observed}.</p>
|
||||
*
|
||||
|
@ -180,14 +180,14 @@ public class GTest {
|
|||
* @throws MaxCountExceededException if an error occurs computing the
|
||||
* p-value.
|
||||
*/
|
||||
public double gTestGoodnessOfFitIntrinsicPValue(final double[] expected, final long[] observed)
|
||||
public double gTestIntrinsic(final double[] expected, final long[] observed)
|
||||
throws NotPositiveException, NotStrictlyPositiveException,
|
||||
DimensionMismatchException, MaxCountExceededException {
|
||||
|
||||
final ChiSquaredDistribution distribution =
|
||||
new ChiSquaredDistribution(expected.length - 2.0);
|
||||
return 1.0 - distribution.cumulativeProbability(
|
||||
gValueGoodnessOfFit(expected, observed));
|
||||
g(expected, observed));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -202,7 +202,7 @@ public class GTest {
|
|||
* use </p><p>
|
||||
* {@code gTest(expected, observed, 0.01)}</p>
|
||||
*
|
||||
* <p>Returns true iff {@link #gTestGoodnessOfFitPValue(double[], long[])
|
||||
* <p>Returns true iff {@link #gTest(double[], long[])
|
||||
* gTestGoodnessOfFitPValue(expected, observed)} < alpha</p>
|
||||
*
|
||||
* <p><strong>Preconditions</strong>: <ul>
|
||||
|
@ -234,7 +234,7 @@ public class GTest {
|
|||
* @throws OutOfRangeException if alpha is not strictly greater than zero
|
||||
* and less than or equal to 0.5
|
||||
*/
|
||||
public boolean gTestGoodnessOfFit(final double[] expected, final long[] observed,
|
||||
public boolean gTest(final double[] expected, final long[] observed,
|
||||
final double alpha)
|
||||
throws NotPositiveException, NotStrictlyPositiveException,
|
||||
DimensionMismatchException, OutOfRangeException, MaxCountExceededException {
|
||||
|
@ -243,7 +243,7 @@ public class GTest {
|
|||
throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL,
|
||||
alpha, 0, 0.5);
|
||||
}
|
||||
return gTestGoodnessOfFitPValue(expected, observed) < alpha;
|
||||
return gTest(expected, observed) < alpha;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -345,7 +345,7 @@ public class GTest {
|
|||
* {@code observed1} or {@code observed2} are zero, or if the count
|
||||
* at the same index is zero for both arrays.
|
||||
*/
|
||||
public double gValueDataSetsComparison(final long[] observed1, final long[] observed2)
|
||||
public double gDataSetsComparison(final long[] observed1, final long[] observed2)
|
||||
throws DimensionMismatchException, NotPositiveException, ZeroException {
|
||||
|
||||
// Make sure lengths are same
|
||||
|
@ -390,7 +390,7 @@ public class GTest {
|
|||
|
||||
/**
|
||||
* Calculates the root log-likelihood ratio for 2 state Datasets. See
|
||||
* {@link #gValueDataSetsComparison(long[], long[] )}.
|
||||
* {@link #gDataSetsComparison(long[], long[] )}.
|
||||
*
|
||||
* <p>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,
|
||||
|
@ -420,7 +420,7 @@ public class GTest {
|
|||
*/
|
||||
public double rootLogLikelihoodRatio(final long k11, long k12,
|
||||
final long k21, final long k22) {
|
||||
final double llr = gValueDataSetsComparison(
|
||||
final double llr = gDataSetsComparison(
|
||||
new long[]{k11, k12}, new long[]{k21, k22});
|
||||
double sqrt = FastMath.sqrt(llr);
|
||||
if ((double) k11 / (k11 + k12) < (double) k21 / (k21 + k22)) {
|
||||
|
@ -440,7 +440,7 @@ public class GTest {
|
|||
* can reject the null hypothesis that the observed counts conform to the
|
||||
* same distribution. </p>
|
||||
*
|
||||
* <p>See {@link #gTestGoodnessOfFitPValue(double[], long[])} for details
|
||||
* <p>See {@link #gTest(double[], long[])} for details
|
||||
* on how the p-value is computed. The degrees of of freedom used to
|
||||
* perform the test is one less than the common length of the input observed
|
||||
* count arrays.</p>
|
||||
|
@ -469,14 +469,14 @@ public class GTest {
|
|||
* @throws MaxCountExceededException if an error occurs computing the
|
||||
* p-value.
|
||||
*/
|
||||
public double gTestDataSetsComparisonPValue(final long[] observed1,
|
||||
public double gTestDataSetsComparison(final long[] observed1,
|
||||
final long[] observed2)
|
||||
throws DimensionMismatchException, NotPositiveException, ZeroException,
|
||||
MaxCountExceededException {
|
||||
final ChiSquaredDistribution distribution = new ChiSquaredDistribution(
|
||||
(double) observed1.length - 1);
|
||||
return 1 - distribution.cumulativeProbability(
|
||||
gValueDataSetsComparison(observed1, observed2));
|
||||
gDataSetsComparison(observed1, observed2));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -486,9 +486,9 @@ public class GTest {
|
|||
* significance level {@code alpha}. Returns true iff the null
|
||||
* hypothesis can be rejected with 100 * (1 - alpha) percent confidence.
|
||||
* </p>
|
||||
* <p>See {@link #gValueDataSetsComparison(long[], long[])} for details
|
||||
* <p>See {@link #gDataSetsComparison(long[], long[])} for details
|
||||
* on the formula used to compute the G (LLR) statistic used in the test and
|
||||
* {@link #gTestGoodnessOfFitPValue(double[], long[])} for information on how
|
||||
* {@link #gTest(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. </p>
|
||||
|
@ -532,6 +532,6 @@ public class GTest {
|
|||
throw new OutOfRangeException(
|
||||
LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5);
|
||||
}
|
||||
return gTestDataSetsComparisonPValue(observed1, observed2) < alpha;
|
||||
return gTestDataSetsComparison(observed1, observed2) < alpha;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,11 +47,11 @@ public class GTestTest {
|
|||
};
|
||||
|
||||
Assert.assertEquals("G test statistic",
|
||||
0.348721, testStatistic.gValueGoodnessOfFit(exp, obs), 1E-6);
|
||||
final double p_gtgf = testStatistic.gTestGoodnessOfFitPValue(exp, obs);
|
||||
0.348721, testStatistic.g(exp, obs), 1E-6);
|
||||
final double p_gtgf = testStatistic.gTest(exp, obs);
|
||||
Assert.assertEquals("g-Test p-value", 0.55483, p_gtgf, 1E-5);
|
||||
|
||||
Assert.assertFalse(testStatistic.gTestGoodnessOfFit(exp, obs, 0.05));
|
||||
Assert.assertFalse(testStatistic.gTest(exp, obs, 0.05));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -64,11 +64,11 @@ public class GTestTest {
|
|||
70, 79, 3, 4
|
||||
};
|
||||
Assert.assertEquals("G test statistic",
|
||||
13.144799, testStatistic.gValueGoodnessOfFit(exp, obs), 1E-6);
|
||||
final double p_gtgf = testStatistic.gTestGoodnessOfFitPValue(exp, obs);
|
||||
13.144799, testStatistic.g(exp, obs), 1E-6);
|
||||
final double p_gtgf = testStatistic.gTest(exp, obs);
|
||||
Assert.assertEquals("g-Test p-value", 0.004333, p_gtgf, 1E-5);
|
||||
|
||||
Assert.assertTrue(testStatistic.gTestGoodnessOfFit(exp, obs, 0.05));
|
||||
Assert.assertTrue(testStatistic.gTest(exp, obs, 0.05));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -82,12 +82,12 @@ public class GTestTest {
|
|||
};
|
||||
|
||||
Assert.assertEquals("G test statistic",
|
||||
4.5554, testStatistic.gValueGoodnessOfFit(exp, obs), 1E-4);
|
||||
4.5554, testStatistic.g(exp, obs), 1E-4);
|
||||
// Intrinisic (Hardy-Weinberg proportions) P-Value should be 0.033
|
||||
final double p_gtgf = testStatistic.gTestGoodnessOfFitIntrinsicPValue(exp, obs);
|
||||
final double p_gtgf = testStatistic.gTestIntrinsic(exp, obs);
|
||||
Assert.assertEquals("g-Test p-value", 0.0328, p_gtgf, 1E-4);
|
||||
|
||||
Assert.assertFalse(testStatistic.gTestGoodnessOfFit(exp, obs, 0.05));
|
||||
Assert.assertFalse(testStatistic.gTest(exp, obs, 0.05));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -100,11 +100,11 @@ public class GTestTest {
|
|||
807, 759, 184
|
||||
};
|
||||
|
||||
final double g = testStatistic.gValueDataSetsComparison(obs1, obs2);
|
||||
final double g = testStatistic.gDataSetsComparison(obs1, obs2);
|
||||
|
||||
Assert.assertEquals("G test statistic",
|
||||
7.3008170, g, 1E-6);
|
||||
final double p_gti = testStatistic.gTestDataSetsComparisonPValue(obs1, obs2);
|
||||
final double p_gti = testStatistic.gTestDataSetsComparison(obs1, obs2);
|
||||
|
||||
Assert.assertEquals("g-Test p-value", 0.0259805, p_gti, 1E-6);
|
||||
Assert.assertTrue(testStatistic.gTestDataSetsComparison(obs1, obs2, 0.05));
|
||||
|
@ -120,11 +120,11 @@ public class GTestTest {
|
|||
116, 67, 161
|
||||
};
|
||||
|
||||
final double g = testStatistic.gValueDataSetsComparison(obs1, obs2);
|
||||
final double g = testStatistic.gDataSetsComparison(obs1, obs2);
|
||||
|
||||
Assert.assertEquals("G test statistic",
|
||||
6.227288, g, 1E-6);
|
||||
final double p_gti = testStatistic.gTestDataSetsComparisonPValue(obs1, obs2);
|
||||
final double p_gti = testStatistic.gTestDataSetsComparison(obs1, obs2);
|
||||
|
||||
Assert.assertEquals("g-Test p-value", 0.04443, p_gti, 1E-5);
|
||||
Assert.assertTrue(testStatistic.gTestDataSetsComparison(obs1, obs2, 0.05));
|
||||
|
@ -140,10 +140,10 @@ public class GTestTest {
|
|||
42, 49
|
||||
};
|
||||
|
||||
final double g = testStatistic.gValueDataSetsComparison(obs1, obs2);
|
||||
final double g = testStatistic.gDataSetsComparison(obs1, obs2);
|
||||
Assert.assertEquals("G test statistic",
|
||||
2.8187, g, 1E-4);
|
||||
final double p_gti = testStatistic.gTestDataSetsComparisonPValue(obs1, obs2);
|
||||
final double p_gti = testStatistic.gTestDataSetsComparison(obs1, obs2);
|
||||
Assert.assertEquals("g-Test p-value", 0.09317325, p_gti, 1E-6);
|
||||
|
||||
Assert.assertFalse(testStatistic.gTestDataSetsComparison(obs1, obs2, 0.05));
|
||||
|
@ -154,7 +154,7 @@ public class GTestTest {
|
|||
long[] observed1 = {10, -1, 12, 10, 15};
|
||||
long[] observed2 = {15, 10, 10, 15, 5};
|
||||
try {
|
||||
testStatistic.gTestDataSetsComparisonPValue(
|
||||
testStatistic.gTestDataSetsComparison(
|
||||
observed1, observed2);
|
||||
Assert.fail("Expecting NotPositiveException - negative count");
|
||||
} catch (NotPositiveException ex) {
|
||||
|
@ -163,7 +163,7 @@ public class GTestTest {
|
|||
long[] observed3 = {10, 0, 12, 10, 15};
|
||||
long[] observed4 = {15, 0, 10, 15, 5};
|
||||
try {
|
||||
testStatistic.gTestDataSetsComparisonPValue(
|
||||
testStatistic.gTestDataSetsComparison(
|
||||
observed3, observed4);
|
||||
Assert.fail("Expecting ZeroException - double 0's");
|
||||
} catch (ZeroException ex) {
|
||||
|
@ -172,7 +172,7 @@ public class GTestTest {
|
|||
long[] observed5 = {10, 10, 12, 10, 15};
|
||||
long[] observed6 = {0, 0, 0, 0, 0};
|
||||
try {
|
||||
testStatistic.gTestDataSetsComparisonPValue(
|
||||
testStatistic.gTestDataSetsComparison(
|
||||
observed5, observed6);
|
||||
Assert.fail("Expecting ZeroException - vanishing counts");
|
||||
} catch (ZeroException ex) {
|
||||
|
@ -186,13 +186,13 @@ public class GTestTest {
|
|||
final double[] expected = { 1, 1, 2 };
|
||||
final long[] observed2 = {3, 4};
|
||||
try {
|
||||
testStatistic.gTestGoodnessOfFitPValue(expected, observed);
|
||||
testStatistic.gTest(expected, observed);
|
||||
Assert.fail("arrays have different lengths, DimensionMismatchException expected");
|
||||
} catch (DimensionMismatchException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
testStatistic.gTestDataSetsComparisonPValue(observed, observed2);
|
||||
testStatistic.gTestDataSetsComparison(observed, observed2);
|
||||
Assert.fail("arrays have different lengths, DimensionMismatchException expected");
|
||||
} catch (DimensionMismatchException ex) {
|
||||
// expected
|
||||
|
@ -205,13 +205,13 @@ public class GTestTest {
|
|||
final double[] expected = { 1, 1, 2, 3};
|
||||
final long[] observed2 = {3, 4, 5, 0};
|
||||
try {
|
||||
testStatistic.gTestGoodnessOfFitPValue(expected, observed);
|
||||
testStatistic.gTest(expected, observed);
|
||||
Assert.fail("negative observed count, NotPositiveException expected");
|
||||
} catch (NotPositiveException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
testStatistic.gTestDataSetsComparisonPValue(observed, observed2);
|
||||
testStatistic.gTestDataSetsComparison(observed, observed2);
|
||||
Assert.fail("negative observed count, NotPositiveException expected");
|
||||
} catch (NotPositiveException ex) {
|
||||
// expected
|
||||
|
@ -223,7 +223,7 @@ public class GTestTest {
|
|||
final long[] observed = { 0, 1, 2, -3 };
|
||||
final double[] expected = { 1, 0, 2, 3};
|
||||
try {
|
||||
testStatistic.gTestGoodnessOfFitPValue(expected, observed);
|
||||
testStatistic.gTest(expected, observed);
|
||||
Assert.fail("zero expected count, NotStrictlyPositiveException expected");
|
||||
} catch (NotStrictlyPositiveException ex) {
|
||||
// expected
|
||||
|
@ -236,7 +236,7 @@ public class GTestTest {
|
|||
final double[] expected = { 1, 2, 2, 3};
|
||||
final long[] observed2 = { 0, 2, 2, 3 };
|
||||
try {
|
||||
testStatistic.gTestGoodnessOfFit(expected, observed, 0.8);
|
||||
testStatistic.gTest(expected, observed, 0.8);
|
||||
Assert.fail("zero expected count, NotStrictlyPositiveException expected");
|
||||
} catch (OutOfRangeException ex) {
|
||||
// expected
|
||||
|
@ -257,12 +257,12 @@ public class GTestTest {
|
|||
final double[] expected3 = {1, 1, 1, 1, 1};
|
||||
final double tol = 1E-15;
|
||||
Assert.assertEquals(
|
||||
testStatistic.gTestGoodnessOfFitPValue(expected1, observed),
|
||||
testStatistic.gTestGoodnessOfFitPValue(expected2, observed),
|
||||
testStatistic.gTest(expected1, observed),
|
||||
testStatistic.gTest(expected2, observed),
|
||||
tol);
|
||||
Assert.assertEquals(
|
||||
testStatistic.gTestGoodnessOfFitPValue(expected1, observed),
|
||||
testStatistic.gTestGoodnessOfFitPValue(expected3, observed),
|
||||
testStatistic.gTest(expected1, observed),
|
||||
testStatistic.gTest(expected3, observed),
|
||||
tol);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue