07: random variables ii - stanford...

71
07: Random Variables II Lisa Yan April 22, 2020 1

Upload: others

Post on 14-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

07: Random Variables IILisa Yan

April 22, 2020

1

Page 2: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Binomial RV

2

07d_binomial

Page 3: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Consider an experiment: ๐‘› independent trials of Ber(๐‘) random variables.

def A Binomial random variable ๐‘‹ is the number of successes in ๐‘› trials.

Examples:โ€ข # heads in n coin flips

โ€ข # of 1โ€™s in randomly generated length n bit string

โ€ข # of disk drives crashed in 1000 computer cluster(assuming disks crash independently)

Binomial Random Variable

3

๐‘˜ = 0, 1,โ€ฆ , ๐‘›:

๐‘ƒ ๐‘‹ = ๐‘˜ = ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜๐‘‹~Bin(๐‘›, ๐‘)

Support: {0,1,โ€ฆ , ๐‘›}

PMF

๐ธ ๐‘‹ = ๐‘›๐‘Var ๐‘‹ = ๐‘›๐‘(1 โˆ’ ๐‘)Variance

Expectation

Page 4: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020 4

Page 5: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Reiterating notation

The parameters of a Binomial random variable:

โ€ข ๐‘›: number of independent trials

โ€ข ๐‘: probability of success on each trial

5

1. The random

variable

2. is distributed

as a3. Binomial 4. with parameters

๐‘‹ ~ Bin(๐‘›, ๐‘)

Page 6: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Reiterating notation

If ๐‘‹ is a binomial with parameters ๐‘› and ๐‘, the PMF of ๐‘‹ is

6

๐‘‹ ~ Bin(๐‘›, ๐‘)

๐‘ƒ ๐‘‹ = ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

Probability Mass Function for a BinomialProbability that ๐‘‹takes on the value ๐‘˜

Page 7: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Three coin flips

Three fair (โ€œheadsโ€ with ๐‘ = 0.5) coins are flipped.

โ€ข ๐‘‹ is number of heads

โ€ข ๐‘‹~Bin 3, 0.5

Compute the following event probabilities:

7

๐‘‹~Bin(๐‘›, ๐‘) ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

๐‘ƒ ๐‘‹ = 0

๐‘ƒ ๐‘‹ = 1

๐‘ƒ ๐‘‹ = 2

๐‘ƒ ๐‘‹ = 3

๐‘ƒ ๐‘‹ = 7

P(event)๐Ÿค”

Page 8: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Three coin flips

Three fair (โ€œheadsโ€ with ๐‘ = 0.5) coins are flipped.

โ€ข ๐‘‹ is number of heads

โ€ข ๐‘‹~Bin 3, 0.5

Compute the following event probabilities:

8

๐‘‹~Bin(๐‘›, ๐‘) ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

๐‘ƒ ๐‘‹ = 0 = ๐‘ 0 =30

๐‘0 1 โˆ’ ๐‘ 3 =1

8

๐‘ƒ ๐‘‹ = 1

๐‘ƒ ๐‘‹ = 2

๐‘ƒ ๐‘‹ = 3

๐‘ƒ ๐‘‹ = 7

= ๐‘ 1 =31

๐‘1 1 โˆ’ ๐‘ 2 =3

8

= ๐‘ 2 =32

๐‘2 1 โˆ’ ๐‘ 1 =3

8

= ๐‘ 3 =33

๐‘3 1 โˆ’ ๐‘ 0 =1

8

= ๐‘ 7 = 0

P(event) PMF

Extra math note:

By Binomial Theorem,

we can prove

ฯƒ๐‘˜=0๐‘› ๐‘ƒ ๐‘‹ = ๐‘˜ = 1

Page 9: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Consider an experiment: ๐‘› independent trials of Ber(๐‘) random variables.

def A Binomial random variable ๐‘‹ is the number of successes in ๐‘› trials.

Examples:โ€ข # heads in n coin flips

โ€ข # of 1โ€™s in randomly generated length n bit string

โ€ข # of disk drives crashed in 1000 computer cluster(assuming disks crash independently)

Binomial Random Variable

9

๐‘‹~Bin(๐‘›, ๐‘)

Range: {0,1,โ€ฆ , ๐‘›} Variance

Expectation

PMF

๐ธ ๐‘‹ = ๐‘›๐‘Var ๐‘‹ = ๐‘›๐‘(1 โˆ’ ๐‘)

๐‘˜ = 0, 1,โ€ฆ , ๐‘›:

๐‘ƒ ๐‘‹ = ๐‘˜ = ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

Page 10: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Ber ๐‘ = Bin(1, ๐‘)

Binomial RV is sum of Bernoulli RVs

Bernoulli

โ€ข ๐‘‹~Ber(๐‘)

Binomial

โ€ข ๐‘Œ~Bin ๐‘›, ๐‘

โ€ข The sum of ๐‘› independent Bernoulli RVs

10

๐‘Œ =

๐‘–=1

๐‘›

๐‘‹๐‘– , ๐‘‹๐‘– ~Ber(๐‘)

