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.0176 2.1473 0.0126 29202.0474 1272.4404 1.1244 ⋯
b 0.0146 4.3139 0.0249 30209.2597 1292.6280 1.1266 ⋯
c 0.0299 6.4787 0.0373 30141.0692 1258.8843 1.1280 ⋯
d 0.0158 8.6537 0.0500 29987.8825 1269.1073 1.1264 ⋯
e -0.0027 10.7718 0.0616 30593.6997 1257.0249 1.1268 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
a -4.4964 -1.1605 -0.0080 1.1746 4.6015
b -9.0721 -2.3407 0.0284 2.3490 9.0868
c -13.5970 -3.4827 0.0383 3.5207 13.6801
d -18.3699 -4.6573 -0.0307 4.7310 18.2063
e -22.8127 -5.8416 -0.0315 5.8085 22.6989
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.0472 4.9961 0.0495 10202.9020 9050.9028 1.0001 ⋯
b 0.0034 3.9749 0.0391 10313.7527 10172.5632 0.9999 ⋯
d -0.0076 2.0056 0.0198 10223.8545 8928.5954 1.0001 ⋯
e 0.0073 1.0068 0.0101 9892.6870 9793.2398 0.9999 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
a -9.7093 -3.3245 -0.0842 3.3622 9.7766
b -7.8441 -2.6284 -0.0022 2.6643 7.8225
d -3.9248 -1.3681 -0.0303 1.3327 3.9366
e -1.9517 -0.6609 0.0069 0.6732 1.9757
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
.