73
06: Random Variables Lisa Yan April 17, 2020 1

06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

06: Random VariablesLisa Yan

April 17, 2020

1

Page 2: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Random Variables

2

06b_random_variables

Page 3: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Conditional independence review

3

Next Episode Playing in 5 seconds

Page 4: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Random variables are like typed

variables (with uncertainty)

int a = 5;

double b = 4.2;

bit c = 1;

š“ is the number of Pokemon we bring to our future battle.

š“ āˆˆ 1, 2, ā€¦ , 6

šµ is the amount of money we get after we win a battle.

šµ āˆˆ ā„+

š¶ is 1 if we successfully beat the Elite Four. 0 otherwise.

š¶ āˆˆ {0,1}

4

Random variables are like typed variables

name

Random

variablesCS variables

Page 5: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Random Variable

A random variable is a real-valued function defined on a sample space.

5

Experiment š‘‹ = š‘˜

2. What is the event (set of outcomes) where š‘‹ = 2?

Outcome

Example:

3 coins are flipped.

Let š‘‹ = # of heads.

š‘‹ is a random variable.

1. What is the value of š‘‹ for the outcomes:

ā€¢ (T,T,T)?

ā€¢ (H,H,T)?

3. What is š‘ƒ š‘‹ = 2 ? šŸ¤”

Page 6: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Random Variable

A random variable is a real-valued function defined on a sample space.

6

Experiment š‘‹ = š‘˜

2. What is the event (set of outcomes) where š‘‹ = 2?

Outcome

1. What is the value of š‘‹ for the outcomes:

ā€¢ (T,T,T)?

ā€¢ (H,H,T)?

3. What is š‘ƒ š‘‹ = 2 ?

Example:

3 coins are flipped.

Let š‘‹ = # of heads.

š‘‹ is a random variable.

Page 7: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Random variables are NOT events!

It is confusing that random variables and events use the same notation.

ā€¢ Random variables ā‰  events.

ā€¢ We can define an event to be a particular assignmentof a random variable.

7

event

š‘‹ = 2probability

(number b/t 0 and 1)

š‘ƒ(š‘‹ = 2)

Example:

3 coins are flipped.

Let š‘‹ = # of heads.

š‘‹ is a random variable.

Page 8: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Random variables are NOT events!

It is confusing that random variables and events use the same notation.

ā€¢ Random variables ā‰  events.

ā€¢ We can define an event to be a particular assignmentof a random variable.

8

Example:

3 coins are flipped.

Let š‘‹ = # of heads.

š‘‹ is a random variable.

š‘‹ = šŸ {(H, T, T), (T, H, T),

(T, T, H)}

3/8

š‘‹ = šŸ {(H, H, T), (H, T, H),

(T, H, H)}

3/8

š‘‹ = šŸ‘ {(H, H, H)} 1/8

š‘‹ ā‰„ 4 { } 0

š‘‹ = š‘„ Set of outcomes š‘ƒ š‘‹ = š‘˜

š‘‹ = šŸŽ {(T, T, T)} 1/8

Page 9: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

PMF/CDF

9

06c_pmf_cdf

Page 10: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

So far

3 coins are flipped. Let š‘‹ = # of heads. š‘‹ is a random variable.

10

Experiment š‘‹ = ___Outcome

(flip __ heads)š‘ƒ š‘‹ = ___

Can we get a ā€œshorthandā€ for

this last step?

Seems like it might be useful!

Page 11: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Probability Mass Function

3 coins are flipped. Let š‘‹ = # of heads. š‘‹ is a random variable.

11

š‘ƒ(š‘‹ = š‘˜)return value/output

number between

0 and 1

parameter/input š‘˜

A function on š‘˜with range [0,1]

What would be a useful function to define?The probability of the event that a random variable š‘‹ takes on the value š‘˜!

For discrete random variables, this is a probability mass function.

Page 12: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Probability Mass Function

3 coins are flipped. Let š‘‹ = # of heads. š‘‹ is a random variable.

12

š‘ƒ(š‘‹ = š‘˜)

2

0.375return value/output:

probability of the event

š‘‹ = š‘˜

parameter/input š‘˜:

a value of š‘‹

N = 3P = 0.5

def prob_event_y_equals(k):n_ways = scipy.special.binom(N, k)p_way = np.power(P, k) * np.power(1 - P, N-k)return n_ways * p_way

