07: Variance, Bernoulli, Binomial - Stanford University

55
07: Variance, Bernoulli, Binomial Jerry Cain April 12, 2021 1

Transcript of 07: Variance, Bernoulli, Binomial - Stanford University

Page 1: 07: Variance, Bernoulli, Binomial - Stanford University

07: Variance, Bernoulli, BinomialJerry CainApril 12, 2021

1

Page 2: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Quick slide reference

2

3 Variance 07a_variance_i

10 Properties of variance 07b_variance_ii

17 Bernoulli RV 07c_bernoulli

22 Binomial RV 07d_binomial

34 Exercises LIVE

Page 3: 07: Variance, Bernoulli, Binomial - Stanford University

Variance

3

07a_variance_i

Page 4: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Stanford, CA𝐸 high = 68°F𝐸 low = 52°F

Washington, DC𝐸 high = 67°F𝐸 low = 51°F

4

Average annual weather

Is 𝐸 𝑋 enough?

Page 5: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Stanford, CA𝐸 high = 68°F𝐸 low = 52°F

Washington, DC𝐸 high = 67°F𝐸 low = 51°F

5

Average annual weather

0

0.1

0.2

0.3

0.4

0

0.1

0.2

0.3

0.4

𝑃𝑋=π‘₯

𝑃𝑋=π‘₯

Stanford high temps Washington high temps

35 50 65 80 90 35 50 65 80 90

68Β°F 67Β°F

Normalized histograms are approximations of PMFs.

Page 6: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Variance = β€œspread”Consider the following three distributions (PMFs):

β€’ Expectation: 𝐸 𝑋 = 3 for all distributionsβ€’ But the β€œspread” in the distributions is different!β€’ Variance, Var 𝑋 : a formal quantification of β€œspread”

6

Page 7: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Variance

The variance of a random variable 𝑋 with mean 𝐸 𝑋 = πœ‡ is

