Class BinomialTest

java.lang.Object
org.apache.commons.statistics.inference.BinomialTest

public final class BinomialTest extends Object
Implements binomial test statistics.

Performs an exact test for the statistical significance of deviations from a theoretically expected distribution of observations into two categories.

Since:
1.1
See Also:
  • Field Details

  • Constructor Details

    • BinomialTest

      private BinomialTest(AlternativeHypothesis alternative)
      Parameters:
      alternative - Alternative hypothesis.
  • Method Details

    • withDefaults

      public static BinomialTest withDefaults()
      Return an instance using the default options.
      Returns:
      default instance
    • with

      Return an instance with the configured alternative hypothesis.
      Parameters:
      v - Value.
      Returns:
      an instance
    • test

      public SignificanceResult test(int numberOfTrials, int numberOfSuccesses, double probability)
      Performs a binomial test about the probability of success \( \pi \).

      The null hypothesis is \( H_0:\pi=\pi_0 \) where \( \pi_0 \) is between 0 and 1.

      The probability of observing \( k \) successes from \( n \) trials with a given probability of success \( p \) is:

      \[ \Pr(X=k)=\binom{n}{k}p^k(1-p)^{n-k} \]

      The test is defined by the AlternativeHypothesis.

      To test \( \pi < \pi_0 \) (less than):

      \[ p = \sum_{i=0}^k\Pr(X=i)=\sum_{i=0}^k\binom{n}{i}\pi_0^i(1-\pi_0)^{n-i} \]

      To test \( \pi > \pi_0 \) (greater than):

      \[ p = \sum_{i=0}^k\Pr(X=i)=\sum_{i=k}^n\binom{n}{i}\pi_0^i(1-\pi_0)^{n-i} \]

      To test \( \pi \ne \pi_0 \) (two-sided) requires finding all \( i \) such that \( \mathcal{I}=\{i:\Pr(X=i)\leq \Pr(X=k)\} \) and compute the sum:

      \[ p = \sum_{i\in\mathcal{I}}\Pr(X=i)=\sum_{i\in\mathcal{I}}\binom{n}{i}\pi_0^i(1-\pi_0)^{n-i} \]

      The two-sided p-value represents the likelihood of getting a result at least as extreme as the sample, given the provided probability of success on a single trial.

      The test statistic is equal to the estimated proportion \( \frac{k}{n} \).

      Parameters:
      numberOfTrials - Number of trials performed.
      numberOfSuccesses - Number of successes observed.
      probability - Assumed probability of a single trial under the null hypothesis.
      Returns:
      test result
      Throws:
      IllegalArgumentException - if numberOfTrials or numberOfSuccesses is negative; probability is not between 0 and 1; or if numberOfTrials < numberOfSuccesses
      See Also:
    • twoSidedBinomialTest

      private static double twoSidedBinomialTest(int n, int k, double probability, BinomialDistribution distribution)
      Returns the observed significance level, or p-value, associated with a two-sided binomial test about the probability of success \( \pi \).
      Parameters:
      n - Number of trials performed.
      k - Number of successes observed.
      probability - Assumed probability of a single trial under the null hypothesis.
      distribution - Binomial distribution.
      Returns:
      p-value