1 of 113

General Inference

Chris Gregg

CS109, Stanford University

Summer 2026

2 of 113

Why You Need a Model

2

Chris Piech, CS109, 2021

3 of 113

Why You Need a Model

3

Chris Piech, CS109, 2021

4 of 113

Multiple Random Variables. Start of Digital Revolution

Chris Piech, CS109, 2021

5 of 113

Surprisingly Simple (if you can code)

Code

Probability

Chris Piech, CS109, 2021

6 of 113

Four Prototypical Trajectories

Three Guiding Questions

  1. How do people actually define large models?

  1. How can we do inference in large models?

  1. What data can inform the design process?

Chris Piech, CS109, 2021

7 of 113

Four Prototypical Trajectories

At this point you know inference with two random variables

8 of 113

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

9 of 113

Chris Piech, CS109

10 of 113

Likelihood function f(X=x | T=t)? (soln)

Chris Piech, CS109

11 of 113

Observe LiDAR measurement of 4m. f(X=4 | T=t)=?

Chris Piech, CS109

12 of 113

We want a new belief in the true dist., f(T=t | X=4)

Chris Piech, CS109

13 of 113

Four Prototypical Trajectories

Many real world problems have way more than two random variables…

Chris Piech, CS109, 2021

14 of 113

Why You Need a Model

14

Chris Piech, CS109, 2021

15 of 113

Why You Need a Model

15

Chris Piech, CS109, 2021

16 of 113

Multiple Random Variables. Start of Digital Revolution

16

Chris Piech, CS109, 2021

17 of 113

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

18 of 113

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

19 of 113

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

20 of 113

Challenge #2: Joint is Large

  •  

20

 

Flu

Cold

Under-�grad

Tired

Sore�Throat

Fever

Nausea

Strep�Throat

Chest

Pain

Chris Piech, CS109, 2021

21 of 113

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

22 of 113

N can be large…

22

Chris Piech, CS109, 2021

23 of 113

N can be large…

23

Chris Piech, CS109, 2021

24 of 113

Four Prototypical Trajectories

Three Guiding Questions

  1. How do people actually define large models?

  1. How can we do inference in large models?

  1. What data can inform the design process?

Chris Piech, CS109, 2021

25 of 113

Four Prototypical Trajectories

Three Guiding Questions

  1. How do people actually define large models?

  1. How can we do inference in large models?

  1. What data can inform the design process?

Chris Piech, CS109, 2021

26 of 113

Why You Need a Model

26

Chris Piech, CS109, 2021

27 of 113

A simpler WebMD

27

Flu

Under-�grad

Tired

Fever

Chris Piech, CS109, 2021

28 of 113

A simpler WebMD

  •  

28

Flu

Under-�grad

Tired

Fever

Chris Piech, CS109, 2021

29 of 113

Constructing a Bayesian Network

  •  

29

Flu

Under-�grad

Tired

Fever

Chris Piech, CS109, 2021

30 of 113

30

Recall: Probabilistic Model

Fever

Tired

Flu

Undergrad

 

Chris Piech, CS109, 2021

31 of 113

31

Recall: Probabilistic Model

Fever

Tired

Flu

Undergrad

 

Chris Piech, CS109, 2021

32 of 113

32

Recall: Probabilistic Model

Fever

Tired

Flu

Undergrad

 

Check your Understanding:

What is P(Fev=0 | Flu = 1)?

Chris Piech, CS109, 2021

33 of 113

33

Recall: Probabilistic Model

Fever

Tired

Flu

Undergrad

 

Chris Piech, CS109, 2021

34 of 113

Four Prototypical Trajectories

Could we write a python program which makes a fake person from this joint?

Chris Piech, CS109, 2021

35 of 113

To the Code

35

36 of 113

36

37 of 113

37

38 of 113

38

39 of 113

39

40 of 113

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

41 of 113

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

42 of 113

Generative Models make Independence Assumptions

  •  

42

Flu

Under-�grad

Tired

Fever

Chris Piech, CS109, 2021

43 of 113

Generative Models make Independence Assumptions

  • Each random variable is conditionally independent of its causal non-descendants, given its causal parents.

43

Flu

Under-�grad

Tired

Fever

Chris Piech, CS109, 2021

