# Linearity of Expectation

Prerequisite : Random Variable

This post is about mathematical concepts like expectation, linearity of expectation. It covers one of the required topics to understand Randomized Algorithms.

Let us consider the following simple problem.

Given a fair dice with 6 faces, the dice is thrown n times, find expected value of sum of all results.

For example, if n = 2, there are total 36 possible outcomes.

(1, 1), (1, 2), .... (1, 6) (2, 1), (2, 2), .... (2, 6) ................ ................ (6, 1), (6, 2), ..... (6, 6)

**
Expected value** of a discrete random variable is R defined as following. Suppose R can take value r

_{1}with probability p

_{1}, value r

_{2}with probability p

_{2}, and so on, up to value r

_{k}with probability p

_{k}. Then the expectation of this random variable R is defined as

E[R] = r_{1}*p_{1}+ r_{2}*p_{2}+ ... r_{k}*p_{k}

Let us calculate expected value for the above example.

Expected Value of sum = 2*1/36 + 3*1/6 + .... + 7*1/36 + of two dice throws 3*1/36 + 4*1/6 + .... + 8*1/36 + ........................ ......................... 7*1/36 + 8*1/6 + .... + 12*1/36 = 7

The above way to solve the problem becomes difficult when there are more dice throws.

If we know about linearity of expectation, then we can quickly solve the above problem for any number of throws.

**Linearity of Expectation: **Let R_{1} and R_{2} be two discrete random variables on some probability space, then

E[R_{1}+ R_{2}] = E[R_{1}] + E[R_{2}]

Using the above formula, we can quickly solve the dice problem.

Expected Value of sum of 2 dice throws = 2*(Expected value of one dice throw) = 2*(1/6 + 2/6 + .... 6/6) = 2*7/2 = 7 Expected value of sum for n dice throws is = n * 7/2 = 3.5 * n

Some interesting facts about Linearly of Expectation:

1) Linearity of expectation holds for both dependent and independent events. On the other hand the rule E[R_{1}R_{2}] = E[R_{1}]*E[R_{2}] is true only for independent events.

2) Linearity of expectation holds for any number of random variables on some probability space. Let R_{1}, R_{2}, R_{3}, … R_{k} be k random variables, then

E[R_{1} + R_{2} + R_{3} + … + R_{k}] = E[R_{1}] + E[R_{2}] + E[R_{3}] + … + E[R_{k}]

**Another example that can be easily solved with linearity of expectation:**

Hat-Check Problem: Let there be group of n men where every man has one hat. The hats are redistributed and every man gets a random hat back. What is the expected number of men that get their original hat back.

Solution: Let R_{i} be a random variable, the value of random variable is 1 if i’th man gets the same hat back, otherwise 0.

So the expected number of men to get the right hat back is = E[R_{1}] + E[R_{2}] + .. + E[R_{n}] = P(R_{1}= 1) + P(R_{2}= 1) + .... + P(R_{n}= 1) [Here P(R_{i}= 1) indicates probability that R_{i}is 1] = 1/n + 1/n + ... + 1/n = 1

So on average 1 person gets the right hat back.

**Exercise:**

1) Given a fair coin, what is the expected number of heads when coin is tossed n times.

2) Balls and Bins: Suppose we have m balls, labeled i = 1, … , m and n bins, labeled j = 1, .. ,n. Each ball is thrown into one of the bin independently and uniformly at random.

a) What is the expected number of balls in every bin

b) What is the expected number of empty bins.

3) Coupon Collector: Suppose there are n types of coupons in a lottery and each lot contains one coupon (with probability 1 = n each). How many lots have to be bought (in expectation) until we have at least one coupon of each type.

See following for solution of Coupon Collector.

Expected Number of Trials until Success

Linearity of expectation is useful in algorithms. For example, expected time complexity of random algorithms like randomized quick sort is evaluated using linearity of expectation (See this for reference).

**References:**

http://www.cse.iitd.ac.in/~mohanty/col106/Resources/linearity_expectation.pdf

This article is contributed by **Shubham Gupta**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Expectation or expected value of an array
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Maximum number of candies that can be bought
- Maximum array sum that can be obtained after exactly k changes
- Count the number of non-increasing subarrays
- Minimum array elements to be changed to make Recaman's sequence
- Number of subarrays having sum of the form k^m, m >= 0
- Compute maximum of the function efficiently over all sub-arrays
- Determine the numer of squares that line will pass through
- Given count of digits 1, 2, 3, 4, find the maximum sum possible
- Check if product of array containing prime numbers is a perfect square
- Find the good permutation of first N natural numbers
- Find the maximum number of composite summands of a number
- Minimum number operations required to convert n to m | Set-2