1 of 3

BRMS/Bayes, Lesson 4:

Metropolis Hastings and

Hamiltonian Monte Carlo

Marc Ratkovic

Chair of Social Data Science

Department of Data Science

Department of Political Science

University of Mannheim

2 of 3

Overview

  • Generating Data
  • Trace Plot
  • Autocorrelation
  • Posterior Predictive Check

3 of 3

Plots

Worked through code for the Gibbs sampler can be found in this gist. Begin there, work through the code.

  1. Repeat the analysis, using the Metropolis Hastings algorithm in this gist.
    1. Review the code so that you understand what the function is doing.
    2. You will need to enter V, a 2x2 matrix for generating proposals for and theta = log(sigma2).
    3. Yes, mvrnorm needs to be added figure out how to.
    4. Look at a figure with 5 draws. Does the MH algorithm move to a high posterior region?
    5. Look at 5000 draws–does it get there eventually?
    6. What is the relationship between V and the acceptance rate? Can you get an acceptance rate really high? Really low? (.7 is about right.)
  2. Repeat, using Hamiltonian Monte Carlo code from this gist.