A function on š‘˜with range [0,1]

probability mass function

Page 13: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

A random variable š‘‹ is discrete if it can take on countably many values.

ā€¢ š‘‹ = š‘„, where š‘„ āˆˆ š‘„1, š‘„2, š‘„3, ā€¦

The probability mass function (PMF) of a discrete random variable is

š‘ƒ š‘‹ = š‘„

ā€¢ Probabilities must sum to 1:

This last point is a

good way to verify

any PMF you create.

Discrete RVs and Probability Mass Functions

13

š‘–=1

āˆž

š‘ š‘„š‘– = 1

shorthand notation

= š‘ š‘„ = š‘š‘‹(š‘„)

Page 14: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

PMF for a single 6-sided die

Let š‘‹ be a random variable that represents the result of a singledice roll.

ā€¢ Support of š‘‹ : 1, 2, 3, 4, 5, 6

ā€¢ Therefore š‘‹ is a discreterandom variable.

ā€¢ PMF of X:

š‘ š‘„ = į‰Š1/6 š‘„ āˆˆ {1, ā€¦ , 6}0 otherwise

14

1/6

1 2 3 4 5 60

š‘‹ = š‘„

š‘ƒš‘‹=š‘„

Page 15: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Cumulative Distribution Functions

For a random variable š‘‹, the cumulative distribution function (CDF) is defined as

š¹ š‘Ž = š¹š‘‹ š‘Ž = š‘ƒ š‘‹ ā‰¤ š‘Ž ,where āˆ’āˆž < š‘Ž < āˆž

For a discrete RV š‘‹, the CDF is:

š¹ š‘Ž = š‘ƒ š‘‹ ā‰¤ š‘Ž =

all š‘„ā‰¤š‘Ž

š‘(š‘„)

15

Page 16: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Let š‘‹ be a random variable that represents the result of a single dice roll.

16

1/6

1 2 3 4 5 60

š‘‹ = š‘„

š‘ƒš‘‹=š‘„

CDFs as graphsCDF (cumulative

distribution function)š¹ š‘Ž = š‘ƒ š‘‹ ā‰¤ š‘Ž

š¹š‘Ž

š‘‹ = š‘„

5/6

4/6

3/6

2/6

1/6

PMF of š‘‹

CDF of š‘‹

š‘ƒ š‘‹ ā‰¤ 0 = 0

š‘ƒ š‘‹ ā‰¤ 6 = 1

Page 17: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Expectation

17

06d_expectation

Page 18: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Discrete random variables

18

Discrete

Random

Variable, š‘‹

Experiment

outcomes

PMF

š‘ƒ š‘‹ = š‘„ = š‘(š‘„)

Definition

Properties

Support

CDF š¹ š‘„

Without performing the experiment:

ā€¢ The support gives us a ballpark of what values our RV will take on

ā€¢ Next up: How do we report the ā€œaverageā€ value?

Page 19: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Expectation

The expectation of a discrete random variable š‘‹ is defined as:

šø š‘‹ =

š‘„:š‘ š‘„ >0

š‘ š‘„ ā‹… š‘„

ā€¢ Note: sum over all values of š‘‹ = š‘„ that have non-zero probability.

ā€¢ Other names: mean, expected value, weighted average,center of mass, first moment

19

Page 20: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Expectation of a die roll

What is the expected value of a 6-sided die roll?

20

Expectation

of š‘‹šø š‘‹ =

š‘„:š‘ š‘„ >0

š‘ š‘„ ā‹… š‘„

1. Define random variables

2. Solve

š‘ƒ š‘‹ = š‘„ = į‰Š1/6 š‘„ āˆˆ {1, ā€¦ , 6}0 otherwise

š‘‹ = RV for value of roll

šø š‘‹ = 11

6+ 2

1

6+ 3

1

6+ 4

1

6+ 5

1

6+ 6

1

6=7

2

Page 21: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Important properties of expectation

1. Linearity:

šø š‘Žš‘‹ + š‘ = š‘Žšø š‘‹ + š‘

2. Expectation of a sum = sum of expectation:

šø š‘‹ + š‘Œ = šø š‘‹ + šø š‘Œ

3. Unconscious statistician:

šø š‘” š‘‹ =

š‘„

š‘” š‘„ š‘(š‘„)

