1 of 129

Probabilistic Models

Chris Gregg

CS109, Stanford University

Summer 2026

2 of 129

Multiple Random Variables. Start of Digital Revolution

3 of 129

Multiple Random Variables. Start of Digital Revolution

4 of 129

Bayesian Carbon Dating

4

5 of 129

5

First, some review

Chris Piech, CS109

6 of 129

Normal Distribution

5

0

-5

1

CDF of a Normal

F(x)

PDF of a Normal

f(x)

f(x) = derivative of probability

F(x) = P(X < x)

Chris Piech, CS109

7 of 129

Cumulative Distribution Function (CDF)

Table of Φ(z) values are precomputed

CDF of Standard Normal: A function that has been solved for numerically

The cumulative density function (CDF) of any normal

Chris Piech, CS109

8 of 129

Probability Density Function (PDF)

Chris Piech, CS109

9 of 129

Probability Density Function (PDF)

probability density at x

Chris Piech, CS109

10 of 129

Probability Density Function (PDF)

probability density at x

a constant

Chris Piech, CS109

11 of 129

Probability Density Function (PDF)

probability density at x

a constant

“exponential”

Chris Piech, CS109

12 of 129

Probability Density Function (PDF)

probability density at x

the distance to the mean

a constant

“exponential”

Chris Piech, CS109

13 of 129

Probability Density Function (PDF)

probability density at x

the distance to the mean

a constant

“exponential”

sigma shows up twice

Chris Piech, CS109

14 of 129

14

How does python sample from a Gaussian?

Chris Piech, CS109

15 of 129

Some code

import random

for i in range(10):

mean = 5

std = 1

sample = random.gauss(mean, std)

print(sample)

16 of 129

Some code

import random

for i in range(10):

mean = 5

std = 1

sample = random.gauss(mean, std)

print(sample)

4.903977966179095

4.618730222918121

6.677086972180218

4.495202488284518

4.389647328597387

3.2682120965975368

5.495799099102823

6.264671749470066

5.283570354163004

5.117329500833404

17 of 129

Some code

import random

for i in range(10):

mean = 5

std = 1

sample = random.gauss(mean, std)

print(sample)

4.903977966179095

4.618730222918121

6.677086972180218

4.495202488284518

4.389647328597387

3.2682120965975368

5.495799099102823

6.264671749470066

5.283570354163004

5.117329500833404

How does this work?

18 of 129

How Does a Computer Sample a Normal?

5

0

-5

1

CDF of a Normal

F(x)

PDF of a Normal

f(x)

f(x) = derivative of probability

F(x) = P(X < x)

Chris Piech, CS109

19 of 129

How Does a Computer Sample a Normal?

5

0

-5

1

CDF of a Normal

F(x)

PDF of a Normal

f(x)

f(x) = derivative of probability

F(x) = P(X < x)

Inverse Transform Sampling

Chris Piech, CS109

20 of 129

How Does a Computer Sample a Normal?

5

0

-5

1

CDF of a Normal

𝚽(x)

PDF of a Normal

f(x)

Inverse Transform Sampling

Step 1: pick a uniform number y between 0 and 1

Step 2: Find the x such that 𝚽(x) = y

Chris Piech, CS109

21 of 129

How Does a Computer Sample a Normal?

5

0

-5

1

CDF of a Normal

𝚽(x)

Inverse Transform Sampling

Step 1: pick a uniform number y between 0 and 1

Step 2: Find the x such that 𝚽(x) = y

x = 𝚽-1(y)

y=0.855

Chris Piech, CS109

22 of 129

How Does a Computer Sample a Normal?

5

0

-5

1

CDF of a Normal

𝚽(x)

Inverse Transform Sampling

Step 1: pick a uniform number y between 0 and 1

Step 2: Find the x such that 𝚽(x) = y

x = 𝚽-1(y)

y=0.855

Sample 1: 1.201234

Chris Piech, CS109

23 of 129

How Does a Computer Sample a Normal?

5

0

-5

1

CDF of a Normal

𝚽(x)

Inverse Transform Sampling

Step 1: pick a uniform number y between 0 and 1

Step 2: Find the x such that 𝚽(x) = y

