샘플링하고자 하는 대상 분포(target_distribution)에서 샘플링하기 어려운 경우 사용할 수 있는 가장 간단한 방법
상대적으로 샘플링하기 쉬운 제안분포(propasal distribution)에서 샘플링 한 후, 해당 샘플이 대상 분포에서 나올 수 있으면 취하고 그렇지 않은 경우는 버려서(=기각) 대상 분포를 근사한다.


$$ P(x) = \frac{0.3}{1.5*\sqrt{2\pi}}e^{-\frac{1}{2}(\frac{x+3}{1.5})^2} + \frac{0.7}{1.5*\sqrt{2\pi}}e^{-\frac{1}{2}(\frac{x-5}{1.5})^2} $$
import math
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
def p(x_star):
return 0.3/(1.5*math.sqrt(2*math.pi))*math.exp(-(1/2)*(x_star+3)**2) + 0.7/(1.5*math.sqrt(2*math.pi))*math.exp(-(1/2)*(x_star-5)**2)
sample_size = 5000
accepted_u = []
accepted_x = []
a, b, C = -8, 12, 4
while len(accepted_u) < sample_size:
x_star = np.random.uniform(a, b, 1)[0]
u = np.random.uniform(0, 1, 1)[0]
if u <= p(x_star)/(C*sp.stats.uniform.pdf(x_star, a, b-a)):
accepted_u.append(u)
accepted_x.append(x_star)
plt.scatter(accepted_x, accepted_u)
plt.show()
