BB_POST_UPDATE
This function performs conjugate updating for a Binomial likelihood with a Beta prior. It returns the posterior Beta hyperparameters along with posterior mean and variance for the underlying success probability.
With prior \text{Beta}(\alpha, \beta), observed successes x, and trials n, the posterior is:
\alpha' = \alpha + x, \qquad \beta' = \beta + n - x
The posterior mean and variance are then computed from \alpha' and \beta'.
Excel Usage
=BB_POST_UPDATE(alpha, beta, successes, trials)
alpha(float, required): Prior alpha hyperparameter (positive).beta(float, required): Prior beta hyperparameter (positive).successes(int, required): Observed number of successes (count).trials(int, required): Total number of Bernoulli trials (count).
Returns (list[list]): 2D array containing posterior parameters and posterior moments.
Example 1: Balanced prior with moderate observed data
Inputs:
| alpha | beta | successes | trials |
|---|---|---|---|
| 2 | 2 | 6 | 10 |
Excel formula:
=BB_POST_UPDATE(2, 2, 6, 10)
Expected output:
| Result | |
|---|---|
| 8 | 6 |
| 0.571429 | 0.0163265 |
Example 2: Informative prior with additional successes
Inputs:
| alpha | beta | successes | trials |
|---|---|---|---|
| 10 | 4 | 12 | 20 |
Excel formula:
=BB_POST_UPDATE(10, 4, 12, 20)
Expected output:
| Result | |
|---|---|
| 22 | 12 |
| 0.647059 | 0.00652496 |
Example 3: Sparse data with weak prior
Inputs:
| alpha | beta | successes | trials |
|---|---|---|---|
| 1.2 | 1.8 | 1 | 3 |
Excel formula:
=BB_POST_UPDATE(1.2, 1.8, 1, 3)
Expected output:
| Result | |
|---|---|
| 2.2 | 3.8 |
| 0.366667 | 0.0331746 |
Example 4: Posterior update with zero observed successes
Inputs:
| alpha | beta | successes | trials |
|---|---|---|---|
| 3 | 5 | 0 | 8 |
Excel formula:
=BB_POST_UPDATE(3, 5, 0, 8)
Expected output:
| Result | |
|---|---|
| 3 | 13 |
| 0.1875 | 0.0089614 |
Python Code
pass
def bb_post_update(alpha, beta, successes, trials):
"""
Update Beta-Binomial posterior hyperparameters from observed counts.
See: https://en.wikipedia.org/wiki/Beta_distribution#Bayesian_inference
This example function is provided as-is without any representation of accuracy.
Args:
alpha (float): Prior alpha hyperparameter (positive).
beta (float): Prior beta hyperparameter (positive).
successes (int): Observed number of successes (count).
trials (int): Total number of Bernoulli trials (count).
Returns:
list[list]: 2D array containing posterior parameters and posterior moments.
"""
try:
alpha = float(alpha)
beta = float(beta)
successes = int(successes)
trials = int(trials)
if alpha <= 0 or beta <= 0:
return "Error: alpha and beta must be positive"
if trials < 0:
return "Error: trials must be nonnegative"
if successes < 0 or successes > trials:
return "Error: successes must be between 0 and trials"
alpha_post = alpha + successes
beta_post = beta + (trials - successes)
total = alpha_post + beta_post
mean_post = alpha_post / total
var_post = (alpha_post * beta_post) / ((total ** 2) * (total + 1.0))
return [[alpha_post, beta_post], [mean_post, var_post]]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Prior alpha hyperparameter (positive).
Prior beta hyperparameter (positive).
Observed number of successes (count).
Total number of Bernoulli trials (count).