x = 𝚽-1(y)

y=0.855

Sample 1: 1.201234

y=0.3259

Chris Piech, CS109

24 of 129

How Does a Computer Sample a Normal?

5

0

-5

1

CDF of a Normal

𝚽(x)

Inverse Transform Sampling

Step 1: pick a uniform number y between 0 and 1

Step 2: Find the x such that 𝚽(x) = y

x = 𝚽-1(y)

y=0.855

Sample 1: 1.201234

y=0.3259

Sample 2:

-0.45123

Chris Piech, CS109

25 of 129

25

End of review

Chris Piech, CS109

26 of 129

Epsilon: Useful perspective

* In the limit as ϵx goes to zero

27 of 129

Relative Probability of Continuous Variables

x

f(x)

Time to finish pset 3

How much more likely are you to complete pset 3 in 10 hours than in 5?

X = time to finish pset 3

X ~ N(μ = 10, σ2 = 2)

28 of 129

Relative Probability of Continuous Variables

x

f(x)

Time to finish pset 3

How much more likely are you to complete pset 3 in 10 hours than in 5?

X = time to finish pset 3

X ~ N(μ = 10, σ2 = 2)

29 of 129

Relative Probability of Continuous Variables

x

f(x)

Time to finish pset 3

How much more likely are you to complete pset 3 in 10 hours than in 5?

X = time to finish pset 3

X ~ N(μ = 10, σ2 = 2)

30 of 129

Relative Probability of Continuous Variables

x

f(x)

Time to finish pset 3

How much more likely are you to complete pset 3 in 10 hours than in 5?

X = time to finish pset 3

X ~ N(μ = 10, σ2 = 2)

31 of 129

Relative Probability of Continuous Variables

x

f(x)

Time to finish pset 3

How much more likely are you to complete pset 3 in 10 hours than in 5?

X = time to finish pset 3

X ~ N(μ = 10, σ2 = 2)

32 of 129

Where are we in CS109?

Chris Piech, CS109

33 of 129

CS109

Probability Fundamentals

Single Random Variables

Probabilistic Models

Uncertainty Theory

Machine Learning

Piech + Cain, CS109, Stanford University

34 of 129

Discrete Probabilistic Models

34

11b_discrete_joint

Chris Piech, CS109

35 of 129

The world is full of interesting probability problems

Have multiple random variables interacting with one another

Chris Piech, CS109

36 of 129

Joint probability mass functions

  •  

36

 

random variable

Chris Piech, CS109

37 of 129

Joint probability mass functions

  •  

37

 

probability of�an event

 

random variable

Chris Piech, CS109

38 of 129

Joint probability mass functions

  •  

38

 

 

probability mass function

 

random variable

probability of�an event

Chris Piech, CS109

39 of 129

Joint probability mass functions

  •  

39

 

random variable

 

probability of�an event

 

probability mass function

Chris Piech, CS109

40 of 129

Joint probability mass functions

  •  

40

 

random variable

 

probability of�an event

 

probability mass function

 

random variables

Chris Piech, CS109

41 of 129

Joint probability mass functions

  •  

41

 

random variable

 

probability of�an event

 

probability mass function

 

probability of the intersection�of two events

 

recall: the comma

 

random variables

Chris Piech, CS109

42 of 129

Joint probability mass functions

  •  

42

 

random variable

 

probability of�an event

 

probability mass function

 

probability of the intersection�of two events

 

recall: the comma

 

 

joint probability mass function

random variables

Chris Piech, CS109

43 of 129

Two dice

  •  

43

Chris Piech, CS109

44 of 129

Two dice

  •  

44

 

Chris Piech, CS109

45 of 129

Two dice

  •  

45

 

1

2

3

4

5

6

1

1/36

...

...

...

...

1/36

2

...

...

...

...

...

...

3

...

...

...

...

...

...

4

...

...

...

...

...

...

5

...

...

...

...

...

...

6

1/36

...

...

...

...

1/36

 

Chris Piech, CS109

46 of 129

Two dice

  •  

46

 

1

2

3

4

5

6

1

1/36

...

...

...

...

1/36

2

...

...

...

...

...

...

3

...

...

...

...

...

...

4

