Chain Panel

Author(s) Paul Miles | August 7, 2018

This tutorial provides a demonstration of the chain panel plotting routine available in mcmcplot.

In [2]:
# import required packages
import numpy as np
from mcmcplot import mcmatplot as mcm
import warnings
warnings.filterwarnings("ignore")

Generate Random Chains

The plotting routines are designed to be used in conjunction with the result of a MCMC simulation. For the purpose of this example, we consider a randomly generated chain. We will consider a chain with 3 parameters that have the following distributions:

  • $p_{0} \sim N(1.0, 0.5)$
  • $p_{1} \sim N(2.5, 3.0)$
  • $p_{2} \sim N(-1.3, 0.75)$
In [3]:
nsimu = 1000
npar = 3
mu = np.array([1.0, 2.5, -1.3])
sig = np.array([0.5, 3.0, 0.75])
chain = np.zeros([nsimu, npar])
for ii in range(npar):
    chain[:,ii] = sig[ii]*np.random.randn(nsimu,) + mu[ii]

Default Chain Panel

The chain panel simply demonstrates the sampling history of the chains. Note, the plotting routines output the figure handle as well as a dictionary containing the settings used in generating the plot.

In [21]:
f, settings = mcm.plot_chain_panel(chains = chain)
print(settings)
{'maxpoints': 500, 'fig': {'figsize': (5, 4), 'dpi': 100}, 'plot': {'color': 'b', 'marker': '.', 'linestyle': 'none'}, 'xlabel': {'s': 'Iteration'}, 'ylabel': {}, 'add_pm2std': False, 'mean': {'color': 'k', 'marker': None, 'linestyle': '-', 'linewidth': 3}, 'sig': {'color': 'r', 'marker': None, 'linestyle': '--', 'linewidth': 3}}

Add Lines Represented Mean and $\pm$ 2 Standard Deviations

We can adjust various aspects of the figure. For example, we can add lines to represent the mean and $\pm$ 2 standard deviations (adjust the colors), and make the sample points black.

In [49]:
f, settings = mcm.plot_chain_panel(chains = chain, settings = dict(add_pm2std = True, mean = dict(color = 'b'), plot = dict(color = 'k')))
f.savefig('basic_chain_panel.png')

As a sanity check we observe that the reported standard deviation for $p_{0}$ was 0.5, and the red lines are roughly at 0 and 2 as expected.

Change Marker Style and Define Parameter Names

We can change the marker style, define the parameter names to be displayed on the plots, and change the dimensions of the figure.

In [28]:
user_settings = dict(plot = dict(marker = 's', mfc = 'none'), fig = dict(figsize = (6,6)))
names = ['a', 'b', 'c']
f, settings = mcm.plot_chain_panel(chains = chain, names = names, settings = user_settings)

Update Label Features

You can also edit the settings for the axis labels.

In [30]:
user_settings = dict(ylabel = dict(fontsize = 22))
f, settings = mcm.plot_chain_panel(chains = chain, names = names, settings = user_settings)

Manually Manipulate Plot Features

With the figure handle, you can also individual edit different aspects of the plot. See matplotlib's documentation for more details on editing figures.

In [48]:
f, settings = mcm.plot_chain_panel(chains = chain)
ax = f.get_axes()
for ai in ax:
    ai.set_ylabel(ylabel = ai.get_ylabel(), fontsize = 22)
    ai.set_yticklabels(labels = ai.get_yticklabels(), fontsize = 22)
# reset positions to avoid overlap    
f.tight_layout()