binomialTest
Performs an exact binomial test for whether the proportion of successes equals probability.
The null hypothesis is that the true probability of success in each trial equals probability. Unlike asymptotic tests, this test computes exact p-values using the binomial distribution, making it appropriate for any sample size. The confidence interval method can be selected via ciMethod: Clopper-Pearson (exact, conservative), Wilson (score-based, narrower), or Agresti-Coull (adjusted Wald, simple approximation).
Example:
val result = binomialTest(successes = 7, trials = 10, probability = 0.5)
result.statistic // 0.7 (observed proportion)
result.pValue // two-sided exact p-value
result.confidenceInterval // 95% Clopper-Pearson CI for the proportion
result.isSignificant() // true if p < 0.05
// Use Wilson score interval for narrower CI
val wilson = binomialTest(successes = 45, trials = 100, ciMethod = CIMethod.WILSON)
wilson.confidenceInterval // Wilson score intervalReturn
a TestResult containing the observed proportion as the statistic, the exact p-value, a confidence interval computed using ciMethod, and additional info with "successes", "trials", and "hypothesizedProbability".
Parameters
the number of observed successes. Must be in [0, trials].
the total number of trials. Must be non-negative.
the hypothesized probability of success per trial. Must be in [0, 1]. Defaults to 0.5.
the direction of the alternative hypothesis. Defaults to Alternative.TWO_SIDED. Alternative.LESS tests if the true proportion is less than probability, Alternative.GREATER tests if it is greater.
the confidence level for the confidence interval. Must be in (0, 1). Defaults to 0.95 (95%).
the method used to compute the confidence interval for the proportion. Defaults to CIMethod.CLOPPER_PEARSON (exact interval). Use CIMethod.WILSON for narrower intervals recommended in A/B testing, or CIMethod.AGRESTI_COULL for a simple approximation.