Diagnosing MCMC results

Pints provides a number of functions to diagnose MCMC progress and convergence.

Overview:

pints.rhat(chains, warm_up=0.0)[source]

Returns the convergence measure \(\hat{R}\) for the approximate posterior according to [1].

\(\hat{R}\) diagnoses convergence by checking mixing and stationarity of \(m\) chains (at least two, \(m\geq 2\)). To diminish the influence of starting values, the first portion of each chain can be excluded from the computation. Subsequently, the truncated chains are split in half, resulting in a total number of \(m'=2m\) chains of length \(n'=(1-\text{warm_up})n/2\). The mean of the variances within and between the resulting chains are computed, \(W\) and \(B\) respectively. Based on those variances an estimator of the marginal posterior variance is constructed

\[\widehat{\text{var}}^+ = \frac{n'-1}{n'}W + \frac{1}{n'}B,\]

The estimator overestimates the variance of the marginal posterior if the chains are not well mixed and stationary, but is unbiased if the original chains equal the target distribution. At the same time, the mean within variance \(W\) underestimates the marginal posterior variance for finite \(n\), but converges to the true variance for \(n\rightarrow \infty\). By comparing \(\widehat{\text{var}}^+\) and \(W\) the mixing and stationarity of the chains can be quantified

\[\hat{R} = \sqrt{\frac{\widehat{\text{var}}^+}{W}}.\]

For well mixed and stationary chains \(\hat{R}\) will be close to one.

The mean within \(W\) and mean between \(B\) variance of the \(m'=2m\) chains of length \(n'=(1-\text{warm_up})n/2\) are defined as

\[W = \frac{1}{m'}\sum _{j=1}^{m'}s_j^2\quad \text{where}\quad s_j^2=\frac{1}{n'-1}\sum _{i=1}^{n'}(\psi _{ij} - \bar{\psi} _j)^2,\]
\[B = \frac{n'}{m'-1}\sum _{j=1}^{m'}(\bar{\psi} _j - \bar{\psi})^2.\]

Here, \(\psi _{ij}\) is the jth sample of the ith chain, \(\bar{\psi _j}=\sum _{i=1}^{n'}\psi _{ij}/n'\) is the within chain mean of the parameter \(\psi\) and \(\bar{\psi } = \sum _{j=1}^{m'}\bar{\psi} _{j}/m'\) is the between chain mean of the within chain means.

References

Parameters:
  • chains (np.ndarray of shape (m, n) or (m, n, p)) – A numpy array with \(n\) samples for each of \(m\) chains. Optionally the \(\hat{R}\) for \(p\) parameters can be computed by passing a numpy array with \(m\) chains of length \(n\) for \(p\) parameters.

  • warm_up (float) – First portion of each chain that will not be used for the computation of \(\hat{R}\).

Returns:

rhat\(\hat{R}\) of the posteriors for each parameter.

Return type:

float or np.ndarray of shape (p,)

pints.rhat_all_params(chains)[source]

Deprecated alias of rhat().

pints.effective_sample_size(samples)[source]

Calculates effective sample size (ESS) for a list of n-dimensional samples.

Parameters:

samples – A 2d array of shape (n_samples, n_parameters).