Pairwise Correlation Panel

Author(s) Paul Miles | August 7, 2018

This tutorial provides a demonstration of the pairwie correlation 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 Density Panel

The density panel simply plots each column of the sampling chain against the other columns. Note, the plotting routines output the figure handle as well as a dictionary containing the settings used in generating the plot.

In [4]:
f, settings = mcm.plot_pairwise_correlation_panel(chains = chain)
print(settings)
{'skip': 1, 'fig': {'figsize': (7, 5), 'dpi': 100}, 'plot': {'color': 'b', 'marker': '.', 'linestyle': 'none'}, 'xlabel': {}, 'ylabel': {}, 'title': {}, 'add_5095_contours': False, 'plot_50': {'color': 'g', 'marker': None, 'linewidth': 2, 'linestyle': '--', 'label': '50%'}, 'plot_95': {'color': 'r', 'marker': None, 'linewidth': 2, 'linestyle': '--', 'label': '95%'}, 'add_legend': False, 'legend': {'loc': 1}}

Add 50% and 95% Probability Contours

We can add 50% and 95% probability contours to the panel.

In [37]:
settings = {'add_5095_contours': True, 'plot_95': dict(color = 'r', linewidth = 3), 'plot_50': dict(color = 'c', linewidth = 3),
            'add_legend': True, 'legend': dict(loc = 0, fontsize = 10, bbox_to_anchor=(0.85, 0.75)),
            'fig': dict(figsize = (4,4))}
fp, used_settings = mcm.plot_pairwise_correlation_panel(chains = chain, settings = settings)
fp.savefig('basic_pairwise_panel.png')

As the chains are normally distributed, it is not surprising that the contours are ellipses.

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 [22]:
user_settings = dict(plot = dict(marker = 's', mfc = 'none', linestyle = 'none'), fig = dict(figsize = (6,6)))
names = ['a', 'b', 'c']
f, settings = mcm.plot_pairwise_correlation_panel(chains = chain, names = names, settings = user_settings)

Update Label Features

You can also edit the settings for the axis labels.

In [24]:
user_settings = dict(ylabel = dict(fontsize = 22), title = dict(fontsize = 22))
f, settings = mcm.plot_pairwise_correlation_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 [36]:
f, settings = mcm.plot_pairwise_correlation_panel(chains = chain)
ax = f.get_axes()
for ai in ax:
    ai.set_xlabel(xlabel = ai.get_xlabel(), fontsize = 22)
    ai.set_xticklabels(labels = ai.get_xticklabels(), fontsize = 22)
    ai.set_ylabel(ylabel = ai.get_ylabel(), fontsize = 22)
    ai.set_yticklabels(labels = ai.get_yticklabels(), fontsize = 22)
    ai.set_title(label = ai.get_title(), fontsize = 22)
ai = ax[-1]
ai.set_title(label = 'my title :)', fontsize = 22)
ai.lines[0].set_color('g')
# reset positions to avoid overlap    
f.tight_layout()