Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
1. Random variable
2. Probability mass function (pmf)
3. Cumulative distribution function (cdf)
1.2.1 Recall
A discrete sample space Ω is a finite or listable set of outcomes {𝜔1 , 𝜔2 …}. The probability of
𝜔
an outcome is denoted 𝑃(𝜔). An event 𝐸 is a subset of Ω. The probability of an event 𝐸 is
𝑃(𝐸) = ∑𝜔∈𝐸 𝑃(𝜔).
1.2.2 Random variables as payoff functions
Exercise 1
Roll a die twice and record the outcomes as (i, j), where i is the result of the first roll and j the
result of the second.
𝑌(𝑖,𝑗) = 𝑖𝑗 − 10 (2)
c) which game is better?
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-a… 1/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
These payoff functions are examples of random variables. A random variable assigns a number to
each outcome in a sample space . More formally:
Why is X called a random variable? It's random because its value depends on a random outcome
of an experiment. And we treat X like we would a usual variable: we can add it to other random
variables, square it, and so on.
The probability mass function (pmf) of a discrete random variable is the function
𝑝(𝑎) = 𝑃(𝑋 = 𝑎) Note:
Exercise 4 If our sample space is the set of all pairs of (𝑖,𝑗) coming from rolling two dice and
𝑍(𝑖,𝑗) = 𝑖 + 𝑗 is the sum of the dice then
What is the set 𝑍 ≤ 4?
Note that the definition of 𝐹(𝑎) uses the symbol less than or equal. This will be important for
getting your calculations exactly right.
value 𝑎 : 1 1 3 4 5 6
pmf 𝑝(𝑎) : 1/36 3/36 5/36 7/36 9/36 11/36
cdf 𝐹(𝑎) : 1/36 4/36 9/36 16/36 25/36 36/36
𝐹(𝑎) is called the cumulative distribution function because 𝐹(𝑎) gives the total probability that
accumulates by adding up the probabilities 𝑝(𝑏) as 𝑏 runs from −𝑖𝑛𝑓𝑡𝑦 to 𝑎. For example, in the
table above, the entry 16/36 in column 4 for the cdf is the sum of the values of the pmf from
column 1 to column 4. In notation:
value 𝑎 : 0 1 2 3
pmf𝑝(𝑎) : 1/8 3/8 3/8 1/8
cdf𝐹(𝑎) : 1/8 4/8 7/8 1
The colored graphs show how the cumulative distribution function is built by accumulating
probability as a increases. The black and white graphs are the more standard presentations.
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-a… 3/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
Exercise 5 Plot the pmf and cdf for the maximum of two dice exercise.
In words, (1) says the cumulative probability 𝐹(𝑎) increases or remains constant as 𝑎 increases,
but never decreases; (2) says the accumulated probability is always between 0 and 1; (3) says that
as a gets very large, it becomes more and more certain that 𝑋 ≤ 𝑎 and as a gets very negative it
becomes more and more certain that 𝑋 > 𝑎.
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-a… 4/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
Model: The Bernoulli distribution models one trial in an experiment that can result in either
success or failure This is the most important distribution and it is also the simplest. A random
variable 𝑋
has a Bernoulli distribution with parameter if: 𝑝
𝑋 takes the values 0 and 1.
1.
𝑃(𝑋 = 1) = 𝑝 and 𝑃(𝑋 = 0) = 1 − 𝑝.
2.
We will write 𝑋 ∼ Bernoulli (𝑝) or Ber(𝑝) , which is read “X follows a Bernoulli distribution with
parameter p” or “X is drawn from a Bernoulli distribution with parameter p”.
A simple model for the Bernoulli distribution is to flip a coin with probability of heads, with 𝑝 𝑋=1
on heads and 𝑋=0 on tails. The general terminology is to say 𝑋
is 1 on success and 0 on
failure, with success and failure defined by the context.
Many decisions can be modeled as a binary choice, such as votes for or against a proposal. If 𝑝 is
the proportion of the voting population that favors the proposal, than the vote of a random
individual is modeled by a Bernoulli(p).
Here are the table and graphs of the pmf and cdf for the Bernoulli(1/2) distribution and below that
for the general Bernoulli(p) distribution.
A single Bernoulli trial is, say, one toss of a coin. A single binomial trial consists of n Bernoulli trials.
For coin flips the sample space for a Bernoulli trial is {𝐻,𝑇}
. The sample space for a binomial
trial is all sequences of heads and tails of length n. Likewise a Bernoulli random variable takes
values 0 and 1 and a binomial random variables takes values 0,1,2,…,𝑛
.
Example 2
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-a… 5/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
The number of heads in n flips of a coin with probability 𝑝 of heads follows a Binomial(n, p)
distribution.
We describe 𝑋 ∼ Binomial(𝑛,𝑝)
by giving its values and probabilities. For notation we will use
𝑘to mean an arbitrary number between 0 and n.
We remind you that 𝑛 choose 𝑘 = ( 𝑛𝑘 ) = 𝑛 𝐶𝑘 is the number of ways to choose 𝑘 things out
of a collection of 𝑛 things and it has the formula
𝑛 = 𝑛! (5)
( 𝑘 ) 𝑘!(𝑛 − 𝑘)!
Here is a table for the pmf of a Binomial(n,k) random variable
values 𝑎 : 0 1 2 ⋯ 𝑘
pmf𝑝(𝑎) : (1 − 𝑝)𝑛 ( 𝑛1 ) 𝑝1 (1 − 𝑝)𝑛−1 ( 𝑛2 ) 𝑝2 (1 − 𝑝)𝑛−2 ⋯ ( 𝑘𝑛 ) 𝑝𝑘 (1 −
HHTTT, HTHTT, HTTHT, HTTTH, THHTT, THTHT, THTTH, TTHHT, TTHTH, TTTHH
𝑝2 (1 − 𝑝)3 (7)
.
𝑝
This is because each of the two heads has probability and each of the 3 tails has probability
1−𝑝 . Because the individual tosses are independent, we can multiply probabilities. Therefore, the
total probability of exactly 2 heads is the sum of 10 identical probabilities, i.e.
𝑝(2) = 10𝑝2 (1 − 𝑝)3 .
This guides us to the shorter way to do the computation. We have to count the number of
sequences with exactly 2 heads. To do this we need to choose 2 of the tosses to be heads and the
remaining 3 to be tails. The number of such sequences is the number of ways to choose 2 out of 5
5
things, that is
( 2 ). Since each such sequence has the same probability, 𝑝2 (1 − 𝑝)3 , we get the
probability of exactly 2 heads 𝑝(2) =
5
( 2 ) 𝑝2 (1 − 𝑝)3 .
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-a… 6/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
Here are some binomial probability mass function (here, frequency is the same as probability).
In [12]: #First, we have to create a vector of quantiles as input for the dbinom R
#Then, we can apply the dbinom function to this vector as shown below.
#Note that I have specified the size to be equal to 10 (i.e. the number of
#probability for each binomial draw to be equal to 0.5 (i.e. 50%).
y_dbinom <- dbinom(x_dbinom, size = 10, prob = 0.5) # Apply dbinom functi
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-a… 7/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
In [13]: #First, we have to create a vector of quantiles as input for the dbinom R
#Then, we can apply the dbinom function to this vector as shown below.
#Note that I have specified the size to be equal to 10 (i.e. the number of
#probability for each binomial draw to be equal to 0.1
y_dbinom <- dbinom(x_dbinom, size = 10, prob = 0.1) # Apply dbinom functi
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-a… 8/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
In [14]: #First, we have to create a vector of quantiles as input for the dbinom R
y_dbinom <- dbinom(x_dbinom, size = 20, prob = 0.1) # Apply dbinom functi
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-a… 9/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
In [15]: #First, we have to create a vector of quantiles as input for the dbinom R
y_dbinom <- dbinom(x_dbinom, size = 40, prob = 0.1) # Apply dbinom functi
A geometric distribution models the number of tails before the first head in a sequen
Definition 4 flips (Bernoulli trials).
Example 3 (a) Flip a coin repeatedly. Let 𝑋 be the number of tails before the first heads. So, 𝑋
can equal 0, i.e. the first flip is heads,1,2,…. In principle, it take any nonnegative integer value.
(b) Give a flip of tails the value 0, and heads the value 1. In this case, 𝑋 is the number of 0's
before the first 1.
(c) Give a flip of tails the value 1, and heads the value 0. In this case, 𝑋 is the number of 1's
before the first 0.
(d) Call a flip of tails a success and heads a failure. So, 𝑋 is the number of successes before the
first failure.
(e) Call a flip of tails a failure and heads a success. So, 𝑋 is the number of failures before the
first success.
You create many different scenarios of this type of distribution. The most neutral language is the
number of tails before the first head.
Definition 5 Formal definition. The random variable 𝑋 follows a geometric distribution with
parameter if 𝑝
𝑋 0,1,2,3,…
takes the values
𝑝(𝑘) = 𝑃(𝑋 = 𝑘) = (1 − 𝑝)𝑘𝑝 .
its pmf is given by
value 𝑎 : 0 1 2 3 … 𝑘 …
pmf 𝑝(𝑎) : 𝑝 (1 − 𝑝)𝑝 (1 − 𝑝) 𝑝 (1 − 𝑝)3 𝑝 … (1 − 𝑝)𝑘𝑝 …
2
Table: 𝑋 ∼ geometric (𝑝): X = the number of 0s before the first 1.
The geometric distribution is an example of a discrete distribution that takes an infinite number of
possible values. Things can get confusing when we work with successes and failure since we
might want to model the number of successes before the first failure or we might want the number
of failures before the first success. To keep straight things straight you can translate to the neutral
language of the number of tails before the first heads.
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-… 11/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-… 12/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
Exercise 8 Computing geometric probabilities. Suppose that the inhabitants of an island plan
their families by having babies until the first girl is born. Assume the probability of having a girl
with each pregnancy is 0.5 independent of other pregnancies, that all babies survive and there
are no multiple births. What is the probability that a family has k boys?
The uniform distribution models any situation where all the outcomes are equally likely.
𝑋 ∼ uniform (𝑁) (8)
𝑋 takes values 1,2,3,…,𝑁 , each with probability 1/𝑁 . We have already seen this distribution
many times when modeling the fair coins (𝑁 = 2), dice (𝑁 = 6), birthdays (𝑁 = 365 ), and
poker hands (𝑁 =
52 ).
(5)
1.14 Arithmetic with Random Variables
We can do arithmetic with random variables. For example, we can add subtract, multiply or square
them.
There is a simple, but extremely important idea for counting. It says that if we have a sequence of
numbers that are either 0 or 1 then the sum of the sequence is the number of 1s.
Example 5 Consider the sequence with five 1s. It is easy to see that the sum of this sequence is
5 the number of 1s.
Example 6
𝑛
Toss a fair coin times. Let 𝑋𝑗
be 1 if the jth toss is heads and 0 if it's tails. So, is a 𝑋𝑗
Bernoulli(1/2) random variable. Let 𝑋
be the total number of heads in the n tosses. Assuming the
tosses are independence, we know 𝑋 ∼ binomial(𝑛,1/2)
. We can also write
𝑋 = 𝑋1 + 𝑋2 + 𝑋3 + … + 𝑋𝑛
Again, this is because the terms in the sum on the right are all either 0 or 1. So, the sum is exactly
the number of 𝑋𝑗
that are 1, i.e. the number of heads.
The important thing to see in the example above is that we've written the more complicated
binomial random variable 𝑋
as the sum of extremely simple random variables . This will allow 𝑋𝑗
us to manipulate X algebraically.
Values of 𝑋 𝑥 : 1 2 3 4
pmf 𝑝𝑋 (𝑥) : 1/10 2/10 3/10 4/10 (9)
Values of 𝑌 𝑦 : 1 2 3 4 5
pmf 𝑝𝑌 (𝑦) : 1/15 2/15 3/15 4/15 5/15
Check that the total probability for each random variable is 1. Make a table for the random
variable 𝑋+𝑌 .
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-… 14/15
10/14/2020 Learning Quiz 3 - Discrete Random Variables - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/Dropbox/statistics 2020 fall/learning quizzes/learning quiz 3/Learning Quiz 3 - Discrete Random Variables .ipynb#Events-… 15/15