目录
MCMC(一)蒙特卡罗方法 https://www.cnblogs.com/emanlee/p/12356492.html
MCMC(二)马尔科夫链 https://www.cnblogs.com/emanlee/p/12357341.html
MCMC(三)MCMC采样和M-H采样 https://www.cnblogs.com/emanlee/p/12358022.html
MCMC(四)Gibbs采样 https://www.cnblogs.com/emanlee/p/12358194.html
其中 %matplotlib inline 的解释见 https://www.cnblogs.com/emanlee/p/12358088.html
import random import math from scipy.stats import norm import matplotlib.pyplot as plt %matplotlib inline def norm_dist_prob(theta): y = norm.pdf(theta, loc=3, scale=2) return y T = 5000 pi = [0 for i in range(T)] sigma = 1 t = 0 while t < T-1: t = t + 1 pi_star = norm.rvs(loc=pi[t - 1], scale=sigma, size=1, random_state=None) alpha = min(1, (norm_dist_prob(pi_star[0]) / norm_dist_prob(pi[t - 1]))) u = random.uniform(0, 1) if u < alpha: pi[t] = pi_star[0] else: pi[t] = pi[t - 1] plt.scatter(pi, norm.pdf(pi, loc=3, scale=2)) num_bins = 50 plt.hist(pi, num_bins, normed=1, facecolor='red', alpha=0.7) plt.show()
from