monte_carlo_integrator: One dimensional Monte-Carlo integration

Description Usage Arguments Details Value Examples

View source: R/integrators.R

Description

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.

Usage

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,
  ...
)

Arguments

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 g; must be named

Details

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.

Value

data.frame containing

Examples

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)

shill1729/numerics documentation built on April 17, 2021, 6:46 p.m.