Var 𝑋 = 𝐸 𝑋 βˆ’ πœ‡ (

β€’ Also written as: 𝐸 𝑋 βˆ’ 𝐸 𝑋 !

β€’ Note: Var(X) β‰₯ 0β€’ Other names: 2nd central moment, or square of the standard deviation

Var 𝑋

def standard deviation SD 𝑋 = Var 𝑋7

Units of 𝑋!

Units of 𝑋

Page 8: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Variance of Stanford weather

8

Varianceof 𝑋

Var 𝑋 = 𝐸 𝑋 βˆ’ 𝐸 𝑋 !

Stanford, CA𝐸 high = 68°F𝐸 low = 52°F

0

0.1

0.2

0.3

0.4

𝑃𝑋=π‘₯

Stanford high temps

35 50 65 80 90

𝑋 𝑋 βˆ’ πœ‡ !

57Β°F 121 (Β°F)2

𝐸 𝑋 = πœ‡ = 68Β°F

71Β°F 9 (Β°F)2

75Β°F 49 (Β°F)2

69Β°F 1 (Β°F)2

… …

Variance 𝐸 𝑋 βˆ’ πœ‡ ! = 39 (Β°F)2

Standard deviation = 6.2Β°F

Page 9: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Stanford, CA𝐸 high = 68°F

Washington, DC𝐸 high = 67°F

9

Comparing variance

0

0.1

0.2

0.3

0.4

0

0.1

0.2

0.3

0.4

𝑃𝑋=π‘₯

𝑃𝑋=π‘₯

Stanford high temps Washington high temps

35 50 65 80 90 35 50 65 80 90

Var 𝑋 = 39 Β°F ! Var 𝑋 = 248 Β°F !

Varianceof 𝑋

Var 𝑋 = 𝐸 𝑋 βˆ’ 𝐸 𝑋 !

68Β°F 67Β°F

Page 10: 07: Variance, Bernoulli, Binomial - Stanford University

Properties of Variance

10

07b_variance_ii

Page 11: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Properties of varianceDefinition Var 𝑋 = 𝐸 𝑋 βˆ’ 𝐸 𝑋 !

def standard deviation SD 𝑋 = Var 𝑋

Property 1 Var 𝑋 = 𝐸 𝑋! βˆ’ 𝐸 𝑋 !

Property 2 Var π‘Žπ‘‹ + 𝑏 = π‘Ž!Var 𝑋

11

Units of 𝑋!

Units of 𝑋

β€’ Property 1 is often easier to compute than the definitionβ€’ Unlike expectation, variance is not linear

Page 12: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Properties of varianceDefinition Var 𝑋 = 𝐸 𝑋 βˆ’ 𝐸 𝑋 !

def standard deviation SD 𝑋 = Var 𝑋

Property 1 Var 𝑋 = 𝐸 𝑋! βˆ’ 𝐸 𝑋 !

Property 2 Var π‘Žπ‘‹ + 𝑏 = π‘Ž!Var 𝑋

12

Units of 𝑋!

Units of 𝑋

β€’ Property 1 is often easier to compute than the definitionβ€’ Unlike expectation, variance is not linear

Page 13: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Computing variance, a proof

13

Var 𝑋 = 𝐸 𝑋 βˆ’ 𝐸 𝑋 !

= 𝐸 𝑋! βˆ’ 𝐸 𝑋 !

= 𝐸 𝑋! βˆ’ πœ‡!= 𝐸 𝑋! βˆ’ 2πœ‡! + πœ‡!= 𝐸 𝑋! βˆ’ 2πœ‡πΈ 𝑋 + πœ‡!

= 𝐸 𝑋 βˆ’ πœ‡ ! Let 𝐸 𝑋 = πœ‡

Everyone, please

welcome the second

moment!

Varianceof 𝑋

Var 𝑋 = 𝐸 𝑋 βˆ’ 𝐸 𝑋 !

= 𝐸 𝑋! βˆ’ 𝐸 𝑋 !

=,"

π‘₯ βˆ’ πœ‡ !𝑝 π‘₯

=,"

π‘₯! βˆ’ 2πœ‡π‘₯ + πœ‡! 𝑝 π‘₯

=,"

π‘₯!𝑝 π‘₯ βˆ’ 2πœ‡,"

π‘₯𝑝 π‘₯ + πœ‡!,"

𝑝 π‘₯

β‹… 1

Page 14: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Let Y = outcome of a single die roll. Recall 𝐸 π‘Œ = 7/2 .Calculate the variance of Y.

14

Variance of a 6-sided dieVarianceof 𝑋

Var 𝑋 = 𝐸 𝑋 βˆ’ 𝐸 𝑋 !

= 𝐸 𝑋! βˆ’ 𝐸 𝑋 !

1. Approach #1: Definition

Var π‘Œ =161 βˆ’

72

!+162 βˆ’

72

!

+163 βˆ’

72

!+164 βˆ’

72

!

+165 βˆ’

72

!+166 βˆ’

72

!

2. Approach #2: A property

𝐸 π‘Œ! =161! + 2! + 3! + 4! + 5! + 6!

= 91/6

Var π‘Œ = 91/6 βˆ’ 7/2 !

= 35/12 = 35/12

2nd moment

Page 15: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Properties of varianceDefinition Var 𝑋 = 𝐸 𝑋 βˆ’ 𝐸 𝑋 !

def standard deviation SD 𝑋 = Var 𝑋

Property 1 Var 𝑋 = 𝐸 𝑋! βˆ’ 𝐸 𝑋 !

Property 2 Var π‘Žπ‘‹ + 𝑏 = π‘Ž!Var 𝑋

15

Units of 𝑋!

Units of 𝑋

β€’ Property 1 is often easier to compute than the definitionβ€’ Unlike expectation, variance is not linear

Page 16: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Property 2: A proofProperty 2 Var π‘Žπ‘‹ + 𝑏 = π‘Ž!Var 𝑋

16

Var π‘Žπ‘‹ + 𝑏= 𝐸 π‘Žπ‘‹ + 𝑏 ! βˆ’ 𝐸 π‘Žπ‘‹ + 𝑏 ! Property 1

= 𝐸 π‘Ž!𝑋! + 2π‘Žπ‘π‘‹ + 𝑏! βˆ’ π‘ŽπΈ 𝑋 + 𝑏 !

= π‘Ž!𝐸 𝑋! + 2π‘Žπ‘πΈ 𝑋 + 𝑏! βˆ’ π‘Ž! 𝐸[𝑋] ! + 2π‘Žπ‘πΈ[𝑋] + 𝑏!

= π‘Ž!𝐸 𝑋! βˆ’ π‘Ž! 𝐸[𝑋] !

= π‘Ž! 𝐸 𝑋! βˆ’ 𝐸[𝑋] !

= π‘Ž!Var 𝑋 Property 1

Proof:

Factoring/Linearity of Expectation

Page 17: 07: Variance, Bernoulli, Binomial - Stanford University

Bernoulli RV

17

07c_bernoulli

Page 18: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Consider an experiment with two outcomes: β€œsuccess” and β€œfailure.”def A Bernoulli random variable 𝑋 maps β€œsuccess” to 1 and β€œfailure” to 0.

Other names: indicator random variable, boolean random variable

Examples:β€’ Coin flipβ€’ Random binary digitβ€’ Whether a disk drive crashed

Bernoulli Random Variable

18

𝑃 𝑋 = 1 = 𝑝 1 = 𝑝𝑃 𝑋 = 0 = 𝑝 0 = 1 βˆ’ 𝑝𝑋~Ber(𝑝)

Support: {0,1} VarianceExpectation

PMF

𝐸 𝑋 = 𝑝Var 𝑋 = 𝑝(1 βˆ’ 𝑝)

Remember this nice property of expectation. It will come back!

Page 19: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Jacob Bernoulli

19

Jacob Bernoulli (1654-1705), also known as β€œJames”, was a Swiss mathematician

One of many mathematicians in Bernoulli familyThe Bernoulli Random Variable is named for him

My academic great14 grandfather

Page 20: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Defining Bernoulli RVs

20

Serve an ad.β€’ User clicks w.p. 0.2β€’ Ignores otherwise

Let 𝑋: 1 if clicked

𝑋~Ber(___)𝑃 𝑋 = 1 = ___𝑃 𝑋 = 0 = ___

Roll two dice.β€’ Success: roll two 6’sβ€’ Failure: anything else

Let 𝑋 : 1 if success

𝑋~Ber(___)

𝐸 𝑋 = ___

𝑋~Ber(𝑝) 𝑝" 1 = 𝑝𝑝" 0 = 1 βˆ’ 𝑝𝐸 𝑋 = 𝑝

Run a programβ€’ Crashes w.p. 𝑝‒ Works w.p. 1 βˆ’ 𝑝

Let 𝑋: 1 if crash

𝑋~Ber(𝑝)𝑃 𝑋 = 1 = 𝑝𝑃 𝑋 = 0 = 1 βˆ’ 𝑝 πŸ€”

Page 21: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Defining Bernoulli RVs

21

Serve an ad.β€’ User clicks w.p. 0.2β€’ Ignores otherwise

Let 𝑋: 1 if clicked

𝑋~Ber(___)𝑃 𝑋 = 1 = ___𝑃 𝑋 = 0 = ___

Roll two dice.β€’ Success: roll two 6’sβ€’ Failure: anything else

Let 𝑋 : 1 if success

𝑋~Ber(___)

𝐸 𝑋 = ___

𝑋~Ber(𝑝) 𝑝" 1 = 𝑝𝑝" 0 = 1 βˆ’ 𝑝𝐸 𝑋 = 𝑝

Run a programβ€’ Crashes w.p. 𝑝‒ Works w.p. 1 βˆ’ 𝑝

Let 𝑋: 1 if crash

𝑋~Ber(𝑝)𝑃 𝑋 = 1 = 𝑝𝑃 𝑋 = 0 = 1 βˆ’ 𝑝

Page 22: 07: Variance, Bernoulli, Binomial - Stanford University

Binomial RV

22

07d_binomial

Page 23: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

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

23

π‘˜ = 0, 1, … , 𝑛:

𝑃 𝑋 = π‘˜ = 𝑝 π‘˜ = π‘›π‘˜ 𝑝! 1 βˆ’ 𝑝 "#!𝑋~Bin(𝑛, 𝑝)

Support: {0,1, … , 𝑛}

PMF

𝐸 𝑋 = 𝑛𝑝Var 𝑋 = 𝑛𝑝(1 βˆ’ 𝑝)Variance

Expectation

Page 24: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021 24

Page 25: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Reiterating notation

The parameters of a Binomial random variable:β€’ 𝑛: number of independent trialsβ€’ 𝑝: probability of success on each trial

25

1. The random variable

2. is distributed as a

3. Binomial 4. with parameters

𝑋 ~ Bin(𝑛, 𝑝)

Page 26: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Reiterating notation

If 𝑋 is a binomial with parameters 𝑛 and 𝑝, the PMF of 𝑋 is

26

𝑋 ~ Bin(𝑛, 𝑝)

𝑃 𝑋 = π‘˜ = π‘›π‘˜ 𝑝( 1 βˆ’ 𝑝 )*(

Probability Mass Function for a BinomialProbability that 𝑋takes on the value π‘˜

Page 27: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Three coin flipsThree fair (β€œheads” with 𝑝 = 0.5) coins are flipped.β€’ 𝑋 is number of headsβ€’ 𝑋~Bin 3, 0.5

Compute the following event probabilities:

27

𝑋~Bin(𝑛, 𝑝) 𝑝 π‘˜ = π‘›π‘˜ 𝑝# 1 βˆ’ 𝑝 $%#

𝑃 𝑋 = 0

𝑃 𝑋 = 1

𝑃 𝑋 = 2

𝑃 𝑋 = 3

𝑃 𝑋 = 7P(event)

πŸ€”

Page 28: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Three coin flipsThree fair (β€œheads” with 𝑝 = 0.5) coins are flipped.β€’ 𝑋 is number of headsβ€’ 𝑋~Bin 3, 0.5

Compute the following event probabilities:

28

𝑋~Bin(𝑛, 𝑝) 𝑝 π‘˜ = π‘›π‘˜ 𝑝# 1 βˆ’ 𝑝 $%#

𝑃 𝑋 = 0 = 𝑝 0 = 30 𝑝$ 1 βˆ’ 𝑝 % = &

'

𝑃 𝑋 = 1

𝑃 𝑋 = 2

𝑃 𝑋 = 3

𝑃 𝑋 = 7

= 𝑝 1 = 31 𝑝& 1 βˆ’ 𝑝 ! = %

'

= 𝑝 2 = 32 𝑝! 1 βˆ’ 𝑝 & = %

'

= 𝑝 3 = 33 𝑝% 1 βˆ’ 𝑝 $ = &

'

= 𝑝 7 = 0P(event) PMF

Extra math note:By Binomial Theorem,we can proveβˆ‘()$* 𝑃 𝑋 = π‘˜ = 1

Page 29: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

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

29

𝑋~Bin(𝑛, 𝑝)Range: {0,1, … , 𝑛} Variance

Expectation

PMF

𝐸 𝑋 = 𝑛𝑝Var 𝑋 = 𝑛𝑝(1 βˆ’ 𝑝)

π‘˜ = 0, 1, … , 𝑛:

𝑃 𝑋 = π‘˜ = 𝑝 π‘˜ = π‘›π‘˜ 𝑝! 1 βˆ’ 𝑝 "#!

Page 30: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Ber 𝑝 = Bin(1, 𝑝)

Binomial RV is sum of Bernoulli RVs

Bernoulliβ€’ 𝑋~Ber(𝑝)

Binomialβ€’ π‘Œ~Bin 𝑛, 𝑝‒ The sum of 𝑛 independent

Bernoulli RVs

30

π‘Œ =,+)&

*

𝑋+ , 𝑋+ ~Ber(𝑝)

+

+

+

Page 31: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

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

31

𝑋~Bin(𝑛, 𝑝)Range: {0,1, … , 𝑛}

𝐸 𝑋 = 𝑛𝑝Var 𝑋 = 𝑛𝑝(1 βˆ’ 𝑝)Variance

Expectation

PMF π‘˜ = 0, 1, … , 𝑛:

𝑃 𝑋 = π‘˜ = 𝑝 π‘˜ = π‘›π‘˜ 𝑝! 1 βˆ’ 𝑝 "#!

Proof:

Page 32: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

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

32

𝑋~Bin(𝑛, 𝑝)Range: {0,1, … , 𝑛}

PMF

𝐸 𝑋 = 𝑛𝑝Var 𝑋 = 𝑛𝑝(1 βˆ’ 𝑝)

We’ll prove this later in the course

VarianceExpectation

π‘˜ = 0, 1, … , 𝑛:

𝑃 𝑋 = π‘˜ = 𝑝 π‘˜ = π‘›π‘˜ 𝑝! 1 βˆ’ 𝑝 "#!

Page 33: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

No, give me the variance proof right now

33

proofwiki.org

Page 34: 07: Variance, Bernoulli, Binomial - Stanford University

(live)07: Variance, Bernoulli, and BinomialJerry CainApril 12, 2021

34

Page 35: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Our first common RVs

35

𝑋 ~ Ber(𝑝)

π‘Œ ~ Bin(𝑛, 𝑝)

1. The random variable

2. is distributed as/varies as a

3. 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, oridentify as a function of an existing random variable

Review

Page 36: 07: Variance, Bernoulli, Binomial - Stanford University

Breakout Rooms

Check out the questions on the next slide (Slide 37). Post any clarifications here!https://edstem.org/us/courses/5090/discussion/357155

Breakout rooms: 4 min. Don’t worry if it’s not enough time. Just do your best and we’ll cover these when we return.

36

Page 37: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Statistics: Expectation and variance1. a. Let 𝑋 = the outcome of a fair 4-sided

die roll. What is 𝐸 𝑋 ?b. Let π‘Œ = the sum of three rolls of a fair

4-sided die. What is 𝐸 π‘Œ ?

2. Let 𝑍 = # of tails on 10 flips of abiased coin (w.p. 0.4 of heads). What is 𝐸 𝑍 ?

3. Compare the variances of𝐡2~Ber 0.1 and 𝐡!~Ber(0.5).

37

Page 38: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Statistics: Expectation and variance1. a. Let 𝑋 = the outcome of a fair 4-sided

die roll. What is 𝐸 𝑋 ?b. Let π‘Œ = the sum of three rolls of a fair

4-sided die. What is 𝐸 π‘Œ ?

2. Let 𝑍 = # of tails on 10 flips of abiased coin (w.p. 0.4 of heads). What is 𝐸 𝑍 ?

3. Compare the variances of𝐡2~Ber 0.1 and 𝐡!~Ber(0.5).

38

If you can identify common RVs, just look up statistics instead of re-deriving from definitions.

Page 39: 07: Variance, Bernoulli, Binomial - Stanford University

Think

Slide 40 has a matching question to go over by yourself. We’ll go over it together afterwards.

Post any clarifications here!https://edstem.org/us/courses/5090/discussion/357155

Think by yourself: 1 min Don’t worry if you can’t figure everything out in just one minute. This material is difficult.

39

(by yourself)

Page 40: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Visualizing Binomial PMFs

40

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 100

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

𝑋~Bin(𝑛, 𝑝) 𝑝 𝑖 = π‘›π‘˜ 𝑝# 1 βˆ’ 𝑝 $%#

𝐸 𝑋 = 𝑛𝑝

C. D.Match the distribution of 𝑋 to the graph:1. Bin 10,0.52. Bin 10,0.33. Bin 10,0.74. Bin 5,0.5

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 100

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

A. B.

π‘˜

𝑃𝑋=π‘˜

π‘˜

𝑃𝑋=π‘˜

π‘˜

𝑃𝑋=π‘˜

π‘˜

𝑃𝑋=π‘˜

(by yourself)

Page 41: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Visualizing Binomial PMFs

41

Match the distribution of 𝑋 to the graph:1. Bin 10,0.52. Bin 10,0.33. Bin 10,0.74. Bin 5,0.5

0

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 100

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 100

0.1

0.2

0.3

0 1 2 3 4 5 6 7 8 9 10

A. B.

π‘˜

𝑃𝑋=π‘˜

π‘˜

𝑃𝑋=π‘˜

π‘˜

𝑃𝑋=π‘˜

π‘˜

𝑃𝑋=π‘˜

𝑋~Bin(𝑛, 𝑝) 𝑝 𝑖 = π‘›π‘˜ 𝑝# 1 βˆ’ 𝑝 $%#

𝐸 𝑋 = 𝑛𝑝

Page 42: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Binomial RV is sum of Bernoulli RVs

Bernoulliβ€’ 𝑋~Ber(𝑝)

Binomialβ€’ π‘Œ~Bin 𝑛, 𝑝‒ The sum of 𝑛 independent

Bernoulli RVs

42

π‘Œ =,+)&