+

+

+

Page 11: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Consider an experiment: ๐‘› independent trials of Ber(๐‘) random variables.

def A Binomial random variable ๐‘‹ is the number of successes in ๐‘› trials.

Examples:โ€ข # heads in n coin flips

โ€ข # of 1โ€™s in randomly generated length n bit string

โ€ข # of disk drives crashed in 1000 computer cluster(assuming disks crash independently)

Binomial Random Variable

11

๐‘‹~Bin(๐‘›, ๐‘)

Range: {0,1,โ€ฆ , ๐‘›}

๐ธ ๐‘‹ = ๐‘›๐‘Var ๐‘‹ = ๐‘›๐‘(1 โˆ’ ๐‘)Variance

Expectation

PMF ๐‘˜ = 0, 1,โ€ฆ , ๐‘›:

๐‘ƒ ๐‘‹ = ๐‘˜ = ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

Proof:

Page 12: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Consider an experiment: ๐‘› independent trials of Ber(๐‘) random variables.

def A Binomial random variable ๐‘‹ is the number of successes in ๐‘› trials.

Examples:โ€ข # heads in n coin flips

โ€ข # of 1โ€™s in randomly generated length n bit string

โ€ข # of disk drives crashed in 1000 computer cluster(assuming disks crash independently)

Binomial Random Variable

12

๐‘‹~Bin(๐‘›, ๐‘)

Range: {0,1,โ€ฆ , ๐‘›}

PMF

๐ธ ๐‘‹ = ๐‘›๐‘Var ๐‘‹ = ๐‘›๐‘(1 โˆ’ ๐‘)

Weโ€™ll prove

this later in

the course

Variance

Expectation

๐‘˜ = 0, 1,โ€ฆ , ๐‘›:

๐‘ƒ ๐‘‹ = ๐‘˜ = ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

Page 13: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

No, give me the variance proof right now

13

proofwiki.org

Page 14: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Poisson

14

08a_poisson

Page 15: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Before we start

The natural exponent ๐‘’:

https://en.wikipedia.org/wiki/E_(mathematical_constant)

15

lim๐‘›โ†’โˆž

1 โˆ’๐œ†

๐‘›

๐‘›

= ๐‘’โˆ’๐œ†

Jacob Bernoulli

while studying

compound interest

in 1683

Page 16: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Algorithmic ride sharing

16

๐Ÿ™‹โ€โ™€๏ธ

๐Ÿ™‹โ€โ™€๏ธ

๐Ÿ™‹โ€โ™‚๏ธ

๐Ÿ™‹โ€โ™‚๏ธ๐Ÿ™‹โ€โ™‚๏ธ

Probability of ๐‘˜ requests from this area in the next 1 minute?

On average, ๐œ† = 5 requests per minuteSuppose we know:

Page 17: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Algorithmic ride sharing, approximately

At each second:โ€ข Independent trial

โ€ข You get a request (1) or you donโ€™t (0).

Let ๐‘‹ = # of requests in minute.

๐ธ ๐‘‹ = ๐œ† = 5

17

Probability of ๐‘˜ requests from this area in the next 1 minute?

On average, ๐œ† = 5 requests per minute

0 0 1 0 1 โ€ฆ 0 0 0 0 1

1 2 3 4 5 60

๐‘‹ ~ Bin ๐‘› = 60, ๐‘ = 5/60

Break a minute down into 60 seconds:

๐‘ƒ ๐‘‹ = ๐‘˜ =60๐‘˜

5

60

๐‘˜

1 โˆ’5

60

๐‘›โˆ’๐‘˜

But what if there are tworequests in the same second?๐Ÿค”

Page 18: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Algorithmic ride sharing, approximately

At each millisecond:โ€ข Independent trial

โ€ข You get a request (1) or you donโ€™t (0).

Let ๐‘‹ = # of requests in minute.

๐ธ ๐‘‹ = ๐œ† = 5

18

Probability of ๐‘˜ requests from this area in the next 1 minute?

On average, ๐œ† = 5 requests per minute

Break a minute down into 60,000 milliseconds:

๐‘ƒ ๐‘‹ = ๐‘˜ =๐‘›๐‘˜

๐œ†

๐‘›

๐‘˜

1 โˆ’๐œ†

๐‘›

๐‘›โˆ’๐‘˜

โ€ฆ

1 60,000

๐‘‹ ~ Bin ๐‘› = 60000, ๐‘ = ๐œ†/๐‘›

But what if there are tworequests in the same millisecond?

๐Ÿค”

Page 19: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Algorithmic ride sharing, approximately

For each time bucket:โ€ข Independent trial

โ€ข You get a request (1) or you donโ€™t (0).

Let ๐‘‹ = # of requests in minute.

๐ธ ๐‘‹ = ๐œ† = 5

19

Probability of ๐‘˜ requests from this area in the next 1 minute?

On average, ๐œ† = 5 requests per minute

Break a minute down into infinitely small buckets:

๐‘ƒ ๐‘‹ = ๐‘˜

= lim๐‘›โ†’โˆž

๐‘›๐‘˜

