Simple Monte Carlo Integration Example

Lower limit

A = 0

Upper limit

B = 1

Number of samples

N = 100

Function to integrate

f x = x 3

Foreign function interface

ξ = drand48

Sample value

x i = A + ( B - A ) ξ

Approximation to integral

Integral = 1 N i = 1 N f ( x i )

Variance in approximation to integral

Variance = 1 N i = 1 N f ( x i ) 2 - ( 1 N i = 1 N f ( x i ) ) 2

Standard error in approximation to integral

StandardError = Variance ( N - 1 )