GAMMA_CI_BOUNDS
This function computes credible interval endpoints for a positive rate parameter whose posterior is modeled as a Gamma distribution in shape-rate form.
If \lambda \mid \text{data} \sim \mathrm{Gamma}(\alpha, \beta) with shape \alpha and rate \beta, then the equivalent SciPy parameterization uses shape a=\alpha and scale \theta=1/\beta.
The equal-tailed credible interval at confidence level c is built from posterior quantiles:
[\lambda_L, \lambda_U] = [Q((1-c)/2),\; Q(1-(1-c)/2)]
where Q is the Gamma posterior quantile function.
Excel Usage
=GAMMA_CI_BOUNDS(shape, rate, confidence)
shape(float, required): Posterior Gamma shape parameter alpha (unitless).rate(float, required): Posterior Gamma rate parameter beta (1 per unit).confidence(float, optional, default: 0.95): Credible interval probability level (0 to 1).
Returns (list[list]): 2D table with posterior mean and lower/upper credible bounds for the rate parameter.
Example 1: Typical gamma posterior interval for rate
Inputs:
| shape | rate | confidence |
|---|---|---|
| 8 | 2 | 0.95 |
Excel formula:
=GAMMA_CI_BOUNDS(8, 2, 0.95)
Expected output:
| Mean | Lower | Upper |
|---|---|---|
| 4 | 1.72692 | 7.21134 |
Example 2: Concentrated posterior with larger shape
Inputs:
| shape | rate | confidence |
|---|---|---|
| 25 | 5 | 0.9 |
Excel formula:
=GAMMA_CI_BOUNDS(25, 5, 0.9)
Expected output:
| Mean | Lower | Upper |
|---|---|---|
| 5 | 3.47643 | 6.75048 |
Example 3: Diffuse posterior with small shape
Inputs:
| shape | rate | confidence |
|---|---|---|
| 1.5 | 0.8 | 0.95 |
Excel formula:
=GAMMA_CI_BOUNDS(1.5, 0.8, 0.95)
Expected output:
| Mean | Lower | Upper |
|---|---|---|
| 1.875 | 0.134872 | 5.84275 |
Example 4: High-confidence interval for positive rate
Inputs:
| shape | rate | confidence |
|---|---|---|
| 12 | 3.5 | 0.99 |
Excel formula:
=GAMMA_CI_BOUNDS(12, 3.5, 0.99)
Expected output:
| Mean | Lower | Upper |
|---|---|---|
| 3.42857 | 1.41232 | 6.50836 |
Python Code
from scipy.stats import gamma as scipy_gamma
def gamma_ci_bounds(shape, rate, confidence=0.95):
"""
Compute an equal-tailed Bayesian credible interval for a positive rate parameter using Gamma quantiles.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gamma.html
This example function is provided as-is without any representation of accuracy.
Args:
shape (float): Posterior Gamma shape parameter alpha (unitless).
rate (float): Posterior Gamma rate parameter beta (1 per unit).
confidence (float, optional): Credible interval probability level (0 to 1). Default is 0.95.
Returns:
list[list]: 2D table with posterior mean and lower/upper credible bounds for the rate parameter.
"""
try:
if shape <= 0:
return "Error: shape must be greater than 0"
if rate <= 0:
return "Error: rate must be greater than 0"
if not (0 < confidence < 1):
return "Error: confidence must be between 0 and 1"
tail_probability = (1 - confidence) / 2
scale = 1.0 / rate
lower = float(scipy_gamma.ppf(tail_probability, a=shape, scale=scale))
upper = float(scipy_gamma.ppf(1 - tail_probability, a=shape, scale=scale))
mean = float(shape / rate)
return [
["Mean", "Lower", "Upper"],
[mean, lower, upper]
]
except Exception as e:
return f"Error: {str(e)}"