Metropolis Hasting Algorithm. Whats going wrong?

39 Views Asked by At

Hi I wrote a Langevin Metropolis-Hasting Algorithm on python to evaluate

$$ E[\|X\|^2], $$

where $X\sim \pi$, with

$$\pi(x)= \frac{1}{2\sqrt{2d}}e^{\frac{1}{2}\|x-a\|^2-\frac{1}{2}\|x+a\|^2},~~a_i=\frac{1}{\sqrt{2d}}~\forall i=1,\dots d$$

a Gaussian mixture. I.e I built a Markov Chain for some fixed $h>0$ and $\xi_i\sim N(0,I)$

$$ \tilde{x}_{i+1}:=x_i+ h \nabla \log \pi(x_i) +\sqrt{2 h} \xi_i$$

and setting $x_{i+1}$ according to

\begin{align} &\mathbb{P}( x_{i+1}=\tilde{x}_{i+1})=\alpha(\tilde{x}_{i+1},x_i) \\ & \mathbb{P}( x_{i+1}=x_i)=1-\alpha(\tilde{x}_{i+1},x_i). \end{align}

where the acceptance probability is

$$ \alpha(x,y)=\min(1,\frac{\pi(\tilde{x}_{i+1})\exp\Big( -\frac{1}{4h}\| x_i-\tilde{x}_{i+1}-h\nabla \log \pi(\tilde{x}_{i+1}) \|^2 \Big)}{\pi(x_i)\exp\Big( -\frac{1}{4h}\| \tilde{x}_{i+1}-x_i-h\nabla \log \pi(x_i) \|^2 \Big)}). $$

I calculate $\nabla \log \pi(x)=-2x$.

$\textbf{My question :}$ Does the above look ok ? when I run my code sometimes it works perfectly other times it gets stuck why could this be?

1

There are 1 best solutions below

2
On

Your proposals and acceptance probability seem correct to me.

The main issue I see here, is that you mention "some" $h>0$. Such parameter usually need to be tuned to make sure that your proposals are efficient. Have you done it ?

Cheers!