*

𝑋+ , 𝑋+ ~Ber(𝑝)

+

+

+

Review

Page 43: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Galton Board

43

0 1 2 3 4 5

http://web.stanford.edu/class/cs109/demos/galton.html

Page 44: 07: Variance, Bernoulli, Binomial - Stanford University

ThinkSlide 45 has a question to go over by yourself.

Post any clarifications here!https://edstem.org/us/courses/5090/discussion/357155

Think by yourself and just do your best: 1 min

44

(by yourself)

Page 45: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Galton Board

45

When a marble hits a pin, it has an equal chance of going left or right.Let 𝐡 = the bucket index a ball drops into.What is the distribution of 𝐡?

0 1 2 3 4 5

𝑛 = 5

𝑋~Bin(𝑛, 𝑝) 𝑝 π‘˜ = π‘›π‘˜ 𝑝# 1 βˆ’ 𝑝 $%#

(by yourself)

(Interpret: If 𝐡 is a common random variable, report it,

otherwise report PMF)

Page 46: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Galton Board

46

When a marble hits a pin, it has an equal chance of going left or right.Let 𝐡 = the bucket index a ball drops into.What is the distribution of 𝐡?

0 1 2 3 4 5

𝑛 = 5

𝑋~Bin(𝑛, 𝑝) 𝑝 π‘˜ = π‘›π‘˜ 𝑝# 1 βˆ’ 𝑝 $%#

