1 of 149

1

Parameter Estimation

Chris Gregg

CS109, Stanford University

Summer 2026

2 of 149

Where are we in CS109?

2

You are here

3 of 149

3

4 of 149

4

5 of 149

5

General “Inference”

6 of 149

6

Probabilistic Model

Fever

Tired

Flu

Undergrad

7 of 149

7

Probabilistic Model

Fever

Tired

Flu

Undergrad

If you know the probability of each random variables given the ones that directly cause it, you can joint sample!

8 of 149

8

Four Prototypical Trajectories

But where do those numbers come from?

9 of 149

9

Four Prototypical Trajectories

Suspense

10 of 149

10

Four Prototypical Trajectories

At this point, if you are given a model, with all the involved probabilities, you can make predictions

11 of 149

11

Four Prototypical Trajectories

But what if you want to learn the probabilities in the model?

12 of 149

12

Four Prototypical Trajectories

Machine Learning

13 of 149

13

AI and Machine Learning

ML: Rooted in probability theory

Artificial

Intelligence

Machine

Learning

Deep

Learning

Gen AI

14 of 149

14

Our Path

Parameter Estimation

Deep Learning

Core Algorithms

15 of 149

15

Our Path

Deep Learning

Core Algorithms

Unbiased

estimators

Maximizing

likelihood

16 of 149

16

17 of 149

17

Four Prototypical Trajectories

Review

18 of 149

Shorthand for Equality Events

18

Our shorthand notation

Is shorthand for the event

Is shorthand for the event

Full Notation

19 of 149

19

Four Prototypical Trajectories

End Review

20 of 149

20

21 of 149

21

Once upon a time…

22 of 149

22

…there was parameter estimation

23 of 149

23

What are Parameters?

24 of 149

  • Consider some probability distributions:
    • Ber(p)
    • Poi(λ)
    • Uni(, β)
    • Normal(μ, σ2)
    • Y = mX + b
    • etc…
  • Call these “parametric models”
  • Given model, parameters yield actual distribution
    • Usually refer to parameters of distribution as θ
    • Note that θ that can be a vector of parameters

24

What are Parameters?

25 of 149

  • Consider some probability distributions:
    • Ber(p)
    • Poi(λ)
    • Uni(, β)
    • Normal(μ, σ2)
    • Y = mX + b
    • etc…
  • Call these “parametric models”
  • Given model, parameters yield actual distribution
    • Usually refer to parameters of distribution as θ
    • Note that θ that can be a vector of parameters

25

What are Parameters?

26 of 149

  • Consider some probability distributions:
    • Ber(p)
    • Poi(λ)
    • Uni(, β)
    • Normal(μ, σ2)
    • Y = mX + b
    • etc…
  • Call these “parametric models”
  • Given model, parameters yield actual distribution
    • Usually refer to parameters of distribution as θ
    • Note that θ that can be a vector of parameters

26

What are Parameters?

27 of 149

  • Consider some probability distributions:
    • Ber(p)
    • Poi(λ)
    • Uni(, β)
    • Normal(μ, σ2)
    • Y = mX + b
    • etc…
  • Call these “parametric models”
  • Given model, parameters yield actual distribution
    • Usually refer to parameters of distribution as θ
    • Note that θ that can be a vector of parameters

27

θ = p

θ = λ

θ = (α, β)

θ = (μ, σ2)

θ = (m, b)

What are Parameters?

28 of 149

28

Fever

Tired

Flu

Undergrad

Parameters

What are Parameters?

29 of 149

29

Why Do We Care?

Real World Problem

Formal Model 𝜃

Prediction

Function 𝜃*

Model the problem

Learning Algorithm

Testing

Data

Training Data

Evaluation

score

30 of 149

30

Modelling

Real World Problem

Formal Model 𝜃

Prediction

Function 𝜃*

Model the problem

Learning Algorithm

Testing

Data

Training Data

Evaluation

score

31 of 149

31

Real World Problem

Model the problem

Parameter Estimation (aka Training)

Formal Model 𝜃

Prediction

Function 𝜃*

Learning Algorithm

Testing

Data

Training Data

Evaluation

score

32 of 149

32

We have already seen some

parameter estimators!

33 of 149

33

9 Heads out of 10 Flips. What is your Belief in p?

34 of 149

34