44 of 113

Generative Models make Independence Assumptions

  • This model assumes that Flu and being an Undergraduate are independent.

  • Advanced: it also assumes that fever and tired are conditionally independent given Flu.

  • You need to tell a generative story. The independence assumptions come for free.

44

Flu

Under-�grad

Tired

Fever

Chris Piech, CS109, 2021

45 of 113

Bug: Constructing a Bayesian Network

45

Must by acyclic!

Flu

Under-�grad

Tired

Fever

Chris Piech, CS109, 2021

46 of 113

Four Prototypical Trajectories

Three Guiding Questions

  1. How do people define large models?

  1. How can we do inference in large models?

  1. What data can inform the design process?

Chris Piech, CS109, 2021

47 of 113

Four Prototypical Trajectories

Three Guiding Questions

  1. How do people define large models?

  1. How can we do inference in large models?

  1. What data can inform the design process?

Chris Piech, CS109, 2021

48 of 113

Four Prototypical Trajectories

Chris Piech, CS109, 2021

49 of 113

Algorithm #2: Rejection Sampling

49

Chris Piech, CS109, 2021

50 of 113

Algorithm #2: Rejection Sampling

50

Chris Piech, CS109, 2021

51 of 113

Algorithm #2: Rejection Sampling

51

Chris Piech, CS109, 2021

52 of 113

Algorithm #2: Rejection Sampling

52

Chris Piech, CS109, 2021

53 of 113

Algorithm #2: Rejection Sampling

53

Chris Piech, CS109, 2021

54 of 113

Algorithm #2: Rejection Sampling

54

Chris Piech, CS109, 2021

55 of 113

Algorithm #2: Rejection Sampling

55

Chris Piech, CS109, 2021

56 of 113

Algorithm #2: Rejection Sampling

56

Chris Piech, CS109, 2021

57 of 113

Lets try it!

Chris Piech, CS109, 2021

58 of 113

Rejection sampling algorithm

58

 

Inference�question:

🤔

Chris Piech, CS109, 2021

59 of 113

Rejection sampling algorithm

59

 

 

🤔

Why would this definition of approximate probability make sense?

probability ≈

 

Inference�question:

Chris Piech, CS109, 2021

60 of 113

Why would this approximate probability make sense?

  •  

60

 

 

Recall our definition of probability as a frequency:

 

 

 

Inference�question:

probability ≈

Chris Piech, CS109, 2021

61 of 113

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

62 of 113

Four Prototypical Trajectories

Lets try another question

Chris Piech, CS109, 2021

63 of 113

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 of 113

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 of 113

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

66 of 113

Four Prototypical Trajectories

To the code!

Chris Piech, CS109, 2021

67 of 113

Four Prototypical Trajectories

What’s the matter with

rejection sampling?

Chris Piech, CS109, 2021

68 of 113

68

Probabilistic Model

Fever

Tired

Flu

Undergrad

Chris Piech, CS109, 2021

69 of 113

69

Probabilistic Model

Fever

Tired

Flu

Undergrad

Chris Piech, CS109, 2021

70 of 113

Four Prototypical Trajectories

Back to the code!

Chris Piech, CS109, 2021

71 of 113

71

MCMC

Markov Chain

Monte Carlo

Many Algorithms

Chris Piech, CS109, 2021

72 of 113

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 of 113

73

Many Algorithms

Rejection

Sampling

MCMC

Pyro

Idea2Text

Chris Piech, CS109, 2021

74 of 113

Four Prototypical Trajectories

Three Guiding Questions

  1. How do people define large models?

  1. How can we do inference in large models?

  1. What data can inform the design process?

Chris Piech, CS109, 2021

75 of 113

Four Prototypical Trajectories

Three Guiding Questions

  1. How do people define large models?

  1. How can we do inference in large models?

  1. What data can inform the design process?

Chris Piech, CS109, 2021

76 of 113

Chris Piech, CS109, 2021

77 of 113

From Correlation to Bayes Net!

reggae

rocky

funky

folky

opera

punk

country

dancy

pop

classy

categories

music

Chris Piech, CS109, 2021

78 of 113

Why is it harder to find independences here than for bat DNA expression?

Chris Piech, CS109, 2021

79 of 113

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 of 113

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 of 113