...

...

...

...

...

...

5

...

...

...

...

...

...

6

1/36

...

...

...

...

1/36

 

 

Chris Piech, CS109

47 of 129

Dating at Stanford. Data from a few years ago

47

Single

In a relationship

It's complicated

Freshman

0.13

0.08

0.02

Sophomore

0.16

0.10

0.01

Junior

0.09

0.10

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

Chris Piech, CS109

48 of 129

Joint is Complete Information!

48

A joint distribution is complete information. It can be used to answer any probability question.

Single

Relationship

Complicated

Frosh

0.13

0.08

0.02

Soph

0.16

0.10

0.01

Junior

0.09

0.10

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

Chris Piech, CS109

49 of 129

Joint table: mutually exclusive and covers sample space.

49

Single

Relationship

Complicated

Frosh

0.13

0.08

0.02

Soph

0.16

0.10

0.01

Junior

0.09

0.10

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

X is dating status.

Y is year.

Each combination is mutually exclusive, and they span the sample space

Chris Piech, CS109

50 of 129

Joint table: mutually exclusive and covers sample space.

50

Single

Relationship

Complicated

Frosh

0.13

0.08

0.02

Soph

0.16

0.10

0.01

Junior

0.09

?

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

X is dating status.

Y is year.

Each combination is mutually exclusive, and they span the sample space

Chris Piech, CS109

51 of 129

Joint table: mutually exclusive and covers sample space.

51

Single

Relationship

Complicated

Frosh

0.13

0.08

0.02

Soph

0.16

0.10

0.01

Junior

0.09

0.10

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

X is dating status.

Y is year.

Each combination is mutually exclusive, and they span the sample space

Chris Piech, CS109

52 of 129

Joint table: mutually exclusive and covers sample space.

52

Single

Relationship

Complicated

Frosh

0.07 k

0.04 k

0.01 k

Soph

0.09 k

0.05 k

0.01 k

Junior

0.05 k

0.05 k

0.01 k

Senior

0.01 k

0.03 k

0.01 k

5+

0.03 k

0.03 k

0.02 k

X is dating status.

Y is year.

Each combination is mutually exclusive, and they span the sample space

Chris Piech, CS109

53 of 129

Joint table: mutually exclusive and covers sample space.

53

Single

Relationship

Complicated

Frosh

0.07 k

0.04 k

0.01 k

Soph

0.09 k

0.05 k

0.01 k

Junior

0.05 k

0.05 k

0.01 k

Senior

0.01 k

0.03 k

0.01 k

5+

0.03 k

0.03 k

0.02 k

X is dating status.

Y is year.

Each combination is mutually exclusive, and they span the sample space

Chris Piech, CS109

54 of 129

Joint table: mutually exclusive and covers sample space.

54

Single

Relationship

Complicated

Frosh

0.07 k

0.04 k

0.01 k

Soph

0.09 k

0.05 k

0.01 k

Junior

0.05 k

0.05 k

0.01 k

Senior

0.01 k

0.03 k

0.01 k

5+

0.03 k

0.03 k

0.02 k

X is dating status.

Y is year.

Each combination is mutually exclusive, and they span the sample space

Chris Piech, CS109

55 of 129

Joint table: mutually exclusive and covers sample space.

55

Single

Relationship

Complicated

Frosh

0.07 k

0.04 k

0.01 k

Soph

0.09 k

0.05 k

0.01 k

Junior

0.05 k

0.05 k

0.01 k

Senior

0.01 k

0.03 k

0.01 k

5+

0.03 k

0.03 k

0.02 k

X is dating status.

Y is year.

Each combination is mutually exclusive, and they span the sample space

Chris Piech, CS109

56 of 129

What is the probability someone is in a relationship?

56

Single

Relationship

Complicated

Frosh

0.13

0.08

0.02

Soph

0.16

0.10

0.01

Junior

0.09

0.10

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

We can use the law of total probability!

X is dating status. Y is year.

Frosh

Chris Piech, CS109

57 of 129

What is the probability someone is in a relationship?

57

Single

Relationship

Complicated

Frosh

0.13

0.08

0.02

Soph

0.16

0.10

0.01

Junior

0.09

