Learn R Programming

animation (version 2.0-0)

MC.samplemean: Sample Mean Monte Carlo integration.

Description

Sample Mean Monte Carlo integration. Integrate a function from 0 to 1 using the Sample Mean Monte Carlo algorithm

Usage

MC.samplemean(FUN = function(x) x - x^2, n = ani.options("nmax"), 
    col.rect = c("gray", "black"), adj.x = TRUE, ...)

Arguments

FUN
the function to be integrated
n
number of points to be sampled from the Uniform(0, 1) distribution
col.rect
colors of rectangles (for the past rectangles and the current one)
adj.x
should the locations of rectangles on the x-axis be adjusted? If TRUE, the rectangles will be laid side by side and it is informative for us to assess the total area of the rectangles, otherwise the rectangles will be laid at their exact
...
other arguments passed to rect

Value

  • A list containing
  • xthe Uniform random numbers
  • yfunction values evaluated at x
  • nnumber of points drawn from the Uniform distribtion
  • estthe estimated value of the integral

Details

Sample Mean Monte Carlo integration can compute $$I=\int_0^1 f(x) dx$$ by drawing random numbers $x_i$ from Uniform(0, 1) distribution and average the values of $f(x_i)$. As $n$ goes to infinity, the sample mean will approach to the expectation of $f(X)$ by Law of Large Numbers. The height of the $i$-th rectangle in the animation is $f(x_i)$ and the width is $1/n$, so the total area of all the rectangles is $\sum f(x_i) 1/n$, which is just the sample mean. We can compare the area of rectangles to the curve to see how close is the area to the real integral.

References

http://animation.yihui.name/compstat:sample_mean_monte_carlo

See Also

integrate, MC.hitormiss

Examples

Run this code
oopt = ani.options(interval = 0.2, nmax = ifelse(interactive(), 
    50, 2))
par(mar = c(4, 4, 1, 1))

## when the number of rectangles is large, use border = NA
MC.samplemean(border = NA)$est

integrate(function(x) x - x^2, 0, 1)

## when adj.x = FALSE, use semi-transparent colors
MC.samplemean(adj.x = FALSE, col.rect = c(rgb(0, 0, 
    0, 0.3), rgb(1, 0, 0)), border = NA)

## another function to be integrated
MC.samplemean(FUN = function(x) x^3 - 0.5^3, border = NA)$est

integrate(function(x) x^3 - 0.5^3, 0, 1)

## HTML animation page
saveHTML({
    ani.options(interval = 0.3, nmax = ifelse(interactive(), 
        50, 2))
    MC.samplemean(n = 100, border = NA)
}, img.name = "MC.samplemean", htmlfile = "MC.samplemean.html", 
    title = "Sample Mean Monte Carlo Integration", description = c("", 
        "Generate Uniform random numbers", "and compute the average", 
        "function values."))

ani.options(oopt)

Run the code above in your browser using DataLab