๐œ†

๐‘›

๐‘˜

1 โˆ’๐œ†

๐‘›

๐‘›โˆ’๐‘˜

Who wants to see some cool math?

OMG so small

1 โˆž

๐‘‹ ~ Bin ๐‘›, ๐‘ = ๐œ†/๐‘›

Page 20: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Binomial in the limit

20

๐‘ƒ ๐‘‹ = ๐‘˜

= lim๐‘›โ†’โˆž

๐‘›๐‘˜

๐œ†

๐‘›

๐‘˜

1 โˆ’๐œ†

๐‘›

๐‘›โˆ’๐‘˜ = lim๐‘›โ†’โˆž

๐‘›!

๐‘˜!(๐‘› โˆ’ ๐‘˜)!

๐œ†๐‘˜

๐‘›๐‘˜

1 โˆ’l๐‘›

๐‘›

1 โˆ’l๐‘›

๐‘˜

lim๐‘›โ†’โˆž

1 โˆ’๐œ†

๐‘›

๐‘›

= ๐‘’โˆ’๐œ†

= lim๐‘›โ†’โˆž

๐‘›!

๐‘›๐‘˜(๐‘› โˆ’ ๐‘˜)!

๐œ†๐‘˜

๐‘˜!

1 โˆ’l๐‘›

๐‘›

1 โˆ’l๐‘›

๐‘˜

= lim๐‘›โ†’โˆž

๐‘›!

๐‘›๐‘˜(๐‘› โˆ’ ๐‘˜)!

๐œ†๐‘˜

๐‘˜!

๐‘’โˆ’๐œ†

1 โˆ’l๐‘›

๐‘˜

= lim๐‘›โ†’โˆž

๐‘› ๐‘› โˆ’ 1 โ‹ฏ ๐‘› โˆ’ ๐‘˜ + 1

๐‘›๐‘˜๐‘› โˆ’ ๐‘˜ !

๐‘› โˆ’ ๐‘˜ !

๐œ†๐‘˜

๐‘˜!

๐‘’โˆ’๐œ†

1 โˆ’l๐‘›

๐‘˜

= lim๐‘›โ†’โˆž

๐‘›๐‘˜

๐‘›๐‘˜๐œ†๐‘˜

๐‘˜!

๐‘’โˆ’๐œ†

1=๐œ†๐‘˜

๐‘˜!๐‘’โˆ’๐œ†

Page 21: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Algorithmic ride sharing

21

๐Ÿ™‹โ€โ™€๏ธ

๐Ÿ™‹โ€โ™€๏ธ

๐Ÿ™‹โ€โ™‚๏ธ

๐Ÿ™‹โ€โ™‚๏ธ๐Ÿ™‹โ€โ™‚๏ธ

Probability of ๐‘˜ requests from this area in the next 1 minute?

On average, ๐œ† = 5 requests per minute

๐‘ƒ ๐‘‹ = ๐‘˜ =๐œ†๐‘˜

๐‘˜!๐‘’โˆ’๐œ†

Page 22: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Simeon-Denis Poisson

French mathematician (1781 โ€“ 1840)

โ€ข Published his first paper at age 18

โ€ข Professor at age 21

โ€ข Published over 300 papers

โ€œLife is only good for two things: doing mathematics and teaching it.โ€

22

Page 23: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Consider an experiment that lasts a fixed interval of time.

def A Poisson random variable ๐‘‹ is the number of successes over the experiment duration.

Examples:โ€ข # earthquakes per year

โ€ข # server hits per second

โ€ข # of emails per day

Yes, expectation == variance

for Poisson RV! More later.

Poisson Random Variable

23

๐‘ƒ ๐‘‹ = ๐‘˜ = ๐‘’โˆ’๐œ†๐œ†๐‘˜

๐‘˜!๐‘‹~Poi(๐œ†)

Support: {0,1, 2, โ€ฆ }

PMF

๐ธ ๐‘‹ = ๐œ†Var ๐‘‹ = ๐œ†Variance

Expectation

Page 24: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Earthquakes

There are an average of 2.79 major earthquakes in the world each year.

What is the probability of 3 major earthquakes happening next year?

24

๐‘ ๐‘˜ = ๐‘’โˆ’๐œ†๐œ†๐‘˜

๐‘˜!

1. Define RVs

2. Solve

0

0.05

0.1

0.15

0.2

0.25

0.3

0 1 2 3 4 5 6 7 8 9 10

๐‘ƒ(๐‘‹

= ๐‘˜

)

Number of earthquakes, ๐‘˜

๐‘‹~Poi(๐œ†)

๐ธ ๐‘‹ = ๐œ†

Page 25: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Are earthquakes really Poissonian?

25

Page 26: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Poisson Paradigm

26

08b_poisson_paradigm

Page 27: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

DNA

27

All the movies, images,

emails and other digital

data from more than

600 smartphones

(10,000 GB) can be

stored in the faint pink

smear of DNA at the end

of this test tube.

What is the probability

that DNA storage stays

uncorrupted?

Page 28: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

DNA

