From 3a2c4bfa01e5ad0feaa5156892616f7ccc4a656e Mon Sep 17 00:00:00 2001 From: Seth Axen Date: Fri, 1 Dec 2023 20:03:07 +0100 Subject: [PATCH 1/7] Use new AdvancedHMC syntax --- test/integration/AdvancedHMC/Project.toml | 2 +- test/integration/AdvancedHMC/runtests.jl | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/integration/AdvancedHMC/Project.toml b/test/integration/AdvancedHMC/Project.toml index 47c2aeee..fe958151 100644 --- a/test/integration/AdvancedHMC/Project.toml +++ b/test/integration/AdvancedHMC/Project.toml @@ -16,7 +16,7 @@ TransformVariables = "84d833dd-6860-57f9-a1a7-6da5db126cff" TransformedLogDensities = "f9bc47f6-f3f8-4f3b-ab21-f8bc73906f26" [compat] -AdvancedHMC = "0.4" +AdvancedHMC = "0.4, 0.5" Distributions = "0.25" ForwardDiff = "0.10" LogDensityProblems = "1, 2" diff --git a/test/integration/AdvancedHMC/runtests.jl b/test/integration/AdvancedHMC/runtests.jl index 6e713c55..eaee7089 100644 --- a/test/integration/AdvancedHMC/runtests.jl +++ b/test/integration/AdvancedHMC/runtests.jl @@ -122,13 +122,13 @@ end hamiltonian = Hamiltonian(metric, ∇P) ϵ = find_good_stepsize(hamiltonian, θ₀) integrator = Leapfrog(ϵ) - proposal = NUTS{MultinomialTS,GeneralisedNoUTurn}(integrator) + kernel = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) adaptor = StanHMCAdaptor( MassMatrixAdaptor(metric), StepSizeAdaptor(0.8, integrator) ) samples1, stats1 = sample( hamiltonian, - proposal, + kernel, θ₀, ndraws, adaptor, @@ -144,11 +144,11 @@ end hamiltonian = Hamiltonian(metric, ∇P) ϵ = find_good_stepsize(hamiltonian, θ₀) integrator = Leapfrog(ϵ) - proposal = NUTS{MultinomialTS,GeneralisedNoUTurn}(integrator) + kernel = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) adaptor = StepSizeAdaptor(0.8, integrator) samples2, stats2 = sample( hamiltonian, - proposal, + kernel, result_pf.draws[:, 1], ndraws, adaptor, @@ -164,11 +164,11 @@ end hamiltonian = Hamiltonian(metric, ∇P) ϵ = find_good_stepsize(hamiltonian, θ₀) integrator = Leapfrog(ϵ) - proposal = NUTS{MultinomialTS,GeneralisedNoUTurn}(integrator) + kernel = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) adaptor = StepSizeAdaptor(0.8, integrator) samples3, stats3 = sample( hamiltonian, - proposal, + kernel, result_pf.draws[:, 1], ndraws, adaptor, @@ -184,11 +184,11 @@ end hamiltonian = Hamiltonian(metric, ∇P) ϵ = find_good_stepsize(hamiltonian, θ₀) integrator = Leapfrog(ϵ) - proposal = NUTS{MultinomialTS,GeneralisedNoUTurn}(integrator) + kernel = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) adaptor = StepSizeAdaptor(0.8, integrator) samples4, stats4 = sample( hamiltonian, - proposal, + kernel, result_pf.draws[:, 1], ndraws, adaptor, From 8df999e4face26885030f629b36d9621ec8c5a81 Mon Sep 17 00:00:00 2001 From: Seth Axen Date: Fri, 1 Dec 2023 20:05:43 +0100 Subject: [PATCH 2/7] Update docs to new AdvancedHMC syntax --- docs/Project.toml | 2 +- docs/src/examples/initializing-hmc.md | 12 ++++++------ docs/src/examples/turing.md | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 4a99bf83..187e1a04 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -15,7 +15,7 @@ TransformedLogDensities = "f9bc47f6-f3f8-4f3b-ab21-f8bc73906f26" Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" [compat] -AdvancedHMC = "0.4" +AdvancedHMC = "0.4, 0.5" Documenter = "1" DynamicHMC = "3" ForwardDiff = "0.10" diff --git a/docs/src/examples/initializing-hmc.md b/docs/src/examples/initializing-hmc.md index add0f0dd..2a0bbe70 100644 --- a/docs/src/examples/initializing-hmc.md +++ b/docs/src/examples/initializing-hmc.md @@ -172,11 +172,11 @@ metric = DiagEuclideanMetric(dim) hamiltonian = Hamiltonian(metric, ∇P) ϵ = find_good_stepsize(hamiltonian, init_params) integrator = Leapfrog(ϵ) -proposal = AdvancedHMC.NUTS{MultinomialTS,GeneralisedNoUTurn}(integrator) +kernel = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) adaptor = StepSizeAdaptor(0.8, integrator) samples_ahmc1, stats_ahmc1 = sample( hamiltonian, - proposal, + kernel, init_params, ndraws + nadapts, adaptor, @@ -196,11 +196,11 @@ metric = DenseEuclideanMetric(Matrix(inv_metric)) hamiltonian = Hamiltonian(metric, ∇P) ϵ = find_good_stepsize(hamiltonian, init_params) integrator = Leapfrog(ϵ) -proposal = AdvancedHMC.NUTS{MultinomialTS,GeneralisedNoUTurn}(integrator) +kernel = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) adaptor = StepSizeAdaptor(0.8, integrator) samples_ahmc2, stats_ahmc2 = sample( hamiltonian, - proposal, + kernel, init_params, ndraws + nadapts, adaptor, @@ -220,11 +220,11 @@ metric = Pathfinder.RankUpdateEuclideanMetric(inv_metric) hamiltonian = Hamiltonian(metric, ∇P) ϵ = find_good_stepsize(hamiltonian, init_params) integrator = Leapfrog(ϵ) -proposal = AdvancedHMC.NUTS{MultinomialTS,GeneralisedNoUTurn}(integrator) +kernel = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) adaptor = StepSizeAdaptor(0.8, integrator) samples_ahmc3, stats_ahmc3 = sample( hamiltonian, - proposal, + kernel, init_params, ndraws + nadapts, adaptor, diff --git a/docs/src/examples/turing.md b/docs/src/examples/turing.md index 3bf3055a..66cf2a44 100644 --- a/docs/src/examples/turing.md +++ b/docs/src/examples/turing.md @@ -85,11 +85,11 @@ metric = Pathfinder.RankUpdateEuclideanMetric(inv_metric) hamiltonian = Hamiltonian(metric, ℓπ, ∂ℓπ∂θ) ϵ = find_good_stepsize(hamiltonian, init_params[1]) integrator = Leapfrog(ϵ) -proposal = AdvancedHMC.NUTS{MultinomialTS,GeneralisedNoUTurn}(integrator) +kernel = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) adaptor = StepSizeAdaptor(0.8, integrator) samples, stats = sample( hamiltonian, - proposal, + kernel, init_params[1], ndraws + nadapts, adaptor, From 90a799bbdcc655d29b719997a4c54ac9582cfbbf Mon Sep 17 00:00:00 2001 From: Seth Axen Date: Fri, 1 Dec 2023 20:08:27 +0100 Subject: [PATCH 3/7] Add Turing v0.27 support --- Project.toml | 4 ++-- docs/Project.toml | 2 +- test/integration/Turing/Project.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index 3c3935a9..ef8ac19a 100644 --- a/Project.toml +++ b/Project.toml @@ -27,7 +27,7 @@ UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" [compat] Accessors = "0.1" Distributions = "0.25" -DynamicPPL = "0.20, 0.21, 0.22, 0.23" +DynamicPPL = "0.21, 0.22, 0.23, 0.24" Folds = "0.2" ForwardDiff = "0.10" IrrationalConstants = "0.1.1, 0.2" @@ -46,7 +46,7 @@ SciMLBase = "1.8.1" Statistics = "1.6" StatsBase = "0.33, 0.34" Transducers = "0.4.5" -Turing = "0.24, 0.25, 0.26" +Turing = "0.24, 0.25, 0.26, 0.27" UnPack = "1" julia = "1.6" diff --git a/docs/Project.toml b/docs/Project.toml index 187e1a04..890ad25a 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -26,4 +26,4 @@ StatsFuns = "0.9, 1" StatsPlots = "0.14, 0.15" TransformVariables = "0.6, 0.7, 0.8" TransformedLogDensities = "1" -Turing = "0.24, 0.25, 0.26" +Turing = "0.24, 0.25, 0.26, 0.27" diff --git a/test/integration/Turing/Project.toml b/test/integration/Turing/Project.toml index 1b41377e..669f8981 100644 --- a/test/integration/Turing/Project.toml +++ b/test/integration/Turing/Project.toml @@ -6,5 +6,5 @@ Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" [compat] Pathfinder = "0.5, 0.6, 0.7" -Turing = "0.24, 0.25, 0.26" +Turing = "0.24, 0.25, 0.26, 0.27" julia = "1.6" From a4ec71ea97757ba4f4d4bff37b70576a9f6f90cc Mon Sep 17 00:00:00 2001 From: Seth Axen Date: Fri, 1 Dec 2023 20:37:15 +0100 Subject: [PATCH 4/7] Increase number of draws to check --- test/integration/AdvancedHMC/runtests.jl | 2 +- test/integration/DynamicHMC/runtests.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/AdvancedHMC/runtests.jl b/test/integration/AdvancedHMC/runtests.jl index eaee7089..c2fb387f 100644 --- a/test/integration/AdvancedHMC/runtests.jl +++ b/test/integration/AdvancedHMC/runtests.jl @@ -106,7 +106,7 @@ end end @testset "sample" begin - ndraws = 1_000 + ndraws = 10_000 nadapts = 500 nparams = 5 x = 0:0.01:1 diff --git a/test/integration/DynamicHMC/runtests.jl b/test/integration/DynamicHMC/runtests.jl index 71f7ce97..140d4a5e 100644 --- a/test/integration/DynamicHMC/runtests.jl +++ b/test/integration/DynamicHMC/runtests.jl @@ -76,7 +76,7 @@ end end @testset "DynamicHMC.mcmc_with_warmup" begin - ndraws = 1_000 + ndraws = 10_000 x = 0:0.01:1 y = sin.(x) .+ randn.() .* 0.2 .+ x X = [x x .^ 2 x .^ 3] From cb2941bfcd4464e301ac9e8ed1fac09526daf18a Mon Sep 17 00:00:00 2001 From: Seth Axen Date: Fri, 1 Dec 2023 20:37:40 +0100 Subject: [PATCH 5/7] Make ndraws the number of post-warmup draws --- test/integration/AdvancedHMC/runtests.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/integration/AdvancedHMC/runtests.jl b/test/integration/AdvancedHMC/runtests.jl index c2fb387f..9f47bab9 100644 --- a/test/integration/AdvancedHMC/runtests.jl +++ b/test/integration/AdvancedHMC/runtests.jl @@ -130,7 +130,7 @@ end hamiltonian, kernel, θ₀, - ndraws, + ndraws + nadapts, adaptor, nadapts; drop_warmup=true, @@ -150,7 +150,7 @@ end hamiltonian, kernel, result_pf.draws[:, 1], - ndraws, + ndraws + nadapts, adaptor, nadapts; drop_warmup=true, @@ -170,7 +170,7 @@ end hamiltonian, kernel, result_pf.draws[:, 1], - ndraws, + ndraws + nadapts, adaptor, nadapts; drop_warmup=true, @@ -190,7 +190,7 @@ end hamiltonian, kernel, result_pf.draws[:, 1], - ndraws, + ndraws + nadapts, adaptor, nadapts; drop_warmup=true, From a0667f01c2d45ec030ffca9d5dfdd2db8a1e5f58 Mon Sep 17 00:00:00 2001 From: Seth Axen Date: Fri, 1 Dec 2023 20:38:32 +0100 Subject: [PATCH 6/7] Avoid under/overflow with hypot --- test/integration/AdvancedHMC/runtests.jl | 2 +- test/integration/DynamicHMC/runtests.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/AdvancedHMC/runtests.jl b/test/integration/AdvancedHMC/runtests.jl index 9f47bab9..57bf48ce 100644 --- a/test/integration/AdvancedHMC/runtests.jl +++ b/test/integration/AdvancedHMC/runtests.jl @@ -53,7 +53,7 @@ function compare_estimates(xs1, xs2, α=0.05) p = α / 2 m1, s1 = mean_and_mcse(xs1) m2, s2 = mean_and_mcse(xs2) - zs = @. (m1 - m2) / sqrt(s1^2 + s2^2) + zs = @. (m1 - m2) / hypot(s1, s2) @test all(norminvcdf(p) .< zs .< norminvccdf(p)) end diff --git a/test/integration/DynamicHMC/runtests.jl b/test/integration/DynamicHMC/runtests.jl index 140d4a5e..3bce8fe0 100644 --- a/test/integration/DynamicHMC/runtests.jl +++ b/test/integration/DynamicHMC/runtests.jl @@ -47,7 +47,7 @@ function compare_estimates(xs1, xs2, α=0.05) p = α / 2 m1, s1 = mean_and_mcse(xs1) m2, s2 = mean_and_mcse(xs2) - zs = @. (m1 - m2) / sqrt(s1^2 + s2^2) + zs = @. (m1 - m2) / hypot(s1, s2) @test all(norminvcdf(p) .< zs .< norminvccdf(p)) end From 4ce5a70db4516e864786175544e5d102115bbf75 Mon Sep 17 00:00:00 2001 From: Seth Axen Date: Fri, 1 Dec 2023 20:38:37 +0100 Subject: [PATCH 7/7] Change seed --- test/integration/AdvancedHMC/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/AdvancedHMC/runtests.jl b/test/integration/AdvancedHMC/runtests.jl index 57bf48ce..8f1bcd0e 100644 --- a/test/integration/AdvancedHMC/runtests.jl +++ b/test/integration/AdvancedHMC/runtests.jl @@ -13,7 +13,7 @@ using AdvancedHMC, TransformVariables using TransformedLogDensities: TransformedLogDensity -Random.seed!(0) +Random.seed!(2) struct RegressionProblem{X,Y} x::X