Docsity

04B - Probability Notes - G, Study notes of Computer science

Toggle sidebar
bg1
Chapter 20
Random Variables
So far we focused on probabilities of events —that you win the Monty Hall game;
that you have a rare medical condition, given that you tested positive; . . . . Now we
focus on quantitative questions: How many contestants must play the Monty Hall
game until one of them finally wins? . . . How long will this condition last? How
much will I lose playing 6.042 games all day? Random variables are the mathemat-
ical tool for addressing such questions.
20.1 Random Variable Examples
Definition 20.1.1. A random variable, R, on a probability space is a total function
whose domain is the sample space.
The codomain of R can be anything, but will usually be a subset of the real
numbers. Notice that the name “random variable” is a misnomer; random vari-
ables are actually functions!
For example, suppose we toss three independent, unbiased coins. Let C be the
number of heads that appear. Let M = 1 if the three coins come up all heads or all
tails, and let M = 0 otherwise. Now every outcome of the three coin flips uniquely
determines the values of C and M. For example, if we flip heads, tails, heads, then
C = 2 and M = 0. If we flip tails, tails, tails, then C = 0 and M = 1. In effect, C
counts the number of heads, and M indicates whether all the coins match.
Since each outcome uniquely determines C and M, we can regard them as
functions mapping outcomes to numbers. For this experiment, the sample space
is:
S = {HH H, H HT , H T H, H T T, T HH , T HT , T T H, T T T } .
Now C is a function that maps each outcome in the sample space to a number as
459
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download 04B - Probability Notes - G and more Study notes Computer science in PDF only on Docsity!

Chapter 20

Random Variables

So far we focused on probabilities of events —that you win the Monty Hall game; that you have a rare medical condition, given that you tested positive;.... Now we focus on quantitative questions: How many contestants must play the Monty Hall game until one of them finally wins?... How long will this condition last? How much will I lose playing 6.042 games all day? Random variables are the mathemat ical tool for addressing such questions.

20.1 Random Variable Examples

Definition 20.1.1. A random variable, R, on a probability space is a total function whose domain is the sample space.

The codomain of R can be anything, but will usually be a subset of the real numbers. Notice that the name “random variable” is a misnomer; random vari ables are actually functions! For example, suppose we toss three independent, unbiased coins. Let C be the number of heads that appear. Let M = 1 if the three coins come up all heads or all tails, and let M = 0 otherwise. Now every outcome of the three coin flips uniquely determines the values of C and M. For example, if we flip heads, tails, heads, then C = 2 and M = 0. If we flip tails, tails, tails, then C = 0 and M = 1. In effect, C counts the number of heads, and M indicates whether all the coins match. Since each outcome uniquely determines C and M , we can regard them as functions mapping outcomes to numbers. For this experiment, the sample space is:

S = {HHH, HHT, HT H, HT T, T HH, T HT, T T H, T T T }.

Now C is a function that maps each outcome in the sample space to a number as

459

460 CHAPTER 20. RANDOM VARIABLES

follows: C(HHH) = 3 C(T HH) = 2 C(HHT ) = 2 C(T HT ) = 1 C(HT H) = 2 C(T T H) = 1 C(HT T ) = 1 C(T T T ) = 0.

Similarly, M is a function mapping each outcome another way:

M (HHH) = 1 M (T HH) = 0 M (HHT ) = 0 M (T HT ) = 0 M (HT H) = 0 M (T T H) = 0 M (HT T ) = 0 M (T T T ) = 1.

So C and M are random variables.

20.1.1 Indicator Random Variables

An indicator random variable is a random variable that maps every outcome to ei ther 0 or 1. These are also called Bernoulli variables. The random variable M is an example. If all three coins match, then M = 1; otherwise, M = 0. Indicator random variables are closely related to events. In particular, an in dicator partitions the sample space into those outcomes mapped to 1 and those outcomes mapped to 0. For example, the indicator M partitions the sample space into two blocks as follows:

HHH� �� T T T� HHT� HT H HT T (^) �� T HH T HT T T H�. M = 1 M = 0

