15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan,...
Transcript of 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan,...
![Page 1: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/1.jpg)
15: General InferenceLisa Yan and Jerry CainOctober 16, 2020
1
![Page 2: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/2.jpg)
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)
![Page 3: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/3.jpg)
General Inference: Introduction
3
15a_inference
![Page 4: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/4.jpg)
Lisa Yan and Jerry Cain, CS109, 2020
Inference
4
![Page 5: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/5.jpg)
Lisa Yan and Jerry Cain, CS109, 2020
Inference
5
![Page 6: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/6.jpg)
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?
![Page 7: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/7.jpg)
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
![Page 8: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/8.jpg)
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
![Page 9: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/9.jpg)
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
๐ค
![Page 10: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/10.jpg)
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.
![Page 11: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/11.jpg)
Lisa Yan and Jerry Cain, CS109, 2020
N can be largeโฆ
11
![Page 12: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/12.jpg)
Lisa Yan and Jerry Cain, CS109, 2020
Conditional Probability Independence
Conditionally Independent RVs
12
Conditional Distributions Independent RVsConditional Probability Independence
![Page 13: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/13.jpg)
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
๐ ๐ด๐ต|๐ธ = ๐ ๐ด|๐ธ ๐(๐ต|๐ธ)
![Page 14: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/14.jpg)
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
![Page 15: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/15.jpg)
Bayesian Networks
15
15b_bayes_nets
![Page 16: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/16.jpg)
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!!!
![Page 17: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/17.jpg)
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
![Page 18: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/18.jpg)
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
![Page 19: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/19.jpg)
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
๐ค
โ
![Page 20: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/20.jpg)
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
![Page 21: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/21.jpg)
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
![Page 22: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/22.jpg)
Inference (I): Math
22
15c_inference_math
![Page 23: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/23.jpg)
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
![Page 24: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/24.jpg)
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.
![Page 25: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/25.jpg)
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
![Page 26: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/26.jpg)
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
๐ค
![Page 27: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/27.jpg)
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
![Page 28: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/28.jpg)
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.
![Page 29: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/29.jpg)
(live)15: General InferenceLisa Yan and Jerry CainOctober 16, 2020
29
![Page 30: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/30.jpg)
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
![Page 31: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/31.jpg)
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
๐ค
![Page 32: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/32.jpg)
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
๐ค
![Page 33: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/33.jpg)
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
![Page 34: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/34.jpg)
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.
![Page 35: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/35.jpg)
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)
![Page 36: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/36.jpg)
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
![Page 37: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/37.jpg)
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]
![Page 38: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/38.jpg)
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 =
![Page 39: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/39.jpg)
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?
![Page 40: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/40.jpg)
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)
![Page 41: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/41.jpg)
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
![Page 42: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/42.jpg)
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
![Page 43: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/43.jpg)
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]
![Page 44: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/44.jpg)
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!!
![Page 45: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/45.jpg)
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]
![Page 46: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/46.jpg)
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]
![Page 47: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/47.jpg)
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]
![Page 48: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/48.jpg)
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
![Page 49: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/49.jpg)
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
![Page 50: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/50.jpg)
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
![Page 51: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/51.jpg)
Interlude for jokes/announcements
51
![Page 52: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/52.jpg)
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
![Page 53: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/53.jpg)
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
![Page 54: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/54.jpg)
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]
![Page 55: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/55.jpg)
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:
![Page 56: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/56.jpg)
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:
![Page 57: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/57.jpg)
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 .
![Page 58: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/58.jpg)
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
![Page 59: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/59.jpg)
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:
![Page 60: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/60.jpg)
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
![Page 61: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/61.jpg)
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 =
![Page 62: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/62.jpg)
Lisa Yan and Jerry Cain, CS109, 2020
To the code!
62
![Page 63: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/63.jpg)
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.
![Page 64: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/64.jpg)
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!
![Page 65: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/65.jpg)
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!
? ?
?
?
![Page 66: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/66.jpg)
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]
![Page 67: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/67.jpg)
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 ?
![Page 68: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/68.jpg)
Gibbs sampling (extra)
68
(no video)
![Page 69: 15: General Inferenceweb.stanford.edu/class/cs109/lectures/15_general_inference_blank.pdfLisa Yan, CS109, 2020 Quick slide reference 2 3 General Inference: intro 15a_inference 15 Bayesian](https://reader034.fdocuments.us/reader034/viewer/2022050420/5f8f82b7a3379b7be87e92b3/html5/thumbnails/69.jpg)
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