β€’ Each pin is an independent trialβ€’ One decision made for level 𝑖 = 1, 2, . . , 5β€’ Consider a Bernoulli RV with success 𝑅+ if

ball went right on level 𝑖

β€’ Bucket index 𝐡 = # times ball went right

𝐡~Bin(𝑛 = 5, 𝑝 = 0.5)

Page 47: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Galton Board

47

When a marble hits a pin, it has an equal chance of going left or right.Let 𝐡 = the bucket index a ball drops into.𝐡 is distributed as a Binomial RV,

𝐡~Bin(𝑛 = 5, 𝑝 = 0.5)

0 1 2 3 4 5

𝑛 = 5 Calculate the probability of a ball landing in bucket π‘˜.

𝑃 𝐡 = 0 = 50 0.51 β‰ˆ 0.03

𝑃 𝐡 = 1 = 51 0.51 β‰ˆ 0.16

𝑃 𝐡 = 2 = 52 0.51 β‰ˆ 0.31

𝑋~Bin(𝑛, 𝑝) 𝑝 π‘˜ = π‘›π‘˜ 𝑝# 1 βˆ’ 𝑝 $%#

PMF of Binomial RV!

Page 48: 07: Variance, Bernoulli, Binomial - Stanford University

Interlude for announcements

48

