{"id":26208,"date":"2017-10-26T20:27:20","date_gmt":"2017-10-26T14:57:20","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26208"},"modified":"2017-10-26T20:27:20","modified_gmt":"2017-10-26T14:57:20","slug":"linearity-of-expectation","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/linearity-of-expectation\/","title":{"rendered":"Linearity of Expectation"},"content":{"rendered":"<p>This post is about mathematical concepts like expectation, linearity of expectation. It covers one of the required topics to understand Randomized Algorithms.<\/p>\n<p>Let us consider the following simple problem.<span id=\"more-134756\"><\/span><\/p>\n<p>Given a fair dice with 6 faces, the dice is thrown n times, find expected value of sum of all results.<\/p>\n<p>For example, if n = 2, there are total 36 possible outcomes.<\/p>\n<pre>(1, 1), (1, 2), .... (1, 6)\r\n(2, 1), (2, 2), .... (2, 6)\r\n................\r\n................\r\n(6, 1), (6, 2), ..... (6, 6)<\/pre>\n<p><strong>Expected value<\/strong> of a discrete random variable is R defined as following. Suppose R can take value r<sub>1<\/sub> with probability p<sub>1<\/sub>, value r<sub>2<\/sub> with probability p<sub>2<\/sub>, and so on, up to value r<sub>k<\/sub> with probability p<sub>k<\/sub>. Then the expectation of this random variable R is defined as<\/p>\n<pre>    E[R] = r<sub>1<\/sub>*p<sub>1<\/sub> + r<sub>2<\/sub>*p<sub>2<\/sub> + ... r<sub>k<\/sub>*p<sub>k<\/sub><\/pre>\n<p>Let us calculate expected value for the above example.<\/p>\n<pre>Expected Value of sum = 2*1\/36 + 3*1\/6 + .... + 7*1\/36 + \r\nof two dice throws      3*1\/36 + 4*1\/6 + .... + 8*1\/36 + \r\n                        ........................\r\n                        .........................\r\n                        7*1\/36 + 8*1\/6 + .... + 12*1\/36     \r\n                  \r\n                      =  7<\/pre>\n<p>The above way to solve the problem becomes difficult when there are more dice throws.<\/p>\n<p>If we know about linearity of expectation, then we can quickly solve the above problem for any number of throws.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong><a href=\"http:\/\/en.wikipedia.org\/wiki\/Expected_value#Linearity\" target=\"_blank\" rel=\"noopener\">Linearity of Expectation:<\/a> <\/strong>Let R<sub>1<\/sub> and R<sub>2<\/sub> be two discrete random variables on some probability space, then<\/p>\n<pre>     E[R<sub>1<\/sub> + R<sub>2<\/sub>] = E[R<sub>1<\/sub>] + E[R<sub>2<\/sub>]<\/pre>\n<p>Using the above formula, we can quickly solve the dice problem.<\/p>\n<pre>Expected Value of sum of 2 dice throws = 2*(Expected value of one dice throw)\r\n                                       = 2*(1\/6 + 2\/6 + .... 6\/6)\r\n                                       = 2*7\/2\r\n                                       = 7 \r\n\r\nExpected value of sum for n dice throws is = n * 7\/2 = 3.5 * n<\/pre>\n<p>Some interesting facts about Linearly of Expectation:<br \/>\n1) Linearity of expectation holds for both dependent and independent events. On the other hand the rule E[R<sub>1<\/sub>R<sub>2<\/sub>] = E[R<sub>1<\/sub>]*E[R<sub>2<\/sub>] is true only for independent events.<\/p>\n<p>2) Linearity of expectation holds for any number of random variables on some probability space. Let R<sub>1<\/sub>, R<sub>2<\/sub>, R<sub>3<\/sub>, \u2026 R<sub>k<\/sub> be k random variables, then<br \/>\nE[R<sub>1<\/sub> + R<sub>2<\/sub> + R<sub>3<\/sub> + \u2026 + R<sub>k<\/sub>] = E[R<sub>1<\/sub>] + E[R<sub>2<\/sub>] + E[R<sub>3<\/sub>] + \u2026 + E[R<sub>k<\/sub>]\n[ad type=&#8221;banner&#8221;]\n<p><strong>Another example that can be easily solved with linearity of expectation:<\/strong><br \/>\nHat-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.<\/p>\n<p>Solution: Let R<sub>i<\/sub> be a random variable, the value of random variable is 1 if i\u2019th man gets the same hat back, otherwise 0.<\/p>\n<pre>So the expected number of men to get the right hat back is\r\n  = E[R<sub>1<\/sub>] + E[R<sub>2<\/sub>]  +  .. + E[R<sub>n<\/sub>] \r\n  = P(R<sub>1<\/sub> = 1) + P(R<sub>2<\/sub> = 1) + .... + P(R<sub>n<\/sub> = 1) \r\n  [Here P(R<sub>i<\/sub> = 1)  indicates probability that R<sub>i<\/sub> is 1]\r\n  = 1\/n + 1\/n + ... + 1\/n \r\n  = 1\r\n<\/pre>\n<p>So on average 1 person gets the right hat back.<\/p>\n<p><strong>Exercise:<\/strong><br \/>\n1) Given a fair coin, what is the expected number of heads when coin is tossed n times.<\/p>\n<p>2) Balls and Bins: Suppose we have m balls, labeled i = 1, \u2026 , m and n bins, labeled j = 1, .. ,n. Each ball is thrown into one of the bin independently and uniformly at random.<br \/>\na) What is the expected number of balls in every bin<br \/>\nb) What is the expected number of empty bins.<\/p>\n<p>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.<\/p>\n<p>See following for solution of Coupon Collector.<br \/>\n<a href=\"http:\/\/www.geeksforgeeks.org\/expected-number-of-trials-before-success\/\" target=\"_blank\" rel=\"noopener\">Expected Number of Trials until Success<\/a><\/p>\n<p>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 <a href=\"http:\/\/ocw.mit.edu\/courses\/electrical-engineering-and-computer-science\/6-046j-introduction-to-algorithms-sma-5503-fall-2005\/video-lectures\/lecture-4-quicksort-randomized-algorithms\/\" target=\"_blank\" rel=\"noopener\">this <\/a>for reference).<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Linearity of Expectation Randomized Algorithms &#8211; This post is about mathematical concepts like expectation linearity of expectation It covers  required topic<\/p>\n","protected":false},"author":1,"featured_media":26215,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,83617],"tags":[77843,77836,77839,77840,77842,77837,77829,77835,77833,77828,77827,77832,77830,77834,77838,77831,77841],"class_list":["post-26208","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithm","category-coding","category-randomized-algorithms-2","tag-expectation-of-indicator-random-variable","tag-expectation-of-sum-is-sum-of-expectation","tag-expectation-of-sum-of-dependent-random-variables","tag-law-of-total-expectation-proof","tag-law-of-total-expectation-proof-in-continuous-case","tag-linearity-of-conditional-expectation","tag-linearity-of-expectation-dependent","tag-linearity-of-expectation-examples","tag-linearity-of-expectation-khan-academy","tag-linearity-of-expectation-problems","tag-linearity-of-expectation-proof","tag-linearity-of-expectation-proof-continuous","tag-linearity-of-expectation-variance","tag-linearity-of-expectation-wiki","tag-linearity-of-expectation-wikipedia","tag-linearity-of-variance","tag-properties-of-expectation-of-a-random-variable"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26208"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26208\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/26215"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}