# MCMC - how to compute prior(𝜃)

by mon   Last Updated May 16, 2019 01:19 AM

# Question

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

The likelihood $$P(D|𝜃)$$ will be:

``````def likelihood(mean, sigma, data):
n = len(data)
return -0.5 * n * log(2*np.pi) - n * log(sigma) - np.sum( ((data-mean)**2) / (2*sigma**2) )
`````` ## Prior

I thought prior keeps updated with the accepted $$P(θ′)$$. However, the articles seem not to do that way.

``````def prior(x):
#x = mu, x=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 <=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?

Tags :

## Related Questions

Updated June 03, 2019 12:19 PM

Updated November 30, 2018 09:19 AM

Updated December 02, 2018 17:19 PM

Updated March 14, 2019 19:19 PM

Updated April 22, 2017 09:19 AM