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

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

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) )
```

I thought prior keeps updated with the accepted $P(ΞΈβ²)$. However, the articles seem not to do that way.

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?

Updated December 02, 2018 17:19 PM

- Serverfault Query
- Superuser Query
- Ubuntu Query
- Webapps Query
- Webmasters Query
- Programmers Query
- Dba Query
- Drupal Query
- Wordpress Query
- Magento Query
- Joomla Query
- Android Query
- Apple Query
- Game Query
- Gaming Query
- Blender Query
- Ux Query
- Cooking Query
- Photo Query
- Stats Query
- Math Query
- Diy Query
- Gis Query
- Tex Query
- Meta Query
- Electronics Query
- Stackoverflow Query
- Bitcoin Query
- Ethereum Query