09: continuous rvs - stanford university
TRANSCRIPT
![Page 1: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/1.jpg)
09: Continuous RVsJerry CainApril 16, 2021
1
![Page 2: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/2.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Quick slide reference
2
3 Continuous RVs 09a_continuous_rvs
15 Uniform RV 09b_uniform
22 Exponential RV 09c_exponential
31 Exercises, CDF LIVE
55 Extra material 09e_extra
![Page 3: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/3.jpg)
Continuous RVs
3
09a_continuous_rvs
![Page 4: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/4.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Not all values are discrete
4
import numpy as npnp.random.random() ?
![Page 5: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/5.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
0 β¦ 44 52 60 β¦ 90π₯
0 β¦ 44 52 60 β¦ 9044 β¦ 52 56 60
People heightsYou are volunteering at the local elementary school.β’ To choose a t-shirt for your new buddy Jordan, you need to know their height.
1. What is the probability that yourbuddy is 54.0923857234 inches tall?
2. What is the probability that your buddy is between 52β56 inches tall?
5
Essentially 0
π₯
ππ₯
probability density function
smaller-width bars
β-lysmall bars
π 52 < π β€ 56
π(π₯)
π(π₯)
π₯48 52 56 60
π 52 < π β€ 54+ π 54 < π β€ 56 π 52 < π β€ 56
![Page 6: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/6.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Integrals
6
Integral = area under a curve Loving, not scary
![Page 7: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/7.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Continuous RV definitionA random variable π is continuous if there is a probability density functionπ π₯ β₯ 0 such that for ββ < π₯ < β:
π π β€ π β€ π = /,
-π(π₯) ππ₯
Integrating a PDF must always yield valid probabilities,and therefore the PDF must also satisfy
/./
/π(π₯) ππ₯ = π ββ < π < β = 1
Also written as: π0(π₯)
7
![Page 8: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/8.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
0 β¦ 44 52 60 β¦ 90
Todayβs main takeaway, #1
Integrate π(π₯) to get probabilities.
8
π₯
π π₯ : prob/inch
4 inches
π 52 β€ π β€ 56 = -!"
!#π(π₯) ππ₯PDF Units: probability per units of π
![Page 9: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/9.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Discrete random variable π
Probability mass function (PMF):π π₯
To get probability:π π = π₯ = π π₯
Continuous random variable π
Probability density function (PDF):π π₯
To get probability:
π π β€ π β€ π = /,
-π π₯ ππ₯
9
PMF vs PDF
Both are measures of how likely π is to take on a value.
![Page 10: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/10.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Computing probability
10
π π₯ = 512π₯ if 0 β€ π₯ β€ 2
0 otherwise
Let π be a continuous RV with PDF:
0.00
0.50
1.00
0.00 1.00 2.00
!(")
!What is π π β₯ 1 ?
π π β€ π β€ π = 4!
"π(π₯) ππ₯
![Page 11: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/11.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Computing probability
11
π π₯ = 512π₯ if 0 β€ π₯ β€ 2
0 otherwise
Let π be a continuous RV with PDF:
0.00
0.50
1.00
0.00 1.00 2.00
!(")
!What is π π β₯ 1 ?
Strategy 1: Integrate Strategy 2: Know triangles
π 1 β€ π < β = -$
%π π₯ ππ₯ = -
$
" 12π₯ππ₯
=1212π₯" 2
$
"=122 β
12=34
1 β1212
=34
Waitβ¦is this even legal?π 0 β€ π < 1 = β«&
$ π π₯ ππ₯ ? ?
π π β€ π β€ π = 4!
"π(π₯) ππ₯
![Page 12: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/12.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Todayβs main takeaway, #2
For a continuous random variable π with PDF π π₯ ,π π = π = β«!
! π π₯ ππ₯ = 0.
12
0 β¦ 44 52 60 β¦ 90
π₯
Contrast with PMF in discrete case: π π = π = π π
π π₯ : prob/inch
![Page 13: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/13.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
π₯'π₯"
PDF Properties
13
π π β€ π β€ π = /,
-π(π₯) ππ₯
For a continuous RV π with PDF π,
π₯
π π₯
support: set of π₯where π π₯ > 0
π₯$True/False:1. π π = π = 0
2. π π β€ π β€ π = π π < π < π = π π β€ π < π = π π < π β€ π
3. π(π₯) is a probability
4. In the graphed PDF above,π π₯< β€ π β€ π₯= > π π₯= β€ π β€ π₯>
![Page 14: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/14.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
π₯'π₯"
PDF Properties
14
π π β€ π β€ π = /,
-π(π₯) ππ₯
For a continuous RV π with PDF π,
β
Interval width ππ₯ β 0
π₯
π π₯
support: set of π₯where π π₯ > 0
π₯$
Compare area under the curve π
True/False:1. π π = π = 0
2. π π β€ π β€ π = π π < π < π = π π β€ π < π = π π < π β€ π
3. π(π₯) is a probability
4. In the graphed PDF above,π π₯< β€ π β€ π₯= > π π₯= β€ π β€ π₯>
β
![Page 15: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/15.jpg)
Uniform RV
15
09b_uniform
![Page 16: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/16.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
def An Uniform random variable π is defined as follows:
Uniform Random Variable
16
π π₯ = :1
π½ β πΌif πΌ β€ π₯ β€ π½
0 otherwiseπ~Uni(πΌ, π½)Support: πΌ, π½
(sometimes defined over πΌ, π½ ) Variance
Expectation
πΈ π =πΌ + π½2
Var π =π½ β πΌ =
12
πΌ π½π₯
π π₯1
π½ β πΌ
![Page 17: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/17.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Quick check
17
What is <D.E
if the following graphs are PDFs of Uniform RVs π?
0 25π₯
π π₯
?
1. 2. 3.
1π₯
π π₯
?
3/2
πΌ π½π₯
π π₯1
π½ β πΌ
β5 5π₯
π π₯?
If π~Uni(πΌ, π½), the PDF of π is:
π π₯ = 5<
D.Eif πΌ β€ π₯ β€ π½
0 otherwise
![Page 18: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/18.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
β5 5π₯
π π₯?
1π₯
π π₯
?
3/2
Quick check
18
??
1. 2. 3.
2
110
0 25π₯
π π₯
?125
πΌ π½π₯
π π₯1
π½ β πΌ
What is <D.E
if the following graphs are PDFs of Uniform RVs π?
If π~Uni(πΌ, π½), the PDF of π is:
π π₯ = 5<
D.Eif πΌ β€ π₯ β€ π½
0 otherwise
![Page 19: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/19.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Expectation and Variance
Discrete RV π
19
πΈ π =?(
π₯ π π₯
πΈ π(π) =?(
π(π₯) π π₯
πΈ π = -)%
%π₯π π₯ ππ₯
πΈ π(π) = -)%
%π π₯ π π₯ ππ₯
Continuous RV π
Both continuous and discrete RVsπΈ ππ + π = ππΈ π + π
Var(π) = πΈ (π β πΈ[π])= = πΈ π= β (πΈ[π])=
Var(ππ + π) = π=Var(π)
Linearity of ExpectationProperties of variance
TL;DR: β(*+, ββ«+,
![Page 20: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/20.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Uniform RV expectation
20
-)%
%π₯ β π π₯ ππ₯
=1
π½ β πΌβ D12π₯"
-
.
=1
π½ β πΌβ 12π½" β πΌ"
=12β π½ + πΌ π½ β πΌ
π½ β πΌ
πΈ π =
=πΌ + π½2
Interpretation:Average the start & end
πΌ π½π₯
π π₯1
π½ β πΌ
= --
.π₯ β
1π½ β πΌ
ππ₯
![Page 21: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/21.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
def An Uniform random variable π is defined as follows:
Uniform Random Variable
21
π π₯ = :1
π½ β πΌif πΌ β€ π₯ β€ π½
0 otherwiseπ~Uni(πΌ, π½)Support: πΌ, π½
(sometimes defined over πΌ, π½ ) Variance
Expectation
πΈ π =πΌ + π½2
Var π =π½ β πΌ =
12
On your own time
Just now
πΌ π½π₯
π π₯1
π½ β πΌ
![Page 22: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/22.jpg)
Exponential RV
22
09c_exponential
![Page 23: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/23.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Grid of random variables
23
Number of successes
Ber(π)One trial
Severaltrials
Intervalof time
Bin(π, π)
Poi(π)
Geo(π)
NegBin(π, π)
One success
Severalsuccesses
Interval of time tofirst success
Time until success
π = 1 π = 1
?Exp(π)
![Page 24: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/24.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Consider an experiment that lasts a duration of time until success occurs.def An Exponential random variable π is the amount of time until success.
Examples:β’ Time until next earthquakeβ’ Time for request to reach web serverβ’ Time until end of cell phone contract
Exponential Random Variable
24
π π₯ = Bππ.JK if π₯ β₯ 0
0 otherwiseπ~Exp(π)Support: 0,β
Variance
Expectation
πΈ π =1π
Var π =1π=
(in extra slides)
(on your own)
00.20.40.60.81
0 1 2 3 4 5
π π₯
π₯
![Page 25: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/25.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Interpreting Exp(π)def An Exponential random variable π is the amount of time until success.
25
π~Exp(π) Expectation πΈ π =1π
Based on the expectation πΈ π , what are the units of π?
![Page 26: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/26.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Interpreting Exp(π)def An Exponential random variable π is the amount of time until success.
26
Based on the expectation πΈ π , what are the units of π?
π~Exp(π) Expectation πΈ π =1π
e.g., average # of successes per second For both Poisson and Exponential RVs,π = # successes/time.
![Page 27: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/27.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
Earthquakes
27
1906 Earthquake Magnitude 7.8
![Page 28: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/28.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
EarthquakesMajor earthquakes (magnitude 8.0+) occur once every 500 years.*
1. What is the probability of a major earthquake in the next 30 years?
28
π~Exp(π) π π₯ = ππ#$% if π₯ β₯ 0
*In California, according to historical data form USGS, 2015
πΈ π = 1/π
We know on average:
0.002earthquakes
year
500years
earthquake
1earthquakes500 years
![Page 29: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/29.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
EarthquakesMajor earthquakes (magnitude 8.0+) occur once every 500 years.*
1. What is the probability of a major earthquake in the next 30 years?
29
Define events/ RVs & state goal
Solve
π: when nextearthquake happens
π ~Exp π = 0.002
Want: π π < 30year)$π:
π~Exp(π) π π₯ = ππ#$% if π₯ β₯ 0
β« π&%ππ₯ =1ππ&%
*In California, according to historical data form USGS, 2015
Recall
= 1/500
πΈ π = 1/π
![Page 30: 09: Continuous RVs - Stanford University](https://reader031.vdocuments.mx/reader031/viewer/2022012516/618fac8c7d20b83fe4375050/html5/thumbnails/30.jpg)
Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021
EarthquakesMajor earthquakes (magnitude 8.0+) occur once every 500 years.*
1. What is the probability of a major earthquake in the next 30 years?2. What is the standard deviation of years until the next earthquake?
30
Define events/ RVs & state goal
π: when nextearthquake happens
π ~Exp π = 0.002
Want: π π < 30year)$π:
*In California, according to historical data form USGS, 2015
Solve
π~Exp(π) π π₯ = ππ#$% if π₯ β₯ 0πΈ π = 1/π