BINOMTEST
Overview
The BINOMTEST
function performs a test that the probability of success in a Bernoulli experiment is equal to a specified value. It is used to determine whether the observed number of successes in a fixed number of independent trials is consistent with a hypothesized probability of success. This function is commonly used in quality control, clinical trials, and other applications where binary outcomes are observed. For more details, see the scipy.stats.binomtest documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=BINOMTEST(k, n, [p])
k
(int, required): Number of successes observed.n
(int, required): Number of trials.p
(float, optional, default=0.5): Hypothesized probability of success.
The function returns a 2D list with two elements: the test statistic (number of successes) and the p-value (float), or an error message (string) if the input is invalid.
Examples
Example 1: Fair Coin Test
Test if 5 heads in 10 coin flips is consistent with a fair coin (p=0.5).
Inputs:
k | n | p |
---|---|---|
5 | 10 | 0.5 |
Excel formula:
=BINOMTEST(5, 10, 0.5)
Expected output:
Test Statistic | p-value |
---|---|
5 | 1.0 |
Example 2: Biased Coin
Test if 8 heads in 10 flips is consistent with a fair coin.
Inputs:
k | n | p |
---|---|---|
8 | 10 | 0.5 |
Excel formula:
=BINOMTEST(8, 10, 0.5)
Expected output:
Test Statistic | p-value |
---|---|
8 | 0.109375 |
Example 3: Custom Probability
Test if 7 successes in 12 trials is consistent with p=0.6.
Inputs:
k | n | p |
---|---|---|
7 | 12 | 0.6 |
Excel formula:
=BINOMTEST(7, 12, 0.6)
Expected output:
Test Statistic | p-value |
---|---|
7 | 1.0 |
Example 4: Default Probability
Test if 3 successes in 6 trials is consistent with the default p=0.5.
Inputs:
k | n | p |
---|---|---|
3 | 6 | 0.5 |
Excel formula:
=BINOMTEST(3, 6)
Expected output:
Test Statistic | p-value |
---|---|
3 | 1.0 |
Python Code
from scipy.stats import binomtest as scipy_binomtest
def binomtest(k, n, p=0.5):
"""
Perform a binomial test for the probability of success in a Bernoulli experiment.
Args:
k: Number of successes (int).
n: Number of trials (int).
p: Hypothesized probability of success (float, default 0.5).
Returns:
2D list: [[test statistic (int), p-value (float)]], or error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate inputs
try:
k_int = int(k)
n_int = int(n)
p_float = float(p)
except Exception:
return "Invalid input: k, n must be integers and p must be a float."
if n_int <= 0:
return "Invalid input: n must be positive."
if not (0 <= k_int <= n_int):
return "Invalid input: k must be between 0 and n."
if not (0 <= p_float <= 1):
return "Invalid input: p must be between 0 and 1."
try:
result = scipy_binomtest(k_int, n_int, p_float)
stat = k_int
pval = float(result.pvalue)
# Disallow nan/inf
if not (pval == pval and pval != float('inf') and pval != float('-inf')):
return "Invalid output: p-value is not finite."
return [[stat, pval]]
except Exception as e:
return f"scipy.stats.binomtest error: {e}"