随机算法 (Spring 2013)/Random Variables and Expectations

From TCS Wiki
Revision as of 10:33, 11 March 2013 by imported>Etone (Random Variable)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Random Variable

Definition (random variable)
A random variable X on a sample space Ω is a real-valued function X:ΩR. A random variable X is called a discrete random variable if its range is finite or countably infinite.

For a random variable X and a real value xR, we write "X=x" for the event {aΩX(a)=x}, and denote the probability of the event by

Pr[X=x]=Pr({aΩX(a)=x}).

The independence can also be defined for variables:

Definition (Independent variables)
Two random variables X and Y are independent if and only if
Pr[(X=x)(Y=y)]=Pr[X=x]Pr[Y=y]
for all values x and y. Random variables X1,X2,,Xn are mutually independent if and only if, for any subset I{1,2,,n} and any values xi, where iI,
Pr[iI(Xi=xi)]=iIPr[Xi=xi].

Note that in probability theory, the "mutual independence" is not equivalent with "pair-wise independence", which we will learn in the future.

Expectation

Let X be a discrete random variable. The expectation of X is defined as follows.

Definition (Expectation)
The expectation of a discrete random variable X, denoted by E[X], is given by
E[X]=xxPr[X=x],
where the summation is over all values x in the range of X.

Linearity of Expectation

Perhaps the most useful property of expectation is its linearity.

Theorem (Linearity of Expectations)
For any discrete random variables X1,X2,,Xn, and any real constants a1,a2,,an,
E[i=1naiXi]=i=1naiE[Xi].
Proof.
By the definition of the expectations, it is easy to verify that (try to prove by yourself):

for any discrete random variables X and Y, and any real constant c,

  • E[X+Y]=E[X]+E[Y];
  • E[cX]=cE[X].

The theorem follows by induction.

The linearity of expectation gives an easy way to compute the expectation of a random variable if the variable can be written as a sum.

Example
Supposed that we have a biased coin that the probability of HEADs is p. Flipping the coin for n times, what is the expectation of number of HEADs?
It looks straightforward that it must be np, but how can we prove it? Surely we can apply the definition of expectation to compute the expectation with brute force. A more convenient way is by the linearity of expectations: Let Xi indicate whether the i-th flip is HEADs. Then E[Xi]=1p+0(1p)=p, and the total number of HEADs after n flips is X=i=1nXi. Applying the linearity of expectation, the expected number of HEADs is:
E[X]=E[i=1nXi]=i=1nE[Xi]=np.

The real power of the linearity of expectations is that it does not require the random variables to be independent, thus can be applied to any set of random variables. For example:

E[αX+βX2+γX3]=αE[X]+βE[X2]+γE[X3].

However, do not exaggerate this power!

  • For an arbitrary function f (not necessarily linear), the equation E[f(X)]=f(E[X]) does not hold generally.
  • For variances, the equation var(X+Y)=var(X)+var(Y) does not hold without further assumption of the independence of X and Y.

Conditional Expectation

Conditional expectation can be accordingly defined:

Definition (conditional expectation)
For random variables X and Y,
E[XY=y]=xxPr[X=xY=y],
where the summation is taken over the range of X.

There is also a law of total expectation.

Theorem (law of total expectation)
Let X and Y be two random variables. Then
E[X]=yE[XY=y]Pr[Y=y].

Random Quicksort

Given as input a set S of n numbers, we want to sort the numbers in S in increasing order. One of the most famous algorithm for this problem is the Quicksort algorithm.

  • if |S|>1 do:
    • pick an xS as the pivot;
    • partition S into S1, {x}, and S2, where all numbers in S1 are smaller than x and all numbers in S2 are larger than x;
    • recursively sort S1 and S2;

The time complexity of this sorting algorithm is measured by the number of comparisons.

For the deterministic quicksort algorithm, the pivot is picked from a fixed position (e.g. the first number in the array). The worst-case time complexity in terms of number of comparisons is Θ(n2).

We consider the following randomized version of the quicksort.

  • if |S|>1 do:
    • uniformly pick a random xS as the pivot;
    • partition S into S1, {x}, and S2, where all numbers in S1 are smaller than x and all numbers in S2 are larger than x;
    • recursively sort S1 and S2;

Analysis of Random Quicksort

Our goal is to analyze the expected number of comparisons during an execution of RandQSort with an arbitrary input S. We achieve this by measuring the chance that each pair of elements are compared, and summing all of them up due to Linearity of Expectation.

