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.0018    2.1653    0.0128   28571.5370   1243.0579    1.1252  ⋯
           b   -0.0280    4.3169    0.0258   28165.8170   1210.9655    1.1276  ⋯
           c    0.0107    6.4779    0.0384   28699.5018   1319.2124    1.1303  ⋯
           d   -0.0530    8.5780    0.0496   29980.8573   1259.1659    1.1297  ⋯
           e    0.0392   10.7774    0.0626   29713.7413   1302.2318    1.1234  ⋯
                                                                1 column omitted

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

           a    -4.6127   -1.1740    0.0073    1.1628    4.6071
           b    -9.1584   -2.3380   -0.0256    2.2862    9.1138
           c   -13.5140   -3.5296   -0.0580    3.5358   13.7550
           d   -18.3401   -4.6740   -0.0405    4.6256   18.0275
           e   -22.6074   -5.7949    0.0130    5.7753   23.1106

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.0142    5.0210    0.0506   9843.1538   9973.1240    0.9999   ⋯
           b   -0.0278    3.9302    0.0397   9824.0555   9856.9900    0.9999   ⋯
           d    0.0038    1.9717    0.0204   9376.0278   9327.5425    1.0000   ⋯
           e   -0.0108    1.0013    0.0106   8868.7218   9506.6703    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.8062   -3.3862    0.0050    3.4251    9.9034
           b   -7.7820   -2.6818   -0.0171    2.6246    7.6129
           d   -3.9167   -1.3199    0.0019    1.3547    3.8652
           e   -1.9542   -0.6864   -0.0181    0.6600    1.9904

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.