Unbiased Estimators of Mean and Variance

35 of 149

  •  

35

 

Unbiased Estimators of Mean and Variance

36 of 149

  •  

36

 

 

Unbiased Estimators of Mean and Variance

37 of 149

  •  

37

 

 

Sample variance:

 

Unbiased Estimators of Mean and Variance

38 of 149

  •  

38

 

 

Sample variance:

 

Unbiased Estimators of Mean and Variance

39 of 149

39

Our Path

Deep Learning

Core Algorithms

Unbiased

estimators

Maximizing

likelihood

40 of 149

40

Unbiased Estimation is a limited tool:

how could we use that for fitting WebMD?

41 of 149

41

Great idea in Machine Learning

42 of 149

42

To the Course Reader!

43 of 149

43

We want to choose the parameter value that maximizes the probability of the data.

How to Choose the “Best” Parameters: MLE

44 of 149

44

We want to choose the parameter value that maximizes the probability of the data.

Maximum

Likelihood

Estimation!

How to Choose the “Best” Parameters: MLE

45 of 149

45

“I feel seen”

46 of 149

46

47 of 149

47

We want to choose the parameter value that maximizes the probability of the data.

Maximum

Likelihood

Estimation!

How do we quantify “probability of the data”?

How to Choose the “Best” Parameters: MLE

48 of 149

48

A generalized term for “PDF / PMF / Joint”

of data as a function of parameters

Wikipedia:

Likelihood Definition

49 of 149

49

θ is shorthand for parameter(s)

(if we have a Poisson, θ = λ)

Definition: The probability of our observed data if our parameters were θ.

The Likelihood Function

50 of 149

50

(in our example, this would just be the Poisson PMF)

If we had a single observation, X = x:

The Likelihood Function

Definition: The probability of our observed data if our parameters were θ.

51 of 149

51

For a list of observations, [x1, x2, …, xn]:

The Likelihood Function

Definition: The joint probability of our observed data if our parameters were θ.

52 of 149

52

For a list of observations, [x1, x2, …, xn]:

The Likelihood Function

Definition: The joint probability of our observed data if our parameters were θ.

We assume that data points are I.I.D.

53 of 149

53

For a list of observations, [x1, x2, …, xn]:

The Likelihood Function

Definition: The joint probability of our observed data if our parameters were θ.

We assume that data points are I.I.D.

54 of 149

54

For a list of observations, [x1, x2, …, xn]:

The Likelihood Function

Definition: The joint probability of our observed data if our parameters were θ.

We assume that data points are I.I.D.

We always use f for likelihood in MLE (even for discrete) 🙃

55 of 149

55

The Likelihood Function

n I.I.D. data points

We explicitly specify parameter θ of distribution

This is just a product since Xi are I.I.D.

56 of 149

56

Likelihood (of data given parameters):

Either the

PDF (continuous) or

PMF (discrete), or

joint if multiple variables per datapoint

57 of 149

57

We want to choose the parameter value that maximizes the probability of the data:

How to Choose the “Best” Parameters: MLE

Likelihood

58 of 149

58

We want to choose the parameter value that maximizes the probability of the data:

How to Choose the “Best” Parameters: MLE

Likelihood

To put words into math:

Our best estimate

59 of 149

59

We want to choose the parameter value that maximizes the probability of the data:

How to Choose the “Best” Parameters: MLE

Likelihood

To put words into math:

Our best estimate

Log Likelihood

60 of 149

60

Sidequest: What is argmax?

61 of 149

61

Argmax

62 of 149

62

63 of 149

63

 

64 of 149

64

But how do we compute argmax?

65 of 149

65

Option #1: Straight optimization

66 of 149

66

Finding the Argmax with Calculus

67 of 149

67

Differentiate w.r.t.�argmax’s argument

Finding the Argmax with Calculus

68 of 149

68

Differentiate w.r.t.�argmax’s argument

Finding the Argmax with Calculus

69 of 149

69

Differentiate w.r.t.�argmax’s argument

Finding the Argmax with Calculus

70 of 149

70

Differentiate w.r.t.�argmax’s argument

Set to 0 and solve

Finding the Argmax with Calculus

71 of 149

71

Differentiate w.r.t.�argmax’s argument

Set to 0 and solve

 

Finding the Argmax with Calculus

72 of 149

