Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 63

Bayes' Theorem: Priors and Posteriors

Recommended resources

Hello! Welcome back to our module on Bayesian Methods for Machine Learning.

In our last lesson, we explored the philosophical differences between the frequentist and Bayesian approaches. We established that the core of the Bayesian method is updating our beliefs in light of new evidence. The frequentist focuses on the properties of a procedure over many repetitions, while the Bayesian asks, "How should this data change what I believe?"

Today, we will move from this high-level philosophy to the practical mechanics. Our goal is to specify prior and posterior distributions in the context of Bayes' theorem. We'll formalize the components of the Bayesian formula, learn what "prior" and "posterior" mean as probability distributions, and see how they are used to model uncertainty about a parameter.

1. The Vocabulary of Belief Updating

Before diving into statistical models, let's first build an intuition for the key terms. Bayes' theorem gives us a language for updating beliefs.

Bayes theorem, the geometry of changing beliefs

The video 'Bayes theorem, the geometry of changing beliefs' by 3Blue1Brown provides a brilliant, intuitive walkthrough of the components of Bayes' theorem using the classic 'Steve the librarian' problem.

Please watch from 01:04 to 07:53. As you watch, focus on identifying these three key pieces of information: The initial belief before any evidence is presented (the base rate of librarians). The strength of the evidence itself (how well the description fits a librarian vs. a farmer). The final, updated belief after considering both the initial belief and the evidence.

The video beautifully illustrates the three core components of Bayesian reasoning:

  • Prior: Your initial belief before seeing new evidence. In the example, this was the low probability of a person being a librarian in the general population (the "base rate").
  • Likelihood: The probability of observing the evidence, given your hypothesis. This was the estimate of how likely the "meek and tidy" description was for a librarian versus a farmer.
  • Posterior: Your updated belief after combining your prior and the likelihood. This was the final calculated probability that Steve was a librarian, given the description.

2. From Events to Model Parameters

Now, let's translate this from a single puzzle to the world of statistical modeling, which is our ultimate goal in machine learning. Instead of a hypothesis like "Steve is a librarian," we are interested in a model parameter, which we'll call . This could be the fairness of a coin, a coefficient in a regression model, or the weight in a neural network.

Bayes' theorem for statistical inference is written as:

This is the same fundamental formula, just with new labels. Let's break it down.

The better way to do statistics | Bayesian #1

The video 'The better way to do statistics' by Very Normal explains this transition from Bayes' theorem for events to Bayes' theorem for parameters and data.

Watch from 07:39 to 11:31. The key is to understand how the terms from the 3Blue1Brown video map onto these new statistical terms: Prior Distribution, Likelihood, and Posterior Distribution.

Let's solidify those definitions:

  • — The Prior Distribution: This is no longer just a single probability but an entire probability distribution over all possible values of the parameter . It represents our belief about before we see any data.
  • — The Likelihood: This function tells us how probable the observed data is for each possible value of . This is the same likelihood function you encountered in Maximum Likelihood Estimation (MLE), and it's the component that brings information from the data into our analysis.
  • — The Posterior Distribution: This is the result we are looking for. It's a new probability distribution for that represents our updated belief after observing the data. It is a principled compromise between our prior belief and the evidence from the data.
  • — The Marginal Likelihood (or Evidence): This term represents the probability of observing the data, averaged over all possible values of . For now, you can think of it as just a normalization constant that ensures the posterior distribution integrates to 1. This is why the relationship is often written as a proportionality:

3. Specifying Priors and Posteriors in Practice

Let's make this concrete with a classic example: estimating the bias of a coin, , which is the probability of getting heads. Let's say we flip the coin times and observe heads.

To perform a Bayesian analysis, we must specify the Prior and the Likelihood. The Posterior will then be derived from them.

Bayes' Theorem for Probability Distributions

The article 'Bayes' Theorem for Probability Distributions' by Matthew Landers provides a clear, step-by-step guide on how to perform this analysis for a proportion parameter like our coin bias.

