Is it possible to calculate a Lebesgue integral in Mathematica? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-08-18T02:05:41Z https://mathematica.stackexchange.com/feeds/question/42047 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://mathematica.stackexchange.com/q/42047 18 Is it possible to calculate a Lebesgue integral in Mathematica? Seyhmus Güngören https://mathematica.stackexchange.com/users/4256 2014-02-10T15:04:18Z 2019-05-14T13:45:36Z <p>As the title says, I wonder if it is possible to calculate a Lebesgue integral in <em>Mathematica</em>, especially when the domain of integration is $\mathbb{R}^N$, or in other words multivatiate Lebesgue integration is of interest. As an example, one could take $f_0$ and $f_1$ as two different (possibly correlated) bi-variate Gaussian density functions and consider</p> <p>$$\int_{\frac{f_1}{f_0}&gt;\tau}f_0\mbox{d}\mu=\int_{\Large\{x,y:\frac{f_1(x,y)}{f_0(x,y)}&gt;\tau\Large\}}f_0(x,y)\mbox{d}x\mbox{d}y$$ for some known number $\tau$.</p> <p>If it is not possible can one manipulate the existent functions of <em>Mathematica</em> to get a method which can calcuate the Lebesgue integral. I am only interested in very fast numerical methods, no analytical results are needed.</p> <p><strong>Note</strong>: Why am I asking such a question here?</p> <ul> <li>I checked google about it and found no answer</li> <li>I checked the <em>Mathematica</em> help and found no result</li> <li>I searched the keyword "Lebesgue" among the questions in <code>mathematica.stackexchange.com</code> but couldnt see any related answer</li> <li>I have no idea how I can do it in <em>Mathematica</em></li> </ul> https://mathematica.stackexchange.com/questions/42047/-/42051#42051 20 Answer by Artes for Is it possible to calculate a Lebesgue integral in Mathematica? Artes https://mathematica.stackexchange.com/users/184 2014-02-10T15:33:17Z 2015-11-02T07:23:31Z <p>The answer is <strong>no</strong>, because of fundamental mathematical limitations which originate in set theory regarding <a href="http://en.wikipedia.org/wiki/Countable_set" rel="nofollow noreferrer">countability</a> (see e.g. <a href="http://en.wikipedia.org/wiki/Cantor%27s_theorem" rel="nofollow noreferrer">Cantor's theorem</a>) - functions over a given set are more numerous than the set's (power) <a href="http://en.wikipedia.org/wiki/Cardinality" rel="nofollow noreferrer">cardinality</a>. Neither <em>Mathematica</em> nor any other system can integrate every function in an even much more restricted class; namely, Riemann integrable <a href="http://en.wikipedia.org/wiki/Riemann_integral" rel="nofollow noreferrer">functions</a>. All Riemann integrals are equal to Lebesgue integrals if the former are well defined. The class of functions which could be integrated over a domain in $\mathbb{R}^n$ in <em>Mathematica</em> is of "measure zero" in the class of Lebesgue integrable functions. More precisely, we need <a href="http://en.wikipedia.org/wiki/Baire_category_theorem" rel="nofollow noreferrer">Baire categories</a> to work with general topological concepts of the class of adequate functions.<br> When we calculate a definite integral, we are going to think of <code>NIntegrate</code> rather than <code>Integrate</code>. </p> <p>Let's try to integrate a simple Lebesgue integrable function defined in $\mathbb{R}$:</p> <pre><code>f[x_] /; x ∈ Rationals &amp;&amp; 0 &lt;= x &lt;= 1 := 1 f[x_] /; ! (x ∈ Rationals) &amp;&amp; 0 &lt;= x &lt;= 1 := 0 f[Sqrt/2] f[1/2] </code></pre> <blockquote> <pre><code>0 1 </code></pre> </blockquote> <p>but neither <code>Integrate</code> nor <code>NIntegrate</code> can calculate adequate integrals:</p> <pre><code>Integrate[ f[x], {x, 0, 1}] NIntegrate[ f[x], {x, 0, 1}] </code></pre> <p>although we know it should be <code>0</code>. Having said that, we can always <strong>supplement</strong> built-in <strong>integration rules</strong> with user-defined ones (see e.g. <a href="https://mathematica.stackexchange.com/questions/8353/why-arent-these-additions-of-integrals-and-summations-equal">Why aren't these additions of integrals and summations equal?</a>) to <strong>expand a class</strong> of <strong>symbolically</strong> or <strong>numerically</strong> integrable functions - for this purpose <em>Mathematica</em> is most likely the best tool.<br> While we could always remedy various problems algorithmically, we shouldn't expect that it could be done in full generality (e.g. because of the finite number of states of computers); otherwise, we should supplement the system's built-in integration rules with <strong>infinitely many</strong> user-defined rules to be able to integrate <strong>every</strong> Lebesgue integrable function.<br> Next editions of <em>Mathematica</em> may involve more powerful symbolic capabilities for measure theory and Lebesgue integration problems, but we should realize that there will always be some limitations of the algorithmic approach to integration in the realm of integrable functions. </p> <p><strong>Edit</strong></p> <p>The above considerations concern the problem of integration of a possibly wide class of functions. However if one restricts to integration of bi-variate Gaussian density functions there is no need for distinction between Riemann and Lebesgue integrals. Since one needs fast numerical results I'd recommend taking a closer look at the <strong>NIntegrate Integration Strategies</strong> tutorial, especially at <a href="http://reference.wolfram.com/mathematica/tutorial/NIntegrateIntegrationStrategies.html#20795380" rel="nofollow noreferrer">Crude Monte Carlo and Quasi Monte Carlo Strategies</a> and <a href="http://reference.wolfram.com/mathematica/tutorial/NIntegrateIntegrationStrategies.html#65285686" rel="nofollow noreferrer">Global Adaptive Monte Carlo and Quasi Monte Carlo Strategies</a> sections. </p> <p>Let's define e.g.</p> <pre><code>f1[x_, y_] := PDF[ BinormalDistribution[{1, 3/2}, {1/2, 3/5}, 1/3], {x, y}] f2[x_, y_] := PDF[ BinormalDistribution[{4/3, 7/3}, {1, 2/3}, 2/5], {x, y}] </code></pre> <p>and choose e.g. <code>τ = 5</code>.</p> <p>An especially fast method would be e.g. <code>Method -&gt; "AdaptiveMonteCarlo"</code> with <code>Boole[ f1[x, y] &gt; 5 f2[x, y]]</code> - appropriate region selector. Instead of <code>Boole</code> we could use <code>HeavisideTheta</code>, e.g. <code>HeavisideTheta[ f1[x, y] - 5 f2[x, y]]</code> but in this case it appears to be fairly slower (see e.g. <a href="https://mathematica.stackexchange.com/questions/39161/how-to-plot-and-find-the-volume-of-a-solid/39164#39164">this</a> for the case when it is much faster). Working with <code>"AdaptiveMonteCarlo"</code>, one should remember that the method provides a rather rough estimation of the result:</p> <pre><code>NIntegrate[ f1[x, y] Boole[ f1[x, y] &gt; 5 f2[x, y]], {x, -∞, ∞}, {y, -∞, ∞}, Method -&gt; "AdaptiveMonteCarlo"] </code></pre> <blockquote> <pre><code>0.370381 </code></pre> </blockquote> <p>A slower but considerably more stable method would be <code>Method -&gt; "AdaptiveQuasiMonteCarlo"</code>.</p> https://mathematica.stackexchange.com/questions/42047/-/119836#119836 11 Answer by Anton Antonov for Is it possible to calculate a Lebesgue integral in Mathematica? Anton Antonov https://mathematica.stackexchange.com/users/34008 2016-07-01T21:51:23Z 2019-05-14T13:45:36Z <blockquote> <p>[...] I am only interested in very fast numerical methods, no analytical results are needed.</p> <p>[...] I have no idea how I can do it in Mathematica</p> </blockquote> <p>The package <a href="https://github.com/antononcube/MathematicaForPrediction/blob/master/Misc/AdaptiveNumericalLebesgueIntegration.m" rel="nofollow noreferrer">AdaptiveNumericalLebesgueIntegration.m</a> has Lebesgue integration strategy and rules implementations and it is discussed in detail in the blog post <a href="https://mathematicaforprediction.wordpress.com/2016/07/01/adaptive-numerical-lebesgue-integration-by-set-measure-estimates/" rel="nofollow noreferrer">"Adaptive numerical Lebesgue integration by set measure estimates"</a> and . </p> <p>Using the profiling capabilities described in the referenced documents the speed of these Lebesgue integration algorithms can be evaluated for integrals of interest. Generally, for usual integrands, the algorithms are slower, but often require much less sampling points than, say, "AdaptiveMonteCarlo", which for expensively to evaluate integrands might produce results faster. </p> <h2>Lebesgue strategy and rules invocation examples</h2> <p>Here are examples of using the integration strategy and rules defined in the package:</p> <pre><code>Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/Misc/AdaptiveNumericalLebesgueIntegration.m"] NIntegrate[Sqrt[x + y], {x, 0, 2}, {y, 0, 1}] (* 2.38176 *) NIntegrate[Sqrt[x + y], {x, 0, 2}, {y, 0, 1}, Method -&gt; {LebesgueIntegration, "Points" -&gt; 2000, "PointGenerator" -&gt; "Sobol", "PointwiseMeasure" -&gt; "VoronoiMesh"}, PrecisionGoal -&gt; 3] (* 2.38236 *) NIntegrate[Sqrt[x + y], {x, 0, 2}, {y, 0, 1}, Method -&gt; {LebesgueIntegrationRule, "Points" -&gt; 6000, "PointGenerator" -&gt; "Sobol", "PointwiseMeasure" -&gt; "VoronoiMesh", "AxisSelector" -&gt; {"MinVariance", "SubsampleFraction" -&gt; 0.05}}, PrecisionGoal -&gt; 3] (* 2.38095 *) NIntegrate[Sqrt[x + y], {x, 0, 2}, {y, 0, 1}, Method -&gt; {GridLebesgueIntegrationRule, "Points" -&gt; 2000, "PointGenerator" -&gt; Random, "GridSizes" -&gt; 6, "AxisSelector" -&gt; Random}, PrecisionGoal -&gt; 3] (* 2.37127 *) </code></pre> <p>In the second integral above the strategy <code>LebesgueIntegration</code> uses the following Voronoi diagram to estimate the set measures:</p> <p><a href="https://i.stack.imgur.com/dhlT7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dhlT7m.png" alt="&quot;VoronoiMeshFor2000SobolPoints&quot;"></a></p> <p>For more details see the mentioned <a href="https://mathematicaforprediction.wordpress.com/2016/07/01/adaptive-numerical-lebesgue-integration-by-set-measure-estimates/" rel="nofollow noreferrer">blog post</a>.</p> <h2>Traces of Lebesgue integration process</h2> <p>Here is an example that demonstrates the "dimension reduction" using the implemented Lebesgue integration strategy for a high dimensional (conventional) integral:</p> <pre><code>res = Reap@NIntegrate[Sqrt[x + y + z], {x, 0, 2}, {y, 0, 1}, {z, 0, 3}, Method -&gt; {LebesgueIntegration, "Points" -&gt; 12000, "PointGenerator" -&gt; Random, "LebesgueIntegralVariableSymbol" -&gt; f}, EvaluationMonitor :&gt; {Sow[f]}, PrecisionGoal -&gt; 4, MaxRecursion -&gt; 7]; res = DeleteCases[res, f, \[Infinity]] (* {10.1842, {{0.344945, 0.426233, 0.584845, 0.809906, 1.07998, 1.37175, 1.66352, ..., 1.50829, 1.51821, 1.53227, 1.54915, 1.56739, 1.58563, 1.6025, 1.61657, 1.62648, 1.63157}}} *) ListPlot[res[[2, 1]], PlotLabel -&gt; Style[Row[{"Integral estimate:", res[]}], Larger]] </code></pre> <p><a href="https://i.stack.imgur.com/bXgaw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bXgaw.png" alt="enter image description here"></a></p> <p>Compare with the result using the default <code>NIntegrate</code> algorithms:</p> <pre><code>NIntegrate[Sqrt[x + y + z], {x, 0, 2}, {y, 0, 1}, {z, 0, 3}] (* 10.2016 *) </code></pre> <h2>References</h2> <p> B. L. Burrows, <a href="http://imamat.oxfordjournals.org/content/26/2/151.accessible-long" rel="nofollow noreferrer">A new approach to numerical integration</a>, 1. Inst. Math. Applics., 26(1980), 151-173.</p> <p> T. He, Dimensionality Reducing Expansion of Multivariate Integration, 2001, Birkhauser Boston. ISBN-13:978-1-4612-7414-8 .</p> <p> A. Antonov, <a href="https://github.com/antononcube/MathematicaForPrediction/blob/master/Documentation/Adaptive-Numerical-Lebesgue-integration-by-set-measure-estimates.pdf" rel="nofollow noreferrer">Adaptive numerical Lebesgue integration by set measure estimates</a>, (2016), <a href="https://github.com/antononcube/MathematicaForPrediction" rel="nofollow noreferrer">MathematicaForPrediction project at GitHub</a>.</p> https://mathematica.stackexchange.com/questions/42047/-/198313#198313 3 Answer by nilo de roock for Is it possible to calculate a Lebesgue integral in Mathematica? nilo de roock https://mathematica.stackexchange.com/users/156 2019-05-14T08:36:13Z 2019-05-14T08:36:13Z <p>I just found a paper, called: "Familiarizing Students with Definition of Lebesgue Integral: Examples of Calculation Directly from Its Definition Using Mathematica" <a href="https://link.springer.com/content/pdf/10.1007%2Fs11786-017-0321-5.pdf" rel="nofollow noreferrer">https://link.springer.com/content/pdf/10.1007%2Fs11786-017-0321-5.pdf</a></p> <p>It might help some, who are looking here for Lebesgue and Mathematica.</p>