Homework 5. Bayes AI

Exercise 1 (Gibbs: Bird feeders) A biologist counts the number of sparrows visiting six bird feeders placed on a given day.

Feeder Number of Birds
1 11
2 22
3 13
4 24
5 19
6 16
  • Assume that the bird counts are independent Poisson random variables with feeder- dependent means \(\lambda_i\), for \(i=1,\ldots,6\).
  • Assume that the means \(\lambda_i\) are independent and identically distributed gamma random variables with shape \(a\) and scale \(b\) (or equivalently, shape $a and mean \(m = ab\) )
  • The mean \(m = ab\) of the Gamma distribution is uniformly distributed on a grid of 200 equally spaced values starting at 5 and ending at 40.
  • The shape \(a\) is independent of the mean \(m\) and has a distribution that takes values on a grid of 200 equally spaced points starting at 1 and ending at 50, with prior probabilities proportional to a gamma density with shape \(1\) and scale \(5\).
  1. Use Gibbs sampling to draw 10000 samples from the joint posterior distribution of the mean \(m\), the shape parameter \(a\), and the six mean parameters \(\lambda_i\), \(i=1,\ldots,6\), conditional on the observed bird counts. Using your sample, calculate 95% credible intervals for the mean m, the shape a, and the six mean parameters \(\lambda_i\), \(i=1,\ldots,6\).
  2. Find the effective sample size for the Monte Carlo samples of the mean \(m\), the shape parameter \(a\), and the six mean parameters \(\lambda_i\), \(i=1,\ldots,6\). The Effective Sample Size (ESS) in Markov Chain Monte Carlo (MCMC) is a measure of the information content or effectiveness of a sample chain. It quantifies how many independent samples would be equivalent to the samples obtained from an MCMC process, which inherently produces correlated samples due to its Markovian nature. Simply speaking, it is the sample size adjusted for autocorrelation. ESS is typically calculated using the formula:

\[ ESS = n \frac{\lambda^2}{\sigma^2} \]

  • \(n\) is the actual number of samples,
  • \(\lambda^2\) is the variance of the samples if they were independent (often referred to as the “lag-zero” variance),
  • \(\sigma^2\) is the variance of the sample mean, which includes the effects of autocorrelation among the samples.

In practice, \(\sigma^2\) can be estimated using the autocovariance function of the Markov chain, which accounts for the correlations between samples at different lags. You can use the effectiveSize function in the coda package to calculate the effective sample size.

  1. Do traceplots for the mean m, the shape parameter a, and the six rate parameters \(\lambda_i\), \(i=1,\ldots,6\). You can use the traceplot and acf functions in the coda package to create traceplots and autocorrelation functions. Discuss the results.
  2. The fourth feeder had the highest bird count and the first feeder had the lowest bird count. Use your Monte Carlo sample to estimate the posterior probability that the first feeder has a smaller mean bird count than the fourth feeder. Explain how you obtained your estimate.
  3. Discuss your results.