MCMC采样和M-H采样
Posted emanlee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MCMC采样和M-H采样相关的知识,希望对你有一定的参考价值。
其中 %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
https://www.cnblogs.com/pinard/p/6638955.html
以上是关于MCMC采样和M-H采样的主要内容,如果未能解决你的问题,请参考以下文章