21

ā€¢ Let š‘‹ = 6-sided dice roll,

š‘Œ = 2š‘‹ āˆ’ 1.

ā€¢ šø š‘‹ = 3.5ā€¢ šø š‘Œ = 6

Sum of two dice rolls:

ā€¢ Let š‘‹ = roll of die 1

š‘Œ = roll of die 2

ā€¢ šø š‘‹ + š‘Œ = 3.5 + 3.5 = 7

These properties let you avoid

defining difficult PMFs.

Page 22: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Proofs (OK to stop here)

22

Page 23: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Important properties of expectation

1. Linearity:

šø š‘Žš‘‹ + š‘ = š‘Žšø š‘‹ + š‘

2. Expectation of a sum = sum of expectation:

šø š‘‹ + š‘Œ = šø š‘‹ + šø š‘Œ

3. Unconscious statistician:

šø š‘” š‘‹ =

š‘„

š‘” š‘„ š‘(š‘„)

23

Review

Page 24: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Linearity of Expectation proof

šø š‘Žš‘‹ + š‘ = š‘Žšø š‘‹ + š‘

Proof:

šø š‘Žš‘‹ + š‘ =

š‘„

š‘Žš‘„ + š‘ š‘ š‘„ =

š‘„

š‘Žš‘„š‘ š‘„ + š‘š‘ š‘„

= š‘Ž

š‘„

š‘„š‘(š‘„) + š‘

š‘„

š‘ š‘„

= š‘Ž šø š‘‹ + š‘ ā‹… 1

24

šø š‘‹ =

š‘„:š‘ š‘„ >0

š‘ š‘„ ā‹… š‘„

Page 25: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Expectation of Sum intuition

šø š‘‹ + š‘Œ = šø š‘‹ + šø š‘Œ

25

(weā€™ll prove this

in two weeks)

š‘‹ š‘Œ š‘‹ + š‘Œ

3 6 9

2 4 6

6 12 18

10 20 30

-1 -2 -3

0 0 0

8 16 24

1

7(28)

Average:1

š‘›

š‘–=1

š‘›

š‘„š‘–1

š‘›

š‘–=1

š‘›

š‘¦š‘–1

š‘›

š‘–=1

š‘›

š‘„š‘– + š‘¦š‘–

Intuition

for now:

+ =

+ =1

7(56)

1

7(84)

šø š‘‹ =

š‘„:š‘ š‘„ >0

š‘ š‘„ ā‹… š‘„

Page 26: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

LOTUS proof

26

Let š‘Œ = š‘”(š‘‹), where š‘” is a real-valued function.

šø š‘” š‘‹ = šø š‘Œ =

š‘—

š‘¦š‘—š‘(š‘¦š‘—)

=

š‘—

š‘¦š‘—

š‘–:š‘” š‘„š‘– = š‘¦š‘—

š‘(š‘„š‘–)

=

š‘—

š‘–:š‘” š‘„š‘– = š‘¦š‘—

š‘¦š‘— š‘(š‘„š‘–)

=

š‘—

š‘–:š‘” š‘„š‘– = š‘¦š‘—

š‘”(š‘„š‘–) š‘(š‘„š‘–)

=

š‘–

š‘”(š‘„š‘–) š‘(š‘„š‘–)

Expectation

of š‘” š‘‹šø š‘” š‘‹ =

š‘„

š‘” š‘„ š‘(š‘„)

For you to review

so that you can

sleep at night

Page 27: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Variance

27

07a_variance_i

Page 28: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Stanford, CA

šø high = 68Ā°F

šø low = 52Ā°F

Washington, DC

šø high = 67Ā°F

šø low = 51Ā°F

28

Average annual weather

Is šø š‘‹ enough?

Page 29: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Stanford, CA

šø high = 68Ā°F

šø low = 52Ā°F

Washington, DC

šø high = 67Ā°F

šø low = 51Ā°F

29

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 30: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

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ā€

30

Page 31: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Variance

The variance of a random variable š‘‹ with mean šø š‘‹ = šœ‡ is

Var š‘‹ = šø š‘‹ āˆ’ šœ‡ 2

ā€¢ Also written as: šø š‘‹ āˆ’ šø š‘‹ 2

ā€¢ Note: Var(X) ā‰„ 0