In the same way, an event, E, partitions the sample space into those outcomes in E and those not in E. So E is naturally associated with an indicator random variable, IE , where IE (p) = 1 for outcomes p ∈ E and IE (p) = 0 for outcomes p /∈ E. Thus, M = IF where F is the event that all three coins match.

20.1.2 Random Variables and Events

There is a strong relationship between events and more general random variables as well. A random variable that takes on several values partitions the sample space into several blocks. For example, C partitions the sample space as follows:

T T T� �� � T T H� T HT�� HT T� T HH� HT H�� HHT� HHH .� �� � C = 0 C = 1 C = 2 C = 3

Each block is a subset of the sample space and is therefore an event. Thus, we can regard an equation or inequality involving a random variable as an event. For example, the event that C = 2 consists of the outcomes T HH, HT H, and HHT. The event C ≤ 1 consists of the outcomes T T T , T T H, T HT , and HT T.

462 CHAPTER 20. RANDOM VARIABLES

As with events, the notion of independence generalizes to more than two ran dom variables.

Definition 20.1.3. Random variables R 1 , R 2 ,... , Rn are mutually independent iff

Pr {R 1 = x 1 AND R 2 = x 2 AND · · ·AND Rn = xn} = Pr {R 1 = x 1 } · Pr {R 2 = x 2 } · · · Pr {Rn = xn}.

for all x 1 , x 2 ,... , xn.

It is a simple exercise to show that the probability that any subset of the variables takes a particular set of values is equal to the product of the probabilities that the individual variables take their values. Thus, for example, if R 1 , R 2 ,... , R 100 are mutually independent random variables, then it follows that:

Pr {R 1 = 7 AND R 7 = 9. 1 AND R 23 = π} = Pr {R 1 = 7}·Pr {R 7 = 9. 1 }·Pr {R 23 = π}.

20.2 Probability Distributions

A random variable maps outcomes to values, but random variables that show up for different spaces of outcomes wind up behaving in much the same way because they have the same probability of taking any given value. Namely, random vari ables on different probability spaces may wind up having the same probability density function.

Definition 20.2.1. Let R be a random variable with codomain V. The probability density function (pdf) of R is a function PDFR : V →[0, 1] defined by:

� PDFR(x) ::= Pr {R = x} 0

if x ∈ range (R) , if x ∈/ range (R).

A consequence of this definition is that � PDFR(x) = 1. x∈range(R)

This follows because R has a value for each outcome, so summing the probabilities over all outcomes is the same as summing over the probabilities of each value in the range of R. As an example, let’s return to the experiment of rolling two fair, independent dice. As before, let T be the total of the two rolls. This random variable takes on values in the set V = { 2 , 3 ,... , 12 }. A plot of the probability density function is shown below:

20.2. PROBABILITY DISTRIBUTIONS 463

3 / 36

PDFR(x)

x ∈ V

The lump in the middle indicates that sums close to 7 are the most likely. The total area of all the rectangles is 1 since the dice must take on exactly one of the sums in V = { 2 , 3 ,... , 12 }. A closely-related idea is the cumulative distribution function (cdf) for a random variable R whose codomain is real numbers. This is a function CDFR : R [0, 1] defined by:

CDFR(x) = Pr {R ≤ x}

As an example, the cumulative distribution function for the random variable T is shown below:

x ∈ V

The height of the i-th bar in the cumulative distribution function is equal to the sum of the heights of the leftmost i bars in the probability density function. This follows from the definitions of pdf and cdf:

CDFR(x) = Pr {R ≤ x} = Pr {R = y} y≤x = PDFR(y) y≤x

1 / 2

0

CDFR(x)

In summary, PDFR(x) measures the probability that R = x and CDFR(x) mea sures the probability that R ≤ x. Both the PDFR and CDFR capture the same

20.2. PROBABILITY DISTRIBUTIONS 465

