Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our last lesson, we explored the Law of Large Numbers and the Central Limit Theorem, cornerstones of frequentist statistics that describe the long-run behavior of sample averages. These theorems are powerful, but they treat probabilities as objective, fixed frequencies. Today, we pivot to a different and equally powerful paradigm for reasoning under uncertainty: Bayesian inference.
Instead of viewing probability as a frequency, the Bayesian perspective treats it as a degree of belief. This lesson introduces the engine of this paradigm: Bayes' theorem. You will learn how to use this theorem to formally update your beliefs about the world as you acquire new, incomplete, or noisy evidence. This process of belief updating is fundamental to many advanced AI systems, from robotic navigation and spam filtering to the probabilistic models that underpin generative AI. Our goal is to derive the theorem from first principles and then apply it to see how new data reshapes our understanding.
The Core Idea: Beliefs are Updated, Not Replaced
Before diving into the mathematics, let's build some intuition. A common cognitive bias is to let new evidence completely overshadow what we knew before. Bayesian reasoning provides a principled way to avoid this by combining prior knowledge with new evidence.
The 3Blue1Brown video below uses a classic psychology puzzle to illustrate this beautifully. It shows how failing to account for the "base rate" (the prior probability) can lead to irrational conclusions.
Bayes theorem, the geometry of changing beliefs
This video provides a fantastic intuitive introduction to the core idea of Bayesian thinking using the "Steve the librarian" example.
Watch the segment from the Steve example. Pay close attention to how the reasoning unfolds. Notice that there are two key pieces of information: The description of Steve's personality. The ratio of farmers to librarians in the population. The key insight is that a rational judgment must incorporate both pieces of information. As the video states, "new evidence does not completely determine your beliefs in a vacuum. It should update prior beliefs."
The Language of Bayesian Inference
The intuitive process described in the video has a formal mathematical language. Let's define the components of Bayes' theorem. We'll use the example of a medical diagnosis, which is a common and powerful application. Let be the event that a patient has a disease, and be the event that they receive a positive test result.
- Prior : This is your initial belief that the patient has the disease before you see any test results. It's the "base rate" or background probability. For a rare disease, this might be very low.
- Likelihood : This is the probability of observing the evidence (a positive test) given that the patient actually has the disease. This is often related to the test's sensitivity.
- Posterior : This is the updated belief that the patient has the disease after you have observed the evidence (the positive test). This is what we usually want to compute.
- Evidence (or Marginal Likelihood) : This is the overall probability of observing the evidence (a positive test) under all circumstances, whether the patient has the disease or not.
The following video from Steve Brunton provides a clear walkthrough of this terminology.
Bayes' Theorem (with Example!)
This clip formally introduces the names for each term in Bayes' theorem and explains the "update" mechanism.
Please watch from the terminology section. The instructor frames the posterior as an update of the prior, which is the central idea.
A First-Principles Derivation of Bayes' Theorem
Given your interest in first principles, we won't just memorize the formula; we will derive it directly from the definition of conditional probability.
Recall the definition of the conditional probability of event A given event B:
where is the joint probability of both A and B occurring.
Now, here is a question for you: How would you write the expression for ? What component is common to both the expression for and your new expression for ?
Answer
The expression for $P(B|A)$ is: $$ P(B|A) = \frac{P(A, B)}{P(A)} $$ The common component is the joint probability, $P(A, B)$.Since the joint probability is the same as , we can express it in two ways by rearranging our conditional probability formulas:
By setting these two expressions equal to each other, we get:
Dividing by gives us the familiar form of Bayes' theorem:
Using our more descriptive terminology (Posterior, Likelihood, Prior, Evidence):
or, for our medical example:
The following resource provides a concise summary of this derivation and related concepts.
13.4: Bayes Rule, Conditional Probability and Independence
This text formally derives Bayes' theorem from the definitions of conditional and joint probability.
Please read the sections Dependence and Conditional Probability and the derivation of Bayes' Theorem. This reinforces the algebraic steps we just walked through.
Expanding the "Evidence" Term
Often, we don't know the evidence directly. However, we can calculate it using the Law of Total Probability. An event can occur either with event or with event "not " (denoted ). Therefore, we can break down as follows:
Using the definition of conditional probability, this becomes:
Substituting this into the denominator of Bayes' theorem gives us its full form, which is often more practical for calculations:
This form is powerful because it allows us to calculate the posterior using only conditional probabilities and priors.
A Powerful Example: The Paradox of Medical Screening
Let's apply the full theorem to a realistic scenario. The cancer screening example from Steve Brunton's lecture is a classic illustration of how Bayesian reasoning can produce counter-intuitive but correct results. It highlights the immense impact of the prior probability, especially for rare events.
Bayes' Theorem (with Example!)
This segment provides a concrete, step-by-step calculation using Bayes' theorem for a medical diagnosis problem.
Watch the example from the cancer screening example. Notice how the full form of Bayes' theorem is used for the denominator. The result is striking: even with a 99% accurate test for a rare disease (0.1% prevalence), a positive test result only yields a 9% probability of actually having the disease. This is because the vast number of healthy people generates more false positives than the small number of sick people generates true positives.
From a Single Update to a Recursive Process
The true power of Bayesian inference in computing is that it's not a one-off calculation. It's a continuous process. The posterior belief from one observation becomes the prior belief for the next. This recursive cycle of prediction and updating is the foundation of many algorithms in robotics and AI.

