How Can I Model Poisson(2) with a Metropolis Hastings Algorithm in R?

  • Thread starter chewingum
  • Start date
In summary: Remember to always double check your code and make sure it follows the correct logic. Good luck with your project!In summary, the conversation discusses using a random walk Metropolis-Hastings algorithm in R to model a Poisson distribution with a lambda of 2. The person is struggling to make examples and is seeking guidance on how to properly implement the algorithm. They are provided with a function for the algorithm and an example of how to use it, along with a reminder to double check their code for accuracy.
  • #1
chewingum
1
0
Hi everyone I'm in my final year of university and doing a project on MCMC mainly for applications to bayesian statistics, I think I understand the concept of it so far however I'm struggling to actually make examples.

I'm trying to model Poisson(2) with a random walk metropolis hastings algorithm in R but really don't know where to even start as I've never used R and just confusing myself.

Code:
theta <- 0 
set.seed(1)
k<-1
lambda<-2
lik<-function(lambda)(k^(lambda)*exp(-lambda))/factorial(k)
alpha<-function(theta,phi) min(lik(phi)/lik(theta),1)
THETA<-NULL
b<-runif(1, min=0, max=1)
for(i in 1: 100)
{

    if( alpha(theta,phi) < b) 
    {theta.star<-theta
   }
    else{theta.star<-theta.star} 
    theta = c(theta, theta.star) 
}    
plot(density(theta))

I know this is wrong but I don't think I'm too far off, could anyone point me in the right direction? :)

Thanks
 
Last edited:
Physics news on Phys.org
  • #2
a lot!It looks like you're on the right track. However, there are some mistakes in your code that need to be corrected. Firstly, you should create a function that generates the Metropolis-Hastings algorithm to sample from the posterior distribution. This would take the current position (theta), the prior distribution (lambda) and the likelihood function (lik) as inputs, and return the next position (theta.star). A good starting point is the following:mh_sample <- function(theta, lambda, lik){ alpha <- min(lik(theta)/lik(lambda), 1) b <- runif(1, min = 0, max = 1) if (alpha < b) { theta.star <- theta } else { theta.star <- rnorm(1, mean = theta, sd = 1) } return(theta.star)}Then, you can use this to generate a sequence of samples by looping through the Metropolis-Hastings algorithm. Here's an example of how you could do this:set.seed(1)k <- 1lambda <- 2lik <- function(lambda)(k^(lambda)*exp(-lambda))/factorial(k)theta <- 0THETA <- NULLfor (i in 1:100){ theta.star <- mh_sample(theta, lambda, lik) theta <- theta.star THETA <- c(THETA, theta.star)}plot(density(THETA))Hopefully this helps to clarify how to use the Metropolis-Hastings algorithm for Bayesian statistics.
 

1. What is Metropolis-Hastings in R?

Metropolis-Hastings is a Markov Chain Monte Carlo (MCMC) algorithm used for sampling from a probability distribution. It is commonly used in Bayesian statistics to estimate the posterior distribution of a parameter.

2. How does Metropolis-Hastings work in R?

In R, the Metropolis-Hastings algorithm involves proposing a new sample from a candidate distribution and then calculating an acceptance probability based on the ratio of the candidate distribution to the current distribution. If the acceptance probability is greater than a randomly generated number, the new sample is accepted and becomes the current sample. Otherwise, the current sample is kept and the process repeats.

3. What are the benefits of using Metropolis-Hastings in R?

Metropolis-Hastings in R allows for sampling from complex probability distributions that cannot be easily sampled from using traditional methods. It also allows for the estimation of high-dimensional parameters and can be easily implemented in R using built-in functions or packages.

4. What are the limitations of Metropolis-Hastings in R?

One limitation of Metropolis-Hastings in R is that it can be computationally expensive and may require a large number of iterations to obtain accurate estimates. It also relies on the choice of the candidate distribution, which can impact the efficiency and accuracy of the algorithm.

5. How can I assess the convergence and performance of Metropolis-Hastings in R?

There are several methods to assess the convergence and performance of Metropolis-Hastings in R, such as visual inspection of trace plots and autocorrelation plots, calculating the effective sample size, and conducting convergence diagnostics tests. Additionally, it is recommended to run multiple chains with different starting values to ensure consistency in the results.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
865
Replies
3
Views
624
  • Introductory Physics Homework Help
Replies
6
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
2K
  • Special and General Relativity
Replies
11
Views
199
  • Advanced Physics Homework Help
Replies
10
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
2K
Back
Top