Skip to content

Commit 45b5da8

Browse files
committed
document MCMC plots
1 parent 356b934 commit 45b5da8

20 files changed

Lines changed: 491 additions & 94 deletions

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ jobs:
1818
matrix:
1919
# https://youtu.be/fQJrEQIRPZU?list=PLP8iPy9hna6Tze1CReUAQweWHyNdXaPNb&t=967
2020
group:
21-
- Basic
22-
- MCMCChains
2321
- CairoMakie
2422
#- JET
2523
version:

Project.toml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,25 @@ Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
99
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1010

1111
[weakdeps]
12+
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
1213
AlgebraOfGraphics = "cbdf2221-f076-402e-a563-3d30da359d67"
13-
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
14+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1415
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
16+
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
1517

1618
[extensions]
17-
FigureHelpersAoGExt = ["CairoMakie", "AlgebraOfGraphics"]
18-
FigureHelpersCairoMakieExt = ["CairoMakie", "KernelDensity"]
19-
FigureHelpersMCMCChainsExt = "MCMCChains"
19+
FigureHelpersAoGExt = ["Makie", "AlgebraOfGraphics"]
20+
FigureHelpersMakieAbstractMCMCExt = ["Makie", "AbstractMCMC"]
21+
FigureHelpersMakieDistributionsExt = ["Makie", "Distributions"]
22+
FigureHelpersMakieExt = ["Makie", "KernelDensity"]
2023

2124
[compat]
25+
AbstractMCMC = "5.7.2"
2226
AlgebraOfGraphics = "0.9.4, 0.10"
23-
CairoMakie = "0.13.2"
27+
Distributions = "0.25.120"
2428
KernelDensity = "0.6.10"
2529
MCMCChains = "6.0.7"
30+
Makie = "0.21.9"
2631
Parameters = "0.12.3"
2732
StatsBase = "0.34.5"
2833
julia = "1.10"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55

66
# FigureHelpers.jl
77

8-
Concepts in Julia that have not found its proper package.
8+
Conveniently adjust figure properties for publication and presentation.
9+
910

docs/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
[deps]
2+
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
3+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
24
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
35
FigureHelpers = "9ae22f58-2487-4805-bfc5-386577db46c8"
6+
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
47
Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Documenter
22
using FigureHelpers
3+
using CairoMakie, Distributions
34