What is the probability that DNA storage stays uncorrupted?โ€ข In DNA (and real networks), we store large strings.โ€ข Let string length be long, e.g., ๐‘› โ‰ˆ 104

โ€ข Probability of corruption of each base pair is very small, e.g., ๐‘ = 10โˆ’6

โ€ข Let ๐‘‹ = # of corruptions.

What is P(DNA storage is uncorrupted) = ๐‘ƒ ๐‘‹ = 0 ?

28

1. Approach 1:

๐‘‹~Bin ๐‘› = 104, ๐‘ = 10โˆ’6

๐‘ƒ ๐‘‹ = ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

= 104

010โˆ’6โ‹…0 1 โˆ’ 10โˆ’6 104โˆ’0

โ‰ˆ 0.99049829

2. Approach 2:

๐‘‹~Poi ๐œ† = 104 โ‹… 10โˆ’6 = 0.01

๐‘ƒ ๐‘‹ = ๐‘˜ = ๐‘’โˆ’๐œ†๐œ†๐‘˜

๐‘˜!= ๐‘’โˆ’0.01

0.010

0!

= ๐‘’โˆ’0.01

โ‰ˆ 0.99049834

โš ๏ธunwieldy!a good

approximation!

โœ…

Page 29: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

The Poisson Paradigm, part 1

Poisson approximates Binomial when ๐‘› is large, ๐‘ is small, and ๐œ† = ๐‘›๐‘ is โ€œmoderate.โ€

Different interpretations of โ€œmoderateโ€:

โ€ข ๐‘› > 20 and ๐‘ < 0.05

โ€ข ๐‘› > 100 and ๐‘ < 0.1

Poisson is Binomial in the limit:

โ€ข ๐œ† = ๐‘›๐‘, where ๐‘› โ†’ โˆž, ๐‘ โ†’ 0

29

Poisson can approximate Binomial.

0

0.05

0.1

0.15

0.2

0.25

0.3

0 1 2 3 4 5 6 7 8 9 10

๐‘ƒ(๐‘‹

= ๐‘˜

)

๐‘‹ = ๐‘˜

Bin(10,0.3)

Bin(100,0.03)

Bin(1000,0.003)

Poi(3)

๐‘‹~Poi(๐œ†)

๐ธ ๐‘‹ = ๐œ†

๐‘Œ~Bin(๐‘›, ๐‘)

๐ธ ๐‘Œ = ๐‘›๐‘

Page 30: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Consider an experiment that lasts a fixed interval of time.

def A Poisson random variable ๐‘‹ is the number of occurrences over the experiment duration.

Examples:โ€ข # earthquakes per year

โ€ข # server hits per second

โ€ข # of emails per day

Time to show intuition for why

expectation == variance!

Poisson Random Variable

30

๐‘ƒ ๐‘‹ = ๐‘˜ = ๐‘’โˆ’๐œ†๐œ†๐‘˜

๐‘˜!๐‘‹~Poi(๐œ†)

Support: {0,1, 2, โ€ฆ } Variance

Expectation

PMF

๐ธ ๐‘‹ = ๐œ†Var ๐‘‹ = ๐œ†

Page 31: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Properties of Poi(๐œ†) with the Poisson paradigm

Recall the Binomial:

Consider ๐‘‹~Poi(๐œ†), where ๐œ† = ๐‘›๐‘ (๐‘› โ†’ โˆž, ๐‘ โ†’ 0):

Proof:

๐ธ ๐‘‹ = ๐‘›๐‘ = ๐œ†Var ๐‘‹ = ๐‘›๐‘ 1 โˆ’ ๐‘ โ†’ ๐œ† 1 โˆ’ 0 = ๐œ†

31

๐‘Œ~Bin(๐‘›, ๐‘)Variance

Expectation ๐ธ ๐‘Œ = ๐‘›๐‘Var ๐‘Œ = ๐‘›๐‘(1 โˆ’ ๐‘)

Expectation ๐ธ ๐‘‹ = ๐œ†Var ๐‘‹ = ๐œ†

๐‘‹~Poi(๐œ†)Variance

Page 32: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

A Real License Plate Seen at Stanford

No, itโ€™s not mineโ€ฆ but I kind of wish it was.

Page 33: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Poisson Paradigm, part 2

Poisson can still provide a good approximation of the Binomial,even when assumptions are โ€œmildlyโ€ violated.

You can apply the Poisson approximation when:

โ€ข โ€Successesโ€ in trials are not entirely independente.g.: # entries in each bucket in large hash table.

โ€ข Probability of โ€œSuccessโ€ in each trial varies (slightly),like a small relative change in a very small pe.g.: Average # requests to web server/sec may fluctuate

slightly due to load on network

33

๐Ÿ‘ˆ

We wonโ€™t explore this too much,

but I want you to know it exists.

Page 34: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Other Discrete RVs

34

08c_other_discrete

Page 35: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Grid of random variables

35

Number of

successes

Ber(๐‘)One trial

Several

trials

Interval

of time

Bin(๐‘›, ๐‘)

Poi(๐œ†) (tomorrow)

One success

