09: continuous rvs - stanford university

30
09: Continuous RVs Jerry Cain April 16, 2021 1

Upload: others

Post on 13-Nov-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 09: Continuous RVs - Stanford University

09: Continuous RVsJerry CainApril 16, 2021

1

Page 2: 09: Continuous RVs - Stanford University

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

Continuous RVs

3

09a_continuous_rvs

Page 4: 09: Continuous RVs - Stanford University

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

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

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

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

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

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

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

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

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

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

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

Uniform RV

15

09b_uniform

Page 16: 09: Continuous RVs - Stanford University

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

PDF

𝐸 𝑋 =𝛼 + 𝛽2

Var 𝑋 =𝛽 βˆ’ 𝛼 =

12

𝛼 𝛽π‘₯

𝑓 π‘₯1

𝛽 βˆ’ 𝛼

Page 17: 09: Continuous RVs - Stanford University

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

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

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

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

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

PDF

𝐸 𝑋 =𝛼 + 𝛽2

Var 𝑋 =𝛽 βˆ’ 𝛼 =

12

On your own time

Just now

𝛼 𝛽π‘₯

𝑓 π‘₯1

𝛽 βˆ’ 𝛼

Page 22: 09: Continuous RVs - Stanford University

Exponential RV

22

09c_exponential

Page 23: 09: Continuous RVs - Stanford University

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

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

PDF

𝐸 𝑋 =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

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

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

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

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

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

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/πœ†