Hello! Welcome to the second module of our course, "Probability and Distributions for Modeling Data."
In our first module, we built a mathematical foundation focused on the mechanics of optimization. We learned about derivatives, gradients, and the landscapes of loss functions, all of which answer the question: How does a model find its best parameters?
Now, we shift our focus to the question: What are we modeling in the first place? This module is about using the language of probability to represent data, model uncertainty, and define the very problems that our optimization algorithms aim to solve.
We'll begin with one of the most fundamental concepts in all of statistics and machine learning. Today's learning outcome is to apply Bayes' theorem to update beliefs given new evidence in a machine learning context. This powerful idea is the basis for an entire branch of machine learning and provides a formal way for our models to "learn" from data.
1. The Core Idea: Updating Beliefs with Evidence
At its heart, Bayesian reasoning is a formal process for updating your beliefs in light of new evidence. Imagine you have an initial hypothesis. When you observe something new, how much should that observation change your confidence in your initial hypothesis? Bayes' theorem provides the answer.
This is a concept that people often get wrong intuitively, usually by ignoring the importance of their initial belief, also known as the "prior" or "base rate". The following video from 3Blue1Brown provides a fantastic intuition for this.
Bayes theorem, the geometry of changing beliefs
Watch the video 'Bayes theorem, the geometry of changing beliefs' by 3Blue1Brown to understand the core idea of updating beliefs and how base rates (priors) are crucial.
Please watch the following two segments: The Librarian/Farmer Problem (01:04 - 04:46): Pay close attention to how the initial ratio of farmers to librarians (the prior) dramatically influences the final probability, even when the evidence seems to point strongly in one direction. Generalizing to Bayes' Theorem (04:46 - 08:37): This part translates the example into the formal equation, defining the key terms. Focus on understanding what 'prior', 'likelihood', and 'posterior' mean conceptually.
As the video explains, the key mantra is that new evidence does not determine your beliefs in a vacuum; it updates prior beliefs.
Let's formalize this. Bayes' theorem is stated as:
Let's break down these terms, which you just heard in the video:
- : The Posterior Probability. This is what we want to calculate: the probability of our hypothesis being true, given that we have observed the evidence . It's our updated belief.
- : The Prior Probability. This is our initial belief in the hypothesis before seeing any evidence. In the video's example, this was the base rate probability of a person being a librarian.
- : The Likelihood. This is the probability of observing the evidence if our hypothesis were true. In the example, this was the probability that a librarian would fit Steve's description.
- : The Evidence Probability. This is the overall probability of observing the evidence, regardless of the hypothesis. It acts as a normalization constant.
Often, is calculated by considering all possible hypotheses. For a simple true/false hypothesis, it's expanded using the law of total probability:
This is exactly what was done in the video by adding the number of meek farmers and meek librarians to get the total number of meek people.
2. A Practical Example: The Base Rate Fallacy
Our intuition often overvalues the likelihood and ignores the prior, a mistake so common it's called the base rate fallacy. Medical testing is the classic example.
Let's work through a scenario. A certain type of cancer affects 0.5% of the population. A test for this cancer is 97% sensitive (it correctly identifies 97% of people who have the disease) and 95% specific (it correctly identifies 95% of people who don't). If a person tests positive, what is the probability they actually have cancer?
Our intuition, looking at the 97% sensitivity, might say the probability is very high. Let's see what Bayes' theorem says. The following article provides an excellent, practical example.
Bayes’ rule with a simple and practical example
The article 'Bayes’ rule with a simple and practical example' from Towards Data Science provides a clear, step-by-step application of the theorem to a drug screening problem, which is conceptually identical to our medical test scenario.
Read the section titled 'Practical example with Python code', focusing on the 'Drug screening' part. The numbers are slightly different, but the logic is the same. Pay attention to how the very low prevalence rate (the prior) dramatically affects the final probability.
Let's map our cancer test problem onto the formula:
- : The patient has cancer.
- : The patient tests positive.
We want to find .
What we know:
- Prior (0.5% prevalence)
- Likelihood (97% sensitivity)
- Specificity . From this, we can deduce the false positive rate: .
First, we calculate the total probability of a positive test, :
Now we can apply Bayes' theorem:
So, there is only an 8.9% chance the person actually has cancer, even with a positive test! This counter-intuitive result is driven by the low prior probability. The number of healthy people who get false positives outweighs the number of sick people who get true positives.
Test your understanding!
Using the numbers from our cancer example, what would happen to the posterior probability if this were a less reliable test with a specificity of only 80% (meaning a 20% false positive rate)? Would the probability of having cancer after a positive test go up or down? Why?
Show answer
The probability would go down significantly. A lower specificity means a higher false positive rate (). This increases the denominator because more healthy people are testing positive. A larger denominator leads to a smaller final posterior probability. This highlights that the reliability of the test in ruling out the healthy population is just as important as its ability to identify the sick population.
3. Bayesian Inference for Machine Learning
Now, let's connect this to machine learning. In Bayesian ML, we treat model parameters as hypotheses. Our goal is to find the most plausible parameters given our data.
- Hypothesis () becomes our model parameters ().
- Evidence () becomes our observed data ().
The theorem is re-written as:
We often use the proportional symbol () because the denominator is a constant that can be difficult to calculate, but it doesn't change the relative probabilities of different parameter values.
This framework gives us a way to find a full probability distribution for our model's parameters, which represents our belief (and uncertainty!) about the parameters after seeing the data. The following video provides an exceptionally clear, practical demonstration of this process.
Introduction to Bayesian data analysis - part 1: What is Bayes?
The video 'Introduction to Bayesian data analysis - part 1: What is Bayes?' by Rasmus Båge walks through a complete example of using Bayesian methods to estimate a parameter from data.
This is a key resource for the lesson. Please watch from 06:18 to 20:51. The video uses an intuitive simulation method to perform Bayesian inference. Focus on these steps: The Problem (06:18 - 08:29): Understanding the goal of estimating the 'signup rate' parameter from the data (6 signups out of 16 people). The Model & Prior (08:29 - 12:52): See how he defines a 'generative model' (the likelihood) and a 'prior' distribution to represent initial uncertainty about the signup rate. The Inference Process (12:52 - 16:39): This is the core. Watch how he repeatedly draws from the prior, simulates data, and filters for draws that match the real data. The result is the posterior distribution. Interpreting the Posterior (16:39 - 20:51): Understand how the final blue distribution represents our updated knowledge about the signup rate and how we can use it to get 'best guesses' and 'credible intervals'.
The process you just saw is a beautiful illustration of Bayesian inference in action. We started with a "flat" prior (total uncertainty) and ended with a posterior distribution peaked around 38%, which represents our updated, data-informed belief about the true signup rate.
4. Sequential Updates: Learning Over Time
One of the most powerful aspects of this framework is its ability to learn sequentially. If we get more data, we don't need to start from scratch. The posterior from our first analysis simply becomes the prior for our next one.
The article on drug screening you looked at earlier has an excellent section that demonstrates this.
Bayes’ rule with a simple and practical example
Let's return to the 'Bayes’ rule with a simple and practical example' article to see how this iterative updating works.
Please read the section titled 'Chaining Bayes’ rule'. Notice how the 8.9% posterior probability from the first positive test becomes the new, much more informative prior for the second test, leading to a much higher posterior.
This "chaining" process is exactly what allows a machine learning model to update its internal parameters as it sees more and more batches of data during training. Each batch of data sharpens the model's "beliefs" about the world.
Conclusion
Today we've unpacked one of the most important ideas in statistics. You've seen how Bayes' theorem provides a rational way to update our beliefs and how this same principle is applied in machine learning to allow models to learn from data.
Key Takeaways:
- Bayes' theorem () is a rule for updating a prior belief into a posterior belief after observing evidence.
- Ignoring the prior (or base rate) can lead to the base rate fallacy, where we make intuitive but incorrect judgments about probability.
- In machine learning, we use Bayesian inference to find the posterior distribution of model parameters given data: .
- This posterior represents the model's learned knowledge and its uncertainty about the parameters.
- The process is naturally sequential: the posterior after one set of evidence becomes the prior for the next, allowing models to learn iteratively.
Next Steps:
In this lesson, our output was a full probability distribution (the posterior). To make practical use of such distributions, we need ways to summarize them. In our next lesson, we will explore two of the most important summary statistics: expected value, which gives us a single "best guess" from a distribution, and variance, which quantifies the uncertainty or spread around that guess.