![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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/38.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/39.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/40.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/41.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/42.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/43.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/44.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/45.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/46.jpg)
(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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/47.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/48.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/49.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/50.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/51.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/52.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/53.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/54.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/55.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/56.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/57.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/58.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/59.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/60.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/61.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/62.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/63.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/64.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/65.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/66.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/67.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/68.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/69.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/70.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/71.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/72.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081409/6088a5d2b059f361c41ad644/html5/thumbnails/73.jpg)
Next Time: Binomial, Poisson, and more βΊ
74