View
20
Download
0
Category
Preview:
Citation preview
15: General InferenceLisa Yan and Jerry CainOctober 16, 2020
1
Lisa Yan and Jerry Cain, CS109, 2020
Quick slide reference
2
3 General Inference: intro 15a_inference
15 Bayesian Networks 15b_bayes_nets
22 Inference (I): Math 15c_inference_math
29 Inference (II): Rejection sampling LIVE
69 Inference (III): Gibbs sampling (extra) (no video)
General Inference: Introduction
3
15a_inference
Lisa Yan and Jerry Cain, CS109, 2020
Inference
4
Lisa Yan and Jerry Cain, CS109, 2020
Inference
5
Lisa Yan and Jerry Cain, CS109, 2020
Inference
6
Flu
Cold
Cancer
Under-grad
Chest pain
TiredSoreThroat
Fever
Nausea
General inference question:
Given the values of some randomvariables, what is the conditionaldistribution of some other randomvariables?
Lisa Yan and Jerry Cain, CS109, 2020
Inference
7
Flu
Cold
Cancer
Under-grad
Chest pain
TiredSoreThroat
Fever
Nausea
One inference question:
π πΉ = 1|π = 1, π = 1
=π πΉ = 1,π = 1, π = 1
π π = 1, π = 1
Lisa Yan and Jerry Cain, CS109, 2020
Inference
8
Flu
Cold
Cancer
Under-grad
Chest pain
TiredSoreThroat
Fever
Nausea
Another inference question:
π πΆ! = 1,π = 1|π = 0, πΉ" = 0
=π πΆ! = 1, π = 1, π = 0, πΉ" = 0
π π = 0, πΉ" = 0
Lisa Yan and Jerry Cain, CS109, 2020
InferenceIf we knew the joint distribution,we can answer all probabilisticinference questions.
What is the size of the joint probability table? A. 2#$% entriesB. π& entriesC. 2# entriesD. None/other/donβt know
Flu
Cold
Cancer
Under-grad
Chest pain
TiredSoreThroat
Fever
Nausea π = 9all binary RVs
π€
Lisa Yan and Jerry Cain, CS109, 2020
InferenceIf we knew the joint distribution,we can answer all probabilisticinference questions.
What is the size of the joint probability table? A. 2#$% entriesB. π& entriesC. 2# entriesD. None/other/donβt know
Flu
Cold
Cancer
Under-grad
Chest pain
TiredSoreThroat
Fever
Nausea π = 9all binary RVs
Naively specifying a joint distribution is often intractable.
Lisa Yan and Jerry Cain, CS109, 2020
N can be largeβ¦
11
Lisa Yan and Jerry Cain, CS109, 2020
Conditional Probability Independence
Conditionally Independent RVs
12
Conditional Distributions Independent RVsConditional Probability Independence
Lisa Yan and Jerry Cain, CS109, 2020
Conditionally Independent RVsRecall that two events π΄ and π΅ areconditionally independent given πΈ if:
π discrete random variables π%, π&, β¦ , π' are called conditionally independent given π if:
for all π₯%, π₯&, β¦ , π₯' , π¦:
π π% = π₯%, π& = π₯&, β¦ , π' = π₯'|π = π¦ =6()%
'
π π( = π₯(|π = π¦
This implies the following (cool to remember for later):
log π π% = π₯%, π& = π₯&, β¦ , π' = π₯'|π = π¦ =:()%
'
log π π( = π₯(|π = π¦
13
π π΄π΅|πΈ = π π΄|πΈ π(π΅|πΈ)
Lisa Yan and Jerry Cain, CS109, 2020
Lec. 12: Independence of multiple random variablesRecall independence ofπ events πΈ%, πΈ&, β¦ , πΈ':
We have independence of π discrete random variables π%, π&, β¦ , π' iffor all π₯%, π₯&, β¦ , π₯':
π π% = π₯%, π& = π₯&, β¦ , π' = π₯' =6()%
'
π π( = π₯(
14
for π = 1,β¦ , π:for every subset πΈ!, πΈ", β¦ , πΈ#:
π πΈ!, πΈ", β¦ , πΈ# = π πΈ! π πΈ" β―π πΈ#
Errata (edited May 3): Removed the independent RV requirement for all subsets of size π = 1, β¦ , π. Do you see why this requirement is unnecessary?(Hint: independence of RVs implies independence of all events)
Errata
Bayesian Networks
15
15b_bayes_nets
Lisa Yan and Jerry Cain, CS109, 2020
A simpler WebMD
Great! Just specify 2* = 16 joint probabilities�
π πΉ+, = π, πΉ"- = π, π = π, π = π
What would a Stanford flu expert do?
16
Flu Under-grad
TiredFever
Describe the joint distribution using causality!!!
Lisa Yan and Jerry Cain, CS109, 2020
Constructing a Bayesian NetworkWhat would a Stanford flu expert do?
1. Describe the joint distribution using causality.
2. Assume conditional independence.
17
Flu Under-grad
TiredFever
Lisa Yan and Jerry Cain, CS109, 2020
Constructing a Bayesian NetworkIn a Bayesian Network,Each random variable is conditionally independent of its non-descendants, given its parents.
β’ Node: random variableβ’ Directed edge: conditional dependency
Examples:β’ π πΉ!" = 1|π = 0, πΉ#$ = 1 = π πΉ!" = 1|πΉ#$ = 1β’ π πΉ#$ = 1,π = 0 = π πΉ#$ = 1 π π = 0
18
Flu Under-grad
TiredFever
Lisa Yan and Jerry Cain, CS109, 2020
Constructing a Bayesian NetworkWhat would a Stanford flu expert do?
1. Describe the joint distribution using causality.
2. Assume conditional independence.3. Provide π values|parents for each
random variable
What conditional probabilitiesshould our expert specify?
19
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1 = 0.8
π€
β
Lisa Yan and Jerry Cain, CS109, 2020
Constructing a Bayesian NetworkWhat would a Stanford flu expert do?
1. Describe the joint distribution using causality.
2. Assume conditional independence.3. Provide π values|parents for each
random variable
What conditional probabilitiesshould our expert specify?
20
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1 = 0.8
π π = 1|πΉ!" = 0, π = 0π π = 1|πΉ!" = 0, π = 1π π = 1|πΉ!" = 1, π = 0π π = 1|πΉ!" = 1, π = 1
Lisa Yan and Jerry Cain, CS109, 2020
What would a CS109 student do?1. Populate a Bayesian network by
asking a Stanford flu expertor
by using reasonable assumptions
2. Answer inference questions
Using a Bayes Net
21
Our focustoday
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1 = 0.8
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
Inference (I): Math
22
15c_inference_math
Lisa Yan and Jerry Cain, CS109, 2020
Bayes Nets: Conditional independenceIn a Bayesian Network,Each random variable is conditionally independent of its non-descendants, given its parents.
β’ Node: random variableβ’ Directed edge: conditional dependency
23
Flu Under-grad
TiredFever
Review
Lisa Yan and Jerry Cain, CS109, 2020
Inference via math
24
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
1. π πΉ+, = 0, π = 1, πΉ"- = 0, π = 1 ?
Compute joint probabilitiesusing chain rule.
Lisa Yan and Jerry Cain, CS109, 2020
Inference via math
25
Flu Under-grad
TiredFever
π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
1. Compute joint probabilities
2. Definition of conditional probability
π πΉ01 = 1, πΉ23 = 0, π = 0, π = 1π πΉ01 = 0, πΉ23 = 0, π = 0, π = 1
π πΉ01 = 1, πΉ23 = 0, π = 0, π = 1β4 π πΉ01 = π₯, πΉ23 = 0, π = 0, π = 1
2. π πΉ+, = 1|πΉ"- = 0, π = 0, π = 1 ?π πΉ!" = 1 = 0.1
= 0.095
Lisa Yan and Jerry Cain, CS109, 2020
Inference via math
3. π πΉ+, = 1|π = 1, π = 1 ?
26
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
π€
Lisa Yan and Jerry Cain, CS109, 2020
Inference via math
3. π πΉ+, = 1|π = 1, π = 1 ?
27
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
1. Compute joint probabilities
2. Definition of conditional probability
π πΉ01 = 1, π = 1, πΉ23 = 1, π = 1β¦
π πΉ01 = 0, π = 1, πΉ23 = 0, π = 1 ?
β5 π πΉ01 = 1, π = 1, πΉ23 = π¦, π = 1β4β5 π πΉ01 = π₯, π = 1, πΉ23 = π¦, π = 1
= 0.122
Lisa Yan and Jerry Cain, CS109, 2020
Inference via math
28
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
Solving inference questions precisely is possible, but sometimes tedious.
Can we use samplingto do approximate inference?
Yes.
(live)15: General InferenceLisa Yan and Jerry CainOctober 16, 2020
29
Lisa Yan and Jerry Cain, CS109, 2020
Constructing a Bayesian NetworkIn a Bayesian Network,Each random variable is conditionally independent of its non-descendants, given its parents.
β’ Node: random variableβ’ Directed edge: conditional dependency
Examples:β’ π πΉ!" = 1|π = 0, πΉ#$ = 1 = π πΉ!" = 1|πΉ#$ = 1β’ π πΉ#$ = 1,π = 0 = π πΉ#$ = 1 π π = 0
30
Flu Under-grad
TiredFever
Review
Breakout Rooms
Check out the question on the next slide (Slide 32). Post any clarifications here!https://us.edstem.org/courses/2678/discussion/146234
Breakout rooms: 4 min. Introduce yourself!
31
π€
Lisa Yan and Jerry Cain, CS109, 2020
Inference via math
π πΉ+, = 1|πΉ"- = 1, π = 1, π = 1 ?
32
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
π€
Lisa Yan and Jerry Cain, CS109, 2020
Inference via math
π πΉ+, = 1|πΉ"- = 1, π = 1, π = 1 ?
33
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
Lisa Yan and Jerry Cain, CS109, 2020
Inference via math
34
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
Solving inference questions precisely is possible, but sometimes tedious.
Can we use samplingto do approximate inference?
Yes.
Lisa Yan and Jerry Cain, CS109, 2020
Inference via math
What is π πΉ!" = 1|π = 1, π = 1 ?
= 0.122
35
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
Review
(from pre-lecture video)
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
Step 0:Have a fully specifiedBayesian Network
36
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
37
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation = β¦
# number of samples with π = 1, π = 1samples_event =
# number of samples with πΉ01 = 1, π = 1, π = 1return len(samples_event)/len(samples_observation)
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion: [flu, und, fev, tir]
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
38
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation = β¦
# number of samples with π = 1, π = 1samples_event =
# number of samples with πΉ01 = 1, π = 1, π = 1return len(samples_event)/len(samples_observation)
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
# samples with πΉ+, = 1, π = 1, π = 1# samples with π = 1, π = 1
ApproximateProbability =
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
39
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
# samples with πΉ+, = 1, π = 1, π = 1# samples with π = 1, π = 1
ApproximateProbability =
π€
Why would this definition of approximate probability make sense?
ThinkSlide 41 has a question to go over by yourself.
Post any clarifications here!https://us.edstem.org/courses/2678/discussion/146234
Think by yourself: 2 min
40
π€(by yourself)
Lisa Yan and Jerry Cain, CS109, 2020
Why would this approximate probability make sense?
41
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
# samples with πΉ+, = 1, π = 1, π = 1# samples with π = 1, π = 1
ApproximateProbability =
Recall our definition of probability as a frequency:
π πΈ = lim'β<
π(πΈ)π
π = # of total trialsπ(πΈ) = # trials where πΈ occurs
Lisa Yan and Jerry Cain, CS109, 2020
Why would this approximate probability make sense?
42
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
# samples with πΉ+, = 1, π = 1, π = 1# samples with π = 1, π = 1
ApproximateProbability =
Recall our definition of probability as a frequency:
π πΈ = lim'β<
π(πΈ)π
π = # of total trialsπ(πΈ) = # trials where πΈ occurs
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
43
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation = β¦
# number of samples with π = 1, π = 1samples_event =
# number of samples with πΉ01 = 1, π = 1, π = 1return len(samples_event)/len(samples_observation)
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion: [flu, und, fev, tir]
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
N_SAMPLES = 100000# Method: Sample a ton# -------------------# create N_SAMPLES with likelihood proportional# to the joint distributiondef sample_a_ton():
samples = []for i in range(N_SAMPLES):
sample = make_sample() # a particlesamples.append(sample)
return samples
44
How do we make a sampleπΉ01 = π, π = π, πΉ23 = π, π = π
according to thejoint probability?
Create a sample using the Bayesian Network!!
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
45
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
# Method: Make Sample# -------------------# create a single sample from the joint distribution# based on the medical "WebMD" Bayesian Networkdef make_sample():
# prior on causal factorsflu = bernoulli(0.1)und = bernoulli(0.8)
# choose fever based on fluif flu == 1: fev = bernoulli(0.9)else: fev = bernoulli(0.05)
# choose tired based on (undergrad and flu)# # TODO: fill in# #
# a sample from the joint has an# assignment to *all* random variablesreturn [flu, und, fev, tir]
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
46
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
# Method: Make Sample# -------------------# create a single sample from the joint distribution# based on the medical "WebMD" Bayesian Networkdef make_sample():
# prior on causal factorsflu = bernoulli(0.1)und = bernoulli(0.8)
# choose fever based on fluif flu == 1: fev = bernoulli(0.9)else: fev = bernoulli(0.05)
# choose tired based on (undergrad and flu)# # TODO: fill in# #
# a sample from the joint has an# assignment to *all* random variablesreturn [flu, und, fev, tir]
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
47
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
# Method: Make Sample# -------------------# create a single sample from the joint distribution# based on the medical "WebMD" Bayesian Networkdef make_sample():
# prior on causal factorsflu = bernoulli(0.1)und = bernoulli(0.8)
# choose fever based on fluif flu == 1: fev = bernoulli(0.9)else: fev = bernoulli(0.05)
# choose tired based on (undergrad and flu)# # TODO: fill in# #
# a sample from the joint has an# assignment to *all* random variablesreturn [flu, und, fev, tir]
Lisa Yan and Jerry Cain, CS109, 2020
# Method: Make Sample# -------------------# create a single sample from the joint distribution# based on the medical "WebMD" Bayesian Networkdef make_sample():
# prior on causal factorsflu = bernoulli(0.1)und = bernoulli(0.8)
# choose fever based on fluif flu == 1: fev = bernoulli(0.9)else: fev = bernoulli(0.05)
# choose tired based on (undergrad and flu)# # TODO: fill in# #
# a sample from the joint has an# assignment to *all* random variablesreturn [flu, und, fev, tir]
Rejection sampling algorithm
48
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm# Method: Make Sample# -------------------# create a single sample from the joint distribution# based on the medical "WebMD" Bayesian Networkdef make_sample():
# prior on causal factorsflu = bernoulli(0.1)und = bernoulli(0.8)
# choose fever based on fluif flu == 1: fev = bernoulli(0.9)else: fev = bernoulli(0.05)
# choose tired based on (undergrad and flu)if flu == 0 and und == 0: tir = bernoulli(0.1)elif flu == 0 and und == 1: tir = bernoulli(0.8)elif flu == 1 and und == 0: tir = bernoulli(0.9)else: tir = bernoulli(1.0)
# a sample from the joint has an# assignment to *all* random variablesreturn [flu, und, fev, tir] 49
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm# Method: Make Sample# -------------------# create a single sample from the joint distribution# based on the medical "WebMD" Bayesian Networkdef make_sample():
# prior on causal factorsflu = bernoulli(0.1)und = bernoulli(0.8)
# choose fever based on fluif flu == 1: fev = bernoulli(0.9)else: fev = bernoulli(0.05)
# choose tired based on (undergrad and flu)if flu == 0 and und == 0: tir = bernoulli(0.1)elif flu == 0 and und == 1: tir = bernoulli(0.8)elif flu == 1 and und == 0: tir = bernoulli(0.9)else: tir = bernoulli(1.0)
# a sample from the joint has an# assignment to *all* random variablesreturn [flu, und, fev, tir] 50
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
Interlude for jokes/announcements
51
Lisa Yan and Jerry Cain, CS109, 2020
Announcements
52
Problem Set 4
Out: Today!Due: Monday 10/26 1pmCovers: Up to and including today
Mid-quarter feedback form
linkOpen until: next Friday
Python tutorial #3
When: Mon 10/19 6-7pm PTRecorded? YesCovers: PS4-PS6 contentNotes: to be posted online
Lisa Yan and Jerry Cain, CS109, 2020
Announcements: CS109 contest
53
Do something cool and creativewith probability
Grand Prize:Two lowest quizzes replaced with 100%
Finalists:Lowest quiz replaced with 100%
Optional Proposal: Mon. 11/2, 11:59pmDue: Sat. 11/14, 11:59pm
https://web.stanford.edu/class/cs109/psets/cs109_contest.pdf
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
54
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation = β¦
# number of samples with π = 1, π = 1samples_event =
# number of samples with πΉ01 = 1, π = 1, π = 1return len(samples_event)/len(samples_observation)
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion: [flu, und, fev, tir]
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
55
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation = β¦
# number of samples with π = 1, π = 1samples_event =
# number of samples with πΉ01 = 1, π = 1, π = 1return len(samples_event)/len(samples_observation)
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
56
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation =
reject_inconsistent(samples, observation)samples_event =
# number of samples with πΉ01 = 1, π = 1, π = 1return len(samples_event)/len(samples_observation)
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
57
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation =
reject_inconsistent(samples, observation)samples_event =
# number of samples with πΉ01 = 1, π = 1, π = 1return len(samples_event)/len(samples_observation)
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
Keep only samples that are consistentwith the observation π = 1, π = 1 .
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
58
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation =
reject_inconsistent(samples, observation)samples_event =
reject_inconsistent(samples_observation, event)return len(samples_event)/len(samples_observation)
Keep only samples that are consistentwith the observation π = 1, π = 0 .
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
# Method: Reject Inconsistent# -------------------# Rejects all samples that do not align with the outcome.# Returns a list of consistent samples.def reject_inconsistent(samples, outcome):
consistent_samples = []for sample in samples:
if check_consistent(sample, outcome):consistent_samples.append(sample)
return consistent_samples
π = 1, π = 1
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
59
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation =
reject_inconsistent(samples, observation)samples_event =
reject_inconsistent(samples_observation, event)return len(samples_event)/len(samples_observation)
Conditional event = samples with πΉ+, = 1, π = 1, π = 1 .
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
60
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation =
reject_inconsistent(samples, observation)samples_event =
reject_inconsistent(samples_observation, event)return len(samples_event)/len(samples_observation)
Conditional event = samples with πΉ+, = 1, π = 1, π = 1 .
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
def reject_inconsistent(samples, outcome):
...
return consistent_samples
πΉ01 = 1πΉ01 = π₯, π = 1, πΉ23 = π¦, π = 1
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling algorithm
61
def rejection_sampling(event, observation):samples = sample_a_ton()samples_observation =
reject_inconsistent(samples, observation)samples_event =
reject_inconsistent(samples_observation, event)return len(samples_event)/len(samples_observation)
# samples with πΉ+, = 1, π = 1, π = 1
What is π πΉ!" = 1|π = 1, π = 1 ?Inferencequestion:
# samples with π = 1, π = 1
ApproximateProbability =
Lisa Yan and Jerry Cain, CS109, 2020
To the code!
62
Lisa Yan and Jerry Cain, CS109, 2020
Rejection sampling
With enough samples, you can correctly compute:β’ Probability estimatesβ’ Conditional probability estimatesβ’ Expectation estimates
Because your samples are a representationof the joint distribution!
63
[flu, und, fev, tir]
P(has flu | undergrad and is tired) = 0.122
If you can sample enough from the joint distribution, you can answer any probability inference question.
Lisa Yan and Jerry Cain, CS109, 2020
Other applications
64
Chemical present?
Chemical detected?
Trajectory deviation
Solar panel failure
Electrical system failure
Battery failure
Communi-cation loss
Take CS238/AA228: Decision Making under Uncertainty!
Lisa Yan and Jerry Cain, CS109, 2020
Challenge with Bayesian Networks
65
Flu Under-grad
TiredFever
What if we donβt know the structure?
Take CS228: Probabilistic Graphical Models!
? ?
?
?
Lisa Yan and Jerry Cain, CS109, 2020
Disadvantages of rejection sampling
What if we never encounter some samples?
66
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
π πΉ#$ = 1|πΉ!" = 1 = 0.9π πΉ#$ = 1|πΉ!" = 0 = 0.05
π πΉ!" = 1|πΉ#$ = 1 ?
[flu=0, und, fev=1, tir]
Lisa Yan and Jerry Cain, CS109, 2020
Disadvantages of rejection sampling
What if we never encounter some samples?
What if random variables are continuous?
67
Flu Under-grad
TiredFever
π πΉ!" = 1 = 0.1 π π = 1 = 0.8
π π = 1|πΉ!" = 0, π = 0 = 0.1π π = 1|πΉ!" = 0, π = 1 = 0.8π π = 1|πΉ!" = 1, π = 0 = 0.9π π = 1|πΉ!" = 1, π = 1 = 1.0
πΉ#$|πΉ!" = 1 ~π©(100,1.81)πΉ#$|πΉ!" = 0~π© 98.25,0.73
π πΉ!" = 1|πΉ#$ = 99.4 ?
Gibbs sampling (extra)
68
(no video)
Lisa Yan and Jerry Cain, CS109, 2020
Gibbs Sampling (not covered)
Basic idea:β’ Fix all observed eventsβ’ Incrementally sample a new value
for each random variableβ’ Difficulty: More coding for computing
different posterior probabilities
Learn in extra slides/extra notebook!(or by taking CS228/CS238)
69
Recommended