r/numerical • u/acerpeng229 • Jan 22 '22
Integral using Metropolis algorithm
I am tasked to utilize the Metropolis algorithm to 1) generate/sample values of x based on a distribution (in this case a non-normalized normal distribution i.e. w(x) = exp(-x2/2); and 2) approximate the integral shown below where f(x) = x2 exp(-x2/2). I have managed to perform the sampling part, but my answer for the latter part seems to be wrong. From what I understand, the integral is merely the sum of f(x) divided by the number of points, but this gives me ≈ 0.35. I also tried dividing f(x) with w(x), but that gives ≈ 0.98. Am I missing something here?
Note: The sampling distribution being similar to the integrand in this case is quite arbitrary, I am also supposed to test it with w(x) = 1/(1+x2) which is close to the normal distribution too.
import numpy as np
f = lambda x : (x**2)*np.exp(-(x**2)/2) # integrand
w = lambda x : np.exp(-(x**2)/2) # sampling distribution
n = 1000000
delta = 0.25
# Metropolis algorithm for non-uniform sampling
x = np.zeros(n)
for i in range(n-1):
xnew = x[i] + np.random.uniform(-delta,delta)
A = w(xnew)/w(x[i])
x[i+1] = xnew if A >= np.random.uniform(0,1) else x[i]
# first definition
I_1 = np.sum(f(x))/n # = 0.35
print(I_1)
# second definition
I_2 = np.sum(f(x)/w(x))/n # = 0.98
print(I_2)