0.10

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

We can use the law of total probability!

X is dating status. Y is year.

Frosh

Chris Piech, CS109

58 of 129

What is the probability someone is in a relationship?

58

Single

Relationship

Complicated

Frosh

0.13

0.08

0.02

Soph

0.16

0.10

0.01

Junior

0.09

0.10

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

We can use the law of total probability!

X is dating status. Y is year.

Frosh

Chris Piech, CS109

59 of 129

What is the probability someone is in a relationship?

59

Single

Relationship

Complicated

Frosh

0.13

0.08

0.02

Soph

0.16

0.10

0.01

Junior

0.09

0.10

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

We can use the law of total probability!

X is dating status. Y is year.

Chris Piech, CS109

60 of 129

What is the probability someone is in a relationship?

60

Single

Relationship

Complicated

Frosh

0.13

0.08

0.02

Soph

0.16

0.10

0.01

Junior

0.09

0.10

0.02

Senior

0.02

0.07

0.01

5+

0.06

0.09

0.04

We can use the law of total probability!

X is dating status. Y is year.

Chris Piech, CS109

61 of 129

Four Prototypical Trajectories

Welcome the marginal

Chris Piech, CS109

62 of 129

Marginal Distribution

  •  

62

Chris Piech, CS109

63 of 129

Marginal Distribution

  •  

63

Use marginal distributions to get a 1-D RV from a joint PMF.

Chris Piech, CS109

64 of 129

Four Prototypical Trajectories

Why is that called the marginal?

Chris Piech, CS109

65 of 129

Quick note on independence

Chris Piech, CS109

66 of 129

If A and B are independent:

66

Joint

For all a, b

Chris Piech, CS109

67 of 129

Four Prototypical Trajectories

Biggest Game Changer this half of CS109

Chris Piech, CS109

68 of 129

Belief in Vision Given User Responses

68

P(Ability to See | Observed Responses)

Chris Piech, CS109

69 of 129

Belief in Age Given Observed C14

69

P(Age| Observed C14)

Chris Piech, CS109

70 of 129

Belief in Age Given Name

70

P(Age| Name)

Chris Piech, CS109

71 of 129

Belief in Author Given Text

71

P(Author| Text)

Author A

Author B

Chris Piech, CS109

72 of 129

Today: Inference

Inference noun

Updating one’s belief about a random variable (or multiple) based on conditional knowledge regarding another random variable (or multiple) in a probabilistic model.

TLDR: conditional probability with random variables.

Chris Piech, CS109

73 of 129

Bayes Theorem

P(B|E) =

P(E|B)

P(B)

P(E)

Posterior belief

Prior belief

Likelihood of

evidence

Normalization constant

Chris Piech, CS109

74 of 129

Bayes with Discrete Random Variables

Let M be a discrete random variable

Let N be a discrete random variable

Chris Piech, CS109

75 of 129

Bayes with Discrete Random Variables

Let M be a discrete random variable

Let N be a discrete random variable

More generally

Chris Piech, CS109

76 of 129

Bayes with Discrete Random Variables

Let M be a discrete random variable

Let N be a discrete random variable

More generally

Shorthand

notation

Chris Piech, CS109

77 of 129

Chris Piech, CS109

78 of 129

I Heard That

78

Let X be the change in gaze (measured in degrees) over 3 seconds after a sound is played

You observe X = 0. What is the probability the baby can hear the sound?

Chris Piech, CS109

79 of 129

Question: Have I Been Given the Joint?

79

Let X be the change in gaze (measured in degrees) over 3 seconds after a sound is played

You observe X = 0. What is the probability the baby can hear the sound?

Chris Piech, CS109

80 of 129

I Heard That

80

You observe X = 0. What is the probability the baby can hear the sound?

Y = 1 means the child can hear the sound

Chris Piech, CS109

81 of 129

I Heard That

81

You observe X = 0. What is the probability the baby can hear the sound?

Y = 1 means the child can hear the sound

Chris Piech, CS109

82 of 129

I Heard That

82

You observe X = 0. What is the probability the baby can hear the sound?

Y = 1 means the child can hear the sound

Chris Piech, CS109

83 of 129

I Heard That with Continuous

