Hello! Welcome to the final lesson in our "Mathematical Foundations for AI" module.
In the last lesson, we looked at how to summarize a probability distribution using its expected value (the center) and variance (the spread). These concepts are great for describing a single random variable. However, much of AI is about figuring out how different variables relate to each other—how evidence influences a hypothesis.
Today, we'll dive into exactly that. The learning outcome for this lesson is to apply Bayes' theorem to solve conditional probability problems. Bayes' theorem is a cornerstone of probability theory and machine learning. It provides a formal way to update our beliefs in light of new evidence. This principle is the engine behind many AI systems, from simple spam filters to complex diagnostic models.
Let's begin by building an intuition for what it means to "update a belief."
1. Intuition: Evidence Updates Beliefs
Before we look at any formulas, let's consider a simple thought experiment.
Imagine you hear a description of a person named Steve: "Steve is very shy and withdrawn, invariably helpful but with very little interest in people... a meek and tidy soul, he has a need for order and structure."
Is Steve more likely to be a librarian or a farmer?
Most people immediately say "librarian" because the description fits the stereotype. However, this line of thinking misses a crucial piece of information: the base rate. There are far more farmers than librarians in the world. How should this fact influence our judgment?
This is precisely the kind of problem Bayes' theorem helps us solve. The following video from 3Blue1Brown provides a brilliant explanation.
Bayes theorem, the geometry of changing beliefs
This video, 'Bayes theorem, the geometry of changing beliefs', uses the 'Steve the Librarian/Farmer' example to build a strong intuition for how prior beliefs should be updated by new evidence. It lays the conceptual groundwork before introducing the formal theorem.
Watch the section from 00:55 to 04:46. Focus on how the video combines two pieces of information: The ratio of farmers to librarians (the 'prior belief'). The likelihood that someone with Steve's personality is a farmer vs. a librarian (the 'evidence').
The key idea from the video is that new evidence does not determine your beliefs in a vacuum; it updates your prior beliefs. Our initial belief about the probability of someone being a librarian is low simply because there are few librarians. The evidence (the personality description) updates this belief, but it may not be strong enough to overcome the low initial probability.
2. Formalizing Bayes' Theorem
Now that we have the intuition, let's formalize it. Bayes' theorem gives us a mathematical formula to perform this belief update.
Let's use for a Hypothesis we care about and for the Evidence we've observed. We want to find , the probability of the hypothesis being true given that we've seen the evidence.