Read the sections 'Specifying the Prior Distribution', 'Generating the Data Based on the Parameters', and 'Updating the Prior Belief'. Focus on: Why the Beta distribution is a suitable choice for the prior on a parameter heta that lies between 0 and 1. Why the Binomial distribution describes the likelihood of observing S heads in n trials.

As the article explains:

  1. Prior Specification: Our parameter is a probability, so it must be between 0 and 1. The Beta distribution is a flexible probability distribution defined on the interval . It is controlled by two parameters, and .

    • A Beta(1, 1) prior is a uniform distribution, representing an uninformative prior where we believe all values of are equally likely.
    • A Beta(30, 30) prior would be sharply peaked around 0.5, representing a strong informative prior that the coin is fair.
  2. Likelihood Specification: The process of getting successes in independent trials is modeled by the Binomial distribution. So, our likelihood function is .

  3. Posterior Derivation: When we combine a Beta prior with a Binomial likelihood, something mathematically convenient happens.

    • Prior:
    • Likelihood:
    • Posterior:

    This resulting expression has the exact form of another Beta distribution! Our posterior distribution is:

This is the core of the process. We specified a Beta prior, used a Binomial likelihood, and found that our posterior is also a Beta distribution with updated parameters. The update rule is simple and intuitive: we just add the number of observed successes () to and the number of observed failures () to .


4. Visualizing the Belief Update

The power of this approach is seeing how our belief distribution changes as we incorporate data.

Bayesian Estimation: Prior and Posterior Distributions for a Biased Coin
This image shows a single Bayesian update. The top graph shows a uniform prior, Beta(1,1), representing our initial uncertainty about the coin's bias. After observing data (9 heads in 12 tosses), the bottom graph shows the posterior distribution, Beta(10,4), which is now peaked around 0.75, reflecting our updated belief.

As we collect more and more data, the likelihood starts to dominate the prior. Our posterior distribution becomes narrower and more peaked, reflecting our increasing certainty about the true value of the parameter.

Evolution of Posterior Probability with Increasing Data
This grid of plots shows how a posterior distribution for a success probability evolves. Starting with a uniform prior (top-left), as more trials and successes are observed, the posterior becomes more concentrated and bell-shaped, indicating growing certainty about the parameter's true value.
Test your understanding!

You are trying to estimate the click-through rate () of a new website button. You don't have much information to start with, so you decide to use an uninformative prior: Beta(α=1, β=1).

You then run an experiment with 200 visitors (n=200), and 30 of them click the button (S=30).

What is your posterior distribution for the click-through rate, ?

Show answer

We simply apply the update rule: Posterior ~ Beta(α + S, β + n - S)

  • New =
  • New =

Your posterior distribution for the click-through rate is Beta(31, 171). Your initial "guess" of the mean was 0.5 (from the Beta(1,1) prior), but your posterior belief will now be centered around a mean of , which is much closer to the data you observed.


Conclusion

In this lesson, we moved from the philosophy of Bayesianism to its practice. We've seen how to formally set up a Bayesian analysis by specifying distributions that represent our beliefs and our model of the data.

Key Takeaways:

  • Bayesian inference uses the language of distributions to represent beliefs about model parameters.
  • The prior distribution quantifies our belief about a parameter before seeing data. It can be informative (based on past knowledge) or uninformative (reflecting ignorance).
  • The posterior distribution represents our updated belief after seeing data. It is the central result of a Bayesian analysis.
  • The posterior is calculated by combining the prior and the likelihood: Posterior Likelihood Prior.
  • In the Beta-Binomial model, the update from prior to posterior is a simple matter of adding observed counts to the prior's parameters.

Preview of the Next Lesson:

The mathematical convenience we saw with the Beta-Binomial model—where the posterior ends up in the same family as the prior—is a special and very important property. In the next lesson, we will explore this concept in more detail, formally defining conjugate priors and explaining their crucial role in simplifying Bayesian computation.