83

Normal Assumption:

For babies who can hear sounds, we approximate their gaze movement after the sound is played as: N(µ = 15, σ2 = 25).

For babies who can not hear sounds, we approximate gaze movement as N(µ = 8, σ2 = 25).

For a new baby we observe a 14 degree movement after the sound is played. What is your belief that a baby can hear, under The Normal Assumption?

Chris Piech, CS109

84 of 129

Four Prototypical Trajectories

How do you handle observing a continuous value?

Chris Piech, CS109

85 of 129

Four Prototypical Trajectories

Aside

Chris Piech, CS109

86 of 129

All the Bayes Belong to Us

M,N are discrete. X, Y are continuous

OG Bayes

Mix Bayes #1

Mix Bayes #2

TLDR:

If random variable is discrete: use PMF

If random variable is continuous: use PDF

Chris Piech, CS109

87 of 129

Mixing Discrete and Continuous

Let X be a continuous random variable

Let N be a discrete random variable

Chris Piech, CS109

88 of 129

Mixing Discrete and Continuous

Let X be a continuous random variable

Let N be a discrete random variable

Chris Piech, CS109

89 of 129

Mixing Discrete and Continuous

Let X be a continuous random variable

Let N be a discrete random variable

Chris Piech, CS109

90 of 129

Mixing Discrete and Continuous

Let X be a continuous random variable

Let N be a discrete random variable

Chris Piech, CS109

91 of 129

Mixing Discrete and Continuous

Change notation

Let X be a continuous random variable

Let N be a discrete random variable

Chris Piech, CS109

92 of 129

All the Bayes Belong to Us

M,N are discrete. X, Y are continuous

OG Bayes

Mix Bayes #1

Mix Bayes #2

TLDR:

If random variable is discrete: use PMF

If random variable is continuous: use PDF

Chris Piech, CS109

93 of 129

LOTP? Chain Rule? You can play too!

N is discrete. X is continuous

Law of total probability

Chain Rule

Chris Piech, CS109

94 of 129

Four Prototypical Trajectories

End Aside

Chris Piech, CS109

95 of 129

I Heard That with Continuous

95

Normal Assumption:

For babies who can hear sounds, we approximate their gaze movement after the sound is played as: N(µ = 15, σ2 = 25).

For babies who can not hear sounds, we approximate gaze movement as N(µ = 8, σ2 = 25).

For a new baby we observe a 14 degree movement after the sound is played. What is your belief that a baby can hear, under The Normal Assumption?

Chris Piech, CS109

96 of 129

Equivalently

96

def sample ():

# bernoulli sample

can_hear = rand_bern(0.75)

if can_hear == 1:

# gaussian sample

return rand_gauss (mu = 15 , std = 5)

else:

# gaussian sample

return rand_gauss (mu = 8, std = 5)

The function sample returned the value 14.

What is the probability that can_hear was 1?

Normal Assumption: For babies who can hear sounds, we approximate their gaze movement after the sound is played as: N(µ = 15, σ2 = 25).

For babies who can not hear sounds, we approximate gaze movement as N(µ = 8, σ2 = 25).

For a new baby we observe a 14 degree movement after the sound is played. What is your belief that a baby can hear, under The Normal Assumption?

Chris Piech, CS109

97 of 129

All the Bayes Belong to Us

M,N are discrete. X, Y are continuous

OG Bayes

Mix Bayes #1

Mix Bayes #2

TLDR:

If random variable is discrete: use PMF

If random variable is continuous: use PDF

Chris Piech, CS109

98 of 129

Goal: Inference

98

Change your belief distribution

(Joint, PMF, or PDF) of random variables, based on observations

*Note in the earlier examples, we were updating Bernoulli Random Variables

Chris Piech, CS109

99 of 129

Four Prototypical Trajectories

Lets Play Number or Function!

Chris Piech, CS109

100 of 129

Number or Function?

Chris Piech, CS109

101 of 129

Number

Number or Function?

Chris Piech, CS109

102 of 129

Number or Function?

Chris Piech, CS109

103 of 129

Function

(a probability mass function

if discrete)

Number or Function?

Chris Piech, CS109

104 of 129

104

Chris Piech, CS109

