Gradient
Lesson 5.2MathCode~55 min

Conditional probability & Bayes

How evidence should change your mind — and why a positive result from a good test can still usually be wrong.

Module 5 · 0/9 done

Zooming in

Last lesson ended with conditional probability: P(rain∣cloudy)P(\text{rain} \mid \text{cloudy}), the chance of rain given that it’s cloudy. Here’s how to compute one. Zoom in on the cloudy days only, forget the rest, and count.

P(A∣B)=P(A and B)P(B)P(A \mid B) = \frac{P(A \text{ and } B)}{P(B)}
Try itCounting with a condition
// 200 days of weather, counted:
//                  rain   no rain
//   cloudy           45        35
//   not cloudy        5       115
const cloudyRain = 45, cloudyDry = 35, clearRain = 5, clearDry = 115
const days = cloudyRain + cloudyDry + clearRain + clearDry

console.log("P(rain)          =", (cloudyRain + clearRain) / days)
// "Given cloudy" means: zoom in on the cloudy days only, then count.
console.log("P(rain | cloudy) =", cloudyRain / (cloudyRain + cloudyDry))
console.log("P(rain | clear)  =", clearRain / (clearRain + clearDry))

// The same thing with the formula  P(A | B) = P(A and B) / P(B):
const pRainAndCloudy = cloudyRain / days
const pCloudy = (cloudyRain + cloudyDry) / days
console.log("by formula       =", pRainAndCloudy / pCloudy)

// It does NOT work backwards: P(cloudy | rain) is a different number.
console.log("P(cloudy | rain) =", cloudyRain / (cloudyRain + clearRain))
Press Run (⌘/Ctrl + Enter) to see output here.