# /Segment39

## To Calculate

1. Suppose the domain of a model are the five integers $x = \{1,2,3,4,5\}$, and that your proposal distribution is: "When $x_1 = 2,3,4$, choose with equal probability $x_2 = x_1 \pm 1$. For $x_1=1$ always choose $x_2 =2$. For $x_1=5$ always choose $x_2 =4$. What is the acceptance probability $\alpha(x_1,x_2)$ for all the possible values of $x_1$ and $x_2$?

2. Suppose the domain of a model is $-\infty < x < \infty$ and your proposal distribution is (perversely),

$q(x_2|x_1) = \begin{cases}\tfrac{7}{2}\exp[-7(x_2-x_1]),\quad & x_2 \ge x_1 \\ \tfrac{5}{2}\exp[-5(x_1-x_2)],\quad & x_2 < x_1 \end{cases}$

Sketch this distribution as a function of $x_2-x_1$. Then, write down an expression for the acceptance probability $\alpha(x_1,x_2)$.

Zoom-in view

import numpy as np
import matplotlib.pyplot as plt
import math

def func(dx21):
if dx21<0:
q=5/2*math.exp(5*dx21)
else:
q=7/2*math.exp(-7*dx21)
return q
v=np.arange(-5,5, 0.001)
x=[]
fx=[]
for i in v:
x.append(i)
fx.append(func(i))
plt.plot(x, fx)
plt.show()



when $x_2 \geq x_1$,

$\alpha(x_1, x_2) = min(1, \frac {\pi(x_2)\frac 52 exp(-5(x_1-x_2))}{\pi(x_1) \frac 72 exp(-7(x_2-x_1))}$

when $x_2 < x_1$,

$\alpha(x_1, x_2) = min(1, \frac {\pi(x_2) \frac 72 exp(-7(x_2-x_1))}{\pi(x_1)\frac 52 exp(-5(x_1-x_2))}$