105 of 129

105

Baby Delivery

Its 17 days before the due date and still no baby

Chris Piech, CS109

106 of 129

106

Baby Delivery

Its 17 days before the due date and still no baby

Chris Piech, CS109

107 of 129

107

Baby Delivery

Its 17 days before the due date and still no baby

Probability baby is born on due date increases by 12%

Chris Piech, CS109

108 of 129

Bayesian Carbon Dating

108

Take a fixed size sample from a dead thing

Count C14 in Sample

Know the probability distribution for when it died

1

2

3

4

Profit

Chris Piech, CS109

109 of 129

Bayesian Carbon Dating

Before observation

After

Observation

Chris Piech, CS109

110 of 129

Warmup with Code

110

def update_belief(m = 900):

"""

Returns a dictionary A, where A[i] contains the

corresponding probability, P(A = i| M = 900).

m is the number of C14 molecules remaining and i

is age in years. i is in the range 100 to 10000

"""

pr_A = {}

n_years = 9901

for i in range(100,10000+1):

prior = 1 / n_years # P(A = i)

likelihood = calc_likelihood(m, i) # P(M=m | A=i)

pr_A[i] = prior * likelihood

# implicitly computes the normalization constant

normalize(pr_A)

return pr_A

Chris Piech, CS109

111 of 129

Warmup with Code

111

def update_belief(m = 900):

"""

Returns a dictionary A, where A[i] contains the

corresponding probability, P(A = i| M = 900).

m is the number of C14 molecules remaining and i

is age in years. i is in the range 100 to 10000

"""

pr_A = {}

n_years = 9901

for i in range(100,10000+1):

prior = 1 / n_years # P(A = i)

likelihood = calc_likelihood(m, i) # P(M=m | A=i)

pr_A[i] = prior * likelihood

# implicitly computes the normalization constant

normalize(pr_A)

return pr_A

Chris Piech, CS109

112 of 129

Warmup with Code

112

def update_belief(m = 900):

"""

Returns a dictionary A, where A[i] contains the

corresponding probability, P(A = i| M = 900).

m is the number of C14 molecules remaining and i

is age in years. i is in the range 100 to 10000

"""

pr_A = {}

n_years = 9901

for i in range(100,10000+1):

prior = 1 / n_years # P(A = i)

likelihood = calc_likelihood(m, i) # P(M=m | A=i)

pr_A[i] = prior * likelihood

# implicitly computes the normalization constant

normalize(pr_A)

return pr_A

Chris Piech, CS109

113 of 129

Normalize in Python

113

# list normalization

def normalize_list(data_list):

total_sum = np.sum(data_list)

return np.array(data_list) / total_sum

>>> norm = normalize_list([10, 20, 30, 40])

>>> np.sum(norm) # 1.0, always (within floating point error)

# dictionary normalization

def normalize_dict(data_dict):

total_sum = sum(data_dict.values())

normalized = {}

for key, value in data_dict.items():

normalized[key] = value / total_sum

return normalized

>>> norm = normalize_dict({'a': 100, 'b': 200, 'c': 300})

>>> np.sum(norm.values()) # 1.0, always (within floating point error)

return

[0.1 0.2 0.3 0.4]

return

{'a': 0.166, 'b': 0.333, 'c': 0.5}

Chris Piech, CS109

114 of 129

Bayesian Carbon Dating: Inference Overview

114

Let A be how many years old the sample is (A = 100 means the sample is 100 years old)

Let M be the observed amount of C14 left in the sample

Such that

Chris Piech, CS109

115 of 129

Understanding why Denom. is a Constant

Notice which term doesn’t change as i changes (four example calculations).

Changes with i

Doesn’t change

A = age

M = measured C14

Chris Piech, CS109

116 of 129

Bayesian Carbon Dating: Inference Overview

116

Let A be how many years old the sample is (A = 100 means the sample is 100 years old)

Let M be the observed amount of C14 left in the sample

Such that

Chris Piech, CS109

117 of 129

Understanding Through Code

117

def update_belief(m = 900):

"""

Returns a dictionary A, where A[i] contains the

corresponding probability, P(A = i| M = 900).

m is the number of C14 molecules remaining and i

is age in years. i is in the range 100 to 10000

"""

