BETA_CI_BOUNDS
This function computes posterior interval endpoints for a Bernoulli/binomial proportion using Beta prior conjugacy and the inverse regularized incomplete beta function.
With prior p \sim \mathrm{Beta}(\alpha_0, \beta_0) and observed successes s in n trials, the posterior is:
p \mid s,n \sim \mathrm{Beta}(\alpha_0 + s,\; \beta_0 + n - s)
The equal-tailed credible interval with confidence level c is obtained from posterior quantiles at probabilities \frac{1-c}{2} and 1-\frac{1-c}{2}.
Excel Usage
=BETA_CI_BOUNDS(successes, trials, alpha_prior, beta_prior, confidence)
successes(int, required): Number of observed successes (count).trials(int, required): Number of observed Bernoulli trials (count).alpha_prior(float, optional, default: 1): Prior Beta alpha shape parameter (unitless).beta_prior(float, optional, default: 1): Prior Beta beta shape parameter (unitless).confidence(float, optional, default: 0.95): Credible interval probability level (0 to 1).
Returns (list[list]): 2D table with posterior parameters, posterior mean, and lower/upper credible bounds for the proportion.
Example 1: Uniform-prior interval for moderate success rate
Inputs:
| successes | trials | alpha_prior | beta_prior | confidence |
|---|---|---|---|---|
| 6 | 10 | 1 | 1 | 0.95 |
Excel formula:
=BETA_CI_BOUNDS(6, 10, 1, 1, 0.95)
Expected output:
| Posterior Alpha | Posterior Beta | Mean | Lower | Upper |
|---|---|---|---|---|
| 7 | 5 | 0.583333 | 0.307905 | 0.832512 |
Example 2: Informative prior shifts posterior interval
Inputs:
| successes | trials | alpha_prior | beta_prior | confidence |
|---|---|---|---|---|
| 18 | 30 | 4 | 3 | 0.9 |
Excel formula:
=BETA_CI_BOUNDS(18, 30, 4, 3, 0.9)
Expected output:
| Posterior Alpha | Posterior Beta | Mean | Lower | Upper |
|---|---|---|---|---|
| 22 | 15 | 0.594595 | 0.460489 | 0.722805 |
Example 3: Low observed success proportion interval
Inputs:
| successes | trials | alpha_prior | beta_prior | confidence |
|---|---|---|---|---|
| 2 | 25 | 1 | 1 | 0.95 |
Excel formula:
=BETA_CI_BOUNDS(2, 25, 1, 1, 0.95)
Expected output:
| Posterior Alpha | Posterior Beta | Mean | Lower | Upper |
|---|---|---|---|---|
| 3 | 24 | 0.111111 | 0.0244581 | 0.251303 |
Example 4: High-confidence posterior interval
Inputs:
| successes | trials | alpha_prior | beta_prior | confidence |
|---|---|---|---|---|
| 12 | 20 | 2 | 2 | 0.99 |
Excel formula:
=BETA_CI_BOUNDS(12, 20, 2, 2, 0.99)
Expected output:
| Posterior Alpha | Posterior Beta | Mean | Lower | Upper |
|---|---|---|---|---|
| 14 | 10 | 0.583333 | 0.326351 | 0.815248 |
Python Code
from scipy.special import betaincinv as scipy_betaincinv
def beta_ci_bounds(successes, trials, alpha_prior=1, beta_prior=1, confidence=0.95):
"""
Compute an equal-tailed Bayesian credible interval for a proportion using a Beta posterior.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.betaincinv.html
This example function is provided as-is without any representation of accuracy.
Args:
successes (int): Number of observed successes (count).
trials (int): Number of observed Bernoulli trials (count).
alpha_prior (float, optional): Prior Beta alpha shape parameter (unitless). Default is 1.
beta_prior (float, optional): Prior Beta beta shape parameter (unitless). Default is 1.
confidence (float, optional): Credible interval probability level (0 to 1). Default is 0.95.
Returns:
list[list]: 2D table with posterior parameters, posterior mean, and lower/upper credible bounds for the proportion.
"""
try:
if trials <= 0:
return "Error: trials must be greater than 0"
if successes < 0 or successes > trials:
return "Error: successes must be between 0 and trials"
if alpha_prior <= 0 or beta_prior <= 0:
return "Error: alpha_prior and beta_prior must be greater than 0"
if not (0 < confidence < 1):
return "Error: confidence must be between 0 and 1"
posterior_alpha = alpha_prior + successes
posterior_beta = beta_prior + (trials - successes)
tail_probability = (1 - confidence) / 2
lower = float(scipy_betaincinv(posterior_alpha, posterior_beta, tail_probability))
upper = float(scipy_betaincinv(posterior_alpha, posterior_beta, 1 - tail_probability))
mean = float(posterior_alpha / (posterior_alpha + posterior_beta))
return [
["Posterior Alpha", "Posterior Beta", "Mean", "Lower", "Upper"],
[float(posterior_alpha), float(posterior_beta), mean, lower, upper]
]
except Exception as e:
return f"Error: {str(e)}"