User Tools

Site Tools


cs401r_w2016:lab10

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cs401r_w2016:lab10 [2016/03/17 20:34]
admin
cs401r_w2016:lab10 [2021/06/30 23:42] (current)
Line 24: Line 24:
 {{ :​cs401r_w2016:​lab9_t3.png?​direct&​400 |}} {{ :​cs401r_w2016:​lab9_t3.png?​direct&​400 |}}
  
-Your notebook should produce similar plots for the HMC algorithm, although you only need to produce two plots (one histogram, and one state evolution plot, instead of three of each).+Your notebook should produce similar plots for the HMC algorithm, although you only need to produce two plots (one histogram, and one state evolution plot, instead of three of each). So to clarify, your notebook should have 6 plots for part one (three histograms, three evolution plots) and two plots for part two (one histogram, one evolution plot).
  
 Your notebook should also include a small writeup of your results, as described below. Your notebook should also include a small writeup of your results, as described below.
Line 46: Line 46:
 <code python> <code python>
 import numpy as np import numpy as np
-def p( x, t=1.0 ): +def p( x, temperature=1.0 ): 
-    return np.exp( -10*t*((x-2)**2) ) + 0.3*np.exp( -0.5*10*t*((x+1)**2) )+    return np.exp( -10*temperature*((x-2)**2) ) + 0.3*np.exp( -0.5*10*temperature*((x+1)**2) )
 </​code>​ </​code>​
  
Line 58: Line 58:
 For each different proposal distribution,​ you should run your MCMC chain for 10,000 steps, and record the sequence of states. ​ Then, you should produce a visualization of the distribution of states, and overlay a plot of the actual target distribution. ​ They may or may not match (see, for example, the first example plot in the Description section). For each different proposal distribution,​ you should run your MCMC chain for 10,000 steps, and record the sequence of states. ​ Then, you should produce a visualization of the distribution of states, and overlay a plot of the actual target distribution. ​ They may or may not match (see, for example, the first example plot in the Description section).
  
-Furthermore,​ for each proposal distribution,​ you should run three independent chains (you can do these sequentially or in parallel, as you like). ​ You should display each of these three chains on a single plot with time on the x-axis and the state on the y-axis. ​ Ideally, you will see each of the three chains mixing between two modes.+Furthermore,​ for each proposal distribution,​ you should run three independent chains (you can do these sequentially or in parallel, as you like). ​ You should display each of these three chains on a single plot with time on the x-axis and the state on the y-axis. ​ Ideally, you will see each of the three chains mixing between two modes; you may notice other features of the behavior of the samplers as well, which you should report in your writeup!
  
 **Part 2: Hamiltonian MCMC** **Part 2: Hamiltonian MCMC**
  
-For this part, you will code the Hamiltonian MCMC algorithm, as discussed in class. ​ To do this, you will need to compute the gradient of the density function with respect to the state. ​ An easy easy way to do this is to use the [[https://​github.com/​HIPS/​autograd|autograd]] package:+For this part, you will code the Hamiltonian MCMC algorithm, as discussed in class. You will run three independent chains and report them in the same graphs.  To do this, you will need to compute the gradient of the density function with respect to the state. ​ An easy easy way to do this is to use the [[https://​github.com/​HIPS/​autograd|autograd]] package:
 <code python> <code python>
 from autograd import grad from autograd import grad
Line 76: Line 76:
 A detailed explanation of Hamiltonian MCMC can be found here:​[[http://​www.mcmchandbook.net/​HandbookChapter5.pdf|Hamiltonian MCMC]]. A detailed explanation of Hamiltonian MCMC can be found here:​[[http://​www.mcmchandbook.net/​HandbookChapter5.pdf|Hamiltonian MCMC]].
  
-You will find the equations describing the leapfrog method in Equations 5.18, 5.19 and 5.20. +  * You will find the equations describing the leapfrog method in Equations 5.18, 5.19 and 5.20. 
-You will find a description of how to convert a given ''​p(x)''​ into a Hamiltonian in Section 5.3.1. +  ​* ​You will find a description of how to convert a given ''​p(x)''​ into a Hamiltonian in Section 5.3.1. 
-You will find a description of the complete HMC algorithm in section 5.3.2.1+  ​* ​You will find a description of the complete HMC algorithm in section 5.3.2.1
  
-Remember that you will alternate between two steps: ​+Remember that you will alternate between two steps: 
 + 
 +  - Sampling the momentum conditioned on the position. ​ This is just sampling from a Gaussian. 
 +  - Proposing a new state for the position, given the momentum. ​ This involves integrating the dynamics, and then accepting or rejecting based on integration error.
  
 You will have to tune two parameters in order to implement HMC: the variance of the momentum variables, and the timestep used for integrating the dynamics. ​ Experiment with both, and report your results using plots like those you prepared for Part 1. You will have to tune two parameters in order to implement HMC: the variance of the momentum variables, and the timestep used for integrating the dynamics. ​ Experiment with both, and report your results using plots like those you prepared for Part 1.
Line 89: Line 92:
 You have now coded two different inference algorithms, and a few variants of each.  For this section, you must provide a small write-up that compares and contrasts each.  Answer at least the following questions: You have now coded two different inference algorithms, and a few variants of each.  For this section, you must provide a small write-up that compares and contrasts each.  Answer at least the following questions:
  
-- What was the acceptance rate of each algorithm? ​ (ie, what percentage of proposals were accepted) +  ​- What was the acceptance rate of each algorithm? ​ (ie, what percentage of proposals were accepted) 
-- Why don't some inference algorithms explore both modes of the density? +  - Why don't some inference algorithms explore both modes of the density? 
-- Why do some algorithms stay in the same state repeatedly? ​ Is this good or bad? +  - Why do some algorithms stay in the same state repeatedly? ​ Is this good or bad? 
-- What were the best values for the variance of the momentum variables and the timestep you found? ​ How did you know that they were good?+  - What were the best values for the variance of the momentum variables and the timestep you found? ​ How did you know that they were good?
  
 ---- ----
cs401r_w2016/lab10.1458246894.txt.gz · Last modified: 2021/06/30 23:40 (external edit)