Studying Bayesian Inference and Markov chain Monte Carlo (MCMC) algorithms, I am facing a self study question on a MCMC approach to a Poisson distribution with parameter $\lambda$.
Using R, my code is:
### Parameters
# observation
x <- 60
# alpha parameter
a <- 10
# beta parameter
b <- 15
First step is to find the posterior distribution for Poisson with parameter $\lambda$ through a prior distribution and likelihood distribution. After some calculations, I got to find my posterior distribution as a Gamma with parameters alpha $(x+a)$ and beta $(b/(b+1))$.
ap <- x+a
bp <- b/(b+1)
Then, the last step is to perform a MCMC simulation. I am doing 5000 iterations with initial value 20.
inter = 5000
MCMC<- rep(0, inter+1)
MCMC[1] <- 20
for(i in 1:inter) {
lam <- rbinom(1,100,MCMC[i]/(100))
numerator <- dgamma(lam,ap,scale=bp)
denominator <- dgamma(MCMC[i],ap,scale=bp)
alfa <- min(1, (numerator/denominator))
print(alfa)
if (runif(1,0,1) <= alfa){
(MCMC[i+1] <- lam)
} else {
(MCMC[i+1] = MCMC[i])
}
}
plot(0:inter, MCMC, type="l", main="MCMC", ylim=c(0,100))
I am not sure if my approach is correct: I am trying to simulate a proposal for the acceptance through a binomial distribution. Then, I do the ratio between the first value of my MCMC and the proposal.