So you might try to be more clever. Suppose you peek in the left envelope and see the number 12. Since 12 is a small number, you might guess that that other number is larger. But perhaps I’m sort of tricky and put small numbers in both envelopes. Then your guess might not be so good! An important point here is that the numbers in the envelopes may not be ran dom. I’m picking the numbers and I’m choosing them in a way that I think will defeat your guessing strategy. I’ll only use randomization to choose the numbers if that serves my end: making you lose!

Intuition Behind the Winning Strategy

Amazingly, there is a strategy that wins more than 50% of the time, regardless of what numbers I put in the envelopes! Suppose that you somehow knew a number x between my lower number and higher numbers. Now you peek in an envelope and see one or the other. If it is bigger than x, then you know you’re peeking at the higher number. If it is smaller than x, then you’re peeking at the lower number. In other words, if you know a number x between my lower and higher numbers, then you are certain to win the game. The only flaw with this brilliant strategy is that you do not know x. Oh well. But what if you try to guess x? There is some probability that you guess cor rectly. In this case, you win 100% of the time. On the other hand, if you guess incorrectly, then you’re no worse off than before; your chance of winning is still 50%. Combining these two cases, your overall chance of winning is better than 50%! Informal arguments about probability, like this one, often sound plausible, but do not hold up under close scrutiny. In contrast, this argument sounds completely implausible— but is actually correct!

Analysis of the Winning Strategy

For generality, suppose that I can choose numbers from the set { 0 , 1 ,... , n}. Call the lower number L and the higher number H. Your goal is to guess a number x between L and H. To avoid confusing equality cases, you select x at random from among the half-integers:

1 1 1 1 2

,... , n − 2

But what probability distribution should you use? The uniform distribution turns out to be your best bet. An informal justification is that if I figured out that you were unlikely to pick some number— say 50 12 — then I’d always put 50 and 51 in the evelopes. Then you’d be unlikely to pick an x between L and H and would have less chance of winning. After you’ve selected the number x, you peek into an envelope and see some number p. If p > x, then you guess that you’re looking at the larger number. If

466 CHAPTER 20. RANDOM VARIABLES

p < x, then you guess that the other number is larger. All that remains is to determine the probability that this strategy succeeds. We can do this with the usual four step method and a tree diagram. Step 1: Find the sample space. You either choose x too low (< L), too high (> H), or just right (L < x < H). Then you either peek at the lower number (p = L) or the higher number (p = H). This gives a total of six possible outcomes.

x just right

1/

1/

1/

1/

1/

1/

L/n

(H−L)/n

(n−H)/n

choice of x

# peeked at result probability

win

win x too high

x too low

win

lose

win

lose

L/2n

L/2n

(H−L)/2n

(H−L)/2n

(n−H)/2n

(n−H)/2n

p=H

p=L

p=H

p=L

p=H

p=L

Step 2: Define events of interest. The four outcomes in the event that you win are marked in the tree diagram. Step 3: Assign outcome probabilities. First, we assign edge probabilities. Your guess x is too low with probability L/n, too high with probability (n − H)/n, and just right with probability (H − L)/n. Next, you peek at either the lower or higher number with equal probability. Multiplying along root-to-leaf paths gives the out come probabilities. Step 4: Compute event probabilities. The probability of the event that you win is the sum of the probabilities of the four outcomes in that event:

Pr {win} =

L

H − L

H − L

n − H 2 n 2 n 2 n 2 n 1 H − L = + 2 2 n 1 1 ≥ 2

2 n

The final inequality relies on the fact that the higher number H is at least 1 greater than the lower number L since they are required to be distinct. Sure enough, you win with this strategy more than half the time, regardless of the numbers in the envelopes! For example, if I choose numbers in the range 0 , 1 ,... , 100 , then you win with probability at least 12 + 2001 = 50 .5%. Even better, if I’m allowed only numbers in the range 0 ,... , 10 , then your probability of winning rises to 55%! By Las Vegas standards, those are great odds!

468 CHAPTER 20. RANDOM VARIABLES

heads! That is, the probability of flipping exactly 25 heads —small as it is —is still nearly twice as large as the probability of flipping exactly 24 heads plus the probability of flipping exactly 23 heads plus... the probability of flipping no heads.

