Running Pathfinder on Turing.jl models
This tutorial demonstrates how Turing can be used with Pathfinder.
We'll demonstrate with a regression example.
using AdvancedHMC, Pathfinder, Random, Turing
Random.seed!(39)
@model function regress(x, y)
α ~ Normal()
β ~ Normal()
σ ~ truncated(Normal(); lower=0)
y .~ Normal.(α .+ β .* x, σ)
end
x = 0:0.1:10
y = @. 2x + 1.5 + randn() * 0.2
model = regress(collect(x), y)
n_chains = 8
8
For convenience, pathfinder
and multipathfinder
can take Turing models as inputs and produce MCMCChains.Chains
objects as outputs.
result_single = pathfinder(model; ndraws=1_000)
Single-path Pathfinder result
tries: 1
draws: 1000
fit iteration: 22 (total: 26)
fit ELBO: -1.95 ± 0.03
fit distribution: Distributions.MvNormal{Float64, Pathfinder.WoodburyPDMat{Float64, LinearAlgebra.Diagonal{Float64, Vector{Float64}}, Matrix{Float64}, Matrix{Float64}, Pathfinder.WoodburyPDFactorization{Float64, LinearAlgebra.Diagonal{Float64, Vector{Float64}}, LinearAlgebra.QRCompactWYQ{Float64, Matrix{Float64}, Matrix{Float64}}, LinearAlgebra.UpperTriangular{Float64, Matrix{Float64}}}}, Vector{Float64}}(
dim: 3
μ: [1.4759842060455182, 2.0060275818274436, -1.5479825354725543]
Σ: [0.0018296810848421195 -0.0002589735319675424 -0.000618345292003903; -0.0002589735319675424 5.0682815503249e-5 5.534978328717582e-5; -0.000618345292003903 5.534978328717582e-5 0.0064256358711586746]
)
result_multi = multipathfinder(model, 1_000; nruns=n_chains)
Multi-path Pathfinder result
runs: 8
draws: 1000
Pareto shape diagnostic: 0.49 (good)
Here, the Pareto shape diagnostic indicates that it is likely safe to use these draws to compute posterior estimates.
When passed a DynamicPPL.Model
, Pathfinder also gives access to the posterior draws in a familiar Chains
object.
result_multi.draws_transformed
Chains MCMC chain (1000×3×1 Array{Float64, 3}):
Iterations = 1:1:1000
Number of chains = 1
Samples per chain = 1000
parameters = α, β, σ
Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat ⋯
Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯
α 1.4724 0.0478 0.0015 1065.3828 913.0206 0.9994 ⋯
β 2.0062 0.0080 0.0003 1015.5842 915.7711 1.0005 ⋯
σ 0.2167 0.0155 0.0005 970.6707 981.1794 1.0044 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
α 1.3695 1.4449 1.4716 1.5058 1.5650
β 1.9912 2.0009 2.0062 2.0113 2.0233
σ 0.1877 0.2055 0.2167 0.2263 0.2487
We can also use these posterior draws to initialize MCMC sampling.
init_params = collect.(eachrow(result_multi.draws_transformed.value[1:n_chains, :, 1]))
8-element Vector{Vector{Float64}}:
[1.479212845730115, 2.004653673862574, 0.21849816572974648]
[1.4671204662341366, 2.012072479722318, 0.20509591806923821]
[1.4923892790853188, 2.0086214878196524, 0.228872014351796]
[1.4791378091021459, 2.002988635852001, 0.22831136691078066]
[1.4671474996393061, 2.0005490100751167, 0.2218504094567497]
[1.5145012645039009, 1.9997383313646964, 0.22909663767338675]
[1.4368178924986255, 2.008327650310952, 0.20435142557946706]
[1.4738700123059079, 2.004895674382526, 0.21028541630912995]
chns = sample(model, Turing.NUTS(), MCMCThreads(), 1_000, n_chains; init_params, progress=false)
Chains MCMC chain (1000×15×8 Array{Float64, 3}):
Iterations = 501:1:1500
Number of chains = 8
Samples per chain = 1000
Wall duration = 7.11 seconds
Compute duration = 5.25 seconds
parameters = α, β, σ
internals = lp, n_steps, is_accept, acceptance_rate, log_density, hamiltonian_energy, hamiltonian_energy_error, max_hamiltonian_energy_error, tree_depth, numerical_error, step_size, nom_step_size
Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat ⋯
Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯
α 1.4759 0.0419 0.0007 3813.7080 3621.4712 1.0032 ⋯
β 2.0059 0.0073 0.0001 3934.7941 4375.7196 1.0023 ⋯
σ 0.2169 0.0154 0.0002 5036.6452 4494.3731 1.0012 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
α 1.3942 1.4486 1.4753 1.5038 1.5579
β 1.9918 2.0010 2.0060 2.0108 2.0204
σ 0.1881 0.2059 0.2161 0.2267 0.2486
We can use Pathfinder's estimate of the metric and only perform enough warm-up to tune the step size.
inv_metric = result_multi.pathfinder_results[1].fit_distribution.Σ
metric = Pathfinder.RankUpdateEuclideanMetric(inv_metric)
kernel = HMCKernel(Trajectory{MultinomialTS}(Leapfrog(0.0), GeneralisedNoUTurn()))
adaptor = StepSizeAdaptor(0.8, 1.0) # adapt only the step size
nuts = AdvancedHMC.HMCSampler(kernel, metric, adaptor)
n_adapts = 50
n_draws = 1_000
chns = sample(
model,
externalsampler(nuts),
MCMCThreads(),
n_draws + n_adapts,
n_chains;
n_adapts,
init_params,
progress=false,
)[n_adapts + 1:end, :, :] # drop warm-up draws
Chains MCMC chain (1000×16×8 Array{Float64, 3}):
Iterations = 51:1:1050
Number of chains = 8
Samples per chain = 1000
Wall duration = 2.92 seconds
Compute duration = 2.5 seconds
parameters = α, β, σ
internals = lp, n_steps, is_accept, acceptance_rate, log_density, hamiltonian_energy, hamiltonian_energy_error, max_hamiltonian_energy_error, tree_depth, numerical_error, step_size, nom_step_size, is_adapt
Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat ⋯
Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯
α 1.4741 0.0518 0.0010 7513.5356 5475.5536 1.0005 ⋯
β 2.0058 0.0115 0.0002 6448.2684 4316.4928 1.0013 ⋯
σ 0.2192 0.0568 0.0015 6307.6893 4617.7063 1.0008 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
α 1.3887 1.4468 1.4742 1.5028 1.5598
β 1.9912 2.0014 2.0062 2.0110 2.0208
σ 0.1884 0.2059 0.2159 0.2267 0.2502
See Initializing HMC with Pathfinder for further examples.