Python review session next

Python Review Session 2

When: Today 2:00-3:30pm PTRecorded? obvi, right hereNotes: to be posted online

Page 49: 07: Variance, Bernoulli, Binomial - Stanford University

Think, thenBreakout Rooms

Check out the questions on the next slide (Slide 50). Post any clarifications here!https://edstem.org/us/courses/5090/discussion/357155

By yourself: 1 min

Breakout rooms: 5 min.

49

Page 50: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Genetics and NBA Finals1. Each parent has 2 genes per trait (e.g., eye color).β€’ Child inherits 1 gene from each parent with equal likelihood.β€’ Brown eyes are β€œdominant”, blue eyes are ”recessive”:β€’ Child has brown eyes if either or both genes for brown eyes are inherited.β€’ Child has blue eyes otherwise (i.e., child inherits two genes for blue eyes)

β€’ Assume parents each have 1 gene for blue eyes and 1 gene for brown eyes.Two parents have 4 children. What is P(exactly 3 children have brown eyes)?

2. The LA Lakers are going to play the Miami Heat in a7-game series during the 2021 NBA finals.

β€’ The Lakers 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(Lakers winning)?

50

𝑋~Bin(𝑛, 𝑝) 𝑝 π‘˜ = π‘›π‘˜ 𝑝# 1 βˆ’ 𝑝 $%#

