I understand the Metropolis algorithm. Where I get confused is the MH algorithm where asymmetric proposal distributions may be used.
I understand that P(x) and P(x') represent the likelihood/probability density of x and x' according to the target distribution. Likewise, I understand that g(x|x')/g(x'|x) is a term used to correct an asymmetrical proposal distribution. I am not confused by its purpose; I don't understand its execution.
As a toy problem, I've developed an exponential distribution sampler. There are two variations, one that uses a symmetric proposal distribution, the uniform dist. And one that does not: Namely, Beta(a=3,b=2) - 0.5
. I've chosen this distribution because (A) it is asymmetric and mostly positive (but occasionally negative, due to the -0.5 term.)
I have no idea how to find g(x|x')/g(x'|x)
.
Code:
def target(x,lam):
return int(x>0) * lam * np.exp(-x * lam)
def exponential_MH(hops,lam=3):
states = []
burn_in = int(hops*0.2)
current = lam
for i in range(hops):
states.append(current)
# movement = current + random.uniform(-1,1) # does not require asymmetric correction
movement = current + np.random.beta(a=3,b=2)-0.5 # requires asymmetric correction
acceptance = target(x=movement,lam=lam)/target(x=current,lam=lam)
event = random.uniform(0,1)
if acceptance > event:
current = movement
return states[burn_in:]
lam = 1
exp_samples = exponential_MH(hops=10_000,lam=lam)
lines = np.linspace(0,5,10_000)
exp_curve = [lam*np.exp(-l*lam) for l in lines]
plt.hist(exp_samples,normed=1,bins=20)
plt.plot(lines,exp_curve)
Asymmetric proposal distribution plot:
Symmetric proposal distribution plot (with commented lines flipped):
To answer this question, please edit code to reflect a valid g(x|x')/g(x'|x)
for the proposal distribution Beta(a=3,b=2) - 0.5
which perturbations are drawn from.