Posterior predictive test quantities I've been trying to figure out problem 6.2 from Gelman's book, second edition, page 192 on Bayesian data analysis. Can anyone help?
a) Set up predictive test quantities to check the following assumptions:


*

*independent Poisson

*no trend over time


b) use simulations from the posterior predictive distributions to measure the discrepancies, display graphically and give p-values.
 A: a) You want to come up with a function of the data ideally producing values which are very different when the assumption is violated from when the assumption is true. 
For example, if we wanted to test the Poisson assumption itself, we could estimate the Fano factor from the data. For Poisson distributions the Fano factor should be close to 1 and our hope would be that if the distribution is not Poisson, this will show up in the Fano factor.
One alternative hypothesis to the independence assumption could be that consecutive data points are close to each other, i.e., $y_t$ is close to $y_{t - 1}$ for all $t$. What would a reasonable function of $y_1, ..., y_T$ be that could measure whether this is the case?
b) What you need to do is to sample $N$ times from the posterior distribution over Poisson rates, 
$$p(\theta \mid y_1, ..., y_T),$$
and then for each sample $\theta_n$ replicate the data by sampling from $p(y_1, ..., y_T \mid \theta_n)$. This gives you $N$ replicated datasets $y_1^n, ..., y_T^n$ for which you evaluate your test statistic, giving you $N$ values $f_1, ..., f_N$ which you can plot and use to estimate the Bayesian $p$-value.
If you don't know how to sample from the posterior over $\theta$, recapitulate conjugate priors and take a look at the gamma distribution.