ā€¢ Other names: 2nd central moment, or square of the standard deviation

Var š‘‹

def standard deviation SD š‘‹ = Var š‘‹

31

Units of š‘‹2

Units of š‘‹

Page 32: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Variance of Stanford weather

32

Variance

of š‘‹Var š‘‹ = šø š‘‹ āˆ’ šø š‘‹ 2

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

š‘‹ š‘‹ āˆ’ šœ‡ 2

57Ā°F 124 (Ā°F)2

šø š‘‹ = šœ‡ = 68

71Ā°F 9 (Ā°F)2

75Ā°F 49 (Ā°F)2

69Ā°F 1 (Ā°F)2

ā€¦ ā€¦

Variance šø š‘‹ āˆ’ šœ‡ 2 = 39 (Ā°F)2

Standard deviation = 6.2Ā°F

Page 33: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Stanford, CA

šø high = 68Ā°F

Washington, DC

šø high = 67Ā°F

33

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 2 Var š‘‹ = 248 Ā°F 2

Variance

of š‘‹Var š‘‹ = šø š‘‹ āˆ’ šø š‘‹ 2

68Ā°F 67Ā°F

Page 34: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Properties of Variance

34

07b_variance_ii

Page 35: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Properties of variance

Definition Var š‘‹ = šø š‘‹ āˆ’ šø š‘‹ 2

def standard deviation SD š‘‹ = Var š‘‹

Property 1 Var š‘‹ = šø š‘‹2 āˆ’ šø š‘‹ 2

Property 2 Var š‘Žš‘‹ + š‘ = š‘Ž2Var š‘‹

35

Units of š‘‹2

Units of š‘‹

ā€¢ Property 1 is often easier to compute than the definition

ā€¢ Unlike expectation, variance is not linear

Page 36: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Properties of variance

Definition Var š‘‹ = šø š‘‹ āˆ’ šø š‘‹ 2

def standard deviation SD š‘‹ = Var š‘‹

Property 1 Var š‘‹ = šø š‘‹2 āˆ’ šø š‘‹ 2

Property 2 Var š‘Žš‘‹ + š‘ = š‘Ž2Var š‘‹

36

Units of š‘‹2

Units of š‘‹

ā€¢ Property 1 is often easier to compute than the definition

ā€¢ Unlike expectation, variance is not linear

Page 37: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Computing variance, a proof

37

Var š‘‹ = šø š‘‹ āˆ’ šø š‘‹ 2

= šø š‘‹2 āˆ’ šø š‘‹ 2

= šø š‘‹2 āˆ’ šœ‡2= šø š‘‹2 āˆ’ 2šœ‡2 + šœ‡2= šø š‘‹2 āˆ’ 2šœ‡šø š‘‹ + šœ‡2

= šø š‘‹ āˆ’ šœ‡ 2Let šø š‘‹ = šœ‡

Everyone,

please

welcome the

second

moment!

Variance

of š‘‹Var š‘‹ = šø š‘‹ āˆ’ šø š‘‹ 2

= šø š‘‹2 āˆ’ šø š‘‹ 2

=

š‘„

š‘„ āˆ’ šœ‡ 2š‘ š‘„

=

š‘„

š‘„2 āˆ’ 2šœ‡š‘„ + šœ‡2 š‘ š‘„

=

š‘„

š‘„2š‘ š‘„ āˆ’ 2šœ‡

š‘„

š‘„š‘ š‘„ + šœ‡2

š‘„

š‘ š‘„

ā‹… 1

Page 38: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Let Y = outcome of a single die roll. Recall šø š‘Œ = 7/2 .

Calculate the variance of Y.

38

Variance of a 6-sided dieVariance

of š‘‹Var š‘‹ = šø š‘‹ āˆ’ šø š‘‹ 2

= šø š‘‹2 āˆ’ šø š‘‹ 2

1. Approach #1: Definition

Var š‘Œ =1

61 āˆ’

7

2

2

+1

62 āˆ’

7

2

2

+1

63 āˆ’

7

2

2

+1

64 āˆ’

7

2

2

+1

65 āˆ’

7

2

2

+1

66 āˆ’

7

2

2

2. Approach #2: A property

šø š‘Œ2 =1

612 + 22 + 32 + 42 + 52 + 62

= 91/6

Var š‘Œ = 91/6 āˆ’ 7/2 2