81

Spot The Difference

Chris Piech, CS109, 2021

82 of 113

82

Spot The Difference

Chris Piech, CS109, 2021

83 of 113

83

Vary Together

Chris Piech, CS109, 2021

84 of 113

84

Vary Together

Chris Piech, CS109, 2021

85 of 113

85

Vary Together

Chris Piech, CS109, 2021

86 of 113

86

Understanding Covariance

Chris Piech, CS109, 2021

87 of 113

  • Say X and Y are arbitrary random variables
  • Covariance of X and Y:

87

The Dance of the Covariance

Chris Piech, CS109, 2021

88 of 113

  • Say X and Y are arbitrary random variables
  • Covariance of X and Y:

88

The Dance of the Covariance

Chris Piech, CS109, 2021

89 of 113

  • Say X and Y are arbitrary random variables
  • Covariance of X and Y:

89

The Dance of the Covariance

x

y

(x – E[X])(y – E[Y])p(x,y)

Chris Piech, CS109, 2021

90 of 113

  • Say X and Y are arbitrary random variables
  • Covariance of X and Y:

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 of 113

  • Say X and Y are arbitrary random variables
  • Covariance of X and Y:

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 of 113

  • Say X and Y are arbitrary random variables
  • Covariance of X and Y:

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 of 113

  • Say X and Y are arbitrary random variables
  • Covariance of X and Y:

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 of 113

94

Covariance

Poll: (a) positive, (b) negative, (c) zero

Chris Piech, CS109, 2021

95 of 113

95

Covariance

Is the Covariance: (a) positive, (b) negative, (c) zero

Positive

Chris Piech, CS109, 2021

96 of 113

96

Covariance

Is the Covariance: (a) positive, (b) negative, (c) zero

Chris Piech, CS109, 2021

97 of 113

97

Covariance

Is the Covariance: (a) positive, (b) negative, (c) zero

Negative

Chris Piech, CS109, 2021

98 of 113

98

Covariance

Is the Covariance: (a) positive, (b) negative, (c) zero

Chris Piech, CS109, 2021

99 of 113

99

Covariance

Is the Covariance: (a) positive, (b) negative, (c) zero

Zero

Chris Piech, CS109, 2021

100 of 113

  • Say X and Y are arbitrary random variables
  • Covariance of X and Y:

  • Equivalently:

    • X and Y independent ➔ Cov(X,Y) = 0
    • But Cov(X,Y) = 0 does not imply X and Y independent!

100

The Dance of the Covariance

Chris Piech, CS109, 2021

101 of 113

  • Consider the following data:

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

102 of 113

Four Prototypical Trajectories

Correlation

Chris Piech, CS109, 2021

103 of 113

  • Consider the following data:

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 of 113

  • Consider the following data:

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

105 of 113

Cauchy Schwarz, a great way to normalize!

105

Chris Piech, CS109, 2021

106 of 113

  • Say X and Y are arbitrary random variables
    • Correlation of X and Y, denoted ρ(X, Y):

    • Note: -1 ≤ ρ(X, Y) ≤ 1

    • ρ(X, Y) = 1 ⇒ perfectly correlated
    • ρ(X, Y) = -1 ⇒ perfectly negatively correlated
    • ρ(X, Y) = 0 ⇒ absence of linear relationship
      • But, X and Y can still be related in some other way!
    • If ρ(X, Y) = 0, we say X and Y are “uncorrelated”

106

Viva La Correlatión

Chris Piech, CS109, 2021

107 of 113

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 of 113

108

http://www.aei.org/publication/blog/

Rock Music Vs Oil?

High Correlation

Hubbert Peak Theory

Chris Piech, CS109, 2021

109 of 113

109

Tell your friends!

Chris Piech, CS109, 2021

110 of 113

110

http://www.bbc.com/news/magazine-27537142

Divorce Vs Butter?

Chris Piech, CS109, 2021

111 of 113

Four Prototypical Trajectories

Three Guiding Questions

  1. How do people actually define large models?

  1. How can we do inference in large models?

  1. What data can inform the design process?

Chris Piech, CS109, 2021

112 of 113

Four Prototypical Trajectories

What haven’t we talked about?

Chris Piech, CS109, 2021

113 of 113

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