13: Conditional Probability

OI, Ch. 3

Smith College

Feb 27, 2026

Warmup

Suppose you have three cards:

  • one is red on both sides
  • one is green on both sides
  • one is red on one side and green on the other
  • You draw a card. You are looking at a red face.

Question

What is the probability that when you turn the card over, the other side will also be red?

Simulation

library(tidyverse)
deck <- tibble(
  Front = c("R", "R", "G"), 
  Back = c("R", "G", "G")
)
n <- 100000
sim <- deck |>
  sample_n(size = n, replace = TRUE) |>
  mutate(
    draw = sample(c("Front", "Back"), size = n, replace = TRUE),
    see = ifelse(draw == "Front", Front, Back),
    flip = ifelse(draw == "Front", Back, Front)
  )
sim |>
  head()
# A tibble: 6 × 5
  Front Back  draw  see   flip 
  <chr> <chr> <chr> <chr> <chr>
1 R     R     Back  R     R    
2 R     R     Back  R     R    
3 G     G     Back  G     G    
4 G     G     Front G     G    
5 R     R     Front R     R    
6 R     R     Back  R     R    

Results

sim |>
  filter(see == "R") |>
  summarize(
    see_red = n(), 
    flip_red = sum(flip == "R")
  ) |>
  mutate(
    see_red_pct = see_red / n, 
    flip_red_pct = flip_red / see_red
  )
# A tibble: 1 × 4
  see_red flip_red see_red_pct flip_red_pct
    <int>    <int>       <dbl>        <dbl>
1   49874    33334       0.499        0.668

Conditional Probability

Properties

Let \(A, B\) be two events. Then:

  • Law of Total Probability: \[ \Pr(A) = \Pr(A \cap B) + \Pr(A \cap B^c) \]
  • Definition of Conditional Probability: \[ \Pr(A |B) = \frac{\Pr(A \cap B)}{\Pr(B)} \]

Picture

  • Caveat: It is never safe to assume that \(\Pr(A|B) = \Pr(B|A)\)!

Independence

\[\begin{align*} A,B \text{ indep.} &\iff \Pr(A | B) = \Pr(A) \\ &\iff \Pr(B | A) = \Pr(B) \end{align*}\]

  • Note that this implies \(\Pr(A \cap B) = \Pr(A) \cdot \Pr(B)\)
  • General multiplication rule: \[ \Pr(A|B) \cdot \Pr(B) = \Pr(A \cap B) = \Pr(B|A)\cdot \Pr(A) \]

Tip

Do not confuse independence with disjointness! Two events that are disjoint are not independent, since if one happens, you know the other one doesn’t happen!

Bayes’ Rule

\[ \Pr(A|B) = \frac{\Pr(B|A) \cdot \Pr(A)}{\Pr(B)} \]

Contingency tables

  • Joint probabilities in the middle of the table
library(openintro)
yawn |>
  group_by(group, result) |>
  count() |>
  pivot_wider(names_from = result, values_from = n)
# A tibble: 2 × 3
# Groups:   group [2]
  group `not yawn`  yawn
  <fct>      <int> <int>
1 ctrl          12     4
2 trmt          24    10

Tables with margins

  • marginal probabilities on the outside
library(janitor)
yawn |>
  tabyl(group, result) |>
  adorn_totals(where = c("row", "col"))
 group not yawn yawn Total
  ctrl       12    4    16
  trmt       24   10    34
 Total       36   14    50