= 35/12 = 35/12

Page 39: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Properties of variance

Definition Var š‘‹ = šø š‘‹ āˆ’ šø š‘‹ 2

def standard deviation SD š‘‹ = Var š‘‹

Property 1 Var š‘‹ = šø š‘‹2 āˆ’ šø š‘‹ 2

Property 2 Var š‘Žš‘‹ + š‘ = š‘Ž2Var š‘‹

39

Units of š‘‹2

Units of š‘‹

ā€¢ Property 1 is often easier to compute than the definition

ā€¢ Unlike expectation, variance is not linear

Page 40: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Property 2: A proof

Property 2 Var š‘Žš‘‹ + š‘ = š‘Ž2Var š‘‹

40

Var š‘Žš‘‹ + š‘

= šø š‘Žš‘‹ + š‘ 2 āˆ’ šø š‘Žš‘‹ + š‘ 2 Property 1

= šø š‘Ž2š‘‹2 + 2š‘Žš‘š‘‹ + š‘2 āˆ’ š‘Žšø š‘‹ + š‘ 2

= š‘Ž2šø š‘‹2 + 2š‘Žš‘šø š‘‹ + š‘2 āˆ’ š‘Ž2 šø[š‘‹] 2 + 2š‘Žš‘šø[š‘‹] + š‘2

= š‘Ž2šø š‘‹2 āˆ’ š‘Ž2 šø[š‘‹] 2

= š‘Ž2 šø š‘‹2 āˆ’ šø[š‘‹] 2

= š‘Ž2Var š‘‹ Property 1

Proof:

Factoring/

Linearity of

Expectation

Page 41: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Bernoulli RV

41

07c_bernoulli

Page 42: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Jacob Bernoulli

42

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

One of many mathematicians in Bernoulli family

The Bernoulli Random Variable is named for him

My academic great14 grandfather

Page 43: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

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

43

š‘ƒ š‘‹ = 1 = š‘ 1 = š‘š‘ƒ š‘‹ = 0 = š‘ 0 = 1 āˆ’ š‘š‘‹~Ber(š‘)

Support: {0,1} Variance

Expectation

PMF

šø š‘‹ = š‘

Var š‘‹ = š‘(1 āˆ’ š‘)

Remember this nice property of

expectation. It will come back!

Page 44: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Defining Bernoulli RVs

44

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 45: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Defining Bernoulli RVs

45

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 46: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

(live)06: Random Variables ISlides by Lisa Yan

July 6, 2020

46

Todayā€™s discussion thread: https://us.edstem.org/courses/667/discussion/84211

Page 47: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Discrete random variables

47

Discrete

Random

Variable, š‘‹

Experiment

outcomesš‘ƒ š‘‹ = š‘„ = š‘(š‘„)

šø š‘‹

Definition

Properties

Review

Note: Random Variables

also called distributions

Support

Page 48: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Event-driven probability

ā€¢ Relate only binary eventsā—¦ Either happens (šø)

ā—¦ or doesnā€™t happen (šøš¶)

ā€¢ Can only report probability

ā€¢ Lots of combinatorics

Random Variables

ā€¢ Link multiple similar events together (š‘‹ = 1, š‘‹ = 2,ā€¦ , š‘‹ = 6)

ā€¢ Can compute statistics: report the ā€œaverageā€ outcome

ā€¢ Once we have the PMF (discrete RVs), we can do regular math

48

A Whole New World with Random Variables

Page 49: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

PMF for the sum of two dice

Let š‘Œ be a random variable that represents the sum oftwo independent dice rolls.

Support of š‘Œ: 2, 3, ā€¦ , 11, 12

š‘ š‘¦ =

š‘¦āˆ’1

36š‘¦ āˆˆ ā„¤, 2 ā‰¤ š‘¦ ā‰¤ 6

13āˆ’š‘¦

36š‘¦ āˆˆ ā„¤, 7 ā‰¤ š‘¦ ā‰¤ 12

0 otherwise

Sanity check:

49

6/36

0

š‘Œ = š‘¦

5/36

2 3 4 5 6 7 8 9 10 11 12

4/36

3/36

2/36

1/36

š‘ƒš‘Œ=š‘¦

š‘¦=2

12

š‘ š‘¦ = 1

Page 50: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Think, then Breakout Rooms

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

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

