Skip to content

Commit

Permalink
Fixed double-counting error in 2-sided test; added test cases. Resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
psteitz committed Aug 28, 2016
1 parent 1c9a0d2 commit 098ae5c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ public double binomialTest(int numberOfTrials, int numberOfSuccesses, double pro
final double pHigh = distribution.probability(criticalValueHigh);

if (pLow == pHigh) {
pTotal += 2 * pLow;
if (criticalValueLow == criticalValueHigh) { // One side can't move
pTotal += pLow;
} else {
pTotal += 2 * pLow;
}
criticalValueLow++;
criticalValueHigh--;
} else if (pLow < pHigh) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package org.hipparchus.stat.inference;

import org.hipparchus.distribution.discrete.BinomialDistribution;
import org.hipparchus.exception.MathIllegalArgumentException;
import org.hipparchus.util.FastMath;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -77,4 +79,90 @@ public void testBinomialTestAcceptReject() {
Assert.assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.GREATER_THAN, alpha01));
Assert.assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.LESS_THAN, alpha05));
}

/**
* All successes with p >> 0.5 - p-value picks up all mass points.
*/
@Test
public void testAllSuccessesTwoSidedHighP() {
Assert.assertEquals(1d, testStatistic.binomialTest(200, 200, 0.9950429, AlternativeHypothesis.TWO_SIDED),
Double.MIN_VALUE);
}

/**
* All successes with p = 0.5 - p-value is the sum of the two tails.
*/
@Test
public void testAllSuccessesTwoSidedEvenP() {
Assert.assertEquals(2 * FastMath.pow(0.5, 5),
testStatistic.binomialTest(5, 5, 0.5,
AlternativeHypothesis.TWO_SIDED),
Double.MIN_VALUE);
}

/**
* All successes with p = 0.5 - p-value is the sum of the two tails.
*/
@Test
public void testNoSuccessesTwoSidedEvenP() {
Assert.assertEquals(2 * FastMath.pow(0.5, 5),
testStatistic.binomialTest(5, 0, 0.5,
AlternativeHypothesis.TWO_SIDED),
Double.MIN_VALUE);
}

/**
* All successes with p < 0.5 - p-value is 5 mass point.
*/
@Test
public void testAllSuccessesTwoSidedLowP() {
final BinomialDistribution dist = new BinomialDistribution(5, 0.4);
Assert.assertEquals(dist.probability(5),
testStatistic.binomialTest(5, 5, 0.4,
AlternativeHypothesis.TWO_SIDED),
Double.MIN_VALUE);
}

@Test
/**
* No successes, p > 0.5 - p-value is 0 mass point.
*/
public void testNoSuccessesTwoSidedHighP() {
final BinomialDistribution dist = new BinomialDistribution(5, 0.9);
Assert.assertEquals(dist.probability(0),
testStatistic.binomialTest(5, 0, 0.9,
AlternativeHypothesis.TWO_SIDED),
Double.MIN_VALUE);
}


/**
* In this case, the distribution looks like this:
* 0: 0.32768
* 1: 0.4096
* 2: 0.2048
* 3: 0.0512
* 4: 0.0064
* 5: 3.2E-4
* Algorithm picks up 5, 4, 3, 2 and then 0, so result is 1 - mass at 1.
*/
@Test
public void testNoSuccessesTwoSidedLowP() {
final BinomialDistribution dist = new BinomialDistribution(5, 0.2);
Assert.assertEquals(1 - dist.probability(1),
testStatistic.binomialTest(5, 0, 0.2,
AlternativeHypothesis.TWO_SIDED),
Double.MIN_VALUE);
}

/**
* No successes has highest mass, so end up with everything here.
*/
@Test
public void testNoSuccessesTwoSidedVeryLowP() {
Assert.assertEquals(1d,
testStatistic.binomialTest(5, 0, 0.001,
AlternativeHypothesis.TWO_SIDED),
Double.MIN_VALUE);
}
}

0 comments on commit 098ae5c

Please sign in to comment.