Skip to content

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.

julia
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.0006    2.1707    0.0126   29652.0649   1239.2545    1.1247  ⋯
           b    0.0395    4.3180    0.0250   29705.2165   1208.3821    1.1282  ⋯
           c    0.0542    6.4753    0.0386   28092.9532   1264.9172    1.1276  ⋯
           d    0.0682    8.6546    0.0500   30015.0985   1205.9390    1.1227  ⋯
           e   -0.0498   10.7335    0.0630   29310.2361   1206.4397    1.1284  ⋯
                                                                1 column omitted

Quantiles
  parameters       2.5%     25.0%     50.0%     75.0%     97.5%
      Symbol    Float64   Float64   Float64   Float64   Float64

           a    -4.5610   -1.1678    0.0035    1.1754    4.6644
           b    -9.0052   -2.2720    0.0080    2.3684    9.1485
           c   -13.5129   -3.4939    0.0361    3.5483   13.7025
           d   -18.3781   -4.6175    0.0355    4.7729   18.3195
           e   -23.1132   -5.8340    0.0163    5.8705   22.5492

You can plot the results from all chains in the Chains object:

julia
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:

julia
pairplot(chn1[:,:,1], chn1[:,:,2], chn1[:,:,3])

You can title the series indepdendently as well:

julia
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:

julia
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.0222    5.0193    0.0496   10221.3530   9785.4448    1.0000  ⋯
           b   -0.1176    3.9922    0.0401    9901.4740   9878.9601    1.0003  ⋯
           d    0.0058    1.9791    0.0196   10146.0630   9834.0446    1.0000  ⋯
           e    0.0051    0.9866    0.0098   10047.8680   9287.6746    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.6741   -3.3689    0.0191    3.3762    9.9747
           b   -7.9012   -2.8274   -0.0657    2.5721    7.6633
           d   -3.8266   -1.3050    0.0043    1.3490    3.8622
           e   -1.9652   -0.6449    0.0030    0.6635    1.9465

Just pass them all to pairplot:

julia
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.