Joseph94m/MCMC

Always add an accepted sample

Joseph94m opened this issue · 1 comments

The official design of the metropolis hastings algorithm is to always accept a sample.
accepted.append(x) should be added in the else in the following code.

    x = param_init
    accepted = []
    rejected = []   
    for i in range(iterations):
        x_new =  transition_model(x)    
        x_lik = likelihood_computer(x,data)
        x_new_lik = likelihood_computer(x_new,data) 
        if (acceptance_rule(x_lik + np.log(prior(x)),x_new_lik+np.log(prior(x_new)))):            
            x = x_new
            accepted.append(x_new)
        else:
           >>>>accepted.append(x)<<<<
            rejected.append(x_new)            
                
    return np.array(accepted), np.array(rejected)```

Actually that's not needed. I've done some tests, the results are mostly similar, however it does mess up the plots. Other references do not append x to the list of accepted samples. For instance:
https://stephens999.github.io/fiveMinuteStats/MH_intro.html and https://en.wikipedia.org/wiki/Metropolis–Hastings_algorithm