Diagnosing the samples

This section is focused on diagnostic samples for Metropolis and NUTS. Since we are approximating the posterior with a finite number of samples, is important to check whether we have a valid sample—otherwise any analysis from it will be totally flawed. There are several tests we can perform, some are visual and some are quantitative. These tests are designed to spot problems with our samples, but they are unable to prove we have the correct distribution; they can only provide evidence that the sample seems reasonable. If we find problems with the sample, the are many solutions to try:

  • Increase the number of samples.
  • Remove a number of samples from the beginning of the trace. This is know as burn-in. The PyMC3 tuning phase helps reduce the need for burn-in.
  • Modify sampler parameters, such as increasing the length of the tuning phase, or increase the target_accept parameter for the NUTS sampler. Under certain circumstances, PyMC3 will provide suggestions on what to change.
  • Re-parametrize the model, that is, express the model in a different but equivalent way.
  • Transform the data. We already saw an example of this, in Chapter 4Generalizing Linear Models, and Chapter 5Model Comparison, we show that centering the data improves sampling from linear models.

To make the explanations concrete, we are going to use a minimalist hierarchical model, with two parameters: a global parameter, a, and a local parameter, b (the per group parameter). And that's all, we do not even have likelihood/data in this model! I am omitting the data here to emphasize that some of the properties we will discuss (especially in the section Divergences) are related to the structure of the model and not the data. We will discuss two alternative parameterizations of the same model:

with pm.Model() as centered_model:
a = pm.HalfNormal('a', 10)
b = pm.Normal('b', 0, a, shape=10)
trace_cm = pm.sample(2000, random_seed=7)

with pm.Model() as non_centered_model:
a = pm.HalfNormal('a', 10)

b_shift = pm.Normal('b_offset', mu=0, sd=1, shape=10)
b = pm.Deterministic('b', 0 + b_shift * a)
trace_ncm = pm.sample(2000, random_seed=7)

The difference between the centered and non-centered models is that for the former we fit the group-level parameter directly, and for the later we model the group-level parameter as a shifted and scaled Gaussian. We will explore the differences using several plots and numerical summaries.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.212.196