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.0087    2.1719    0.0128   29078.8818   1272.2126    1.1254  ⋯
           b    0.0194    4.3197    0.0257   28958.9071   1186.2168    1.1268  ⋯
           c   -0.0505    6.4955    0.0392   27476.6687   1233.0089    1.1248  ⋯
           d   -0.0670    8.5873    0.0497   29778.4916   1214.1447    1.1270  ⋯
           e    0.0089   10.8199    0.0620   30341.2674   1158.7179    1.1306  ⋯
                                                                1 column omitted

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

           a    -4.5579   -1.1667    0.0055    1.1716    4.6097
           b    -9.0888   -2.3089    0.0133    2.3259    9.0911
           c   -13.8758   -3.5349   -0.0036    3.4746   13.5973
           d   -18.4743   -4.6921    0.0040    4.5636   18.0720
           e   -22.7805   -5.8415    0.0308    5.8826   22.7212

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.0729    5.0101    0.0509    9686.7627    9473.6454    0.9999 ⋯
           b    0.0117    3.9851    0.0396   10114.8015    9902.1024    1.0003 ⋯
           d   -0.0504    1.9901    0.0202    9717.6067   10009.0295    1.0000 ⋯
           e    0.0071    1.0006    0.0101    9730.3603    9795.9200    1.0003 ⋯
                                                                1 column omitted

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

           a   -9.6929   -3.3055    0.0661    3.4106    9.9951
           b   -7.6775   -2.6939    0.0380    2.7174    7.8472
           d   -3.8895   -1.3944   -0.0667    1.2783    3.9334
           e   -1.9285   -0.6637    0.0087    0.6682    1.9847

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.