I'm giving part of a presentation on numerical integration. While the talk itself will go into better forms of numerical integration (mainly importance sampling and stratified sampling), I'm mentioning during part of my section Monte Carlo integration sampling from the uniform distribution.
I've found that:
mean(sin(runif(1e8, 0, pi)))
is giving an answer of 0.636597
, rather than 1
that is expected. This answer seems pretty consistent with increasing sample size, and I'm unsure why there's so much error. Other computations such as:
mean(sin(runif(1e6, 0, 2 * pi)))
give 0.0005398996
, much closer to the expected answer of 0
.
Can someone help me see why
mean(sin(runif(1e8, 0, pi)))
is giving such an inaccurate answer? Is this user error, or is it to be expected when sampling from the uniform distribution?