The components are:
- Posterior Probability : The updated probability of the hypothesis being true after considering the evidence . This is what we usually want to calculate.
- Prior Probability : The initial probability of the hypothesis being true, before considering the evidence. In our example, this was the base rate of librarians in the population.
- Likelihood : The probability of observing the evidence if the hypothesis were true. In our example, this is the probability that a person has Steve's personality given they are a librarian.
- Evidence : The total probability of observing the evidence, regardless of the hypothesis. This term acts as a normalization constant.
Breaking Down the 'Evidence' Term
The denominator, , can be tricky to calculate directly. We usually expand it using the law of total probability. It's the sum of the probabilities of seeing the evidence in all possible scenarios. For a simple case where the hypothesis is either true () or false (), we calculate it as:
This means: (Probability of evidence if hypothesis is true * probability of hypothesis being true) + (Probability of evidence if hypothesis is false * probability of hypothesis being false).
Plugging this into the main formula gives us the most common form of Bayes' theorem used in practice:
To see a clear derivation and another explanation of these terms, the following video is very helpful.
Bayes' Theorem (with Example!)
This video from the Steve Brunton channel, 'Bayes' Theorem (with Example!)', provides a concise derivation of the formula from the definition of conditional probability and clearly defines the terms posterior, prior, and update.
Watch from the beginning to 10:57. Focus on how the theorem is just a rearrangement of the conditional probability formula and how the denominator P(A) is expanded using the law of total probability.
3. Applying Bayes' Theorem: A Practical Example
Let's apply the theorem to a classic problem that highlights why it's so important: medical testing. This example shows how our intuition can be misleading and how Bayes' theorem provides the correct way to think.
Problem:
Suppose a test for a particular drug is 97% sensitive and 95% specific.
- Sensitivity means it correctly identifies 97% of users (a 97% true positive rate).
- Specificity means it correctly identifies 95% of non-users (a 95% true negative rate).
Let's also assume that 0.5% of the general population are users of the drug. If a randomly selected person tests positive, what is the probability they are actually a drug user?
This is a perfect scenario for a software engineer like you to see how to translate a word problem into a structured calculation. The following resource not only explains the theory but also provides a Python function to solve it.
Bayes' rule application using Python
The notebook 'Bayes' rule application using Python' by Dr. Tirthajyoti Sarkar provides an excellent, code-first approach to solving this type of problem. It directly aligns with your background and shows how to implement the theorem programmatically.
Read the sections 'Drug screening example', 'Theory', 'Custom function', and 'Run with the details given in the original question'. Pay close attention to how the problem's parameters (sensitivity, specificity, prevalence) map to the terms in the Bayes' formula within the Python function.
Step-by-Step Calculation
Let's break down the problem from the article and map it to our formula:
- Hypothesis (): The person is a drug user.
- Evidence (): The person tested positive.
- We want to find: .
Now let's identify the probabilities we're given:
- Prior : The prevalence in the population is 0.5%, so .
- This also means .
- Likelihood : The test's sensitivity is 97%, so .
- Likelihood of evidence if hypothesis is false : The test's specificity is 95%, which is the probability of a negative test for a non-user (). The probability of a positive test for a non-user (a false positive) is . So, .
Now, let's plug these into the full formula:
The result is surprising: even with a positive result from a seemingly accurate test, there is only an 8.9% chance that the person is actually a drug user. This counter-intuitive result is known as the base rate fallacy. The very low prevalence (prior) of drug users means that the absolute number of false positives from the large non-user population swamps the true positives from the small user population.
The Power of Iterative Updates
One of the most powerful aspects of Bayesian reasoning is that it's iterative. If a person tests positive, our new belief that they are a user is 8.9%. This is no longer the 0.5% general population rate. If we administer a second test, this 8.9% becomes our new prior.
The same notebook Bayes' rule application using Python demonstrates this beautifully in the section "How about applying the Bayes' rule twice (or more)?". After a second positive test, the probability jumps to 65.4%, and after a third, it's 97.3%! This shows how we can become increasingly confident as we gather more evidence.
Test your understanding!
Let's use the factory problem from the same resource (LINK, section "Factory quality assesment example"). A factory has three machines (A, B, C) producing its entire output.
- Machine A produces 20% of the output.
- Machine B produces 30% of the output.
- Machine C produces 50% of the output.
The defect rate for each machine is:
- Machine A: 5%
- Machine B: 3%
- Machine C: 1%
If you pick a defective item at random, what is the probability it was produced by Machine A?
Show answer
Let be the event that an item is defective. Let , , and be the events that the item came from Machine A, B, or C, respectively. We want to find .
1. Identify the Priors:
2. Identify the Likelihoods:
3. Calculate the Total Probability of a Defect, :
This is the denominator in Bayes' theorem. We sum the probability of a defect coming from each machine.
So, 2.4% of all items produced are defective.
4. Apply Bayes' Theorem to find :
So, there is a 41.7% chance that the defective item came from Machine A. Notice how this posterior probability (41.7%) is much higher than the prior probability that an item comes from Machine A (20%). The evidence (the defect) strongly updated our belief.
Conclusion
Congratulations on completing the "Mathematical Foundations for AI" module! Today, you've mastered a fundamental tool for reasoning under uncertainty.
Here are the key takeaways from this lesson:
- Bayes' Theorem is a formula for updating the probability of a hypothesis () based on new evidence () to get a posterior probability ().
- The formula is .
- Priors Matter: Ignoring the base rate (prior probability) can lead to highly inaccurate conclusions, a mistake known as the base rate fallacy.
- Iterative Updates: The posterior probability from one observation can serve as the prior for the next, allowing us to systematically strengthen our beliefs as we gather more data.
This lesson marks the end of our journey through the essential math for AI. You've refreshed your knowledge of linear algebra, calculus, and probability. Now, it's time to put this theory into practice.
In the next module, "Python Tools for AI and Data Manipulation," we will get hands-on with the essential libraries that every AI engineer uses daily: NumPy and Pandas. You'll learn how to use these powerful tools to perform the vector, matrix, and statistical operations we've been discussing, but at a scale and speed required for real-world machine learning.