Hello! Welcome to your fourth lesson in the "Bayesian Methods for Machine Learning" module.
In our last lesson, we explored the why behind conjugate priors, understanding their crucial role in simplifying the calculation of the posterior distribution by sidestepping a potentially intractable integration problem. We learned that for a given likelihood, a conjugate prior ensures the posterior comes from the same family of distributions.
Today, we transition from the "why" to the "how." Our goal is to compute the posterior distribution for a simple model with a conjugate prior. We'll move beyond the theoretical elegance and get our hands dirty with the mechanics of updating our beliefs. By the end of this lesson, you'll be able to take a prior distribution, combine it with new data, and calculate the exact parameters of the resulting posterior distribution. This is a fundamental skill in applying Bayesian methods.
1. Revisiting the Core Calculation: The Beta-Binomial Model
Let's ground our work in the most common conjugate pair: the Beta-Binomial model. This model is used when we want to infer a probability, (e.g., a conversion rate, the chance of a coin landing heads), which can be any value between 0 and 1.
The setup is:
- Prior: Our initial belief about is modeled by a Beta distribution, .
- Likelihood: The evidence from our data comes from a Binomial process. We observe successes in trials, so the likelihood is given by the Binomial PMF, .
The key insight from our previous lesson was that the posterior is proportional to the product of the prior and the likelihood. Let's watch a short video that walks through this multiplication step-by-step.
20 - Beta conjugate prior to Binomial and Bernoulli likelihoods
This video, 'Beta conjugate prior to Binomial and Bernoulli likelihoods' from Ox educ, provides a concise mathematical proof of how the Beta prior and Binomial likelihood combine. Focus on how the terms involving the parameter heta (which we've been calling \pi) are grouped together.
Watch from 01:15 to 05:01. Pay close attention to: How Bayes' theorem is written in its proportional form: Posterior ∝ Likelihood × Prior. The algebraic step where the terms from the Binomial likelihood ( heta^z (1- heta)^{n-z}) and the Beta prior ( heta^{\alpha-1} (1- heta)^{eta-1}) are multiplied. How the exponents are simply added together, resulting in a new expression that has the same form (or 'kernel') as a Beta distribution.
As the video demonstrated, the mathematical core of the update is this:
When we drop the constant terms (those that don't depend on ), the multiplication looks like this:
By grouping the terms with and , we get:
This final expression is the kernel of a new Beta distribution. Because we recognize this form, we know our posterior distribution is:
This is the beauty of conjugacy: a potentially complex calculus problem becomes a simple act of addition.
2. The Beta-Binomial Update Rule in Practice
Let's make this even more concrete. The resource below, Chapter 3 of the online book Bayes Rules!, provides a fantastic, in-depth walkthrough with a practical example.
Chapter 3 The Beta-Binomial Bayesian Model
We'll now read through sections of 'Chapter 3: The Beta-Binomial Bayesian Model' to see the derivation in a narrative context and to formalize the update rule.
Please read sections 3.3 ('The Beta posterior model') and 3.4 ('The Beta-Binomial model'). In Section 3.3, focus on the derivation starting with 'the posterior conditional pdf of \pi strikes a balance...'. Follow the math that shows how a Beta(45, 55) prior and data of y=30, n=50 lead to a Beta(75, 75) posterior. In Section 3.4, pay close attention to the general formula presented, which summarizes the entire update process.
The key takeaway from that reading is the general update rule for any Beta-Binomial model:
- Prior:
- Data: Observe successes and failures.
- Posterior: , where:
- (Prior pseudo-successes + Observed successes)
- (Prior pseudo-failures + Observed failures)
This simple addition is the computational heart of the process.
3. Example: The Milgram Obedience Study
Let's apply this rule to a real-world example discussed in the Bayes Rules! chapter: the famous Milgram experiment, which tested people's obedience to authority.
Suppose a psychologist, before running the study, has a strong prior belief that very few people would obey orders to inflict harm. They model their belief about (the proportion of people who would obey) with a Beta(1, 10) distribution. This prior is heavily skewed towards zero, with a mean of , reflecting their belief that obedience is unlikely.
Now, they run the experiment with n = 40 participants and observe that y = 26 of them obey the authority figure completely.
Let's compute the posterior distribution for .
-
Identify Prior Parameters:
-
Identify Data:
- (successes, i.e., people who obeyed)
- (total trials)
- (failures)
-
Apply the Update Rule:
-
State the Posterior Distribution:
- The posterior distribution for is Beta(27, 24).
Interpreting the Result:
The psychologist's belief has shifted dramatically. The posterior distribution is now centered around . The strong evidence from the data has overwhelmed the initial, skeptical prior.

Test your understanding!
You are an ML engineer working on a new feature. You want to estimate the click-through rate (). Based on similar features, you set a prior of Beta(5, 95), suggesting a low click-through rate around 5%.
You run an A/B test with n=500 users, and y=45 of them click the new feature.
What is the posterior distribution for the click-through rate ?
Show answer
- Prior: Beta(5, 95) ->
- Data: y=45, n=500 ->
- Posterior Parameters:
- Posterior Distribution: The posterior distribution is Beta(50, 550).
The posterior mean is now , which is a blend of your 5% prior belief and the 9% () click-rate observed in the data.
4. Beyond the Beta-Binomial
The principle of finding a posterior by updating parameters is not limited to the Beta-Binomial case. It applies to all conjugate pairs. Let's briefly look at another example to see the same pattern at play.
Consider modeling data from a Normal distribution with a known mean but an unknown variance . The conjugate prior for the variance is the Inverse Gamma distribution.
Conjugate Prior for Variance of Normal Distribution with known mean
This video shows the derivation for the conjugate prior of the variance of a Normal distribution. You don't need to follow every mathematical detail. The goal is to recognize the same pattern: combining a prior and a likelihood, grouping terms, and identifying the kernel of the posterior.
Watch the video from the beginning to 02:11 to see how the likelihood is set up. Then, skip to 03:48 and watch until 08:15. Notice how the prior (Inverse Gamma) and likelihood (Normal) are multiplied, terms are grouped, and the resulting expression is identified as another Inverse Gamma distribution with updated parameters.
Just as with the Beta-Binomial case, the end result is a simple set of rules for updating the parameters of the Inverse Gamma prior to get the Inverse Gamma posterior. This reinforces the idea that conjugacy provides a computational shortcut across many different types of models.
Conclusion
In this lesson, we moved from theory to practice, focusing on the mechanics of computing a posterior distribution. You learned how to combine a prior belief with observed data to form an updated, posterior belief, all without performing any direct integration.
Key Takeaways:
- Computing the posterior for a conjugate pair involves multiplying the prior and likelihood functions and identifying the mathematical "kernel" of the resulting distribution.
- For the Beta-Binomial model, this process simplifies to a straightforward update rule:
- This process of updating parameters is a general feature of all conjugate models, not just the Beta-Binomial pair.
- The posterior distribution represents a mathematical blend of prior knowledge and observed evidence.
Preview of the Next Lesson:
We have now successfully computed the full posterior distribution, which describes all we know about our parameter of interest. But how do we summarize and communicate this knowledge? In the next lesson, we will learn how to construct and interpret Bayesian credible intervals and compare them to the frequentist confidence intervals you may already know. This will give us a powerful tool for quantifying our uncertainty.