Think by yourself: 1 min

Breakout rooms: 5 min.

50

šŸ¤”

Page 51: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Example random variable

Consider 5 flips of a coin which comes up heads with probability š‘.Each coin flip is an independent trial. Let š’€ = # of heads on 5 flips.

1. What is the support of š‘Œ? In other words, what are the values that š‘Œ can take on with non-zero probability?

2. Define the event š‘Œ = 2. What is š‘ƒ š‘Œ = 2 ?

3. What is the PMF of š‘Œ? In other words, whatis š‘ƒ š‘Œ = š‘˜ , for š‘˜ in the support of š‘Œ?

51

šŸ¤”

Page 52: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Example random variable

Consider 5 flips of a coin which comes up heads with probability š‘.Each coin flip is an independent trial. Let š’€ = # of heads on 5 flips.

1. What is the support of š‘Œ? In other words, what are the values that š‘Œ can take on with non-zero probability?

2. Define the event š‘Œ = 2. What is š‘ƒ š‘Œ = 2 ?

3. What is the PMF of š‘Œ? In other words, whatis š‘ƒ š‘Œ = š‘˜ , for š‘˜ in the support of š‘Œ?

52

0, 1, 2, 3, 4, 5

š‘ƒ š‘Œ = š‘˜ =52

š‘2 1 āˆ’ š‘ 3

š‘ƒ š‘Œ = š‘˜ =5š‘˜

š‘š‘˜ 1 āˆ’ š‘ 5āˆ’š‘˜

Page 53: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Expectation

Remember that the expectation of a die roll is 3.5.

53

š‘‹ = RV for value of roll

šø š‘‹ = 11

6+ 2

1

6+ 3

1

6+ 4

1

6+ 5

1

6+ 6

1

6=7

2

Review

šø š‘‹ =

š‘„:š‘ š‘„ >0

š‘ š‘„ ā‹… š‘„ Expectation: The average value

of a random variable

Page 54: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Lying with statistics

54

ā€œThere are three kinds of lies:

lies, damned lies, and statisticsā€

ā€“popularized by Mark Twain, 1906

Page 55: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Lying with statistics

A school has 3 classes with 5, 10, and 150 students.

What is the average class size?

55

1. Interpretation #1

ā€¢ Randomly choose a classwith equal probability.

ā€¢ š‘‹ = size of chosen class

šø š‘‹ = 51

3+ 10

1

3+ 150

1

3

=165

3= 55

2. Interpretation #2

ā€¢ Randomly choose a studentwith equal probability.

ā€¢ š‘Œ = size of chosen class

šø š‘Œ

= 55

165+ 10

10

165+ 150

150

165

=22635

165ā‰ˆ 137

Average student perception of class sizeWhat universities usually report

Page 56: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Interlude for fun/announcements

56

Page 57: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

The pedagogy behind concept checks

ā€¢ Spaced practice (vs. ā€practice makes perfectā€): better memory retention

ā€¢ Low-stakes testing: better concept retrieval, actively connect concepts

It is okay if you donā€™t understand material off-the-bat. In fact,

learning research suggests that you will learn more in the long run.

Announcements

57

Problem Set 2

Out: last Wed.

Due: Friday

Covers: through today(ish)

Problem Set 1

Due: last Wed.

On-time grades: this Wed.

Solutions: next Wed.

https://www.dartmouth.edu/~cogedlab/pubs/Kang(2016,PIBBS).pdf

http://learninglab.psych.purdue.edu/downloads/2006_Roediger_Karpicke_PsychSci.pdfJOKE HERE

Page 58: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Ethics in Probability: Misleading Averages

ā€œAverages can be misleading when a distribution is heavily skewed at one end, with a small number of unrepresentative outliers pulling the average in their direction. ā€œ

ā€œIn 2011, for example, the average income of the 7,878 households in Steubenville, Ohio, was $46,341. But if just two people, Warren Buffett and Oprah Winfrey, relocated to that city, the average household income in Steubenville would rise 62 percent overnight, to $75,263 per household.ā€

58

https://www.nytimes.com/2013/05/26/opinion/sunday/wh

en-numbers-mislead.html

Page 59: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Important properties of expectation

1. Linearity:

šø š‘Žš‘‹ + š‘ = š‘Žšø š‘‹ + š‘