72

Differentiate w.r.t.�argmax’s argument

Set to 0 and solve

 

 

 

Finding the Argmax with Calculus

73 of 149

73

Argmax of Log

Claim:

Log is monotonic

x ≤ y ⇔ log(x) ≤ log(y) for all x, y > 0

74 of 149

74

Argmax of Log

75 of 149

75

Log I Love You

76 of 149

76

Natural Log

77 of 149

77

End Sidequest

78 of 149

78

Let’s go deeper!

79 of 149

79

MLE For Poisson

80 of 149

80

We observed the following samples:

[6, 1, 2, 1, 2, 3, 3, 2, 1, 3, 1, 3]

What is lambda, ?

λ

MLE For Poisson

81 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

81

1. What is the likelihood of one Xi

2. What is the likelihood of all the data

3. What is the log-likelihood all the data

4. Find the value of λ which maximizes log likelihood

MLE For Poisson

82 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

82

2. What is the likelihood of all the data

3. What is the log-likelihood all the data

4. Find the value of λ which maximizes log likelihood

MLE For Poisson

83 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

83

3. What is the log-likelihood all the data

4. Find the value of λ which maximizes log likelihood

MLE For Poisson

84 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

84

4. Find the value of λ which maximizes log likelihood

MLE For Poisson

85 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

85

4. Find the value of λ which maximizes log likelihood

MLE For Poisson

86 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

86

4. Find the value of λ which maximizes log likelihood

MLE For Poisson

87 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

87

MLE For Poisson

88 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

88

MLE For Poisson

89 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

Differentiate w.r.t. λ, and set to 0:

89

MLE For Poisson

90 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

90

MLE For Poisson

91 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate λ
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

    • Differentiate w.r.t. λ, and set to 0:

91

MLE For Poisson

92 of 149

92

Isn’t that the same as

the sample mean?

93 of 149

93

Yes. For Poisson.

94 of 149

94

MLE For Poisson

95 of 149

95

MLE For Poisson

96 of 149

96

We know sand is distributed as a pareto with PDF

MLE For Pareto

97 of 149

97

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Pareto(α). Use Maximum Likelihood to estimate α.
    • Likelihood:

    • Log-likelihood:

    • Chose α to be the argmax of LL:

MLE for a Pareto

3. Find the value of α which maximizes log likelihood

1. What is the likelihood of all the data

2. What is the log-likelihood all the data

98 of 149

98

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Pareto(α). Use Maximum Likelihood to estimate α.
    • Likelihood:

    • Log-likelihood:

    • Chose α to be the argmax of LL:

MLE for a Pareto

3. Find the value of α which maximizes log likelihood

2. What is the log-likelihood all the data

99 of 149

99

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Pareto(α). Use Maximum Likelihood to estimate α.
    • Likelihood:

    • Log-likelihood:

    • Chose α to be the argmax of LL:

MLE for a Pareto

3. Find the value of α which maximizes log likelihood

100 of 149

100

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Pareto(α). Use Maximum Likelihood to estimate α.
    • Likelihood:

    • Log-likelihood:

    • Chose α to be the argmax of LL, set equal to 0, then solve for : :

MLE for a Pareto

101 of 149

101

Time to finish Medical Diagnosis in Seconds

MLE for Erlang

102 of 149

102

MLE for Erlang

103 of 149

103

 

104 of 149

104

Gradient Ascent

Walk uphill and you will find a local maxima

(if your step size is small enough)

105 of 149

105

Gradient Ascent

Walk uphill and you will find a local maxima

(if your step size is small enough)

Especially good if function is convex

106 of 149

106

Gradient Ascent

Repeat many times

Walk uphill and you will find a local maxima

(if your step size is small enough)

This is some profound life philosophy

Step size constant

107 of 149

Initialize: θj = random for all 0 ≤ jm

107

Gradient Ascent

Repeat many times:

Calculate all gradient[j]s based on data

𝜃j += η * gradient[j] for all 0 ≤ jm

108 of 149

108

To the code!

109 of 149

109

Gradient Ascent for MLE of Erlang

110 of 149

110

Derived these partial derivatives of log likelihood

Gradient Ascent for MLE of Erlang

111 of 149

111

MLE For Bernoulli

112 of 149

112

Don’t we already have the Beta?