Several

successes

Interval of time to

first success

Time until

success

๐‘› = 1

Focus on understanding how and when to use RVs, not on memorizing PMFs.

Page 36: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Consider an experiment: independent trials of Ber(๐‘) random variables.

def A Geometric random variable ๐‘‹ is the # of trials until the first success.

Examples:โ€ข Flipping a coin (๐‘ƒ heads = ๐‘) until first heads appears

โ€ข Generate bits with ๐‘ƒ bit = 1 = ๐‘ until first 1 generated

Geometric RV

36

๐‘ƒ ๐‘‹ = ๐‘˜ = 1 โˆ’ ๐‘ ๐‘˜โˆ’1๐‘๐‘‹~Geo(๐‘)

Support: {1, 2, โ€ฆ }

PMF

๐ธ ๐‘‹ =1

๐‘

Var ๐‘‹ =1โˆ’๐‘

๐‘2Variance

Expectation

Page 37: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Consider an experiment: independent trials of Ber(๐‘) random variables.

def A Negative Binomial random variable ๐‘‹ is the # of trials until ๐‘Ÿ successes.

Examples:โ€ข Flipping a coin until ๐‘Ÿ๐‘กโ„Ž heads appears

โ€ข # of strings to hash into table until bucket 1 has ๐‘Ÿ entries

Negative Binomial RV

37

๐‘ƒ ๐‘‹ = ๐‘˜ =๐‘˜ โˆ’ 1๐‘Ÿ โˆ’ 1

1 โˆ’ ๐‘ ๐‘˜โˆ’๐‘Ÿ๐‘๐‘Ÿ๐‘‹~NegBin(๐‘Ÿ, ๐‘)

Support: {๐‘Ÿ, ๐‘Ÿ + 1,โ€ฆ }

PMF

๐ธ ๐‘‹ =๐‘Ÿ

๐‘

Var ๐‘‹ =๐‘Ÿ 1โˆ’๐‘

๐‘2Variance

Expectation

(fixed lecture error)

Geo ๐‘ = NegBin(1, ๐‘)

Page 38: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Grid of random variables

38

Number of

successes

Ber(๐‘)One trial

Several

trials

Interval

of time

Bin(๐‘›, ๐‘)

Poi(๐œ†)

Geo(๐‘)

NegBin(๐‘Ÿ, ๐‘)

(tomorrow)

One success

Several

successes

Interval of time to

first success

Time until

success

๐‘› = 1 ๐‘Ÿ = 1

Page 39: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Catching Pokemon

Wild Pokemon are captured by throwing Pokeballs at them.

โ€ข Each ball has probability p = 0.1 of capturing the Pokemon.

โ€ข Each ball is an independent trial.

What is the probability that you catch the Pokemon on the 5th try?

39

1. Define events/ RVs & state goal

A. ๐‘‹~Bin 5, 0.1B. ๐‘‹~Poi 0.5C. ๐‘‹~NegBin 5, 0.1D. ๐‘‹~NegBin 1, 0.1E. ๐‘‹~Geo 0.1F. None/other

2. Solve

๐‘‹~some distribution

Want: ๐‘ƒ ๐‘‹ = 5

๐Ÿค”

Page 40: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Wild Pokemon are captured by throwing Pokeballs at them.

โ€ข Each ball has probability p = 0.1 of capturing the Pokemon.

โ€ข Each ball is an independent trial.

What is the probability that you catch the Pokemon on the 5th try?

A. ๐‘‹~Bin 5, 0.1B. ๐‘‹~Poi 0.5C. ๐‘‹~NegBin 5, 0.1D. ๐‘‹~NegBin 1, 0.1E. ๐‘‹~Geo 0.1F. None/other

Catching Pokemon

40

1. Define events/ RVs & state goal

2. Solve

๐‘‹~some distribution

Want: ๐‘ƒ ๐‘‹ = 5

Page 41: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

2. Solve

Catching Pokemon

Wild Pokemon are captured by throwing Pokeballs at them.

โ€ข Each ball has probability p = 0.1 of capturing the Pokemon.

โ€ข Each ball is an independent trial.

What is the probability that you catch the Pokemon on the 5th try?

41

1. Define events/ RVs & state goal

2. Solve

๐‘‹~Geo 0.1

Want: ๐‘ƒ ๐‘‹ = 5

๐‘‹~Geo(๐‘) ๐‘ ๐‘˜ = 1 โˆ’ ๐‘ ๐‘˜โˆ’1๐‘

Page 42: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

(live)08: Random Variables IIIIOishi Banerjee and Cooper RaterinkAdapted from Lisa YanJuly 8, 2020

42

Page 43: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Our first common RVs

43

๐‘‹ ~ Ber(๐‘)

๐‘Œ ~ Bin(๐‘›, ๐‘)

1. The random

variable

2. is distributed

as a3. Bernoulli 4. with parameter

Example: Heads in one coin flip,

P(heads) = 0.8 = p

Example: # heads in 40 coin flips,

P(heads) = 0.8 = p

otherwise Identify PMF, or

identify as a function of an

existing random variable

