MATH 351 Homework 09

We'll use Hamiltonian Monte Carlo to fit linear regression with the following dataset about finches. We aim to fit a model that predicts winglength using a line on the variable middletoelength.

  1. Write two Python functions, each with arguments theta and data. The first will calculate the following log density function. The second Python function shoulud return the gradient of the following log density function with respect to θ=(β0,β1,σ)T\theta = (\beta_0, \beta_1, \sigma)^T. log(f(β0,β1,σ))Nσ0.5e2σn=1N(yn(β0+β1xn))2eσ+σ0.5β020.5β12\log{(f(\beta_0, \beta_1, \sigma))} \propto - N \sigma - 0.5 e^{-2\sigma} \sum_{n=1}^N (y_n - (\beta_0 + \beta_1 x_n))^2 - e^{\sigma} + \sigma - 0.5 \beta_0^2 - 0.5 \beta_1^2
  2. Use Hamiltonian monte carlo to estimate distributions of the unknown parameters θ=(β0,β1,σ)T\theta = (\beta_0, \beta_1, \sigma)^T.
  3. Calculate means and standard deviations of each parameter; don't forget to constrain (re-transform) σ\sigma before calculating means and standard deviations. Make plots for each parameter separately, traceplots and histograms. Make a scatter plot of the two coefficients β\beta and overlay a few sampled points from HMC. All calculations and plots should exclude the warmup points.