Yes! But this example is critical for developing towards deep learning.

113 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate p
    • Probability mass function can be written as:

113

MLE For Bernoulli

114 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate p
    • Probability mass function can be written as:

114

MLE For Bernoulli

115 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Ber(p)
    • Probability mass function,

115

Differentiable PMF for Bernoulli

116 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Ber(p)
    • Probability mass function,

116

PMF of Bernoulli

Differentiable PMF for Bernoulli

117 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Ber(p)
    • Probability mass function,

117

0

1

PMF of Bernoulli

Differentiable PMF for Bernoulli

118 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Ber(p)
    • Probability mass function,

118

0

1

p

PMF of Bernoulli

Differentiable PMF for Bernoulli

119 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Ber(p)
    • Probability mass function,

119

0

1

p

1 - p

PMF of Bernoulli

Differentiable PMF for Bernoulli

120 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Ber(p)
    • Probability mass function,

120

0

1

p

1 - p

PMF of Bernoulli

PMF of Bernoulli (p = 0.2)

Differentiable PMF for Bernoulli

121 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Xi ~ Ber(p)
    • Probability mass function,

121

0

1

p

1 - p

PMF of Bernoulli

PMF of Bernoulli (p = 0.2)

Differentiable PMF for Bernoulli

122 of 149

122

Bernoulli PMF

123 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate p
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

123

1. What is the likelihood of one Xi

2. What is the likelihood of all the data

3. What is the log-likelihood all the data

4. Find the value of p which maximizes log likelihood

Maximum Likelihood For Bernoulli

124 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate p
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

124

2. What is the likelihood of all the data

3. What is the log-likelihood all the data

4. Find the value of p which maximizes log likelihood

Maximum Likelihood For Bernoulli

125 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate p
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

125

3. What is the log-likelihood all the data

4. Find the value of p which maximizes log likelihood

Maximum Likelihood For Bernoulli

126 of 149

  • Consider I.I.D. random variables X1, X2, ..., Xn
    • Use Maximum Likelihood to estimate p
    • Probability mass function can be written as:

    • Likelihood:
    • Log-likelihood:

126

4. Find the value of p which maximizes log likelihood

Maximum Likelihood For Bernoulli

127 of 149

Take Derivative:

127

128 of 149

Take Derivative:

128

Take the derivative wrt p

129 of 149

Take Derivative:

129

Take the derivative wrt p

Derivative of a sum!

130 of 149

Take Derivative:

130

Take the derivative wrt p

Derivative of a sum!

Derivative of a sum!

131 of 149

Take Derivative:

131

Take the derivative wrt p

Derivative of a sum!

Derivative of a sum!

132 of 149

Take Derivative:

132

Take the derivative wrt p

Derivative of a sum!

Derivative of log p

Derivative of a sum!

133 of 149

Take Derivative:

133

Take the derivative wrt p

Derivative of a sum!

Derivative of log p

Derivative of a sum!

134 of 149

Take Derivative:

134

Take the derivative wrt p

Derivative of a sum!

Derivative of log p

Derivative of a sum!

Derivative of log (1-p)

135 of 149

Set to Zero:

135

136 of 149

Set to Zero:

136

137 of 149

Set to Zero:

137

Let

To make life easier

138 of 149

Set to Zero:

138

Let

And

To make life easier

139 of 149

Set to Zero:

139

Let

And

To make life easier

140 of 149

Set to Zero:

140

Let

And

To make life easier

141 of 149

Set to Zero:

141

Let

And

To make life easier

142 of 149

Set to Zero:

142

Let

And

To make life easier

143 of 149

Set to Zero:

143

Let

And

To make life easier

144 of 149

Set to Zero:

144

Let

And

To make life easier

145 of 149

145

Isn’t that the same as

unbiased estimator?

146 of 149

146

Yes. For Bernoulli.

147 of 149

147

MLE for Bernoulli is Sample Mean

148 of 149

148

The medicine is tried on 20 patients. It “works” for 14 and “doesn’t work” for 6. What is your new belief that the drug works?

In other words I have 20 IID samples from a Bernoulli. Estimate p. The data is [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0]

MLE estimate:

Prior

Posterior

mode

Beta estimate:

MLE vs. Beta

149 of 149

149

Think about the difference between a point estimate and a distribution

p = 0.75

p =