Review

Page 44: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

ThinkThe next slide has a matching question to go over by yourself. Weโ€™ll go over it together afterwards.

Post any clarifications here!

https://us.edstem.org/courses/667/discussion/84212

Think by yourself: 2 min

44

๐Ÿค”(by yourself)

Page 45: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Visualizing Binomial PMFs

45

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

๐‘‹~Bin(๐‘›, ๐‘) ๐‘ ๐‘– =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

๐ธ ๐‘‹ = ๐‘›๐‘

C. D.

Match the distribution

to the graph:

1. Bin 10,0.5

2. Bin 10,0.3

3. Bin 10,0.7

4. Bin 5,0.5

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

A. B.

๐‘˜

๐‘ƒ๐‘‹=๐‘˜

๐‘˜

๐‘ƒ๐‘‹=๐‘˜

๐‘˜

๐‘ƒ๐‘‹=๐‘˜

๐‘˜

๐‘ƒ๐‘‹=๐‘˜

๐Ÿค”(by yourself)

Page 46: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Visualizing Binomial PMFs

46

Match the distribution

to the graph:

1. Bin 10,0.5

2. Bin 10,0.3

3. Bin 10,0.7

4. Bin 5,0.5

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

C. D.

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

A. B.

๐‘˜

๐‘ƒ๐‘‹=๐‘˜

๐‘˜

๐‘ƒ๐‘‹=๐‘˜

๐‘˜

๐‘ƒ๐‘‹=๐‘˜

๐‘˜

๐‘ƒ๐‘‹=๐‘˜

๐‘‹~Bin(๐‘›, ๐‘) ๐‘ ๐‘– =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

๐ธ ๐‘‹ = ๐‘›๐‘

Page 47: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Binomial RV is sum of Bernoulli RVs

Bernoulli

โ€ข ๐‘‹~Ber(๐‘)

Binomial

โ€ข ๐‘Œ~Bin ๐‘›, ๐‘

โ€ข The sum of ๐‘› independent Bernoulli RVs

47

๐‘Œ =

๐‘–=1

๐‘›

๐‘‹๐‘– , ๐‘‹๐‘– ~Ber(๐‘)

+

+

+

Review

Page 48: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

NBA Finals and genetics

48

Page 49: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Think, thenBreakout Rooms

Check out the questions on the next slide. Post any clarifications here!

https://us.edstem.org/courses/667/discussion/84212

By yourself: 2 min

Breakout rooms: 5 min.

49

๐Ÿค”

Page 50: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

NBA Finals and genetics

1. The Golden State Warriors are going to play the Toronto Raptors in a7-game series during the 2019 NBA finals.

โ€ข The Warriors have a probability of 58% of winning each game, independently.

โ€ข A team wins the series if they win at least 4 games (we play all 7 games).

What is P(Warriors winning)?

2. Each person has 2 genes per trait (e.g., eye color).โ€ข Child receives 1 gene (equally likely) from each parent

โ€ข Brown is โ€œdominantโ€, blue is โ€recessiveโ€:

โ€ข Child has brown eyes if either (or both) genes are brown

โ€ข Blue eyes only if both genes are blue.

โ€ข Parents each have 1 brown and 1 blue gene.

A family has 4 children. What is P(exactly 3 children with brown eyes)?

50

๐Ÿค”

Page 51: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

NBA Finals

The Golden State Warriors are going to play the TorontoRaptors in a 7-game series during the 2019 NBA finals.โ€ข The Warriors have a probability of 58% of

winning each game, independently.

โ€ข A team wins the series if they win at least 4 games(we play all 7 games).

What is P(Warriors winning)?

51

1. Define events/ RVs & state goal

๐‘‹: # games Warriors win

๐‘‹~Bin(7, 0.58)

Want:

Desired probability? (select all that apply)

A. ๐‘ƒ ๐‘‹ > 4B. ๐‘ƒ ๐‘‹ โ‰ฅ 4C. ๐‘ƒ ๐‘‹ > 3D. 1 โˆ’ ๐‘ƒ ๐‘‹ โ‰ค 3E. 1 โˆ’ ๐‘ƒ ๐‘‹ < 3

๐‘‹~Bin(๐‘›, ๐‘) ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

Page 52: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Desired probability? (select all that apply)

A. ๐‘ƒ ๐‘‹ > 4B. ๐‘ƒ ๐‘‹ โ‰ฅ 4C. ๐‘ƒ ๐‘‹ > 3D. 1 โˆ’ ๐‘ƒ ๐‘‹ โ‰ค 3E. 1 โˆ’ ๐‘ƒ ๐‘‹ < 3

NBA Finals

The Golden State Warriors are going to play the TorontoRaptors in a 7-game series during the 2019 NBA finals.โ€ข The Warriors have a probability of 58% of

winning each game, independently.

โ€ข A team wins the series if they win at least 4 games(we play all 7 games).

What is P(Warriors winning)?

52

1. Define events/ RVs & state goal

๐‘‹: # games Warriors win

๐‘‹~Bin(7, 0.58)

Want:

