Skip to content

Note

Click here to download the full example code

Introduction to Distributions

When you have a few chains and want to contrast them all with each other, you probably want a summary plot.

To show you how they work, let's make some sample data that all has the same average.

from chainconsumer import Chain, ChainConsumer, Truth, make_sample

# Here's what you might start with
df_1 = make_sample(num_dimensions=4, seed=1, randomise_mean=True)
df_2 = make_sample(num_dimensions=5, seed=2, randomise_mean=True)
print(df_1.head())

Out:

          A         B          C          D  log_posterior
0 -1.110967  5.000946   9.216181  14.195246      -3.603779
1 -0.222413  5.369600   8.916333  14.985672      -2.897507
2 -0.673672  5.443502  10.123202  13.701132      -3.117740
3 -0.246591  3.939420   8.475686  15.313994      -2.623498
4 -1.429470  5.828876   9.729602  14.351873      -2.463961

Using distributions

# And now we give this to chainconsumer
c = ChainConsumer()
c.add_chain(Chain(samples=df_1, name="An Example Contour"))
fig = c.plotter.plot_distributions()

$A = -0.76^{+1.03}_{-0.98}$, $B = 4.82^{+0.99}_{-1.01}$, $C = 9.40^{+1.01}_{-0.99}$, $D = 14.50^{+1.03}_{-0.98}$

If you want the summary stats you'll need to keep it just one chain. And if you don't want them, you can pass summarise=False to the PlotConfig.

When you add a second chain, you'll see the summaries disappear.

c.add_chain(Chain(samples=df_2, name="Another contour!"))
c.add_truth(Truth(location={"A": 0, "B": 0}))
fig = c.plotter.plot_distributions(col_wrap=3, columns=["A", "B"])

plot 3 distributions

Total running time of the script: ( 0 minutes 1.559 seconds)

Download Python source code: plot_3_distributions.py

Download Jupyter notebook: plot_3_distributions.ipynb

Gallery generated by mkdocs-gallery