Page 51: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

Genetic inheritance1. Each parent has 2 genes per trait (e.g., eye color).β€’ Child inherits 1 gene from each parent with equal likelihood.β€’ Brown eyes are β€œdominant”, blue eyes are ”recessive”:β€’ Child has brown eyes if either or both genes for brown eyes are inherited.β€’ Child has blue eyes otherwise (i.e., child inherits two genes for blue eyes)

β€’ Assume parents each have 1 gene for blue eyes and 1 gene for brown eyes.Two parents have 4 children. What is P(exactly 3 children have brown eyes)?

51

Big Q: Fixed parameter or random variable?Parameters What is common among all outcomes

of our experiment?

Random variable What differentiates our event from the rest of the sample space?

Page 52: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

1. Each parent has 2 genes per trait (e.g., eye color).β€’ Child inherits 1 gene from each parent with equal likelihood.β€’ Brown eyes are β€œdominant”, blue eyes are ”recessive”:β€’ Child has brown eyes if either or both genes for brown eyes are inherited.β€’ Child has blue eyes otherwise (i.e., child inherits two genes for blue eyes)

β€’ Assume parents each have 1 gene for blue eyes and 1 gene for brown eyes.Two parents have 4 children. What is P(exactly 3 children have brown eyes)?

52

Genetic inheritance