๐‘‹~Bin(๐‘›, ๐‘) ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

Page 53: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

NBA Finals

The Golden State Warriors are going to play the Toronto Raptors in a 7-game series during the 2019 NBA finals.โ€ข The Warriors have a probability of 58% of

winning each game, independently.

โ€ข A team wins the series if they win at least 4 games(we play all 7 games).

What is P(Warriors winning)?

53

Cool Algebra/Probability Fact: this is identical to the probability

of winning if we define winning = first to win 4 games

1. Define events/ RVs & state goal

2. Solve

๐‘‹: # games Warriors win

๐‘‹~Bin(7, 0.58)

Want: ๐‘ƒ ๐‘‹ โ‰ฅ 4

๐‘ƒ ๐‘‹ โ‰ฅ 4 =

๐‘˜=4

7

๐‘ƒ ๐‘‹ = ๐‘˜ =

๐‘˜=4

7

7๐‘˜

0.58๐‘˜ 0.42 7โˆ’๐‘˜

๐‘‹~Bin(๐‘›, ๐‘) ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

Page 54: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Genetic inheritance

Each person has 2 genes per trait (e.g., eye color).โ€ข Child receives 1 gene (equally likely) from each parent

โ€ข Brown is โ€œdominantโ€, blue is โ€recessiveโ€:

โ€ข Child has brown eyes if either (or both) genes are brown

โ€ข Blue eyes only if both genes are blue.

โ€ข Parents each have 1 brown and 1 blue gene.

A family has 4 children. What is P(exactly 3 children with brown eyes)?

54

๐‘‹~Bin(๐‘›, ๐‘) ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

A. Product of 4 independent events

B. Probability tree

C. Bernoulli, success ๐‘ = 3 children with brown eyes

D. Binomial, ๐‘› = 3 trials, success ๐‘ = brown-eyed child

E. Binomial, ๐‘› = 4 trials, success ๐‘ = brown-eyed child

Subset

of ideas:

Page 55: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Each person has 2 genes per trait (e.g., eye color).โ€ข Child receives 1 gene (equally likely) from each parent

โ€ข Brown is โ€œdominantโ€, blue is โ€recessiveโ€:

โ€ข Child has brown eyes if either (or both) genes are brown

โ€ข Blue eyes only if both genes are blue.

โ€ข Parents each have 1 brown and 1 blue gene.

A family has 4 children. What is P(exactly 3 children with brown eyes)?

55

Genetic inheritance

1. Define events/ RVs & goal

3. Solve

๐‘‹: # brown-eyed children,

๐‘‹~Bin(4, ๐‘)๐‘: ๐‘ƒ brownโˆ’eyed child

Want: ๐‘ƒ ๐‘‹ = 3

2. Identify knownprobabilities

๐‘‹~Bin(๐‘›, ๐‘) ๐‘ ๐‘˜ =๐‘›๐‘˜

๐‘๐‘˜ 1 โˆ’ ๐‘ ๐‘›โˆ’๐‘˜

Page 56: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Interlude for jokes/announcements

57

Page 57: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Announcements

58

Midterm Quiz

Time frame: Mon-Tues, July 20-21 5pm-5pm PT

Covers: Up to and including Lecture 11

Info and practice: http://web.stanford.edu/class/archive/cs/cs109/cs109.1208/exams/quizzes.ht

ml

Page 58: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Interesting probability news

59

https://theconversation.com/p

olly-knows-probability-this-

parrot-can-predict-the-chances-

of-something-happening-

132767

Page 59: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Discrete RVs

60

LIVE

The hardest part of problem-solving is

determining what is a random variable .

Page 60: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Grid of random variables

61

Number of

successes

Ber(๐‘)One trial

Several

trials

Interval

of time

Bin(๐‘›, ๐‘)

Poi(๐œ†)

Geo(๐‘)

NegBin(๐‘Ÿ, ๐‘)

(today!)

One success

Several

successes

Interval of time to

first success

Time until

success

๐‘› = 1 ๐‘Ÿ = 1

Review

Page 61: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Grid of random variables

62

Number of

successes

Ber(๐‘)One trial

Several

trials

Interval

of time

Bin(๐‘›, ๐‘)

Poi(๐œ†)

Geo(๐‘)

NegBin(๐‘Ÿ, ๐‘)

(today!)

One success

Several

successes

Interval of time to

first success

Time until

success

๐‘› = 1 ๐‘Ÿ = 1

Review

Page 62: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Breakout Rooms

Check out the question on the next slide. Post any clarifications here!

https://us.edstem.org/courses/667/discussion/84212

Breakout rooms: 5 min. Introduce yourself!

63

๐Ÿค”

Page 63: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

An RV Tour

How would you model the following?

1. # of snapchats you receive in a day

2. # of children until the first one withbrown eyes (same parents)

3. Whether stock went up or down in a day

4. # of probability problems you try until you get 5 correct (if you are randomly correct)

5. # of years in some decade with at least 6 Atlantic hurricanes

64

Choose from:

A. Ber ๐‘B. Bin ๐‘›, ๐‘

