Question
How to compute the prior $P(𝜃)$ and $P(𝜃')$ in MCMC when calculating the posteriors?
Prior
I thought prior keeps updated with the accepted θ'. However, the way it is computed in the articles referenced seem to do different ways.
From Scratch: Bayesian Inference, Markov Chain Monte Carlo and Metropolis Hastings, in python is using 1.
def prior(x):
#x[0] = mu, x[1]=sigma (new or current)
#returns 1 for all valid values of sigma. Log(1) =0, so it does not affect the summation.
#returns 0 for all invalid values of sigma (<=0). Log(0)=-infinity, and Log(negative number) is undefined.
#It makes the new sigma infinitely unlikely.
if(x[1] <=0):
return 0
return 1
MCMC sampling for dummies keeps using the initial prior, mu_prior_mu=0, mu_prior_sd=1. (In this article, log is applied to the P, hence using norm.pdf directly).
def sampler(data, samples=4, mu_init=.5, proposal_width=.5, plot=False, mu_prior_mu=0, mu_prior_sd=1.):
...
# Compute prior probability of current and proposed mu
prior_current = norm(mu_prior_mu, mu_prior_sd).pdf(mu_current)
prior_proposal = norm(mu_prior_mu, mu_prior_sd).pdf(mu_proposal)
Metropolis-Hastings sample also keep using the initial prior. In this article, the distribution is binomial so using beta function.
def target(lik, prior, n, h, theta):
if theta < 0 or theta > 1:
return 0
else:
return lik(n, theta).pmf(h)*prior.pdf(theta) # <--- prior = st.beta(a, b) where a = 10, b = 10
n = 100
h = 61
a = 10
b = 10
lik = st.binom
prior = st.beta(a, b)
sigma = 0.3
for i in range(niters):
theta_p = theta + st.norm(0, sigma).rvs()
rho = min(1, target(lik, prior, n, h, theta_p)/target(lik, prior, n, h, theta ))
u = np.random.uniform()
if u < rho:
naccept += 1
theta = theta_p
Are these correct ways? If so, kindly explain why no need to use the updated 𝜃.