You are on page 1of 28

Random Variables

A random variable is a function that associates a number with each outcome


of the sample space of a random experiment.
• A discrete random variable is a random variable whose sample space has
a finite or at most a countably infinite number of values.
i. Toss a fair coin 3 times and let X be the number of heads.

ii. Toss a fair coin and let X be the number of tosses until the third
head occurs.

• A continuous random variable can take any value within a finite or in-
finite interval of the real number line (−∞, ∞).
i. Let X be the pH level of a lake.

• The probability distribution of a random variable specifies how the total


probability of its sample space is distributed over the values of its sample
space.
Cumulative Distribution Function

The cumulative distribution function (CDF) of a random variable X is the


function
F (x) = P (X ≤ x)
for x ∈ (−∞, ∞).

1
Properties of the CDF:
i. It is non-decreasing: If a ≤ b then F (a) ≤ F (b).
ii. F (−∞) = 0 and F (∞) = 1.
iii. If a < b, then P (a < X ≤ b) = F (b) − F (a).

Example: Toss a fair coin 3 times and let X be the number of heads.
Outcomes:

The PMF for X:


x 0 1 2 3
p(x)
The CDF:

2
Properties of the CDF of a discrete random variable: Let x1 < x2 <
· · · denote the possible values of X. Then
i. F is a step function with jumps occurring only at the values x of SX .
The size of the jump at each x of SX equals p(x).
ii. The CDF can be obtained from the PMF:
X
F (x) = p(xi )
xi ≤x

iii. The PMF can be obtained from the CDF:

p(x1 ) = F (x1 ) and p(xi ) = F (xi ) − F (xi−1 ) for i = 2, 3, . . .

iv. The probability of a < X ≤ b is given as


X
P (a < X ≤ b) = F (b) − F (a) = p(xi )
a<xi ≤b

Example: Suppose the CDF of a random variable X is given by





 0, x < −1

1/3, −1 ≤ x < 1
F (x) =


 1/2, 1 ≤ x < 2

1, x≥2

a. Find the PMF of X.

b. Find P (−1 < X ≤ 2).

3
Density Function for a Continuous Random Variable
• For a continuous random variable P (X = x) = 0 for all x.
• The CDF is a continuous function.
Example: We say X is the uniform in [0, 1] random variable if X has CDF

0, x < 0

F (x) = P (X ≤ x) = x, 0 ≤ x ≤ 1

1, x > 1

• We consider the probability that the random variable takes values in an


interval.
Example: If X ∼ U (0, 1), the probability that X is within any two subinter-
vals of equal length of [0, 1] is equal.

For a continuous random variable, instead of a PMF, we have a probabil-


ity density function, or PDF.

The probability density function (PDF) of a continuous random variable X


is a nonnegative function f such that
Z b
P (a < X < b) = area under f between a and b = f (x)dx
a