The General Binomial Distribution

Now let J be the number of heads that come up on n independent coins, each of which is heads with probability p. Then J has a general binomial density function:

PDFJ (k) =

n pk(1 − p)n−k^. k

As before, there are nk^ sequences with k heads and n − k tails, but now the prob ability of each such sequence is pk(1 − p)n−k^. As an example, the plot below shows the probability density function PDFJ (k) corresponding to flipping n = 20 independent coins that are heads with probabilty p = 0. 75. The graph shows that we are most likely to get around k = 15 heads, as you might expect. Once again, the probability falls off quickly for larger and smaller values of k.

20.2. PROBABILITY DISTRIBUTIONS 469

20.2.5 Problems

Class Problems

Guess the Bigger Number Game

Team 1:

  • Write different integers between 0 and 7 on two pieces of paper.
  • Put the papers face down on a table.

Team 2:

  • Turn over one paper and look at the number on it.
  • Either stick with this number or switch to the unseen other number.

Team 2 wins if it chooses the larger number.

Problem 20.1. In section 20.2.3, Team 2 was shown to have a strategy that wins 4/7 of the time no matter how Team 1 plays. Can Team 2 do better? The answer is “no,” because Team 1 has a strategy that guarantees that it wins at least 3/7 of the time, no matter how Team 2 plays. Describe such a strategy for Team 1 and explain why it works.

Problem 20.2. Suppose X 1 , X 2 , and X 3 are three mutually independent random variables, each having the uniform distribution

Pr {Xi = k} equal to 1 / 3 for each of k = 1, 2 , 3.

Let M be another random variable giving the maximum of these three random variables. What is the density function of M?

Homework Problems

Problem 20.3. A drunken sailor wanders along main street, which conveniently consists of the points along the x axis with integral coordinates. In each step, the sailor moves one unit left or right along the x axis. A particular path taken by the sailor can be

20.3. AVERAGE & EXPECTED VALUE 471

20.3 Average & Expected Value

The expectation of a random variable is its average value, where each value is weighted according to the probability that it comes up. The expectation is also called the expected value or the mean of the random variable. For example, suppose we select a student uniformly at random from the class, and let R be the student’s quiz score. Then E [R] is just the class average —the first thing everyone wants to know after getting their test back! For similar reasons, the first thing you usually want to know about a random variable is its expected value.

Definition 20.3.1.

E [R] ::= x ·Pr {R = x} (20.1) x∈range(R) = x ·PDFR(x). x∈range(R)

Let’s work through an example. Let R be the number that comes up on a fair, six-sided die. Then by (20.1), the expected value of R is:

�^6

E [R] = k · 6 k= 1 1 1 1 1 1 = 1 · + 2 + 3 + 4 + 5 + 6 6

This calculation shows that the name “expected value” is a little misleading; the random variable might never actually take on that value. You don’t ever expect to roll a 3 12 on an ordinary die! There is an even simpler formula for expectation:

Theorem 20.3.2. If R is a random variable defined on a sample space, S, then

E [R] = R(ω) Pr {ω} (20.2) ω∈S

The proof of Theorem 20.3.2, like many of the elementary proofs about expec tation in this chapter, follows by judicious regrouping of terms in the defining sum (20.1):

472 CHAPTER 20. RANDOM VARIABLES

Proof.

E [R] ::= x ·Pr {R = x} (Def 20.3.1 of expectation) x∈range(R) ⎛ ⎞

= x ⎝^ Pr {ω}⎠^ (def of Pr {R = x}) x∈range(R) ω∈[R=x] = x Pr {ω} (distributing x over the inner sum) x∈range(R) ω∈[R=x] = R(ω) Pr {ω} (def of the event [R = x]) x∈range(R) ω∈[R=x] = R(ω) Pr {ω} ω∈S

The last equality follows because the events [R = x] for x ∈ range (R) partition the sample space, S, so summing over the outcomes in [R = x] for x ∈ range (R) is the same as summing over S. �

