Overview

13 Conditional probability with Bayes’ theorem

This chapter explains how Bayes’ theorem lets us update beliefs when new evidence appears. Using a simple survey about colds and sneezing, it introduces prior probability, conditional probability, joint probability, and posterior probability, showing how the probability of a cause can be revised after observing an effect. The key idea is that evidence changes confidence in a diagnosis, and Bayes’ theorem provides the exact arithmetic for that update.

The chapter then connects the math to code by representing conditional probabilities as likelihood functions and combining them with priors to form joint distributions. It shows how a likelihood function can model real-world relationships, such as how having a cold affects the chance of sneezing, and how the same machinery can simulate survey data. Because the library uses integer weights, the text also explains how to use the least common multiple to keep the joint distribution exact while preserving the correct proportions.

Finally, the chapter demonstrates how to reason backward from observed effects to likely causes by filtering a joint distribution to obtain a posterior. It applies this idea to medical testing, showing why a rare disease can still have a very low posterior probability even after a highly accurate positive test, and to a coin-flip example where repeated heads shift belief toward a double-headed coin. It ends by broadening the idea to practical uses in software, and by showing that independent random events can be handled with the same joint-distribution tools, including a concise helper for combining distributions and projecting results such as rolling with advantage.

Summary

  • Our opinions before seeing additional evidence, such as the probability that a random person has a cold, are prior probabilities. We represent them as probability distribution objects in our system.
  • Our opinions about conditional probabilities, such as the different probabilities that a person has been sneezing depending on whether they have a cold, are represented as likelihood functions that take a condition and return a distribution.
  • Likelihood functions ideally capture facts about the world that are independent of priors. The fact that people with colds sneeze more than those without is independent of the number of people in the world with a cold right now. Likelihood functions are some of the building blocks of statistical models of reality.
  • Given a prior and a likelihood function, we can compute the joint distribution: the distribution of every possible pair of conditions.
  • From the joint distribution, we can use a combination of a filter and projection to compute the posterior likelihood function. This function takes an observation of an effect, such as sneezing, and returns an improved estimate of a cause, such as having a cold.
  • Bayesian probability can be counterintuitive, particularly when a likelihood function represents the accuracy of a test for a rare condition. If false positives are more likely than true positives, seemingly accurate tests can give highly misleading results.
  • Programs make decisions about actions to take in a real world full of uncertainty. When making evidence-based decisions in software, make sure you’re applying Bayesian logic. Just because a measurement is 99.5% accurate doesn’t imply 99.5% confidence in the result; the prior must be included in the calculation.

FAQ

What is Bayes’ theorem used for in this chapter?Bayes’ theorem is used to reason backward from observed evidence to likely causes, allowing us to update a prior belief into a posterior probability.
What is the difference between prior probability and posterior probability?A prior probability is your belief before observing new evidence, while a posterior probability is your updated belief after incorporating that evidence.
What is a conditional probability?A conditional probability is the probability of one event given that another event has occurred, written with a vertical bar, such as P(S|C).
What is a joint probability?A joint probability is the probability that two events happen together, such as P(C&S) for having a cold and sneezing.
How does the chapter relate joint probability to conditional probability?The chapter shows that joint probability can be computed by multiplying a prior by a conditional probability, for example P(C) × P(S|C) = P(C&S).
What is a likelihood function in this chapter?A likelihood function maps a condition, such as having a cold, to a probability distribution over outcomes, such as whether someone sneezed.
How does the chapter use a marginal table to explain Bayesian reasoning?The marginal table organizes observed data so the total rows and columns can be used to estimate prior, conditional, and joint probabilities.
How is Bayes’ theorem implemented in code in this chapter?The chapter builds a posterior function by creating a joint distribution, filtering it to match the observed evidence, and projecting out the variable of interest.
Why does the chapter say that a positive medical test may still imply a low probability of disease?Because if the disease is rare, false positives can outnumber true positives, so even an accurate test may yield a low posterior probability after a positive result.
What does the chapter show about independent random events?It shows that independent events can also be combined using the same joint-distribution approach, as illustrated by rolling two d20 dice and taking the maximum for advantage.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free