{"id":1231,"date":"2021-09-24T23:30:39","date_gmt":"2021-09-25T03:30:39","guid":{"rendered":"http:\/\/aristotle2digital.blogwyrm.com\/?p=1231"},"modified":"2022-05-22T11:14:50","modified_gmt":"2022-05-22T15:14:50","slug":"monte-carlo-integration-part-1","status":"publish","type":"post","link":"https:\/\/aristotle2digital.blogwyrm.com\/?p=1231","title":{"rendered":"Monte Carlo Integration &#8211; Part 1"},"content":{"rendered":"\n<p>It is a peculiar fact that randomness, when married with a set of deterministic rules, often leads to richness that is absent in a set of deterministic rules by themselves.&nbsp; (Of course, there is no richness in randomness by itself \u2013 just noise).&nbsp; An everyday example can be found in the richness of poker that exceeds the mechanistic (albeit complex) rules of chess.&nbsp; This behavior carries over into the realm of numerical analysis where Monte Carlo integration presents a versatile method of computing the value of definite integrals.&nbsp; Given the fact that most integrands don\u2019t have an anti-derivative, this technique can be very powerful indeed.<\/p>\n\n\n\n<p>To understand how it works, consider a function $g(x)$, which is required to be computable over the range $(a,b)$, but nothing else.&nbsp; We will now decompose this function in a non-obvious way<\/p>\n\n\n\n<p>\\[ g(x) = \\frac{f(x)}{p(x)} \\; , \\]<\/p>\n\n\n\n<p>where $p(x)$ is a normalized function on the same interval<\/p>\n\n\n\n<p>\\[ \\int_a^b dx p(x) = 1 \\; . \\]<\/p>\n\n\n\n<p>If we draw $N$ realizations from $p(x)$<\/p>\n\n\n\n<p>\\[ x_1, x_2, \\ldots, x_N \\; \\]<\/p>\n\n\n\n<p>and we evaluate $g(x)$ at each value<\/p>\n\n\n\n<p>\\[ g_1, g_2, \\ldots, g_N \\; , \\]<\/p>\n\n\n\n<p>where $g_i \\equiv g(x_i)$.<\/p>\n\n\n\n<p>On one hand, the expected value is<\/p>\n\n\n\n<p>\\[ E[g(x)] = \\frac{1}{N} \\sum_i g_i \\equiv {\\bar g} .\\]<\/p>\n\n\n\n<p>This is the empirical value of the sampling of $g(x)$ according to $p(x)$.&nbsp;<\/p>\n\n\n\n<p>The theoretical value is<\/p>\n\n\n\n<p>\\[ E[g(x)] = \\int_a^b dx p(x) g(x) = \\int_a^b dx p(x) \\frac{f(x)}{p(x)} = \\int_a^b dx f(x) \\; . \\]<\/p>\n\n\n\n<p>So, we can evaluate the integral<\/p>\n\n\n\n<p>\\[ I = \\int_a^b dx f(x) \\; \\]<\/p>\n\n\n\n<p>by sampling and averaging $g(x)$ with a sampling realization of $p(x)$.<\/p>\n\n\n\n<p>The simplest probability distribution to consider is uniform on the range<\/p>\n\n\n\n<p>\\[ p(x) = \\frac{1}{b -a} \\; \\]<\/p>\n\n\n\n<p>and with $g(x) = (b-a) f(x)$ we can evaluate<\/p>\n\n\n\n<p>\\[ I = \\int_a^b dx f(x) = \\frac{1}{N} \\sum_i g_i = \\frac{b-a}{N} \\sum_i f_i \\; , \\]<\/p>\n\n\n\n<p>where $f_i \\equiv f(x_i) $.<\/p>\n\n\n\n<p>Before pressing on with the theory, let\u2019s take a look at some specific examples.<\/p>\n\n\n\n<p>For the first example, let\u2019s look at the polynomial<\/p>\n\n\n\n<p>\\[ f_p(x) = 3.4 x^3 \u2013 5.9 x^2 + 0.3 x \u2013 15.2 \\; \\]<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"720\" src=\"http:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_p_look.png\" alt=\"\" class=\"wp-image-1240\" srcset=\"https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_p_look.png 720w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_p_look-300x300.png 300w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_p_look-150x150.png 150w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_p_look-54x54.png 54w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/figure>\n\n\n\n<p>This function has the antiderivative<\/p>\n\n\n\n<p>\\[ F_p(x) = \\frac{3.4}{4} x^4 &#8211; \\frac{5.9}{3} x^3 + \\frac{0.3}{2} x^2 &#8211; 15.2 x \\; . \\]<\/p>\n\n\n\n<p>The integral of $f_p$ from $x_{min} = -2$ to $x_{max} = 3.5$ has the exact value<\/p>\n\n\n\n<p>\\[ \\int_{x_{min}}^{x_{max}} dx f_p = F_p(x_{max}) \u2013 F_p(x_{min}) = -68.46354166666669 \\; . \\]<\/p>\n\n\n\n<p>A simple script in python using numpy, scipy, and a Jupyter notebook was implemented to explore numerical techniques to perform the integral.&nbsp; The <a href=\"https:\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.integrate.quad.html\">scipy.integrate.quad<\/a> function, which uses traditional quadrature based on the Fortran QUADPACK routines, returned the value of -68.46354166666667, which is exactly (to within machine precision) the analytic result.&nbsp; The Monte Carlo integration, which is implemented with the following lines<\/p>\n\n\n\n<div class=\"myQuoteDiv\">\n<pre class=\"wp-block-preformatted\">N = 100_000\nrand_x = x_min + (x_max - x_min)*np.random.random(size=N)\nrand_y = f(rand_x)\nI_poly_est&nbsp; = (x_max - x_min)*np.average(rand_y)<\/pre>\n<\/div>\n\n\n\n<p>returned a value of -68.57203483190794 using 100,000 samples (generally called a trial \u2013 a terminology that will be used hereafter) with a relative error of 0.00158.&nbsp; This is not bad agreement but clearly orders of magnitude worse than the deterministic algorithm.&nbsp; We\u2019ll return to this point later.<\/p>\n\n\n\n<p>One more example is worth discussing.&nbsp; This time the function is a nasty, nested function given by<\/p>\n\n\n\n<p>\\[ f_n = 4 \\sin \\left( 2 \\pi e^{\\cos x^2} \\right) \\; , \\]<\/p>\n\n\n\n<p>which looks like<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"720\" src=\"http:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_n_look.png\" alt=\"\" class=\"wp-image-1239\" srcset=\"https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_n_look.png 720w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_n_look-300x300.png 300w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_n_look-150x150.png 150w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/f_n_look-54x54.png 54w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/figure>\n\n\n\n<p>over the range $[-2,3.5]$.&nbsp; Even though this function doesn\u2019t have an anti-derivative, its integral clearly exists since the function is continuous with no divergences.&nbsp; The deterministic algorithm found in scipy.integrate.quad generates the value of -2.824748541066965, which we will take as truth.&nbsp; The same Monte Carlo scheme as above yields a value -2.8020549889975164 or relative error 0.008034.<\/p>\n\n\n\n<p>So, we can see that the method is versatile and reasonably accurate.&nbsp; However, there are two questions that one might immediately pose: 1) why use it if there are more accurate methods available and 2) can the estimates be made more accurate?&nbsp; The remainder of this post will deal with answering question 1 while the subsequent post will answer the second question.<\/p>\n\n\n\n<p>To answer question 1, we will cite, without proof, the answer to question 2 that states the error $\\epsilon$ in the estimate of the integral goes to zero as the number of samples according to<\/p>\n\n\n\n<p>\\[ \\epsilon \\sim \\frac{1}{\\sqrt{N}} \\; \\]<\/p>\n\n\n\n<p>independent of the number of dimensions.&nbsp;<\/p>\n\n\n\n<p>To determine the error for a conventional quadrature method (e.g. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Trapezoidal_rule\">trapezoidal<\/a> or <a href=\"https:\/\/en.wikipedia.org\/wiki\/Simpson%27s_rule\">Simpson\u2019s<\/a>), we follow the argument from Steve Koonin\u2019s Computational Physics p 201.&nbsp; Assume that we will evaluate the integrand $N$ times (corresponding to the $N$ times it will be evaluated with the Monte Carlo scheme).&nbsp; The $d$-dimensional region over for the integral consists of a volume ${V} ~ {h}^d$.&nbsp; Distributing the $N$ points over the volume means that the grid spacing $h ~ N^{-1\/d}$.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"857\" height=\"476\" src=\"http:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/Grid_size.png\" alt=\"\" class=\"wp-image-1238\" srcset=\"https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/Grid_size.png 857w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/Grid_size-300x167.png 300w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/Grid_size-768x427.png 768w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2021\/11\/Grid_size-810x450.png 810w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/figure>\n\n\n\n<p>Koonin states that an analysis similar to those used for the usual one-dimensional formulae gives the error as going $O\\left(h^{d+2}\\right)$.&nbsp; The total error of the quadrature thus goes as<\/p>\n\n\n\n<p>\\[ N O\\left(h^{d+2}\\right) = O\\left(N^{-2\/d}\\right) \\; .\\]<\/p>\n\n\n\n<p>When $d \\geq 4$, the error in estimating the integral via Monte Carlo becomes smaller than that for standard quadrature techniques for a given computing time invested.&nbsp; This reason alone justifies why the Monte Carlo technique is so highly favored.&nbsp; In particular, Monte Carlo proves invaluable in propagating variations through dynamical systems whose dimensionality precludes using a gridded method like traditional quadrature.<\/p>\n\n\n\n<p>The next post will derive why the Monte Carlo error goes as $1\/\\sqrt(N)$.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is a peculiar fact that randomness, when married with a set of deterministic rules, often leads to richness that is absent in a set of deterministic rules by themselves.&nbsp;&#8230; <a class=\"read-more-button\" href=\"https:\/\/aristotle2digital.blogwyrm.com\/?p=1231\">Read more &gt;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1231","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1231"}],"version-history":[{"count":0,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1231\/revisions"}],"wp:attachment":[{"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}