import matplotlib.pyplot as plt
import numpy as np
from espei.analysis import truncate_arrays
= np.load('trace.npy')
trace = np.load('lnprob.npy')
lnprob = truncate_arrays(trace, lnprob)
trace, lnprob
= trace.shape[0]
num_chains = trace.shape[2]
num_parameters = np.arange(1,trace.shape[1]+1)
iterations = plt.subplots(nrows=num_parameters, gridspec_kw=dict(hspace=0.4), figsize=(4, 3*num_parameters))
fig, axs for parameter in range(num_parameters):
= axs[parameter]
ax 'Iterations')
ax.set_xlabel('Parameter value')
ax.set_ylabel(
ax.plot(iterations, trace[..., parameter].T) fig.show()
MCMC Parameter Trace Plots
Looking at how each parameter chain evolves across the chains can show if any particular chains are diverging from the rest, if there are multiple modes being explored, or how wide the distribution of parameters are relative to each other.
Each line corresponds to one chain in the ensemble.
and if we plot again after 500 iterations of burn-in:
= 500
burn_in_iterations = plt.subplots(nrows=num_parameters, gridspec_kw=dict(hspace=0.4), figsize=(4, 3*num_parameters))
fig, axs for parameter in range(num_parameters):
= axs[parameter]
ax 'Iterations')
ax.set_xlabel('Parameter value')
ax.set_ylabel(
ax.plot(iterations[burn_in_iterations:], trace[:, burn_in_iterations:, parameter].T) fig.show()