Solution

ID: past-exam-of-the-chemistry-course-of-the-year-4-of-the-natural-sciences-course-of-the-university-of-cambridge/2022/paper-1/7/b/solution

A weighting function must first be normalized to a probability density. The constant choice on means
Equivalently, transform and use the already normalized on . Independent samples give the unbiased Monte Carlo integration estimator
function uniform_monte_carlo(f, a, b, N):
    require N >= 2
    total = 0
    total_squared = 0
    for j in 1,...,N:
        u = independent_uniform_0_1()
        value = f(a+(b-a)*u)
        total += value
        total_squared += value*value
    average = total/N
    sample_variance = (total_squared-N*average*average)/(N-1)
    integral = (b-a)*average
    standard_error = abs(b-a)*sqrt(sample_variance/N)
    return integral, standard_error
For a general positive normalized importance density , replace each sample contribution by with . Here the uniform choice is the special case of importance sampling that assigns equal weight to every region of equal length.

New to topics? Read the docs here!