In general, the defining sum (20.1) is better for calculating expected values and has the advantage that it does not depend on the sample space, but only on the density function of the random variable. On the other hand, the simpler sum over all outcomes (20.2)is sometimes easier to use in proofs about expectation.

20.3.1 Expected Value of an Indicator Variable

The expected value of an indicator random variable for an event is just the proba bility of that event.

Lemma 20.3.3. If IA is the indicator random variable for event A, then

E [IA] = Pr {A}.

Proof.

E [IA] = 1 · Pr {IA = 1} + 0 ·Pr {IA = 0} = Pr {IA = 1} = Pr {A}. (def of IA)

For example, if A is the event that a coin with bias p comes up heads, E [IA] = Pr {IA = 1} = p.

474 CHAPTER 20. RANDOM VARIABLES

Proof.

E [R] ::= r ·Pr {R = r} (Def 20.3.1 of expectation) r∈range(R) = r · Pr {R = r | Ai} Pr {Ai} (Law of Total Probability) r i = r · Pr {R = r | Ai} Pr {Ai} (distribute constant r) r i = r · Pr {R = r | Ai} Pr {Ai} (exchange order of summation) i r = Pr {Ai} r · Pr {R = r | Ai} (factor constant Pr {Ai}) i r = Pr {Ai} E [R | Ai]. (Def 20.3.4 of cond. expectation) i

20.3.3 Mean Time to Failure

A computer program crashes at the end of each hour of use with probability p, if it has not crashed already. What is the expected time until the program crashes? If we let C be the number of hours until the crash, then the answer to our problem is E [C]. Now the probability that, for i > 0 , the first crash occurs in the ith hour is the probability that it does not crash in each of the first i − 1 hours and it does crash in the ith hour, which is (1 − p)i−^1 p. So from formula (20.1) for expectation, we have

E [C] = i ·Pr {R = i} i∈N = i(1 − p)i−^1 p i∈N+ = p i(1 − p)i−^1 i∈N+ 1 = p (by (17.1)) (1 − (1 − p))^2 1 = p

A simple alternative derivation that does not depend on the formula (17.1) (which you remembered, right?) is based on conditional expectation. Given that the computer crashes in the first hour, the expected number of hours to the first crash is obviously 1! On the other hand, given that the computer does not crash in the first hour, then the expected total number of hours till the first crash is the

20.3. AVERAGE & EXPECTED VALUE 475

expectation of one plus the number of additional hours to the first crash. So,

E [C] = p · 1 + (1 − p) E [C + 1] = p + E [C] − p E [C] + 1 − p,

from which we immediately calculate that E [C] = 1/p. So, for example, if there is a 1% chance that the program crashes at the end of each hour, then the expected time until the program crashes is 1 / 0. 01 = 100 hours. As a further example, suppose a couple really wants to have a baby girl. For simplicity assume there is a 50% chance that each child they have is a girl, and the genders of their children are mutually independent. If the couple insists on having children until they get a girl, then how many baby boys should they expect first? This is really a variant of the previous problem. The question, “How many hours until the program crashes?” is mathematically the same as the question, “How many children must the couple have until they get a girl?” In this case, a crash corresponds to having a girl, so we should set p = 1/ 2. By the preceding analysis, the couple should expect a baby girl after having 1 /p = 2 children. Since the last of these will be the girl, they should expect just one boy. Something to think about: If every couple follows the strategy of having chil dren until they get a girl, what will eventually happen to the fraction of girls born in this world?

20.3.4 Linearity of Expectation

Expected values obey a simple, very helpful rule called Linearity of Expectation. Its simplest form says that the expected value of a sum of random variables is the sum of the expected values of the variables.

Theorem 20.3.6. For any random variables R 1 and R 2 ,

E [R 1 + R 2 ] = E [R 1 ] + E [R 2 ].

Proof. Let T ::= R 1 + R 2. The proof follows straightforwardly by rearranging terms in the sum (20.2)

� E [T ] = ω∈S

T (ω) · Pr {ω} �

(Theorem 20.3.2)

ω∈S

(R 1 (ω) + R 2 (ω)) · Pr {ω} � �

(def of T )

