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.4748    0.0465    0.0015    931.3334   809.2502    1.0020    ⋯
           β    2.0061    0.0076    0.0002    959.7284   825.6883    0.9999    ⋯
           σ    0.2177    0.0151    0.0005   1008.8839   856.3196    0.9997    ⋯
                                                                1 column omitted

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

           α    1.3843    1.4443    1.4736    1.5070    1.5785
           β    1.9906    2.0010    2.0064    2.0113    2.0214
           σ    0.1867    0.2071    0.2175    0.2283    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.4385712947204663, 2.0124531815275466, 0.22904948833216876]
 [1.4310600350829619, 2.0147325585717835, 0.21996283990803556]
 [1.5002556397980622, 2.003403475861116, 0.22333298780409003]
 [1.4294167790341619, 2.01261352543737, 0.19601159618654096]
 [1.4589565127911095, 2.0110309527354775, 0.20341109008509684]
 [1.4852487222683175, 2.002876073142997, 0.23306041711870143]
 [1.4594382602969214, 2.002946535777802, 0.21961434030119226]
 [1.5219652756468702, 2.001773415039179, 0.19921379658345859]
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     = 6.66 seconds
Compute duration  = 4.9 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.4767    0.0435    0.0008   3242.8022   3099.0731    1.0021   ⋯
           β    2.0059    0.0075    0.0001   3463.0034   3814.4803    1.0016   ⋯
           σ    0.2166    0.0157    0.0002   5169.4255   4494.8310    1.0025   ⋯
                                                                1 column omitted

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

           α    1.3918    1.4476    1.4769    1.5057    1.5627
           β    1.9913    2.0009    2.0059    2.0110    2.0204
           σ    0.1882    0.2055    0.2157    0.2267    0.2503

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.74 seconds
Compute duration  = 2.34 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.4750    0.0424    0.0005   7523.8083   6310.0739    1.0004   ⋯
           β    2.0060    0.0074    0.0001   6840.6333   5656.1131    1.0008   ⋯
           σ    0.2168    0.0157    0.0002   6722.9557   5627.0576    1.0003   ⋯
                                                                1 column omitted

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

           α    1.3916    1.4468    1.4750    1.5028    1.5585
           β    1.9914    2.0011    2.0061    2.0110    2.0205
           σ    0.1890    0.2057    0.2160    0.2267    0.2501

See Initializing HMC with Pathfinder for further examples.