{"id":4121,"date":"2024-08-23T17:30:32","date_gmt":"2024-08-23T22:30:32","guid":{"rendered":"https:\/\/laurentlessard.com\/bookproofs\/?p=4121"},"modified":"2024-08-25T16:07:45","modified_gmt":"2024-08-25T21:07:45","slug":"round-round-get-a-round","status":"publish","type":"post","link":"https:\/\/laurentlessard.com\/bookproofs\/round-round-get-a-round\/","title":{"rendered":"Round, round, get a round"},"content":{"rendered":"<p>This week&#8217;s <a href=\"https:\/\/thefiddler.substack.com\/p\/round-round-get-a-round-i-get-a-round\">Fiddler<\/a> is about rounding!<\/p>\n<blockquote><p>\nLet $\\text{round}(x)$ be the value of $x$ rounded to the nearest integer. Suppose $x_1,\\dots,x_n$ are independent uniformly distributed random variables in $[0,1]$. Find the probability that<br \/>\n\\[<br \/>\n\\text{round}(x_1+\\cdots+x_n) = \\text{round}(x_1)+\\cdots+\\text{round}(x_n)<br \/>\n\\]\n<\/p><\/blockquote>\n<p>My solution:<br \/>\n<a href=\"javascript:Solution('soln_roundround','toggle_roundround')\" id=\"toggle_roundround\">[Show Solution]<\/a><\/p>\n<div id=\"soln_roundround\" style=\"display: none\">\n<p>Let&#8217;s call the probability we seek $p(n)$. The values of the $x_i$ determine what sums are even possible, so let&#8217;s consider some cases based on the possible values of $\\text{round}(x_1)+\\cdots+\\text{round}(x_n)$. Suppose that $k$ of the variables are in in the interval $[\\tfrac{1}{2},1]$ and the remaining $n-k$ variables are in $[0,\\tfrac{1}{2}]$. The probability of this occurring is $\\tfrac{1}{2^n}\\binom{n}{k}$. In this case,<br \/>\n\\[<br \/>\n\\sum_{i=1}^n \\text{round}(x_i) = k<br \/>\n\\]Note: We can ignore the issue of whether $\\tfrac{1}{2}$ rounds to $0$ or $1$ since the probability of $\\tfrac{1}{2}$ occurring is zero. Define a set of rescaled variables $y_i$ in $[0,1]$ as follows:<br \/>\n\\[<br \/>\ny_i = \\begin{cases}<br \/>\n2x_i-1 &#038; \\text{if }i=1,\\dots,k\\\\<br \/>\n2x_i &#038; \\text{if }i=k+1,\\dots,n<br \/>\n\\end{cases}<br \/>\n\\]Now let&#8217;s calculate the probability that the sum of the $x_i$ also rounds to $k$ and express it in terms of the $y_i$:<br \/>\n\\begin{align*}<br \/>\n\\mathrm{Prob}\\left( \\text{round}\\biggl(\\sum_{i=1}^n x_i\\biggr) = k\\right)<br \/>\n&#038;= \\mathrm{Prob}\\left( k-\\tfrac{1}{2} \\leq \\sum_{i=1}^n x_i \\leq k+\\tfrac{1}{2} \\right) \\\\<br \/>\n&#038;= \\mathrm{Prob}\\left( k-1 \\leq \\sum_{i=1}^n y_i \\leq k+1 \\right)<br \/>\n\\end{align*}The sum of the $y_i$, which is a sum of $n$ independent variables in $[0,1]$ follows a so-called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Irwin%E2%80%93Hall_distribution\">Irwin-Hall distribution<\/a>, whose CDF is given by:<br \/>\n\\[<br \/>\nF(x) = \\text{Prob}\\bigl( y_1+\\cdots+y_n \\leq x \\bigr) = \\frac{1}{n!}\\sum_{k=0}^{\\lfloor x \\rfloor} (-1)^k \\binom{n}{k}(x-k)^n<br \/>\n\\]Therefore, the probability we seek is $F(k+1)-F(k-1)$, or:<br \/>\n\\[<br \/>\n\\frac{1}{n!}\\sum_{m=0}^{k+1} (-1)^m \\binom{n}{m}(k+1-m)^n<br \/>\n-\\frac{1}{n!}\\sum_{m=0}^{k-1} (-1)^m \\binom{n}{m}(k-1-m)^n<br \/>\n\\]Rearranging terms a bit, we obtain:<br \/>\n\\[<br \/>\n\\frac{(-1)^{n+k}}{n!} \\binom{n}{k}+<br \/>\n\\frac{1}{n!}\\sum_{m=0}^{k} (-1)^m \\binom{n}{m}\\bigl( (k+1-m)^n-(k-1-m)^n\\bigr)<br \/>\n\\]By writing out the terms carefully, we can group like $n^\\text{th}$ powers and we obtain a simpler equivalent expression:<br \/>\n\\[<br \/>\n\\frac{1}{n!}\\sum_{m=0}^k(-1)^m\\left[ \\binom{n+1}{m}-\\binom{n+1}{m-1}\\right](k+1-m)^n<br \/>\n\\]Remember this was the probability that the two roundings are the same when $k$ of the $x_i$&#8217;s round to $1$. Multiplying each such probability by its prior probability and summing, we get the total probability that the two roundings are the same:<br \/>\n\\[<br \/>\np(n) = \\frac{1}{2^n\\,n!}\\sum_{k=0}^n \\binom{n}{k}\\sum_{m=0}^k(-1)^m\\left[ \\binom{n+1}{m}-\\binom{n+1}{m-1}\\right](k+1-m)^n<br \/>\n\\]This can be further simplified by grouping $n^\\text{th}$ powers once again and observing that all odd terms cancel out. After quite a bit of algebra, we obtain the simplest expression possible, which is:<\/p>\n<p style=\"text-align: center;\"><span style=\"background-color: #AFC8E6; padding: 25px 10px 25px 10px;\">$\\displaystyle<br \/>\np(n) = \\frac{1}{2^n\\, n!}\\sum _{k=0}^{\\left\\lfloor \\frac{n}{2}\\right\\rfloor } (-1)^k \\binom{n+1}{k} (n+1-2k)^n<br \/>\n$<\/span><\/p>\n<p>We can evaluate this for different values of $n$ and we obtain:<br \/>\n\\[<br \/>\n\\begin{array}{c|cccccccccc}<br \/>\nn &#038; 1 &#038; 2 &#038; 3 &#038; 4 &#038; 5 &#038; 6 &#038; 7 &#038; 8 &#038; 9 &#038; 10\\\\ \\hline<br \/>\np(n) &#038; 1 &#038; \\frac{3}{4} &#038; \\frac{2}{3} &#038; \\frac{115}{192} &#038; \\frac{11}{20} &#038; \\frac{5887}{11520} &#038; \\frac{151}{315} &#038; \\frac{259723}{573440} &#038; \\frac{15619}{36288} &#038; \\frac{381773117}{928972800}<br \/>\n\\end{array}<br \/>\n\\]<\/p>\n<p>The sequence $2^n \\, n!\\, p(n)$ is actually a well-known sequence. It is <a href=\"https:\/\/oeis.org\/A261398\">A261398 in OEIS<\/a> (the Online Encyclopedia of Integer Sequences), and according to OEIS it has no known closed-form formula and the one above is the simplest one available. Good enough for me!<\/p>\n<h3>Visualization in 2D and beyond<\/h3>\n<p>We can visualize the probabilities for the case $n=2$ by coloring in the set of points $(x_1,x_2)$ for which the sum of the rounded values equals the rounded value of the sum. This yields:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round2.png\" alt=\"\" width=\"497\" height=\"490\" class=\"aligncenter size-full wp-image-4126\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round2.png 497w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round2-300x296.png 300w\" sizes=\"auto, (max-width: 497px) 85vw, 497px\" \/><\/p>\n<p>We can check the shaded region is $\\frac{3}{4}$ of the total area, as anticipated. We can do the same for $n=3$. This time, we plot the set of points $(x_1,x_2,x_3)$ and the probability is the shaded volume:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round3.png\" alt=\"\" width=\"487\" height=\"534\" class=\"aligncenter size-full wp-image-4125\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round3.png 487w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round3-274x300.png 274w\" sizes=\"auto, (max-width: 487px) 85vw, 487px\" \/><\/p>\n<p>Again, we can check the volume is $\\frac{2}{3}$. Unfortunately, we can&#8217;t visualize the case $n=4$, but we can try&#8230; This should be a 4D hypercube. In the same way that each slice of a 3D cube is a square, each slice of a 4D hypercube is a 3D cube. Here is an animation showing what happens as we vary $x_4 \\in [0,1]$ and we plot the cube resulting from the valid choices of $(x_1,x_2,x_3)$ as a function of $x_4$. The abrupt change occurs when $x_4=\\tfrac{1}{2}$ as this causes the sum of rounded values to jump by $1$.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round4.gif\" alt=\"\" width=\"480\" height=\"532\" class=\"aligncenter size-full wp-image-4129\" \/><\/p>\n<p>So now, if you can imagine, the probability we&#8217;re looking for is the &#8220;hyper-volume&#8221; of this 4D shape, which is given by integrating the volume above as we vary $x_4$, and as it turns out,<br \/>\n\\[<br \/>\np(4) = \\int_{0}^1 \\text{Volume}(x_4)\\,\\mathrm{d}x_4 = \\frac{115}{192}.<br \/>\n\\]<\/p>\n<h3>Approximation<\/h3>\n<p>Recall that the solution we found above is given by<br \/>\n\\[<br \/>\np(n) = \\sum_{k=0}^n \\frac{1}{2^n}\\binom{n}{k}\\bigl( F(k+1)-F(k-1) \\bigr)<br \/>\n\\]where $F(x)$ is the CDF of the Irwin-Hall distribution. The formula we found for this is quite messy, so let&#8217;s see if we can approximate it.<\/p>\n<p>First, the Irwin-Hall distribution can be <a href=\"https:\/\/en.wikipedia.org\/wiki\/Irwin%E2%80%93Hall_distribution\">well-approximated<\/a> by a normal distribution. This makes sense, since summing a large number of identically distributed random variables tends to a normal distribution by the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Central_limit_theorem\">central limit theorem<\/a>. In this case, the limiting distribution has mean $\\mu=\\frac{n}{2}$ and variance $\\sigma^2=\\frac{n}{12}$. Therefore, we can approximate:<br \/>\n\\begin{align*}<br \/>\nF(k+1)-F(k-1) &#038;\\approx 2F'(k) \\\\<br \/>\n&#038;\\approx \\frac{2}{\\sqrt{2\\pi \\sigma^2}} \\exp\\biggl( -\\frac{(x-\\mu)^2}{2\\sigma^2} \\biggr) \\\\<br \/>\n&#038;= \\frac{1}{\\sqrt{\\pi n\/24}}\\exp\\biggl( -\\frac{(k-\\tfrac{n}{2})^2}{n\/6} \\biggr)<br \/>\n\\end{align*}Next, the weighted binomial sum is actually a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Binomial_distribution\">binomial distribution<\/a>, which we can also approximate using a normal distribution. In general, we have $B(n,p) \\approx \\mathcal{N}(\\mu,\\sigma^2)$, with $\\mu=np$ and $\\sigma^2=np(1-p)$. Therefore, if $g$ is any function, we can write the binomial sum as an expectation:<br \/>\n\\begin{align*}<br \/>\n\\sum_{k=0}^n \\frac{1}{2^n}\\binom{n}{k} g(k)<br \/>\n&#038;= \\mathbb{E}_{k \\sim B(n,\\tfrac{1}{2})}\\bigl( g(k) \\bigr) \\\\<br \/>\n&#038;\\approx \\mathbb{E}_{k \\sim \\mathcal{N}(\\tfrac{n}{2},\\tfrac{n}{4})}\\bigl( g(k) \\bigr) \\\\<br \/>\n&#038;=\\frac{1}{\\sqrt{\\pi n\/2}} \\int_{-\\infty}^\\infty \\exp\\biggl(-\\frac{(k-\\tfrac{n}{2})^2}{n\/2} \\biggr)g(k)\\,\\mathrm{d}k<br \/>\n\\end{align*}Setting $g(k)=F(k+1)-F(k-1)$ and substituting our previous approximation, we find:<br \/>\n\\begin{align*}<br \/>\np(n) &#038;\\approx \\frac{1}{\\sqrt{\\pi n\/2}}\\frac{1}{\\sqrt{\\pi n\/24}}\\int_{-\\infty}^\\infty  \\exp\\biggl( -\\frac{(k-\\tfrac{n}{2})^2}{n\/6} \\biggr)\\exp\\biggl(-\\frac{(x-\\tfrac{n}{2})^2}{n\/2} \\biggr)\\,\\mathrm{d}k \\\\<br \/>\n&#038;= \\frac{\\sqrt{48}}{\\pi n} \\int_{-\\infty}^\\infty \\exp\\biggl(-\\frac{(k-\\tfrac{n}{2})^2}{n\/8} \\biggr) \\,\\mathrm{d}k \\\\<br \/>\n&#038;= \\frac{\\sqrt{48}}{\\pi n} \\sqrt{\\pi n\/8} \\\\<br \/>\n\\end{align*}Therefore, we have:<\/p>\n<p style=\"text-align: center;\"><span style=\"background-color: #AFC8E6; padding: 25px 10px 25px 10px;\">$\\displaystyle<br \/>\np(n) \\approx \\sqrt{\\frac{6}{\\pi n}}<br \/>\n$<\/span><\/p>\n<p>To check our approximation, we can plot it together with the true values:<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round4.png\" alt=\"\" width=\"788\" height=\"469\" class=\"aligncenter size-full wp-image-4135\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round4.png 788w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round4-300x179.png 300w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2024\/08\/round4-768x457.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p>Not bad! So in conclusion, we have an exact formula and an asymptotically exact approximation for the probability that<br \/>\n\\[<br \/>\n\\text{round}(x_1+\\cdots+x_n) = \\text{round}(x_1)+\\cdots+\\text{round}(x_n)<br \/>\n\\]They are given by:<\/p>\n<p style=\"text-align: center;\"><span style=\"background-color: #AFC8E6; padding: 25px 10px 25px 10px;\">$\\displaystyle<br \/>\np(n) = \\frac{1}{2^n\\, n!}\\sum _{k=0}^{\\left\\lfloor \\frac{n}{2}\\right\\rfloor } (-1)^k \\binom{n+1}{k} (n+1-2k)^n<br \/>\n\\approx \\sqrt{\\frac{6}{\\pi n}}<br \/>\n$<\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This week&#8217;s Fiddler is about rounding! Let $\\text{round}(x)$ be the value of $x$ rounded to the nearest integer. Suppose $x_1,\\dots,x_n$ are independent uniformly distributed random variables in $[0,1]$. Find the probability that \\[ \\text{round}(x_1+\\cdots+x_n) = \\text{round}(x_1)+\\cdots+\\text{round}(x_n) \\] My solution: [Show Solution] Let&#8217;s call the probability we seek $p(n)$. The values of the $x_i$ determine what &hellip; <a href=\"https:\/\/laurentlessard.com\/bookproofs\/round-round-get-a-round\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Round, round, get a round&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":4129,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[42],"tags":[9,43,8],"class_list":["post-4121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-fiddler","tag-binomial","tag-fiddler","tag-probability"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/4121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/comments?post=4121"}],"version-history":[{"count":19,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/4121\/revisions"}],"predecessor-version":[{"id":4147,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/4121\/revisions\/4147"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media\/4129"}],"wp:attachment":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media?parent=4121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/categories?post=4121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/tags?post=4121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}