C. Poi ๐œ†D. Geo ๐‘E. NegBin ๐‘Ÿ, ๐‘

๐Ÿค”

Page 64: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

An RV Tour

How would you model the following?

1. # of snapchats you receive in a day

2. # of children until the first one withbrown eyes (same parents)

3. Whether stock went up or down in a day

4. # of probability problems you try until you get 5 correct (if you are randomly correct)

5. # of years in some decade with at least 6 Atlantic hurricanes

65

E. NegBin ๐‘Ÿ = 5, ๐‘

Choose from:

A. Ber ๐‘B. Bin ๐‘›, ๐‘

A. Ber ๐‘ or B. Bin 1, ๐‘

D. Geo ๐‘ or E. NegBin 1, ๐‘

C. Poi ๐œ†

B. Bin ๐‘› = 10, ๐‘ , where

๐‘ = ๐‘ƒ โ‰ฅ 6 hurricanes in a year

calculated from C. Poi ๐œ†

C. Poi ๐œ†D. Geo ๐‘E. NegBin ๐‘Ÿ, ๐‘

Page 65: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

CS109 Learning Goal: Use new RVs

Letโ€™s say you are learning about servers/networks.

You read about the M/D/1 queue:

โ€œThe service time busy period is distributed as a Borel with parameter๐œ‡ = 0.2.โ€

Goal: You can recognize terminology and understand experiment setup.

66

๐Ÿ˜Ž

Page 66: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Poisson RV

67

LIVE

Page 67: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Poisson Random Variable

In CS109, a Poisson RV ๐‘‹~Poi(๐œ†)most often models

โ€ข # of successes over a fixed interval of time.๐œ† = ๐ธ[๐‘‹], average success/interval

โ€ข Approximation of ๐‘Œ~Bin(๐‘›, ๐‘) where ๐‘› is large and ๐‘ is small.๐œ† = ๐ธ ๐‘Œ = ๐‘›๐‘

โ€ข Approximation of Binomial even when successin trials are not entirely independent.

68

๐‘ƒ ๐‘‹ = ๐‘˜ = ๐‘’โˆ’๐œ†๐œ†๐‘˜

๐‘˜!๐‘‹~Poi(๐œ†)

Support: {0,1, 2,โ€ฆ }

PMF

๐ธ ๐‘‹ = ๐œ†Var ๐‘‹ = ๐œ†Variance

Expectation

Review

(explored in problem set 3)

Page 68: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Breakout Rooms The next slide has two questions to go over

in groups.

Post any clarifications here!

https://us.edstem.org/courses/667/discussion/84212

Breakout rooms: 5 mins

69

๐Ÿค”

Page 69: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

Web server load

1. Consider requests to a web server in 1 second.โ€ข In the past, server load averages 2 hits/second.

โ€ข Let ๐‘‹ = # hits the server receives in a second.

What is ๐‘ƒ ๐‘‹ < 5 ?

2. Can the following BinomialRVs be approximated with Poisson?

70

๐‘‹~Poi(๐œ†)๐‘ ๐‘˜ = ๐‘’โˆ’๐œ†

๐œ†๐‘˜

๐‘˜!๐ธ ๐‘‹ = ๐œ†

๐Ÿค”

Page 70: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

1. Web server load

Consider requests to a web server in 1 second.โ€ข In the past, server load averages 2 hits/second.

โ€ข Let ๐‘‹ = # hits the server receives in a second.

What is ๐‘ƒ ๐‘‹ < 5 ?

71

๐‘‹~Poi(๐œ†)๐‘ ๐‘˜ = ๐‘’โˆ’๐œ†

๐œ†๐‘˜

๐‘˜!

1. Define RVs 2. Solve

๐ธ ๐‘‹ = ๐œ†

Page 71: 07: Random Variables II - Stanford Universityweb.stanford.edu/class/archive/cs/cs109/cs109.1208/lectures/07_rvs_ii.pdfLisa Yan, CS109, 2020 Consider an experiment: ๐‘›independent

Lisa Yan, CS109, 2020

2. Can these Binomial RVs be approximated?

72

0

0.05

0.1

0 10 20 30 40 50 60 70 80 90

๐‘ƒ(๐‘‹

= ๐‘˜

)

Bin(100,0.5)Poi(50)

0

0.1

0.2

0.3

0 10 20 30 40 50 60 70 80 90

๐‘ƒ(๐‘‹

= ๐‘˜

)

Bin(100,0.04)Poi(4)

0

0.1

0.2

0.3

0 10 20 30 40 50 60 70 80 90

๐‘ƒ(๐‘‹

= ๐‘˜

) Bin(100,0.96) Poi(4)

โœ…

โŒ

โš ๏ธCan approximate

Bin(100,1-0.96)

Poisson approximates Binomial when ๐‘› is large, ๐‘ is small, and ๐œ† = ๐‘›๐‘ is โ€œmoderate.โ€

Different interpretations of โ€œmoderateโ€:

โ€ข ๐‘› > 20 and ๐‘ < 0.05

โ€ข ๐‘› > 100 and ๐‘ < 0.1