If we want to calculate int_0^1 1/sqrt(2pi) (-x^2/2) dx, there is no easy integral table for use to get the exact value. While, we can use Monte Carlo simulation, based on the law of large numbers. The following is R code.
MCnormal <- function(n) {
a <- runif(n)
b <- dnorm(a)
c <- mean(b)
real <- pnorm(1) - pnorm(0)
diff <- abs(real - c)/ real
return(list(simulated = c, real = real, diff = diff))
}
If we want to calculate int_0^1 1/sqrt(2pi) (-x^2/2) dx, there is no easy integral table for use to get the exact value. While, we can use Monte Carlo simulation, based on the law of large numbers. The following is R code.
The large the n, the more precise the estimation.
When n comes to 1e6, the difference is only .006%, which is pretty negligible.