1. Define events/ RVs & state goal

3. Solve

𝑋: # brown-eyed children,𝑋~Bin(4, 𝑝)

𝑝: 𝑃 brownβˆ’eyed child

Want: 𝑃 𝑋 = 3

2. Identify knownprobabilities

Page 53: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

NBA FinalsThe LA Lakers are going to play the Miami Heatin a 7-game series during the 2021 NBA finals.β€’ The Lakers 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(Lakers winning)?

53

1. Define events/ RVs & state goal

𝑋: # games Lakers win𝑋~Bin(7, 0.58)

Want:

Big Q: Fixed parameter or random variable?Parameters

Random variable

Event based on RV

# of total gamesprob. Lakers winning a game

# of games Lakers win

Page 54: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

NBA FinalsThe LA Lakers are going to play the Miami Heatin a 7-game series during the 2021 NBA finals.β€’ The Lakers 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(Lakers winning)?

54

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 Lakers win𝑋~Bin(7, 0.58)

Want: 𝑃 𝑋 β‰₯ 4

𝑃 𝑋 β‰₯ 4 = ,()2

3

𝑃 𝑋 = π‘˜ = ,()2

37π‘˜ 0.58( 0.42 34(

Page 55: 07: Variance, Bernoulli, Binomial - Stanford University

Lisa Yan, Chris Piech, Mehran Sahami, and Jerry Cain, CS109, Spring 2021

See you on Wednesday

55