Pick a depth. Each prompt opens in your AI pre-loaded with the lesson. Click a row to preview the prompt.
The law of total probability lets you compute when you only know on a partition of the universe. In practice this is how almost every real probability gets calculated: you condition on a hidden cause, sum out, and collapse to the marginal. Diagnostics, mixture models, importance sampling, dynamic programming on probabilistic transitions — they all reduce to this one identity. Once you see it everywhere, you stop trying to compute probabilities directly when conditioning would make the problem trivial.
If partitions (pairwise disjoint, covering all of ) with each , then the marginal is the weighted average of the conditionals , weighted by .
// main.go
// Two urns. Pick an urn, then a ball.
// Urn 1: 7 red, 3 blue. Urn 2: 2 red, 8 blue.
// Pick urn 1 with prob 0.6, urn 2 with prob 0.4.
package main
import (
"fmt"
"math/rand"
)
func main() {
pU1, pU2 := 0.6, 0.4
pRgivenU1 := 7.0 / 10.0
pRgivenU2 := 2.0 / 10.0
pR := pRgivenU1*pU1 + pRgivenU2*pU2
fmt.Println("P(red) =", pR) // 0.5
// Verify by Monte Carlo
rng := rand.New(rand.NewSource(0))
N := 200_000
hits := 0
for i := 0; i < N; i++ {
var pR2 float64
if rng.Float64() < pU1 {
pR2 = pRgivenU1
} else {
pR2 = pRgivenU2
}
if rng.Float64() < pR2 {
hits++
}
}
fmt.Println("Monte Carlo P(red) =", float64(hits)/float64(N))
}go run main.go