Integration with MCMCChains.jl
MCMC packages like Turing often produce results in the form of an MCMCChains.Chain. There is special support in PairPlots.jl for plotting these chains.
Note
The integration between PairPlots and MCMCChains only works on Julia 1.9 and above. On previous versions, you can work around this by running pairplot(DataFrame(chn))
.
Plotting chains
For this example, we'll use the following code to generate a Chain
. In a real code, you would likely receive a chain as a result of sampling from a model.
chn1 = Chains(randn(10000, 5, 3) .* [1 2 3 4 5] .* [1;;;2;;;3], [:a, :b, :c, :d, :e])
Chains MCMC chain (10000×5×3 Array{Float64, 3}):
Iterations = 1:1:10000
Number of chains = 3
Samples per chain = 10000
parameters = a, b, c, d, e
Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat ⋯
Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯
a 0.0077 2.1511 0.0124 29951.3781 1311.4094 1.1244 ⋯
b 0.0221 4.3302 0.0254 28998.6843 1170.9858 1.1249 ⋯
c 0.0511 6.4435 0.0376 29381.6875 1221.4644 1.1279 ⋯
d -0.1029 8.6820 0.0499 30314.9085 1287.5702 1.1236 ⋯
e -0.0097 10.8159 0.0631 29122.4889 1339.2690 1.1204 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
a -4.5788 -1.1532 0.0062 1.1791 4.5578
b -9.0991 -2.3192 0.0081 2.3271 9.3216
c -13.5240 -3.4226 0.0251 3.5665 13.6726
d -18.5693 -4.7576 -0.1011 4.5645 18.3286
e -22.7840 -5.8279 -0.0590 5.7453 22.9612
You can plot the results from all chains in the Chains object:
using CairoMakie, PairPlots
pairplot(chn1)
The labels are taken from the column names of the chains. You can modify them by passing in a dictionary mapping column names to strings, LaTeX strings, or Makie rich text objects.
Plotting individual chains separately
If you have multiple parallel chains and want to plot them in different colors, you can pass each one to pairplot
:
pairplot(chn1[:,:,1], chn1[:,:,2], chn1[:,:,3])
You can title the series indepdendently as well:
c1 = Makie.wong_colors(0.5)[1]
c2 = Makie.wong_colors(0.5)[2]
c3 = Makie.wong_colors(0.5)[3]
pairplot(
PairPlots.Series(chn1[:,:,1], label="chain 1", color=c1, strokecolor=c1),
PairPlots.Series(chn1[:,:,2], label="chain 2", color=c2, strokecolor=c2),
PairPlots.Series(chn1[:,:,3], label="chain 3", color=c3, strokecolor=c3),
)
If your chains are well converged, then the different series should look the same.
Comparing the results of two simulations
You may want to compare the results of two simulations. Consider the following chains:
chn2 = Chains(randn(10000, 5, 1) .* [1 2 3 4 5], [:a, :b, :c, :d, :e])
chn3 = Chains(randn(10000, 4, 1) .* [5 4 2 1], [:a, :b, :d, :e]);
Chains MCMC chain (10000×4×1 Array{Float64, 3}):
Iterations = 1:1:10000
Number of chains = 1
Samples per chain = 10000
parameters = a, b, d, e
Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat ⋯
Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯
a 0.0075 4.9950 0.0511 9558.0972 9578.2402 0.9999 ⋯
b 0.0932 3.9341 0.0396 9864.4075 10165.5483 1.0001 ⋯
d 0.0024 1.9884 0.0204 9453.2010 10173.3586 1.0001 ⋯
e -0.0003 1.0071 0.0101 9922.8971 9718.2039 1.0000 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
a -9.8312 -3.3377 0.0164 3.3650 9.7691
b -7.4889 -2.5680 0.0763 2.7293 7.8065
d -3.8589 -1.3411 0.0091 1.3514 3.9039
e -1.9547 -0.6685 0.0012 0.6894 1.9622
Just pass them all to pairplot
:
pairplot(chn2, chn3)
Note how the parameters of the chains do not have to match exactly. Here, chn2
has an additional variable not present in chn3
.