Author(s) Paul Miles | August 7, 2018
This tutorial provides a demonstration of the chain panel plotting routine available in mcmcplot
.
# import required packages
import numpy as np
from mcmcplot import mcmatplot as mcm
import warnings
warnings.filterwarnings("ignore")
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:
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]
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.
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}}
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.
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.
We can change the marker style, define the parameter names to be displayed on the plots, and change the dimensions of the figure.
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)
You can also edit the settings for the axis labels.
user_settings = dict(ylabel = dict(fontsize = 22))
f, settings = mcm.plot_chain_panel(chains = chain, names = names, settings = user_settings)
With the figure handle, you can also individual edit different aspects of the plot. See matplotlib's documentation for more details on editing figures.
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()