4
Example: If X ∼ U (0, 1), the PDF is
(
1, 0 ≤ x ≤ 1
f (x) =
0, elsewhere

Let X be a continuous random variable with PDF f (x) and CDF F (x).
Then
R∞
• −∞ f (x)dx = 1
• The CDF can be obtained from the PDF:
Z x
F (x) = P (X ≤ x) = f (y)dy
−∞

• The PDF can be obtained from the CDF:


d
f (x) = F 0 (x) = F (x)
dx
Example: Let X denote the resistance of a randomly chosen resistor, and
suppose that its PDF is given by
(
kx, 8 ≤ x ≤ 10
f (x) =
0, elsewhere

Find k.

5
Example: Let X be a random variable with CDF
(
0, x≤0
F (x) =
1 − e−x , x > 0

a. Find the PDF of X.

b. Find P (0.5 < X < 1).

Example: A random variable X is said to have the uniform in [A, B] distri-


bution if its PDF is
(
1
B−A , A ≤ x ≤ B
f (x) =
0, elsewhere

Find the CDF of X.

6
Note: We can simulate a random sample of size n from a U (A, B) distribu-
tion using runif(n, A, B) in R.

Example: Simulate a random sample of size 7 from the U (−5, 6) distribution.


> set.seed(3200)
> runif(7, -5, 6)
[1] 0.48400095 4.11695810 -4.30168488 3.57560977 3.41278161 2.51635610
0.07199335

Parameters of Probability Distributions

• We can summarize a probability distribution with the expected value,


variance, standard deviation, and percentiles.

Expected Value

For a discrete random variable X with sample space SX and PMF p(x),
the expected value is X
E(X) = µX = xp(x)
x∈SX

Example: Let X be the random variable with PMF

x -1 1 2
p(x) 13 16 12

For a continuous random variable X with PDF f (x), the expected value is
Z ∞
E(X) = µX = xf (x)dx
−∞

7
Example: Let X ∼ U (−3, 2). Find E(X).

We can also use R to perform integration.

Example: Let X be a continuous random variable with PDF


(
e−x , x > 0
f (x) =
0, elsewhere

> f <- function(x){x*exp(-x)};


> integrate(f, lower=0, upper=Inf)
1 with absolute error < 6.4e-06

Expected Value of a Function of a Random Variable


1. If X is discrete with sample space SX and PMF pX (x), and h(x) is a
function on SX , the expected value of Y = h(X) is
X
E(h(X)) = h(x)pX (x)
x∈SX

2. If X is continuous with PDF fX (x), the expected value of Y = h(X) is


Z ∞
E(h(X)) = h(x)fX (x)dx
−∞

8
Note: If h(x) = ax + b and Y = aX + b, then

E(h(X)) = aE(X) + b

Example: A bookstore purchases three copies of a book at $6 each and sells


them for $12 each. Unsold copies are returned for $2 each. Let X be the
number of copies sold and Y the net revenue. Find E(Y ) if the PMF of X is
x 0 1 2 3
p(x) 0.1 0.2 0.2 0.5

Example: Let X be a continuous random variable with PDF


(
e−x , x > 0
f (x) =
0, elsewhere

If Y = X 3 , find E(Y ).

> f <- function(x){x^3*exp(-x)};


> integrate(f, lower=0, upper=Inf)
6 with absolute error < 2.6e-06

9
Variance and Standard Deviation
• The variance σX
2
, or Var(X), of a random variable X is
2
σX = E[(X − µX )2 ]
where µX = E(X) is the expected value of X.

Note: An easier formula to use is


2
σX = E(X 2 ) − [E(X)]2

• The standard deviation of X is the positive square root of the variance:


q
σX = σX 2

Example: Let X be the random variable with PMF


x -1 1 2
p(x) 13 16 12

Example: Let X ∼ U (−3, 2). Find Var(X).

2
Note: If the variance of X is σX and Y = a + bX, then
σY2 = b2 σX
2

10
Population Percentiles
• For continuous random variables.
Let X be a continuous random variable with CDF F and α a number between
0 and 1. The 100(1 − α)-th percentile of X is xα such that

F (xα ) = P (X ≤ xα ) = 1 − α

• The 50th percentile is the median, µ̃X .


• The 25th percentile is the lower quartile, Q1 .
• The 75th percentile is the upper quartile, Q3 .
• The interquartile range, IQR, is Q3 − Q1 .

Example: Let X be a continuous random variable with PDF


(
e−x , x > 0
f (x) =
0, elsewhere

Find the IQR.

11
Models for Discrete Random Variables
Recall: A discrete random variable is a random variable whose sample space
has a finite or at most a countably infinite number of values.
• We can group some discrete random variables into classes.

Bernoulli Distribution
• A Bernoulli trial or experiment is one whose outcome is either a success
or a failure.

• A Bernoulli random variable X takes the value 1 if the outcome is a


success and 0 if the outcome is a failure.

• The probability of success is p.

• Notation: X ∼ Bern(p).

The PMF:
x 0 1
p(x) 1 − p p
What is the expected value of X?

What is the variance of X?

12
Example: The probability that an electronic product will last more that 5000
hours is 0.05. Let X take the value 1 if a randomly selected product lasts
more than 5000 hours and the value 0 otherwise. Find the mean value and
variance of X.

Binomial Distribution
• A binomial experiment is when n Bernoulli experiments, each having
probability of success p, are performed independently.
• The binomial random variable Y is the number of successes in the n
Bernoulli trials.

• The parameters of a binomial random variable are n and p.


• Notation: Y ∼ Bin(n, p).
The PMF:
 
n y
p(y) = P (Y = y) = p (1 − p)n−y , y = 0, 1, . . . , n
y

R will compute the PMF, P (Y = y): dbinom(y,n,p)

R will also compute the CDF, P (Y ≤ y): pbinom(y,n,p)

13
Example: Suppose 70% of all purchases in a certain store are made with a
credit card. Let Y denote the number of credit card uses in the next 10
purchases. What is P (5 ≤ Y ≤ 8)?

> dbinom(5,10,0.7) + dbinom(6,10,0.7) + dbinom(7,10,0.7) + dbinom(8,10,0.7)


[1] 0.8033427

> pbinom(8,10,0.7) - pbinom(4,10,0.7)


[1] 0.8033427

The expected value and variance of Y ∼ Bin(n, p) are

E(Y ) = np σY2 = np(1 − p)

Example: What is the expected number of credit card purchases? What is


the variance?

Note: When n = 1, a binomial random variable is a Bernoulli random vari-


able.

Hypergeometric Distribution
• Suppose a population consists of M1 objects labeled 1 and M2 objects
labeled 0, and that a sample of size n is selected at random without
replacement.
• The hypergeometric random variable X is the number of objects labeled
1 in the sample.
• Notation: X ∼ Hyp(M1 , M2 , n)

14
The PMF:
M1 M2
 
x n−x
p(x) = P (X = x) = M1 +M2

n

The sample space of X:


Sx = {max(0, n − M2 ), . . . , min(n, M1 )}
• R will compute the PMF, P (X = x): dhyper(x,M1 ,M2 ,n)
• R will also compute the CDF, P (X ≤ x): phyper(x,M1 ,M2 ,n)

Example: A crate contains 50 light bulbs of which 5 are defective and 45 are
not. A quality control inspector randomly samples 4 bulbs without replace-
ment. Let X be the number of defective bulbs in the sample.

Find the probability that less than 2 bulbs are defective.

> dhyper(0,5,45,4) + dhyper(1,5,45,4)


[1] 0.9550369
> phyper(1,5,45,4)
[1] 0.9550369

The expected value and variance of X ∼ Hyp(M1 , M2 , n) are


  
nM1 2 nM 1 M 1 N − n
E(X) = σX = 1−
N N N N −1
where N = M1 + M2 .

15
Example: What is the expected number of defective light bulbs in the sam-
ple? What is the variance?

Example: A forest contains 30 elk of which 10 are captured, tagged, and


released. A certain time later, five of the 30 elk are captured. Find the prob-
ability that two of the five captured elk are tagged.

Note: The hypergeometric distribution can sometimes be approximated us-


ing the binomial distribution.

• For large population size N , the difference between sampling with and
without replacement is very small.
• Suppose X ∼ Hyp(M1 , M2 , n) and n
N ≤ 0.05 where N = M1 + M2 . Then
P (X = x) ≈ P (Y = x)
M1
where Y ∼ Bin(n, p = N ).
Example: Suppose M1 = 100, M2 = 900, and n = 25. Find P (X = 3).
i. Using the hypergeometric distribution:

> dhyper(3,100,900,25)
[1] 0.229574

16
ii. Using the binomial approximation:

> dbinom(3,25,0.1)
[1] 0.2264973

Geometric Distribution
• In a geometric experiment, independent Bernoulli trials, each with prob-
ability of success p, are performed until the first success occurs.
• The geometric random variable X is the number of trials up to and in-
cluding the first success.

• Notation: X ∼ Geo(p)
The PMF:

p(x) = P (X = x) = (1 − p)x−1 p, x = 1, 2, 3, . . .

The CDF:

F (x) = P (X ≤ x) = 1 − (1 − p)x , x = 1, 2, 3, . . .

The expected value and variance of X ∼ Geo(p) are


1 2 1−p
E(X) = σX =
p p2

17
Example: Suppose you need to find a store that carries a special printer ink.
You know that of the stores that carry printer ink, 15% of them carry the
special ink. You randomly call each store until one has the ink you need.
a. What is the probability that you first find the special ink at the third
store you call?

b. What is the probability you first find the special ink in 3 calls or less?

c. What is the expected number of calls until you first find the special ink?
What is the variance?

Negative Binomial Distribution


• In a negative binomial experiment, independent Bernoulli trials, each
with probability of success p, are performed until the rth success occurs.
• The negative binomial random variable Y is the total number of trials
up to and including the rth success.
• Notation: Y ∼ NB(r, p)
• The sample space of Y is SY = {r, r + 1, r + 2, . . .}
The PMF:  
y−1 r
p(y) = P (Y = y) = p (1 − p)y−r
r−1

18
• R will compute the PMF, P (Y = y): dnbinom(y − r,r,p)
• R will also compute the CDF, P (Y ≤ y): pnbinom(y − r,r,p)
Example: An oil company conducts a geological study that indicates that an
exploratory oil well should have a 20% chance of striking oil.
a. What is the probability the first strike comes on the third well drilled?

b. What is the probability that the third strike comes on the seventh well
drilled?

> dnbinom(4,3,0.2)
[1] 0.049152

The expected value and variance of Y ∼ NB(r, p) are


r r(1 − p)
E(Y ) = σY2 =
p p2
Example: What is the expected number of wells to be drilled until striking
oil for the third time?

Poisson Distribution
• The Poisson distribution is used to model the probability that a number
of events occur in an interval of time or space.
• The Poisson random variable X denotes the number of events that oc-
curred.
• Notation: X ∼ Poisson(λ)

19
Examples:
i. Let X equal the number of cars passing through an intersection in one
minute.
ii. Let X equal the number of students arriving during office hours.
iii. Let X equal the number of Alaskan salmon caught in a squid driftnet.

The PMF:
e−λ λx
p(x) = P (X = x) = , x = 0, 1, 2, . . . , λ>0
x!
The mean and variance for X ∼ Poisson(λ) are
2
E(X) = λ σX =λ
Example: Let X equal the number of typos on a printed page with a mean
of 3 typos per page.
a. What is the probability of one typo on a randomly selected page?

b. What is the probability of at least one typo on a randomly selected page?

• R will compute the PMF, P (X = x): dpois(x,λ)


• R will also compute the CDF, P (X ≤ x): ppois(x,λ)
• R will provide a sample of n Poisson random variables: rpois(n,λ)

Example: Suppose that a person taking Vitamin C supplements contracts an


average of three colds per year, and that this average increases to five colds
per year for persons not taking Vitamin C supplements. Suppose further
that the number of colds a person contracts in a year is a Poisson random
variable.

20
a. Find the probability of no more than two colds for a person taking sup-
plements and a person not taking supplements.

> ppois(2,3)
[1] 0.4231901
> ppois(2,5)
[1] 0.124652

b. Suppose 70% of the population takes Vitamin C supplements. Find the


probability that a randomly selected person will have no more than two
colds in a given year.

Models for Continuous Random Variables


Recall: A continuous random variable can take any value within a finite or
infinite interval of the real line.

Exponential Distribution
• The exponential distribution is often used to model lifetimes of equip-
ment or waiting times until events occur.
• Notation: X ∼ Exp(λ)

21
The PDF: (
λe−λx , x ≥ 0
f (x) =
0, x<0

What is the CDF of X ∼ Exp(λ)?

The expected value and variance of X ∼ Exp(λ) are


1 2 1
E(X) = σX =
λ λ2

22
Example: Let X be the amount of time (in minutes) a postal clerk spends
with his or her customer. The time spent has an exponential distribution
with λ = 0.25.
a. Find the expected time spent with each customer.

b. What is the probability the clerk spends between 2 and 4 minutes with
a customer?

An exponential random variable X has the memoryless property:

P (X > s + t | X > s) = P (X > t)

Example: The number of miles that a particular car can run before its bat-
tery wears out is exponentially distributed with an average of 10,000 miles.
The owner of the car needs to take a 5000-mile trip. What is the probability
that he will be able to complete the trip without having to replace the car
battery?

23
Normal Distribution
• A standard normal random variable Z has PDF and CDF:
Z z
1 −z 2 /2
φ(z) = √ e and Φ(z) = φ(y)dy
2π −∞
for −∞ < z < ∞.

• A random variable X has a normal distribution with parameters µ and


σ 2 if the PDF and CDF are
−(x − µ)2
   
1 x−µ 1
f (x) = φ =√ exp
σ σ 2πσ 2 2σ 2
 
x−µ
F (x) = Φ
σ
for −∞ < x < ∞.
• Notation: X ∼ N (µ, σ 2 )
• The mean and variance of X ∼ N (µ, σ 2 ) are
E(X) = µ and Var(X) = σ 2
• Z is a normal random variable with µ = 0 and σ 2 = 1.

24
R commands for X ∼ N (µ, σ 2 ):

• the PDF, f (x): dnorm(x,µ,σ)


• the CDF, F (x) = P (X ≤ x): pnorm(x,µ,σ)
• the s100th percentile: qnorm(s,µ,σ)
• a random sample of size n: rnorm(n,µ,σ)
Examples: Let X ∼ N (−2, 6).
i. Find P (X ≤ 1)
ii. Find P (X > 2)

iii. Find P (−3 < X < 0)

iv. Find the 85th percentile of X.

v. Simulate a random sample of size 5 from the distribution of X.

> pnorm(1, -2, sqrt(6))


[1] 0.8896643
> 1-pnorm(2, -2, sqrt(6))
[1] 0.05123522
> pnorm(0, -2, sqrt(6)) - pnorm(-3, -2, sqrt(6))
[1] 0.4513462
> qnorm(0.85, -2, sqrt(6))
[1] 0.538733
> rnorm(5, -2, sqrt(6))
[1] 3.036662 -5.768844 -1.020020 5.592904 -2.904931

25
Properties of Normal Random Variables

Suppose X ∼ N (µ, σ 2 ). Then


i. a + bX ∼ N (a + bµ, b2 σ 2 )

X−µ
ii. σ ∼ N (0, 1)

iii. Let xα denote the (1 − α)-100th percentile of X, and let zα denote the
(1 − α)-100th percentile of Z. Then

xα = µ + σzα

Example: A machine manufactures tires with an initial tread thickness that


is normally distributed with mean 10 mm and standard deviation 2 mm. The
tire has a 50,000-mile warranty. In order to last for 50,000 miles the tread
thickness must initially be at least 7.9 mm. If the initial thickness of tread
is measured to be less than 7.9 mm, then the tire is sold as an alternative
brand with a warranty of less than 50,000 miles.
a. Find the proportion of tires sold under the alternative brand.

26
b. The demand for the alternative brand of tires is such that 30% of the
total output should be sold under the alternative brand name. What
should the critical thickness, originally 7.9 mm, be set at in order to
meet the demand?

Q-Q Plots

How do we know when to use the normal distribution to model the dis-
tribution of data from a random sample?
• Plot the sample percentiles against the percentiles from the normal dis-
tribution.
• If the normal distribution is a good approximation, the plotted points
should fall approximately on a straight line.

Example: The vector x in R contains data for which the normal distribution
is a good approximation.
> qqnorm(x)
> qqline(x)

27
Example: The vector y in R contains data for which the normal distribution
is not a good approximation.
> qqnorm(y)
> qqline(y)

28

You might also like