ω∈S

R 1 (ω) Pr {ω} + ω∈S

R 2 (ω) Pr {ω} (rearranging terms)

= E [R 1 ] + E [R 2 ]. (Theorem 20.3.2)

�

A small extension of this proof, which we leave to the reader, implies

20.3. AVERAGE & EXPECTED VALUE 477

gets his own hat, and Gi = 0 otherwise. The number of men that get their own hat is the sum of these indicators:

G = G 1 + G 2 + · · ·+ Gn. (20.4)

These indicator variables are not mutually independent. For example, if n − 1 men all get their own hats, then the last man is certain to receive his own hat. But, since we plan to use linearity of expectation, we don’t have worry about independence! Now since Gi is an indicator, we know 1 /n = Pr {Gi = 1} = E [Gi] by Lemma 20.3.3. Now we can take the expected value of both sides of equation (20.4) and apply lin earity of expectation:

E [G] = E [G 1 + G 2 + · · ·+ Gn] = E [G 1 ] + E [G 2 ] + · · · + E [Gn] � � 1 1 1 1 = + + + = n = 1. n n

n n

So even though we don’t know much about how hats are scrambled, we’ve figured out that on average, just one man gets his own hat back!

Expectation of a Binomial Distribution

Suppose that we independently flip n biased coins, each with probability p of com ing up heads. What is the expected number that come up heads? Let J be the number of heads after the flips, so J has the (n, p)-binomial dis tribution. Now let Ik be the indicator for the kth coin coming up heads. By Lemma 20.3.3, we have E [Ik] = p.

But (^) n

J = Ik, k=

so by linearity (^) � � n n n E [J] = E Ik = E [Ik] = p = pn. k=1 k=1 k=

In short, the expectation of an (n, p)-binomially distributed variable is pn.

The Coupon Collector Problem

Every time I purchase a kid’s meal at Taco Bell, I am graciously presented with a miniature “Racin’ Rocket” car together with a launching device which enables me to project my new vehicle across any tabletop or smooth floor at high velocity. Truly, my delight knows no bounds. There are n different types of Racin’ Rocket car (blue, green, red, gray, etc.). The type of car awarded to me each day by the kind woman at the Taco Bell register

478 CHAPTER 20. RANDOM VARIABLES

appears to be selected uniformly and independently at random. What is the ex pected number of kid’s meals that I must purchase in order to acquire at least one of each type of Racin’ Rocket car? The same mathematical question shows up in many guises: for example, what is the expected number of people you must poll in order to find at least one person with each possible birthday? Here, instead of collecting Racin’ Rocket cars, you’re collecting birthdays. The general question is commonly called the coupon collector problem after yet another interpretation. A clever application of linearity of expectation leads to a simple solution to the coupon collector problem. Suppose there are five different types of Racin’ Rocket, and I receive this sequence:

blue green green red blue orange blue orange gray

Let’s partition the sequence into 5 segments:

blue���� green green red blue orange blue orange gray � �� � � �� � � �� � � �� � X (^0) X 1 X 2 X 3 X 4

The rule is that a segment ends whenever I get a new kind of car. For example, the middle segment ends when I get a red car for the first time. In this way, we can break the problem of collecting every type of car into stages. Then we can analyze each stage individually and assemble the results using linearity of expectation. Let’s return to the general case where I’m collecting n Racin’ Rockets. Let Xk be the length of the kth segment. The total number of kid’s meals I must purchase to get all n Racin’ Rockets is the sum of the lengths of all these segments:

T = X 0 + X 1 + · · ·+ Xn− 1

Now let’s focus our attention on Xk, the length of the kth segment. At the beginning of segment k, I have k different types of car, and the segment ends when I acquire a new type. When I own k types, each kid’s meal contains a type that I already have with probability k/n. Therefore, each meal contains a new type of car with probability 1 − k/n = (n − k)/n. Thus, the expected number of meals until I get a new kind of car is n/(n − k) by the “mean time to failure” formula. So we have:

n E [Xk] = n − k

Linearity of expectation, together with this observation, solves the coupon col