General Inference
Chris Gregg
CS109, Stanford University
Summer 2026
Why You Need a Model
2
Chris Piech, CS109, 2021
Why You Need a Model
3
Chris Piech, CS109, 2021
Multiple Random Variables. Start of Digital Revolution
Chris Piech, CS109, 2021
Surprisingly Simple (if you can code)
Code
Probability
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Three Guiding Questions
Chris Piech, CS109, 2021
Four Prototypical Trajectories
At this point you know inference with two random variables
Last time: Five New Real + Exciting Problems
8
Age from C14
Updated Delivery Prob
Age from Name
Hidden Chambers
Stanford Eye Test
Updating Lidar Belief
Today
Chris Piech, CS109
Chris Piech, CS109
Likelihood function f(X=x | T=t)? (soln)
Chris Piech, CS109
Observe LiDAR measurement of 4m. f(X=4 | T=t)=?
Chris Piech, CS109
We want a new belief in the true dist., f(T=t | X=4)
Chris Piech, CS109
Four Prototypical Trajectories
Many real world problems have way more than two random variables…
Chris Piech, CS109, 2021
Why You Need a Model
14
Chris Piech, CS109, 2021
Why You Need a Model
15
Chris Piech, CS109, 2021
Multiple Random Variables. Start of Digital Revolution
16
Chris Piech, CS109, 2021
Challenge #1: Many Inference Questions
17
Inference question:
Given the values of some random�variables, what are the conditional�distributions of some other random�variables?
Flu
Cold
Under-�grad
Tired
Sore�Throat
Fever
Nausea
Strep�Throat
Chest
Pain
Chris Piech, CS109, 2021
Challenge #1: Many Inference Questions
18
One inference question:
Flu
Cold
Under-�grad
Tired
Sore�Throat
Fever
Nausea
Strep�Throat
Chest
Pain
Chris Piech, CS109, 2021
Challenge #1: Many Inference Questions
19
Another inference question:
Flu
Cold
Under-�grad
Tired
Sore�Throat
Fever
Nausea
Strep�Throat
Chest
Pain
Chris Piech, CS109, 2021
Challenge #2: Joint is Large
20
Flu
Cold
Under-�grad
Tired
Sore�Throat
Fever
Nausea
Strep�Throat
Chest
Pain
Chris Piech, CS109, 2021
Challenge #2: Joint is Large
21
Naively specifying a joint distribution is, in general, intractable.
Flu
Cold
Under-�grad
Tired
Sore�Throat
Fever
Nausea
Strep�Throat
Chest
Pain
Chris Piech, CS109, 2021
N can be large…
22
Chris Piech, CS109, 2021
N can be large…
23
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Three Guiding Questions
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Three Guiding Questions
Chris Piech, CS109, 2021
Why You Need a Model
26
Chris Piech, CS109, 2021
A simpler WebMD
27
Flu
Under-�grad
Tired
Fever
Chris Piech, CS109, 2021
A simpler WebMD
28
Flu
Under-�grad
Tired
Fever
Chris Piech, CS109, 2021
Constructing a Bayesian Network
29
✅
Flu
Under-�grad
Tired
Fever
Chris Piech, CS109, 2021
30
Recall: Probabilistic Model
Fever
Tired
Flu
Undergrad
Chris Piech, CS109, 2021
31
Recall: Probabilistic Model
Fever
Tired
Flu
Undergrad
✅
Chris Piech, CS109, 2021
32
Recall: Probabilistic Model
Fever
Tired
Flu
Undergrad
✅
Check your Understanding:
What is P(Fev=0 | Flu = 1)?
Chris Piech, CS109, 2021
33
Recall: Probabilistic Model
Fever
Tired
Flu
Undergrad
✅
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Could we write a python program which makes a fake person from this joint?
Chris Piech, CS109, 2021
To the Code
35
36
37
38
39
Can You Sample from the Joint?
40
Writing a python program that can sample from the joint, is the same as defining the joint.
Chris Piech, CS109, 2021
Make a Generative Model
41
A good probabilistic model is generative. It explains the process through which the joint is created.
Chris Piech, CS109, 2021
Generative Models make Independence Assumptions
42
✅
✅
Flu
Under-�grad
Tired
Fever
Chris Piech, CS109, 2021
Generative Models make Independence Assumptions
43
Flu
Under-�grad
Tired
Fever
Chris Piech, CS109, 2021
Generative Models make Independence Assumptions
44
Flu
Under-�grad
Tired
Fever
Chris Piech, CS109, 2021
Bug: Constructing a Bayesian Network
45
Must by acyclic!
Flu
Under-�grad
Tired
Fever
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Three Guiding Questions
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Three Guiding Questions
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Chris Piech, CS109, 2021
Algorithm #2: Rejection Sampling
49
Chris Piech, CS109, 2021
Algorithm #2: Rejection Sampling
50
Chris Piech, CS109, 2021
Algorithm #2: Rejection Sampling
51
Chris Piech, CS109, 2021
Algorithm #2: Rejection Sampling
52
Chris Piech, CS109, 2021
Algorithm #2: Rejection Sampling
53
Chris Piech, CS109, 2021
Algorithm #2: Rejection Sampling
54
Chris Piech, CS109, 2021
Algorithm #2: Rejection Sampling
55
Chris Piech, CS109, 2021
Algorithm #2: Rejection Sampling
56
Chris Piech, CS109, 2021
Lets try it!
Chris Piech, CS109, 2021
Rejection sampling algorithm
58
Inference�question:
🤔
Chris Piech, CS109, 2021
Rejection sampling algorithm
59
🤔
Why would this definition of approximate probability make sense?
probability ≈
Inference�question:
Chris Piech, CS109, 2021
Why would this approximate probability make sense?
60
Recall our definition of probability as a frequency:
Inference�question:
probability ≈
Chris Piech, CS109, 2021
61
Each one of these is one joint sample
If you can sample enough from the joint distribution, you can answer any probability question
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Lets try another question
Chris Piech, CS109, 2021
63
Cousin 1
Cousin 2
?
You observe that someone has a recessive gene.
What is the probability that their cousin has the same recessive gene?
Each person has a 1/20 chance of having the recessive gene.
64
Cousin 1
Cousin 2
?
Parent 1
Parent 2
?
?
Grand Parent 1
?
Grand Parent 2
?
Spouse 1
?
Spouse 2
?
You observe that someone has a recessive gene.
What is the probability that their cousin has the same recessive gene?
65
?
?
?
?
?
?
?
You observe that someone has a recessive gene.
What is the probability that their cousin has the same recessive gene?
Cousin 1
Cousin 2
Parent 1
Parent 2
Grand Parent 1
Grand Parent 2
Spouse 1
Spouse 2
Four Prototypical Trajectories
To the code!
Chris Piech, CS109, 2021
Four Prototypical Trajectories
What’s the matter with
rejection sampling?
Chris Piech, CS109, 2021
68
Probabilistic Model
Fever
Tired
Flu
Undergrad
Chris Piech, CS109, 2021
69
Probabilistic Model
Fever
Tired
Flu
Undergrad
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Back to the code!
Chris Piech, CS109, 2021
71
MCMC
Markov Chain
Monte Carlo
Many Algorithms
Chris Piech, CS109, 2021
72
Each one of these is one posterior sample:
[Flu, Undergrad, Fever, Tired]
MCMC is a way to sample with conditioned variables fixed
Many Algorithms
Chris Piech, CS109, 2021
73
Many Algorithms
Rejection
Sampling
MCMC
Pyro
Idea2Text
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Three Guiding Questions
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Three Guiding Questions
Chris Piech, CS109, 2021
Chris Piech, CS109, 2021
From Correlation to Bayes Net!
reggae
rocky
funky
folky
opera
punk
country
dancy
pop
classy
categories
music
Chris Piech, CS109, 2021
Why is it harder to find independences here than for bat DNA expression?
Chris Piech, CS109, 2021
79
Gene1 | Gene2 | Gene3 | Gene4 | Gene5 | Trait |
TRUE | FALSE | TRUE | TRUE | FALSE | FALSE |
FALSE | FALSE | TRUE | TRUE | TRUE | TRUE |
TRUE | FALSE | TRUE | FALSE | FALSE | FALSE |
TRUE | FALSE | TRUE | TRUE | TRUE | FALSE |
FALSE | TRUE | TRUE | TRUE | TRUE | TRUE |
FALSE | FALSE | FALSE | TRUE | FALSE | FALSE |
TRUE | FALSE | FALSE | TRUE | FALSE | FALSE |
TRUE | FALSE | FALSE | TRUE | FALSE | FALSE |
TRUE | FALSE | TRUE | FALSE | FALSE | FALSE |
FALSE | TRUE | FALSE | TRUE | FALSE | FALSE |
TRUE | TRUE | FALSE | TRUE | FALSE | FALSE |
TRUE | FALSE | FALSE | TRUE | FALSE | FALSE |
TRUE | FALSE | TRUE | TRUE | TRUE | FALSE |
FALSE | FALSE | TRUE | TRUE | FALSE | FALSE |
TRUE | FALSE | FALSE | TRUE | FALSE | FALSE |
TRUE | FALSE | FALSE | TRUE | FALSE | FALSE |
… | |||||
TRUE | FALSE | FALSE | TRUE | FALSE | FALSE |
Bat Data
Chris Piech, CS109, 2021
80
Gene5 | Trait |
0.76 | 0.83 |
0.94 | 0.85 |
0.82 | 0.03 |
0.94 | 0.32 |
0.50 | 0.10 |
0.40 | 0.53 |
0.90 | 0.67 |
0.29 | 0.71 |
0.72 | 0.25 |
0.15 | 0.24 |
0.79 | 0.98 |
0.68 | 0.77 |
0.71 | 0.37 |
0.36 | 0.18 |
0.62 | 0.08 |
0.59 | 0.38 |
| |
0.82 | 0.76 |
Expression Amount
Chris Piech, CS109, 2021
81
Spot The Difference
Chris Piech, CS109, 2021
82
Spot The Difference
Chris Piech, CS109, 2021
83
Vary Together
Chris Piech, CS109, 2021
84
Vary Together
Chris Piech, CS109, 2021
85
Vary Together
Chris Piech, CS109, 2021
86
Understanding Covariance
Chris Piech, CS109, 2021
87
The Dance of the Covariance
Chris Piech, CS109, 2021
88
The Dance of the Covariance
Chris Piech, CS109, 2021
89
The Dance of the Covariance
x
y
(x – E[X])(y – E[Y])p(x,y)
Chris Piech, CS109, 2021
90
The Dance of the Covariance
x
y
Above mean
Above mean
Positive
(x – E[X])(y – E[Y])p(x,y)
Chris Piech, CS109, 2021
91
The Dance of the Covariance
x
y
Above mean
Above mean
Positive
Bellow mean
Bellow mean
Positive
(x – E[X])(y – E[Y])p(x,y)
Chris Piech, CS109, 2021
92
The Dance of the Covariance
x
y
(x – E[X])(y – E[Y])p(x,y)
Above mean
Above mean
Positive
Bellow mean
Bellow mean
Positive
Bellow mean
Above mean
Negative
Chris Piech, CS109, 2021
93
The Dance of the Covariance
x
y
Above mean
Above mean
Positive
Bellow mean
Bellow mean
Positive
Bellow mean
Above mean
Negative
Above mean
Bellow mean
Negative
(x – E[X])(y – E[Y])p(x,y)
Chris Piech, CS109, 2021
94
Covariance
Poll: (a) positive, (b) negative, (c) zero
Chris Piech, CS109, 2021
95
Covariance
Is the Covariance: (a) positive, (b) negative, (c) zero
Positive
Chris Piech, CS109, 2021
96
Covariance
Is the Covariance: (a) positive, (b) negative, (c) zero
Chris Piech, CS109, 2021
97
Covariance
Is the Covariance: (a) positive, (b) negative, (c) zero
Negative
Chris Piech, CS109, 2021
98
Covariance
Is the Covariance: (a) positive, (b) negative, (c) zero
Chris Piech, CS109, 2021
99
Covariance
Is the Covariance: (a) positive, (b) negative, (c) zero
Zero
Chris Piech, CS109, 2021
100
The Dance of the Covariance
Chris Piech, CS109, 2021
101
Weight | Height | Weight * Height |
64 | 57 | 3648 |
71 | 59 | 4189 |
53 | 49 | 2597 |
67 | 62 | 4154 |
55 | 51 | 2805 |
58 | 50 | 2900 |
77 | 55 | 4235 |
57 | 48 | 2736 |
56 | 42 | 2352 |
51 | 42 | 2142 |
76 | 61 | 4636 |
68 | 57 | 3876 |
| | |
E[W] = 62.75 | E[H] = 52.75 | E[W*H] = 3355.83 |
Cov(W, H) = E[W*H] – E[W]E[H]
= 3355.83 – (62.75)(52.75)
= 45.77
Covariance and Data
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Correlation
Chris Piech, CS109, 2021
103
Weight | Height | Weight * Height |
64 | 57 | 3648 |
71 | 59 | 4189 |
53 | 49 | 2597 |
67 | 62 | 4154 |
55 | 51 | 2805 |
58 | 50 | 2900 |
77 | 55 | 4235 |
57 | 48 | 2736 |
56 | 42 | 2352 |
51 | 42 | 2142 |
76 | 61 | 4636 |
68 | 57 | 3876 |
| | |
E[W] = 62.75 | E[H] = 52.75 | E[W*H] = 3355.83 |
Cov(W, H) = E[W*H] – E[W]E[H]
= 3355.83 – (62.75)(52.75)
= 45.77
What is Wrong With This?
Chris Piech, CS109, 2021
104
Weight | Height | Weight * Height |
64 | 57 | 3648 |
71 | 59 | 4189 |
53 | 49 | 2597 |
67 | 62 | 4154 |
55 | 51 | 2805 |
58 | 50 | 2900 |
77 | 55 | 4235 |
57 | 48 | 2736 |
56 | 42 | 2352 |
51 | 42 | 2142 |
76 | 61 | 4636 |
68 | 57 | 3876 |
| | |
E[W] = 62.75 | E[H] = 52.75 | E[W*H] = 3355.83 |
Cov(W, H) = E[W*H] – E[W]E[H]
= 3355.83 – (62.75)(52.75)
= 45.77
What is Wrong With This?
Chris Piech, CS109, 2021
Cauchy Schwarz, a great way to normalize!
105
Chris Piech, CS109, 2021
106
Viva La Correlatión
Chris Piech, CS109, 2021
Recall: It is a useful starting point
107
reggae
rocky
funky
folky
opera
punk
country
dancy
pop
classy
categories
music
Chris Piech, CS109, 2021
108
http://www.aei.org/publication/blog/
Rock Music Vs Oil?
High Correlation
Hubbert Peak Theory
Chris Piech, CS109, 2021
109
Tell your friends!
Chris Piech, CS109, 2021
110
http://www.bbc.com/news/magazine-27537142
Divorce Vs Butter?
Chris Piech, CS109, 2021
Four Prototypical Trajectories
Three Guiding Questions
Chris Piech, CS109, 2021
Four Prototypical Trajectories
What haven’t we talked about?
Chris Piech, CS109, 2021
Machine Learning (last section of CS109)
113
Flu
Under-�grad
Tired
Fever
1. Learn this from data
2. Learn this from data
Chris Piech, CS109, 2021