2. Expectation of a sum = sum of expectation:

šø š‘‹ + š‘Œ = šø š‘‹ + šø š‘Œ

3. Unconscious statistician:

šø š‘” š‘‹ =

š‘„

š‘” š‘„ š‘(š‘„)59

Roll a die, outcome is š‘‹. You win $2š‘‹ āˆ’ 1.

What are your expected winnings?

Review

Let š‘‹ = 6-sided dice roll.

šø 2š‘‹ āˆ’ 1 = 2 3.5 āˆ’ 1 = 6

Page 60: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Important properties of expectation

1. Linearity:

šø š‘Žš‘‹ + š‘ = š‘Žšø š‘‹ + š‘

2. Expectation of a sum = sum of expectation:

šø š‘‹ + š‘Œ = šø š‘‹ + šø š‘Œ

3. Unconscious statistician:

šø š‘” š‘‹ =

š‘„

š‘” š‘„ š‘(š‘„)60

Roll a die, outcome is š‘‹. You win $2š‘‹ āˆ’ 1.

What are your expected winnings?

Review

What is the expectation of the sum of

two dice rolls?

Let š‘‹ = 6-sided dice roll.

šø 2š‘‹ āˆ’ 1 = 2 3.5 āˆ’ 1 = 6

Let š‘‹ = roll of die 1, š‘Œ = roll of die 2.

šø š‘‹ + š‘Œ = 3.5 + 3.5 = 7

Page 61: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Important properties of expectation

1. Linearity:

šø š‘Žš‘‹ + š‘ = š‘Žšø š‘‹ + š‘

2. Expectation of a sum = sum of expectation:

šø š‘‹ + š‘Œ = šø š‘‹ + šø š‘Œ

3. Unconscious statistician:

šø š‘” š‘‹ =

š‘„

š‘” š‘„ š‘(š‘„)61

Roll a die, outcome is š‘‹. You win $2š‘‹ āˆ’ 1.

What are your expected winnings?

Review

What is the expectation of the sum of

two dice rolls?

Let š‘‹ = 6-sided dice roll.

šø 2š‘‹ āˆ’ 1 = 2 3.5 āˆ’ 1 = 6

(next up)

Let š‘‹ = roll of die 1, š‘Œ = roll of die 2.

šø š‘‹ + š‘Œ = 3.5 + 3.5 = 7

Page 62: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Think, then Breakout Rooms

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

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

Think by yourself: 1 min

Breakout rooms: 5 min. Introduce yourself!

62

šŸ¤”

Page 63: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Being a statistician unconsciously

Let š‘‹ be a discrete random variable.

ā€¢ š‘ƒ š‘‹ = š‘„ =1

3for š‘„ āˆˆ {āˆ’1, 0, 1}

Let š‘Œ = |š‘‹|. What is šø š‘Œ ?

63

Expectation

of š‘” š‘‹šø š‘” š‘‹ =

š‘„

š‘” š‘„ š‘(š‘„)

A.1

3ā‹… 1 +

1

3ā‹… 0 +

1

3ā‹… āˆ’1 = 0

B. šø š‘Œ = šø 0 = 0

C.1

3ā‹… 0 +

2

3ā‹… 1 =

2

3

D.1

3ā‹… āˆ’1 +

1

3ā‹… 0 +

1

31 =

2

3

E. C and D šŸ¤”

Page 64: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

A.1

3ā‹… 1 +

1

3ā‹… 0 +

1

3ā‹… āˆ’1 = 0

B. šø š‘Œ = šø 0 = 0

C.1

3ā‹… 0 +

2

3ā‹… 1 =

2

3

D.1

3ā‹… āˆ’1 +

1

3ā‹… 0 +

1

31 =

2

3

E. C and D

Being a statistician unconsciously

Let š‘‹ be a discrete random variable.

ā€¢ š‘ƒ š‘‹ = š‘„ =1

3for š‘„ āˆˆ {āˆ’1, 0, 1}

Let š‘Œ = |š‘‹|. What is šø š‘Œ ?

64

Expectation

of š‘” š‘‹šø š‘” š‘‹ =

š‘„

š‘” š‘„ š‘(š‘„)

šø š‘‹

1. Find PMF of š‘Œ: š‘š‘Œ 0 =1

3, š‘š‘Œ 1 =

2

3