Let's see this in action with a coin toss example. Imagine we have a coin and we want to estimate its probability of landing heads, which we'll call . We can start with a prior belief about and then update that belief with each new flip.
The GeeksforGeeks article below provides an excellent walkthrough of this process, including a Python implementation. It also introduces an important concept: conjugate priors, where the prior and posterior distributions belong to the same family, making the math much cleaner. For a coin toss (a Bernoulli process), the Beta distribution is a conjugate prior.
This article explains the step-by-step process of Bayesian updating and provides a clear coin toss example with code.
Please read the article from the beginning, focusing on the sections from the introduction to the implementation. Pay special attention to: The setup: The problem is to infer the coin's bias, \theta. The prior: They start with a Beta(1, 1) distribution, which is a uniform distribution (all values of \theta are equally likely). The update: After observing 7 heads and 3 tails, the posterior becomes a Beta(1+7, 1+3) or Beta(8, 4). The data is simply added to the parameters of the prior. The result: The new distribution is now peaked around \theta \approx 0.67, reflecting our updated belief. The Python code shows how to visualize this change.
Observing this updating process leads to a key question. As we collect more and more coin flips, the posterior distribution becomes sharper and more concentrated around a single value. How does this Bayesian view of a narrowing posterior distribution relate to the Law of Large Numbers we studied in the last lesson?
Answer
They are two perspectives on the same phenomenon. The Law of Large Numbers states that the sample average $\bar{X}_n$ converges to the true mean $\mu$. In the Bayesian context, as we gather more data, the posterior distribution for the parameter $\mu$ (or $\theta$ in the coin example) collapses to a single point at its true value. The likelihood term, which represents the data, increasingly dominates the prior, and our uncertainty about the true parameter value diminishes. The Bayesian posterior convergence provides a distributional view of the certainty that the frequentist LLN describes as a point-wise convergence.Conclusion
In this lesson, we shifted our perspective on probability from long-run frequencies to a measure of belief. We saw that Bayes' theorem provides the mathematical framework for updating these beliefs in a principled manner.
Key Takeaways:
- Bayes' Theorem provides a formal way to update a prior belief into a posterior belief after observing new evidence, mediated by the likelihood of that evidence.
- The formula is derived directly from the definition of conditional probability: .
- The denominator, or evidence, can be calculated using the Law of Total Probability, which is essential for practical applications.
- Bayesian inference is a recursive process. The posterior from one step can serve as the prior for the next, allowing systems to learn and adapt as they acquire more data. This is crucial in fields like robotics and online machine learning.
Preview of the Next Lesson:
In the coin toss example, the Bayesian approach gave us a full probability distribution for the coin's bias . While this distribution captures our uncertainty, we often need to choose a single best value for . How would we do that? One common approach is to pick the value of that makes our observed data most probable. This idea is the foundation of our next topic: Maximum Likelihood Estimation (MLE). We will derive the MLE for a Bernoulli distribution and see how it forms the basis for one of the most common loss functions in deep learning.