Probabilistic Models
Chris Gregg
CS109, Stanford University
Summer 2026
Multiple Random Variables. Start of Digital Revolution
Multiple Random Variables. Start of Digital Revolution
Bayesian Carbon Dating
4
5
First, some review
Chris Piech, CS109
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
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
Probability Density Function (PDF)
Chris Piech, CS109
Probability Density Function (PDF)
probability density at x
Chris Piech, CS109
Probability Density Function (PDF)
probability density at x
a constant
Chris Piech, CS109
Probability Density Function (PDF)
probability density at x
a constant
“exponential”
Chris Piech, CS109
Probability Density Function (PDF)
probability density at x
the distance to the mean
a constant
“exponential”
Chris Piech, CS109
Probability Density Function (PDF)
probability density at x
the distance to the mean
a constant
“exponential”
sigma shows up twice
Chris Piech, CS109
14
How does python sample from a Gaussian?
Chris Piech, CS109
Some code
import random
for i in range(10):
mean = 5
std = 1
sample = random.gauss(mean, std)
print(sample)
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
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?
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
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
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
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
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
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
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
End of review
Chris Piech, CS109
Epsilon: Useful perspective
* In the limit as ϵx goes to zero
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)
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)
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)
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)
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)
Where are we in CS109?
Chris Piech, CS109
CS109
Probability Fundamentals
Single Random Variables
Probabilistic Models
Uncertainty Theory
Machine Learning
Piech + Cain, CS109, Stanford University
Discrete Probabilistic Models
34
11b_discrete_joint
Chris Piech, CS109
The world is full of interesting probability problems
Have multiple random variables interacting with one another
Chris Piech, CS109
Joint probability mass functions
36
random variable
Chris Piech, CS109
Joint probability mass functions
37
probability of�an event
random variable
Chris Piech, CS109
Joint probability mass functions
38
probability mass function
random variable
probability of�an event
Chris Piech, CS109
Joint probability mass functions
39
random variable
probability of�an event
probability mass function
Chris Piech, CS109
Joint probability mass functions
40
random variable
probability of�an event
probability mass function
random variables
Chris Piech, CS109
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
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
Two dice
43
Chris Piech, CS109
Two dice
44
Chris Piech, CS109
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Four Prototypical Trajectories
Welcome the marginal
Chris Piech, CS109
Marginal Distribution
62
Chris Piech, CS109
Marginal Distribution
63
Use marginal distributions to get a 1-D RV from a joint PMF.
Chris Piech, CS109
Four Prototypical Trajectories
Why is that called the marginal?
Chris Piech, CS109
Quick note on independence
Chris Piech, CS109
If A and B are independent:
66
Joint
For all a, b
Chris Piech, CS109
Four Prototypical Trajectories
Biggest Game Changer this half of CS109
Chris Piech, CS109
Belief in Vision Given User Responses
68
P(Ability to See | Observed Responses)
Chris Piech, CS109
Belief in Age Given Observed C14
69
P(Age| Observed C14)
Chris Piech, CS109
Belief in Age Given Name
70
P(Age| Name)
Chris Piech, CS109
Belief in Author Given Text
71
P(Author| Text)
Author A
Author B
Chris Piech, CS109
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
Bayes Theorem
P(B|E) =
P(E|B)
P(B)
P(E)
Posterior belief
Prior belief
Likelihood of
evidence
Normalization constant
Chris Piech, CS109
Bayes with Discrete Random Variables
Let M be a discrete random variable
Let N be a discrete random variable
Chris Piech, CS109
Bayes with Discrete Random Variables
Let M be a discrete random variable
Let N be a discrete random variable
More generally
Chris Piech, CS109
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
Chris Piech, CS109
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
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
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
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
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
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
Four Prototypical Trajectories
How do you handle observing a continuous value?
Chris Piech, CS109
Four Prototypical Trajectories
Aside
Chris Piech, CS109
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
Mixing Discrete and Continuous
Let X be a continuous random variable
Let N be a discrete random variable
Chris Piech, CS109
Mixing Discrete and Continuous
Let X be a continuous random variable
Let N be a discrete random variable
Chris Piech, CS109
Mixing Discrete and Continuous
Let X be a continuous random variable
Let N be a discrete random variable
Chris Piech, CS109
Mixing Discrete and Continuous
Let X be a continuous random variable
Let N be a discrete random variable
Chris Piech, CS109
Mixing Discrete and Continuous
Change notation
Let X be a continuous random variable
Let N be a discrete random variable
Chris Piech, CS109
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
LOTP? Chain Rule? You can play too!
N is discrete. X is continuous
Law of total probability
Chain Rule
Chris Piech, CS109
Four Prototypical Trajectories
End Aside
Chris Piech, CS109
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
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
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
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
Four Prototypical Trajectories
Lets Play Number or Function!
Chris Piech, CS109
Number or Function?
Chris Piech, CS109
Number
Number or Function?
Chris Piech, CS109
Number or Function?
Chris Piech, CS109
Function
(a probability mass function
if discrete)
Number or Function?
Chris Piech, CS109
104
Chris Piech, CS109
105
Baby Delivery
Its 17 days before the due date and still no baby
Chris Piech, CS109
106
Baby Delivery
Its 17 days before the due date and still no baby
Chris Piech, CS109
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
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
Bayesian Carbon Dating
Before observation
After
Observation
Chris Piech, CS109
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
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
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
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
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
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
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
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
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
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
Four Prototypical Trajectories
Carbon Dating Likelihood Math
Chris Piech, CS109
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
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
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
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
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
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
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
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
Posterior Belief in Age
Chris Piech, CS109