2. Compute šø[š‘Œ]

Use LOTUS by using PMF of X:

1. š‘ƒ š‘‹ = š‘„ ā‹… š‘„2. Sum up

šø šø š‘‹

āŒ

āŒ

Page 65: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Think

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

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

Think by yourself: 2 min

65

šŸ¤”(by yourself)

Page 66: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

St. Petersburg Paradox

ā€¢ A fair coin (comes up ā€œheadsā€ with š‘ = 0.5)

ā€¢ Define š‘Œ = number of coin flips (ā€œheadsā€) before first ā€œtailsā€

ā€¢ You win $2š‘Œ

How much would you pay to play? (How much you expect to win?)

66

Expectation

of š‘” š‘‹

A. $10000

B. $āˆžC. $1

D. $0.50

E. $0 but let me play

F. I will not play

šø š‘” š‘„ =

š‘„

š‘” š‘„ š‘(š‘„)

šŸ¤”(by yourself)

Page 67: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

ā€¢ A fair coin (comes up ā€œheadsā€ with š‘ = 0.5)

ā€¢ Define š‘Œ = number of coin flips (ā€œheadsā€) before first ā€œtailsā€

ā€¢ You win $2š‘Œ

How much would you pay to play? (How much you expect to win?)

67

St. Petersburg Paradox

1. Define random variables

2. Solve

For š‘– ā‰„ 0:

Let š‘Š = your winnings, 2š‘Œ.

šø š‘Š = šø 2š‘Œ =1

2

1

20 +1

2

2

21 +1

2

3

22 +ā‹Æ

š‘ƒ š‘Œ = š‘– =1

2

š‘–+1

=

š‘–=0

āˆž1

2

š‘–+1

2š‘– =

š‘–=0

āˆž1

2= āˆž

Expectation

of š‘” š‘‹šø š‘” š‘„ =

š‘„

š‘” š‘„ š‘(š‘„)

Page 68: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020 68

St. Petersburg + Reality

1. Define random variables

2. Solve šø š‘Š =1

2

1

20 +1

2

2

21 +1

2

3

22 +ā‹Æ

=

š‘–=0

š‘˜1

2

š‘–+1

2š‘– =

š‘–=0

161

2= 8.5

Expectation

of š‘” š‘‹šø š‘” š‘„ =

š‘„

š‘” š‘„ š‘(š‘„)

What if dealer has only $65,536?

ā€¢ Same game

ā€¢ If you win over $65,536, dealer leaves the country

ā€¢ Define š‘Œ = # heads before first tails

ā€¢ You win š‘Š = $2š‘Œ

š‘˜ = log2 65,536

= 16

For š‘– ā‰„ 0:

Let š‘Š = your winnings, 2š‘Œ.

š‘ƒ š‘Œ = š‘– =1

2

š‘–+1

Page 69: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Breakout Rooms

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

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

Breakout rooms: 5 min. Introduce yourself!

69

šŸ¤”

Page 70: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Statistics: Expectation and variance

1. a. Let š‘‹ = the outcome of a 4-sideddie roll. What is šø š‘‹ ?

b. Let š‘Œ = the sum of three rolls of a4-sided die. What is šø š‘Œ ?

2. Compare the variances ofšµ1~Ber 0.1 and šµ2~Ber(0.5).

3. a. Let š‘ = # of tails on 10 flips of abiased coin (w.p. 0.4 of heads). What is šø š‘ ?

b. What is Var(š‘)?

70

šŸ¤”

Page 71: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Statistics: Expectation and variance

1. a. Let š‘‹ = the outcome of a 4-sideddie roll. What is šø š‘‹ ?

b. Let š‘Œ = the sum of three rolls of a4-sided die. What is šø š‘Œ ?

2. Compare the variances ofšµ1~Ber 0.1 and šµ2~Ber(0.5).

3. a. Let š‘ = # of tails on 10 flips of abiased coin (w.p. 0.4 of heads). What is šø š‘ ?

b. What is Var(š‘)?

71

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

Page 72: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Lisa Yan, CS109, 2020

Our first common RVs

73

š‘‹ ~ 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

Review

Page 73: 06: Random Variables - Stanford University...The probability of the event that a random variable takes on the value š‘˜! For discrete random variables, this is a probability mass

Next Time: Binomial, Poisson, and more ā˜ŗ

74