My purpose here is to show a Riemann approximation
for $\int_0^1 x^2\, dx = 1/3$ with enough rectangles to approximate the integral.
Then to do a Monte Carlo integration in which
uniformly chosen points in the interval of integration
are substituted for centers of bases of rectangles.
# Riemann approx with m rectangles
m = 1000; a = 0; b = 1
w = (b-a)/m # rectangle widths
d = seq(a+w/2,b-w/2, len=m) # centers
h = d^2 # rectangle heights
sum(w*h) # rectamg;e areas
[1] 0.3333332
# MC emulates Riemann
# with random uniform grid
m = 10^6; a=0; b=1
w = (b-a)/m # "average width"
d = runif(m, a, b)
h = d^2
sum(w*h)
[1] 0.3332943
Addendum 1: per question in comment by @Dave: MC approximation of the density function of $\mathsf{Beta}(2,2)$ to verify it integrates to unity.
# Approx integration: BETA(2,2) density
m = 10^6; a=0; b=1
w = (b-a)/m
d = runif(m, a, b)
h = dbeta(d, 2, 2) # BETA(2,2) PDF
sum(w*h)
[1] 1.000299 # aprx 1
Addendum 2: About extending MC to multiple dimensions.
Suppose we want to verify the probability $0.3413^2 = 0.1165$ in the unit square under a standard bivariate normal distribution. We put points uniformly at
random in the unit square and sum their corresponding densities:
set.seed(1234)
m = 10^4; u1=runif(m); u2 = runif(m)
h = dnorm(u1)*dnorm(u2)
mean(h) # mc aprx
[1] 0.1163528
diff(pnorm(c(0,1)))^2 # exact
[1] 0.1165162
Also, we find the probability $0.0677$ of the standard bivariate normal distribution within the triangle with vertices $(0,0), (0,1), (1.0).$ We put points uniformly at random in the triangle, sum the corresponding values of the density, and multiply by the area $1/2$ of the triangle. [The exact value can be obtained by symmetry, using a 45-degree rotation.]
h.acc = h[u1+u2<=1]
.5*mean(h.acc) # mc aprx
[1] 0.06768097
diff(pnorm(c(sqrt(1/2),0)))^2 # exact
[1] 0.06773003
Perhaps see
this Q&A for additional MC integration methods.
function
to define the specific function, thenintegrate
(with your function asintegrand
and appropriate limits) to get the integral. $\endgroup$upper - lower
: Try finding your integral for a range which is not of width $1$, for example $\int\limits_{0.4}^{0.41} x^2\,dx$. You will get a lot of random values between $0.4^2=0.16$ and $0.41^2=0.1681$ with an average of around $0.164$. But that is far too high for the integral itself which, considering bounding rectangles for the area under the curve, must be between $0.16(0.41-0.4)$ and $0.1681(0.41-0.4)$ because of the narrowness of the integral. Hence the need to scale byupper - lower
. The correct answer is about $0.001640333$ $\endgroup$