45
push!(LOAD_PATH,"../src/")
56
makedocs(sitename="FigureHelpers.jl",

docs/src/makie.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,55 @@ hidexdecoration!
4545
axis_contents
4646
```
4747

48-
## Tailored plots
48+
## Plots of univariate Distributions
49+
50+
```@setup doc
51+
using FigureHelpers
52+
using CairoMakie
53+
```
54+
```@example doc
55+
using Distributions
56+
d = LogNormal()
57+
fig = density_dist(d; normalize=true, label="normalized");
58+
density_dist!(content(fig[1, 1]), d) # unnormalized into the same figure
59+
fig
60+
```
61+
62+
```@docs
63+
density_dist
64+
```
65+
66+
## Plots of MCMCChains
67+
### Overview: combined trace-plot with density plot.
68+
For `linkaxes=true`, all the density plots are on the same x-scale.
69+
70+
```@setup doc
71+
using FigureHelpers
72+
using CairoMakie
73+
```
74+
```@example doc
75+
using MCMCChains
76+
chn = Chains(rand(500, 2, 3), [:a, :b]);
77+
fig = plot_chn(chn; linkaxes=true)
78+
fig
79+
```
80+
81+
```@docs
82+
plot_chn
83+
```
84+
85+
### Tailored density plot
86+
Supports more keyword arguments to adjust color and labels.
87+
88+
```@example doc
89+
using MCMCChains
90+
chn = Chains(rand(500, 2, 3), [:a, :b]);
91+
fig = density_params(chn, ["a","b"], labels=["bla","foo","bar"]);
92+
fig[:, 2] = Legend(fig, content(fig[1,1]), "Chain", framevisible = false)
93+
fig
94+
```
95+
4996
```@docs
5097
density_params
5198
```
99+

ext/FigureHelpersAoGExt.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ function __init__()
55
end
66

77

8-
isdefined(Base, :get_extension) ?
9-
(using CairoMakie,AlgebraOfGraphics) : (using ..CairoMakie,..AlgebraOfGraphics)
8+
using Makie, AlgebraOfGraphics
109
import FigureHelpers as CP
1110
using FigureHelpers
1211

ext/FigureHelpersMCMCChainsExt.jl

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,109 @@ end
66

77
import FigureHelpers as CM
88
using FigureHelpers
9+
using MCMCChains
10+
11+
12+
function CP.density_params(chns, pars=names(chns, :parameters);
13+
makie_config::MakieConfig=MakieConfig(),
14+
fig = figure_conf(cm2inch.((8.3,8.3/1.618)); makie_config),
15+
column = 1, xlims=nothing,
16+
labels=nothing, colors = nothing, ylabels = nothing, normalize = false,
17+
kwargs_axis = repeat([()],length(pars)),
18+
prange = (0.025, 0.975), # do not extend x-scale to outliers
19+
kwargs...
20+
)
21+
n_chains = size(chns,3)
22+
n_samples = length(chns)
23+
labels_ch = isnothing(labels) ? string.(1:n_chains) : string.(labels)
24+
ylabels = isnothing(ylabels) ? string.(pars) : ylabels
25+
!isnothing(xlims) && (length(xlims) != length(pars)) && error(
26+
"Expected length(xlims)=$(length(xlims)) (each a Tuple or nothing) to be length(pars)=$(length(pars))")
27+
for (i, param) in enumerate(pars)
28+
ax = Axis(fig[i, column]; ylabel=ylabels[i], yaxisposition = :right, kwargs_axis[i]...)
29+
if isnothing(colors)
30+
pal = fig.scene.theme.palette
31+
#pal = ax.palette
32+
colors = pal.color[]
33+
end
34+
for i_chain in 1:n_chains
35+
_values = chns[:, param, i_chain]
36+
if !isnothing(prange)
37+
qmin,qmax = StatsBase.quantile(_values, prange)
38+
_values = _values[qmin .<= _values .<= qmax]
39+
end
40+
col = colors[i_chain]
41+
if normalize
42+
k = KernelDensity.kde(_values)
43+
md = maximum(k.density)
44+
lines!(ax, k.x, k.density ./ md; label=labels_ch[i_chain], color = col, kwargs...)
45+
else
46+
density!(ax, _values; label=labels_ch[i_chain], color = (col, 0.3), strokecolor = col, strokewidth = 1,
47+
#strokearound = true,
48+
kwargs...)
49+
end
50+
end
51+
xlim = CP.passnothing(getindex)(xlims, i)
52+
!isnothing(xlim) && xlims!(ax, xlim)
53+
#hideydecorations!(ax, ticklabels=false, ticks=false, grid=false)
54+
hideydecorations!(ax, label=false, ticklabels=true)
55+
# if i < length(params)
56+
# hidexdecorations!(ax; grid=false)
57+
# else
58+
# ax.xlabel = "Parameter estimate"
59+
# end
60+
end
61+
# axes = [only(contents(fig[i, 2])) for i in 1:length(params)]
62+
# linkxaxes!(axes...)
63+
#axislegend(only(contents(fig[2, column])))
64+
fig
65+
end
66+
67+
function histogram_params(chns, pars=names(chns, :parameters);
68+
makie_config::MakieConfig=MakieConfig(),
69+
fig = figure_conf(cm2inch.((8.3,8.3/1.618)); makie_config),
70+
column = 1, xlims=nothing,
71+
labels=nothing, colors = nothing, ylabels = nothing, normalization = :pdf,
72+
kwargs_axis = repeat([()],length(pars)),
73+
ylimits = (0,1),
74+
kwargs...
75+
)
76+
n_chains = size(chns,3)
77+
n_samples = length(chns)
78+
labels_ch = isnothing(labels) ? string.(1:n_chains) : string.(labels)
79+
ylabels = isnothing(ylabels) ? string.(pars) : ylabels
80+
!isnothing(xlims) && (length(xlims) != length(pars)) && error(
81+
"Expected length(xlims)=$(length(xlims)) (each a Tuple or nothing) to be length(pars)=$(length(pars))")
82+
for (i, param) in enumerate(pars)
83+
ax = Axis(fig[i, column]; ylabel=ylabels[i], kwargs_axis[i]...)
84+
ylims!(ax, ylimits)
85+
if isnothing(colors)
86+
colors = ax.palette.color[]
87+
end
88+
for i_chain in 1:n_chains
89+
_values = chns[:, param, i_chain]
90+
col = colors[i_chain]
91+
hist!(ax, _values; label=labels_ch[i_chain], color = (col, 0.3), strokecolor = col, strokewidth = 1,
92+
normalization,
93+
#strokearound = true,
94+
kwargs...)
95+
end
96+
xlim = passnothing(getindex)(xlims, i)
97+
!isnothing(xlim) && xlims!(ax, xlim)
98+
#hideydecorations!(ax, ticklabels=false, ticks=false, grid=false)
99+
hideydecorations!(ax, label=false, ticklabels=true)
100+
# if i < length(params)
101+
# hidexdecorations!(ax; grid=false)
102+
# else
103+
# ax.xlabel = "Parameter estimate"
104+
# end
105+
end
106+
# axes = [only(contents(fig[i, 2])) for i in 1:length(params)]
107+
# linkxaxes!(axes...)
108+
#axislegend(only(contents(fig[2, column])))
109+
fig
110+
end
111+
9112

10113
end # module
11114

0 commit comments

Comments
 (0)