How to compute the prior $P(𝜃)$ and $P(𝜃')$ in MCMC when calculating the posteriors?

enter image description here


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
        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 𝜃.


1 Answer 1


Whenever we have a prior on a parameter $p(\theta)$, and some likelihood function $p(\mathcal{D} | \theta)$, the posterior is proportional to

$$\pi(\theta) \propto p(\mathcal{D} | \theta) p(\theta),$$

since the evidence does not depend on $\theta$ - it has been averaged out. Now, if you want to run a MCMC sampler that targets the density $\pi(\theta)$, you define a proposal mechanism $\theta' \sim q(\theta'|\theta)$. Then your Metropolis-Hastings ratio is

$$\frac{\pi(\theta')q(\theta|\theta')} {\pi(\theta)q(\theta'|\theta)} = \frac{p(\mathcal{D} | \theta') p(\theta')q(\theta|\theta')} {p(\mathcal{D} | \theta) p(\theta)q(\theta'|\theta)},$$

so as you can see the prior does enter into the acceptance ratio. This also illustrates a nice principle if you choose your prior as your proposal mechanism, such that $\theta' \sim p(\theta')$. In this particular case the priors cancel out with the proposal ratio, and you are now just comparing the likelihood of the relative points.

  • $\begingroup$ Thanks for the follow up. However, kindly provide an answer if the prior calculations in the articles are correct, and why? I appreciate if you kindly help and provide the proposal ratio in the calculations and where it came from. $\endgroup$
    – mon
    May 18, 2019 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.