Let ai denote the ith smallest element in S. Let Xij{0,1} be the random variable which indicates whether ai and aj are compared during the execution of RandQSort. That is:

Xij={1ai and aj are compared0otherwise.

Elements ai and aj are compared only if one of them is chosen as pivot. After comparison they are separated (thus are never compared again). So we have the following observations:

Observation 1: Every pair of ai and aj are compared at most once.

Therefore the sum of Xij for all pair {i,j} gives the total number of comparisons. The expected number of comparisons is E[i=1nj>iXij]. Due to Linearity of Expectation, E[i=1nj>iXij]=i=1nj>iE[Xij]. Our next step is to analyze E[Xij] for each {i,j}.

By the definition of expectation and Xij,

E[Xij]=1Pr[ai and aj are compared]+0Pr[ai and aj are not compared]=Pr[ai and aj are compared].

We are going to bound this probability.

Observation 2: ai and aj are compared if and only if one of them is chosen as pivot when they are still in the same subset.

This is easy to verify: just check the algorithm. The next one is a bit complicated.

Observation 3: If ai and aj are still in the same subset then all {ai,ai+1,,aj1,aj} are in the same subset.

We can verify this by induction. Initially, S itself has the property described above; and partitioning any S with the property into S1 and S2 will preserve the property for both S1 and S2. Therefore Claim 3 holds.

Combining Observation 2 and 3, we have:

Observation 4: ai and aj are compared only if one of {ai,aj} is chosen from {ai,ai+1,,aj1,aj}.

And,

Observation 5: Every one of {ai,ai+1,,aj1,aj} is chosen equal-probably.

This is because the Random Quicksort chooses the pivot uniformly at random.

Observation 4 and 5 together imply:

Pr[ai and aj are compared]2ji+1.
Remark: Perhaps you feel confused about the above argument. You may ask: "The algorithm chooses pivots for many times during the execution. Why in the above argument, it looks like the pivot is chosen only once?" Good question! Let's see what really happens by looking closely.

For any pair ai and aj, initially {ai,ai+1,,aj1,aj} are all in the same set S (obviously!). During the execution of the algorithm, the set which containing {ai,ai+1,,aj1,aj} are shrinking (due to the pivoting), until one of {ai,ai+1,,aj1,aj} is chosen, and the set is partitioned into different subsets. We ask for the probability that the chosen one is among {ai,aj}. So we really care about "the last" pivoting before {ai,ai+1,,aj1,aj} is split.

Formally, let Y be the random variable denoting the pivot element. We know that for each ak{ai,ai+1,,aj1,aj}, Y=ak with the same probability, and Y{ai,ai+1,,aj1,aj} with an unknown probability (remember that there might be other elements in the same subset with {ai,ai+1,,aj1,aj}). The probability we are looking for is actually Pr[Y{ai,aj}Y{ai,ai+1,,aj1,aj}], which is always 2ji+1, provided that Y is uniform over {ai,ai+1,,aj1,aj}.

The conditional probability rules out the irrelevant events in a probabilistic argument.

Summing all up:

E[i=1nj>iXij]=i=1nj>iE[Xij]i=1nj>i2ji+1=i=1nk=2ni+12k(Let k=ji+1)i=1nk=1n2k=2nk=1n1k=2nH(n).

H(n) is the nth Harmonic number. It holds that

H(n)=lnn+O(1).

Therefore, for an arbitrary input S of n numbers, the expected number of comparisons taken by RandQSort to sort S is O(nlogn).


Distributions of Coin Flips

We introduce several important distributions induced by independent coin flips (independent probabilistic experiments), including: Bernoulli trial, geometric distribution, binomial distribution.

Bernoulli trial (Bernoulli distribution)

Bernoulli trial describes the probability distribution of a single (biased) coin flip. Suppose that we flip a (biased) coin where the probability of HEADS is p. Let X be the 0-1 random variable which indicates whether the result is HEADS. We say that X follows the Bernoulli distribution with parameter p. Formally,

X={1with probability p0with probability 1p.

Geometric distribution

Suppose we flip the same coin repeatedly until HEADS appears, where each coin flip is independent and follows the Bernoulli distribution with parameter p. Let X be the random variable denoting the total number of coin flips. Then X has the geometric distribution with parameter p. Formally, Pr[X=k]=(1p)k1p.

For geometric X, E[X]=1p. This can be verified by directly computing E[X] by the definition of expectations. There is also a smarter way of computing E[X], by using indicators and the linearity of expectations. For k=0,1,2,, let Yk be the 0-1 random variable such that Yk=1 if and only if none of the first k coin flipings are HEADS, thus E[Yk]=Pr[Yk=1]=(1p)k. A key observation is that X=k=0Yk. Thus, due to the linearity of expectations,

E[X]=E[k=0Yk]=k=0E[Yk]=k=0(1p)k=11(1p)=1p.

Binomial distribution

Suppose we flip the same (biased) coin for n times, where each coin flip is independent and follows the Bernoulli distribution with parameter p. Let X be the number of HEADS. Then X has the binomial distribution with parameters n and p. Formally, Pr[X=k]=(nk)pk(1p)nk.

A binomial random variable X with parameters n and p is usually denoted by B(n,p).

As we saw above, by applying the linearity of expectations, it is easy to show that E[X]=np for an X=B(n,p).

Balls into Bins

Birthday Problem

There are m students in the class. Assume that for each student, his/her birthday is uniformly and independently distributed over the 365 days in a years. We wonder what the probability that no two students share a birthday.

Due to the pigeonhole principle, it is obvious that for m>365, there must be two students with the same birthday. Surprisingly, for any m>57 this event occurs with more than 99% probability. This is called the birthday paradox. Despite the name, the birthday paradox is not a real paradox.

We can model this problem as a balls-into-bins problem. m different balls (students) are uniformly and independently thrown into 365 bins (days). More generally, let n be the number of bins. We ask for the probability of the following event E

  • E: there is no bin with more than one balls (i.e. no two students share birthday).

We first analyze this by counting. There are totally nm ways of assigning m balls to n bins. The number of assignments that no two balls share a bin is (nm)m!.

Thus the probability is given by:

Pr[E]=(nm)m!nm.

Recall that (nm)=n!(nm)!m!. Then

Pr[E]=(nm)m!nm=n!nm(nm)!=nnn1nn2nn(m1)n=k=1m1(1kn).

There is also a more "probabilistic" argument for the above equation. To be rigorous, we need the following theorem, which holds generally and is very useful for computing the AND of many events.

By the definition of conditional probability, Pr[AB]=Pr[AB]Pr[B]. Thus, Pr[AB]=Pr[B]Pr[AB]. This hints us that we can compute the probability of the AND of events by conditional probabilities. Formally, we have the following theorem:

Theorem:

Let E1,E2,,En be any n events. Then
Pr[i=1nEi]=k=1nPr[Eki<kEi].

Proof: It holds that Pr[AB]=Pr[B]Pr[AB]. Thus, let A=En and B=E1E2En1, then

Pr[E1E2En]=Pr[E1E2En1]Pr[Eni<nEi].

Recursively applying this equation to Pr[E1E2En1] until there is only E1 left, the theorem is proved.

Now we are back to the probabilistic analysis of the birthday problem, with a general setting of m students and n possible birthdays (imagine that we live in a planet where a year has n days).

The first student has a birthday (of course!). The probability that the second student has a different birthday is (11n). Given that the first two students have different birthdays, the probability that the third student has a different birthday from the first two is (12n). Continuing this on, assuming that the first k1 students all have different birthdays, the probability that the kth student has a different birthday than the first k1, is given by (1k1n). So the probability that all m students have different birthdays is the product of all these conditional probabilities:

Pr[E]=(11n)(12n)(1m1n)=k=1m1(1kn),

which is the same as what we got by the counting argument.

There are several ways of analyzing this formular. Here is a convenient one: Due to Taylor's expansion, ek/n1k/n. Then

k=1m1(1kn)k=1m1ekn=exp(k=1m1kn)=em(m1)/2nem2/2n.

The quality of this approximation is shown in the Figure.

Therefore, for m=2nln1ϵ, the probability that Pr[E]ϵ.

Coupon Collector

Suppose that a chocolate company releases n different types of coupons. Each box of chocolates contains one coupon with a uniformly random type. Once you have collected all n types of coupons, you will get a prize. So how many boxes of chocolates you are expected to buy to win the prize?

The coupon collector problem can be described in the balls-into-bins model as follows. We keep throwing balls one-by-one into n bins (coupons), such that each ball is thrown into a bin uniformly and independently at random. Each ball corresponds to a box of chocolate, and each bin corresponds to a type of coupon. Thus, the number of boxes bought to collect n coupons is just the number of balls thrown until none of the n bins is empty.

Theorem
Let X be the number of balls thrown uniformly and independently to n bins until no bin is empty. Then E[X]=nH(n), where H(n) is the nth harmonic number.
Proof.
Let Xi be the number of balls thrown while there are exactly i1 nonempty bins, then clearly X=i=1nXi.

When there are exactly i1 nonempty bins, throwing a ball, the probability that the number of nonempty bins increases (i.e. the ball is thrown to an empty bin) is

pi=1i1n.

Xi is the number of balls thrown to make the number of nonempty bins increases from i1 to i, i.e. the number of balls thrown until a ball is thrown to a current empty bin. Thus, Xi follows the geometric distribution, such that

Pr[Xi=k]=(1pi)k1pi

For a geometric random variable, E[Xi]=1pi=nni+1.

Applying the linearity of expectations,

E[X]=E[i=1nXi]=i=1nE[Xi]=i=1nnni+1=ni=1n1i=nH(n),

where H(n) is the nth Harmonic number, and H(n)=lnn+O(1). Thus, for the coupon collectors problem, the expected number of coupons required to obtain all n types of coupons is nlnn+O(n).


Only knowing the expectation is not good enough. We would like to know how fast the probability decrease as a random variable deviates from its mean value.

Theorem
Let X be the number of balls thrown uniformly and independently to n bins until no bin is empty. Then Pr[Xnlnn+cn]<ec for any c>0.
Proof.
For any particular bin i, the probability that bin i is empty after throwing nlnn+cn balls is
(11n)nlnn+cn<e(lnn+c)=1nec.

By the union bound, the probability that there exists an empty bin after throwing nlnn+cn balls is

Pr[Xnlnn+cn]<n1nec=ec.

Occupancy Problem

Now we ask about the loads of bins. Assuming that m balls are uniformly and independently assigned to n bins, for 1in, let Xi be the load of the ith bin, i.e. the number of balls in the ith bin.

An easy analysis shows that for every bin i, the expected load E[Xi] is equal to the average load m/n.

Because there are totally m balls, it is always true that i=1nXi=m.

Therefore, due to the linearity of expectations,

i=1nE[Xi]=E[i=1nXi]=E[m]=m.

Because for each ball, the bin to which the ball is assigned is uniformly and independently chosen, the distributions of the loads of bins are identical. Thus E[Xi] is the same for each i. Combining with the above equation, it holds that for every 1im, E[Xi]=mn. So the average is indeed the average!


Next we analyze the distribution of the maximum load. We show that when m=n, i.e. n balls are uniformly and independently thrown into n bins, the maximum load is O(lognloglogn) with high probability.

Theorem
Suppose that n balls are thrown independently and uniformly at random into n bins. For 1in, let Xi be the random variable denoting the number of balls in the ith bin. Then
Pr[max1inXi3lnnlnlnn]<1n.
Proof.
Let M be an integer. Take bin 1. For any particular M balls, these M balls are all thrown to bin 1 with probability (1/n)M, and there are totally (nM) distinct sets of M balls. Therefore, applying the union bound,
Pr[X1M](nM)(1n)M=n!M!(nM)!nM=1M!n(n1)(n2)(nM+1)nM=1M!i=0M1(1in)1M!.

According to Stirling's approximation, M!2πM(Me)M, thus

1M!(eM)M.
Figure 1

Due to the symmetry. All Xi have the same distribution. Apply the union bound again,

Pr[max1inXiM]=Pr[(X1M)(X2M)(XnM)]nPr[X1M]n(eM)M.

When M=3lnn/lnlnn,

(eM)M=(elnlnn3lnn)3lnn/lnlnn<(lnlnnlnn)3lnn/lnlnn=e3(lnlnlnnlnlnn)lnn/lnlnn=e3lnn+3lnlnlnnlnn/lnlnne2lnn=1n2.

Therefore,

Pr[max1inXi3lnnlnlnn]n(eM)M<1n.

When m>n, Figure 1 illustrates the results of several random experiments, which show that the distribution of the loads of bins becomes more even as the number of balls grows larger than the number of bins.

Formally, it can be proved that for m=Ω(nlogn), with high probability, the maximum load is within O(mn), which is asymptotically equal to the average load.