pr_A = {}

n_years = 9901

for i in range(100,10000+1):

prior = 1 / n_years # P(A = i)

likelihood = calc_likelihood(m, i) # P(M=m | A=i)

pr_A[i] = prior * likelihood

# implicitly computes the normalization constant

normalize(pr_A)

return pr_A

Chris Piech, CS109

118 of 129

Understanding Through Code

118

def update_belief(m = 900):

"""

Returns a dictionary A, where A[i] contains the

corresponding probability, P(A = i| M = 900).

m is the number of C14 molecules remaining and i

is age in years. i is in the range 100 to 10000

"""

pr_A = {}

n_years = 9901

for i in range(100,10000+1):

prior = 1 / n_years # P(A = i)

likelihood = calc_likelihood(m, i) # P(M=m | A=i)

pr_A[i] = prior * likelihood

# implicitly computes the normalization constant

normalize(pr_A)

return pr_A

Chris Piech, CS109

119 of 129

Understanding Through Code

119

def update_belief(m = 900):

"""

Returns a dictionary A, where A[i] contains the

corresponding probability, P(A = i| M = 900).

m is the number of C14 molecules remaining and i

is age in years. i is in the range 100 to 10000

"""

pr_A = {}

n_years = 9901

for i in range(100,10000+1):

prior = 1 / n_years # P(A = i)

likelihood = calc_likelihood(m, i) # P(M=m | A=i)

pr_A[i] = prior * likelihood

# implicitly computes the normalization constant

normalize(pr_A)

return pr_A

Chris Piech, CS109

120 of 129

Four Prototypical Trajectories

Carbon Dating Likelihood Math

Chris Piech, CS109

121 of 129

Probability of Having 900 Remain

121

There were originally 1000 C14 molecules.

Each molecule remains independently with equal probability pi

What is the probability that 900 remain?

Chris Piech, CS109

122 of 129

Probability of Having 900 Remain

122

There were originally 1000 C14 molecules.

Each molecule remains independently with equal probability pi

What is the probability that 900 remain?

Chris Piech, CS109

123 of 129

Probability of Having 900 Remain

123

There were originally 1000 C14 molecules.

Each molecule remains independently with equal probability pi

What is the probability that 900 remain?

Each molecules’ time to live is exponential with λ = 1/8267

Chris Piech, CS109

124 of 129

Probability of Having 900 Remain

124

There were originally 1000 C14 molecules.

Each molecule remains independently with equal probability pi

What is the probability that 900 remain?

Let T be the time to decay for any one molecule

Each molecules’ time to live is exponential with λ = 1/8267

Chris Piech, CS109

125 of 129

Probability of Having 900 Remain

125

There were originally 1000 C14 molecules.

Each molecule remains independently with equal probability pi

What is the probability that 900 remain?

Let T be the time to decay for any one molecule

Each molecules’ time to live is exponential with λ = 1/8267

Chris Piech, CS109

126 of 129

Probability of Having 900 Remain

126

There were originally 1000 C14 molecules.

Each molecule remains independently with equal probability pi

What is the probability that 900 remain?

Let T be the time to decay for any one molecule

Each molecules’ time to live is exponential with λ = 1/8267

Chris Piech, CS109

127 of 129

Probability of Having 900 Remain

127

def calc_likelihood(m = 900, age):

"""

Computes P(M = m | A = age), the probability of

having m molecules left given the sample is age

years old. Uses the exponential decay of C14

"""

n_original = 1000

p_remain = math.exp(-age/C14_MEAN_LIFE)

return stats.binom.pmf(m, n_original, p_remain)

Chris Piech, CS109

128 of 129

Probability of Having 900 Remain

128

def calc_likelihood(m = 900, age):

"""

Computes P(M = m | A = age), the probability of

having m molecules left given the sample is age

years old. Uses the exponential decay of C14

"""

n_original = 1000 + delta_start(age)

p_remain = math.exp(-age/C14_MEAN_LIFE)

return stats.binom.pmf(m, n_original, p_remain)

Chris Piech, CS109

129 of 129

Posterior Belief in Age

Chris Piech, CS109