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?

enter image description here

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

enter image description here

Prior

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?

Tags : bayesian mcmc


Related Questions


Updated November 30, 2018 09:19 AM

Updated March 14, 2019 19:19 PM

Updated April 22, 2017 09:19 AM

Updated May 18, 2018 03:19 AM