Description Usage Arguments Details Value Examples
One dimensional integrals over an interval can be approximated via sample means of (functions of) uniformly distributed random variables, via the SLLN and error estimates can be obtained via CLT. This naive approach is not ideal for multi-dimensional integrals, however. The integrand must be known.
1 2 3 4 5 6 7 8 9 | monte_carlo_integrator(
g,
a = 0,
b = 1,
naive = FALSE,
n = 10^6,
sig_lvl = 0.05,
...
)
|
g |
the known function to integrate over a given interval |
a |
(finite) left endpoint of the given interval |
b |
(finite) right endpoint of the given interval |
naive |
boolean whether to naive estimate or efficient estimator, see details. |
n |
number of variates to simulate |
sig_lvl |
significance level of confidence intervals |
... |
additional arguments to pass to the function |
The naive estimate computes the sample mean of g(U) over an IID sample of uniformly distributed RVs, U_1,...,U_n while the efficient estimator uses the sample mean of 0.5(g(U)+g(a+b-U)), which can be shown to have a lower variance than the aforementioned.
data.frame containing
estimate the point estimate of the integral,
lb the lower bound of the confidence interval,
ub the upper bound of the confidence interval,
std_error the standard error of the point-estimate.
1 2 3 4 | # Sophomore's dream: value to 10 decimals is 0.7834305107...
monte_carlo_integrator(function(x) x^x, 0, 1)
# Integral related to gamma function: exact value is Gamma(p+1), here p = 2
monte_carlo_integrator(function(x) log(1/x)^2, 0, 1)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.