4 Day 4 (January 26)

4.1 Announcements

  • Please read (and re-read) Ch. 4 in BBM2L before class on Tuesday
    • MCMC
    • Metropolis-Hastings algorithm
  • Assignment 2 is due Sunday Jan. 29

4.2 Numerical Integration

  • Why do we need integrals to do Bayesian statistics?

    • Example using Bayes theorem to estimate population growth rate of whooping cranes
    • Why it is important to keep track of what we are calculating
  • Numerical approximation vs. analytical solutions

  • Definition of a definite integral \[\int_{a}^b f(z)dz = \lim_{Q\to\infty} \sum_{q=1}^{Q}\Delta q f(z_q)\] where \(\Delta q =\frac{b-a}{Q}\) and \(z_q = a + \frac{q}{2}\Delta q\).

  • Riemann approximation (midpoint rule)\[\int_{a}^b f(z)dz \approx \sum_{q=1}^{Q}\Delta q f(z_q)\] where \(\Delta q =\frac{b-a}{Q}\) and \(z_q = a + \frac{2q - 1}{2}\Delta q\).

  • Using similar approach in R (Adaptive quadrature)

    fn <- function(y){dnorm(y,0,1)}
    integrate(f=fn,lower=-4,upper=4,subdivisions=10)
    ## 0.9999367 with absolute error < 4.8e-12

4.3 Monte Carlo Integration

  • Deterministic vs stochastic methods to approximate integrals
    • Work well for high-dimensional multiple integrals
    • Easy to program
  • Monte Carlo integration
    • \[\begin{eqnarray} \text{E}(g(y)) &=& \int g(y)[y|\theta]dy\\ &\approx& \frac{1}{Q}\sum_{q=1}^{Q}g(y_q) \end{eqnarray}\]
    • Examples:
    1. \[\text{E}(y) = \int_{-\infty}^\infty y\frac{1}{\sqrt{2\pi\sigma^2}}\textit{e}^{-\frac{1}{2\sigma^2}(y - \mu)^2}dy\]
    y <- rnorm(n = 10^6, mean = 2, sd = 3)
    mean(y)
    ## [1] 1.998085
    1. \[\text{E}((y-\mu)^2) = \int_{-\infty}^\infty (y-\mu)^2\frac{1}{\sqrt{2\pi\sigma^2}}\textit{e}^{-\frac{1}{2\sigma^2}(y - \mu)^2}dy\]
    y <- rnorm(n = 10^6, mean = 2, sd = 3)
    mean((y - 2)^2)
    ## [1] 8.996605
    1. \[\text{E}(\frac{1}{y} ) = \int_{-\infty}^\infty \frac{1}{y}\frac{1}{\sqrt{2\pi\sigma^2}}\textit{e}^{-\frac{1}{2\sigma^2}(y - \mu)^2}dy\]
    y <- rnorm(n = 10^6, mean = 2, sd = 4)
    mean(1/y)
    ## [1] -0.05182447
  • Live example using z scores