From ef2eb152827ec39b5c90e1f43d43bf1b641274c0 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 18:39:41 +0100 Subject: [PATCH 01/11] Compat for Turing v0.41 --- Project.toml | 2 +- ext/SliceSamplingTuringExt.jl | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Project.toml b/Project.toml index 21a79c2..e813e86 100644 --- a/Project.toml +++ b/Project.toml @@ -21,7 +21,7 @@ Distributions = "0.25" LinearAlgebra = "1" LogDensityProblems = "2" Random = "1" -Turing = "0.40" +Turing = "0.41" julia = "1.10" [extras] diff --git a/ext/SliceSamplingTuringExt.jl b/ext/SliceSamplingTuringExt.jl index d20fc71..7a72ef2 100644 --- a/ext/SliceSamplingTuringExt.jl +++ b/ext/SliceSamplingTuringExt.jl @@ -38,11 +38,9 @@ end function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDensityFunction) n_max_attempts = 1000 - model = ℓ.model - vi = Turing.DynamicPPL.VarInfo(rng, model, Turing.SampleFromUniform()) - vi_spl = last(Turing.DynamicPPL.evaluate_and_sample!!(rng, model, vi, Turing.SampleFromUniform())) - θ = vi_spl[:] - ℓp = LogDensityProblems.logdensity(ℓ, θ) + model, vi = ℓ.model, ℓ.varinfo + vi_spl = last(Turing.DynamicPPL.init!!(rng, model, vi, Turing.DynamicPPL.InitFromUniform())) + ℓp = Turing.DynamicPPL.getlogjoint_internal(vi_spl) init_attempt_count = 1 for attempts in 1:n_max_attempts @@ -52,12 +50,12 @@ function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDe # NOTE: This will sample in the unconstrained space. vi_spl = last( - Turing.DynamicPPL.evaluate_and_sample!!( - rng, model, vi, Turing.SampleFromUniform() + Turing.DynamicPPL.init!!( + rng, model, vi_spl, Turing.InitFromUniform() ), ) + ℓp = Turing.DynamicPPL.getlogjoint_internal(vi_spl) θ = vi_spl[:] - ℓp = LogDensityProblems.logdensity(ℓ, θ) if all(isfinite.(θ)) && isfinite(ℓp) return θ @@ -65,6 +63,7 @@ function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDe end @error "Failed to find valid initial parameters after $(n_max_attempts) attempts; consider providing explicit initial parameters using the `initial_params` keyword" + θ = vi_spl[:] return θ end From 2b6e49e9903c812a0d4ef9818525b7554f78e758 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 18:40:07 +0100 Subject: [PATCH 02/11] Bump patch --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e813e86..3ee0dd5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "SliceSampling" uuid = "43f4d3e8-9711-4a8c-bd1b-03ac73a255cf" -version = "0.7.8" +version = "0.7.9" [deps] AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" From 717011b56b7d0de85a171ae3f5e5e53169f21c95 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 18:41:25 +0100 Subject: [PATCH 03/11] use original vi (shouldn't make a difference) --- ext/SliceSamplingTuringExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/SliceSamplingTuringExt.jl b/ext/SliceSamplingTuringExt.jl index 7a72ef2..0972f32 100644 --- a/ext/SliceSamplingTuringExt.jl +++ b/ext/SliceSamplingTuringExt.jl @@ -51,7 +51,7 @@ function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDe # NOTE: This will sample in the unconstrained space. vi_spl = last( Turing.DynamicPPL.init!!( - rng, model, vi_spl, Turing.InitFromUniform() + rng, model, vi, Turing.InitFromUniform() ), ) ℓp = Turing.DynamicPPL.getlogjoint_internal(vi_spl) From 5d5779b5dc6ef45e668dcb82da9fbf5e809273ae Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 18:42:22 +0100 Subject: [PATCH 04/11] Update comment --- ext/SliceSamplingTuringExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/SliceSamplingTuringExt.jl b/ext/SliceSamplingTuringExt.jl index 0972f32..ee42bc8 100644 --- a/ext/SliceSamplingTuringExt.jl +++ b/ext/SliceSamplingTuringExt.jl @@ -48,7 +48,7 @@ function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDe @warn "Failed to find valid initial parameters after $(init_attempt_count) attempts; consider providing explicit initial parameters using the `initial_params` keyword" end - # NOTE: This will sample in the unconstrained space. + # NOTE: This will sample in the unconstrained space if ℓ.varinfo is linked vi_spl = last( Turing.DynamicPPL.init!!( rng, model, vi, Turing.InitFromUniform() From 56f72628ee7264806d21588f08990c8d8d3063bb Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 18:43:16 +0100 Subject: [PATCH 05/11] bump deps in docs/ and test/ as well --- docs/Project.toml | 2 +- test/Project.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 3d162a2..83fff61 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -28,5 +28,5 @@ Random = "1" SliceSampling = "0.7.1" StableRNGs = "1" Statistics = "1" -Turing = "0.37, 0.38, 0.39, 0.40" +Turing = "0.41" julia = "1.10" diff --git a/test/Project.toml b/test/Project.toml index 2c43400..b6ae6f6 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -18,5 +18,5 @@ MCMCTesting = "0.3" Random = "1" StableRNGs = "1" Test = "1" -Turing = "0.37, 0.38, 0.39, 0.40" +Turing = "0.41" julia = "1.10" From 4c693c7b18cd3ef1a539d00eb02c4d9d2ca862e1 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 18:44:52 +0100 Subject: [PATCH 06/11] Format --- ext/SliceSamplingTuringExt.jl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ext/SliceSamplingTuringExt.jl b/ext/SliceSamplingTuringExt.jl index ee42bc8..893c360 100644 --- a/ext/SliceSamplingTuringExt.jl +++ b/ext/SliceSamplingTuringExt.jl @@ -39,7 +39,9 @@ function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDe n_max_attempts = 1000 model, vi = ℓ.model, ℓ.varinfo - vi_spl = last(Turing.DynamicPPL.init!!(rng, model, vi, Turing.DynamicPPL.InitFromUniform())) + vi_spl = last( + Turing.DynamicPPL.init!!(rng, model, vi, Turing.DynamicPPL.InitFromUniform()) + ) ℓp = Turing.DynamicPPL.getlogjoint_internal(vi_spl) init_attempt_count = 1 @@ -49,11 +51,7 @@ function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDe end # NOTE: This will sample in the unconstrained space if ℓ.varinfo is linked - vi_spl = last( - Turing.DynamicPPL.init!!( - rng, model, vi, Turing.InitFromUniform() - ), - ) + vi_spl = last(Turing.DynamicPPL.init!!(rng, model, vi, Turing.InitFromUniform())) ℓp = Turing.DynamicPPL.getlogjoint_internal(vi_spl) θ = vi_spl[:] From 064dccb0767113d0b21bdbdc6e1d636437fd0616 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 18:48:15 +0100 Subject: [PATCH 07/11] That was a mistake; use getlogdensity instead of hardcoding the function --- ext/SliceSamplingTuringExt.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/SliceSamplingTuringExt.jl b/ext/SliceSamplingTuringExt.jl index 893c360..3eba4bd 100644 --- a/ext/SliceSamplingTuringExt.jl +++ b/ext/SliceSamplingTuringExt.jl @@ -42,7 +42,7 @@ function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDe vi_spl = last( Turing.DynamicPPL.init!!(rng, model, vi, Turing.DynamicPPL.InitFromUniform()) ) - ℓp = Turing.DynamicPPL.getlogjoint_internal(vi_spl) + ℓp = ℓ.getlogdensity(vi_spl) init_attempt_count = 1 for attempts in 1:n_max_attempts @@ -52,7 +52,7 @@ function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDe # NOTE: This will sample in the unconstrained space if ℓ.varinfo is linked vi_spl = last(Turing.DynamicPPL.init!!(rng, model, vi, Turing.InitFromUniform())) - ℓp = Turing.DynamicPPL.getlogjoint_internal(vi_spl) + ℓp = ℓ.getlogdensity(vi_spl) θ = vi_spl[:] if all(isfinite.(θ)) && isfinite(ℓp) From cb9ebb4a4eb615c2113359abf912751a3acad6d8 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 19:14:26 +0100 Subject: [PATCH 08/11] fix initial_params in tests --- test/turing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/turing.jl b/test/turing.jl index b0e1886..c7a2700 100644 --- a/test/turing.jl +++ b/test/turing.jl @@ -49,7 +49,7 @@ model, externalsampler(sampler), n_samples; - initial_params=[1.0, 0.1], + initial_params=InitFromParams((s = 1.0, m = 0.1)), progress=false, ) From 0e123749e67813385abed5cf935a160d00f3968c Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 19:26:48 +0100 Subject: [PATCH 09/11] Fix docs and format --- .github/workflows/Docs.yml | 2 ++ test/turing.jl | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/Docs.yml b/.github/workflows/Docs.yml index 475399d..024f8e1 100644 --- a/.github/workflows/Docs.yml +++ b/.github/workflows/Docs.yml @@ -24,6 +24,8 @@ jobs: steps: - name: Build and deploy Documenter.jl docs uses: TuringLang/actions/DocsDocumenter@main + with: + julia-version: 1.11 - name: Run doctests shell: julia --project=docs --color=yes {0} diff --git a/test/turing.jl b/test/turing.jl index c7a2700..db02909 100644 --- a/test/turing.jl +++ b/test/turing.jl @@ -49,7 +49,7 @@ model, externalsampler(sampler), n_samples; - initial_params=InitFromParams((s = 1.0, m = 0.1)), + initial_params=InitFromParams((s=1.0, m=0.1)), progress=false, ) From f97f89f4bc90005a2536ce6e23c703a6e53afe26 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 22 Oct 2025 20:24:22 +0100 Subject: [PATCH 10/11] Fix more docs --- docs/src/gibbs_polar.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/src/gibbs_polar.md b/docs/src/gibbs_polar.md index 8372708..191ae17 100644 --- a/docs/src/gibbs_polar.md +++ b/docs/src/gibbs_polar.md @@ -63,8 +63,9 @@ end model = demo() n_samples = 1000 -latent_chain = sample(model, externalsampler(LatentSlice(10)), n_samples; initial_params=ones(10)) -polar_chain = sample(model, externalsampler(GibbsPolarSlice(10)), n_samples; initial_params=ones(10)) +initial_params = InitFromParams((x = ones(10),)) +latent_chain = sample(model, externalsampler(LatentSlice(10)), n_samples; initial_params=initial_params) +polar_chain = sample(model, externalsampler(GibbsPolarSlice(10)), n_samples; initial_params=initial_params) l = @layout [a; b] p1 = Plots.plot(1:n_samples, latent_chain[:,1,:], ylims=[-10,10], label="LSS") From 18dacad4d01e1a665f60667c6a6c6add31625b38 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Fri, 24 Oct 2025 11:35:23 +0100 Subject: [PATCH 11/11] Fix docstring --- src/multivariate/gibbspolar.jl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/multivariate/gibbspolar.jl b/src/multivariate/gibbspolar.jl index f5be0d3..7872ee4 100644 --- a/src/multivariate/gibbspolar.jl +++ b/src/multivariate/gibbspolar.jl @@ -18,10 +18,7 @@ Gibbsian polar slice sampling algorithm by P. Schär, M. Habeck, and D. Rudolf [ The initial window size `w` must be set at least an order of magnitude larger than what is sensible for other slice samplers. Otherwise, a large number of rejections might be experienced. !!! warning - When initializing the chain (*e.g.* the `initial_params` keyword arguments in `AbstractMCMC.sample`), it is necessary to inialize from a point \$\$x_0\$\$ that has a sensible norm \$\$\\lVert x_0 \\rVert > 0\$\$, otherwise, the chain will start from a pathologic point in polar coordinates. This might even result in the sampler getting stuck in an infinite loop. (This can be prevented by setting `max_proposals`.) If \$\$\\lVert x_0 \\rVert \\leq 10^{-5}\$\$, the current implementation will display a warning. - -!!! info - For Turing users: `Turing` might change `initial_params` to match the support of the posterior. This might lead to \$\$\\lVert x_0 \\rVert\$\$ being small, even though the vector you passed to`initial_params` has a sufficiently large norm. If this is suspected, simply try a different initialization value. + When initializing the chain (*e.g.* the `initial_params` keyword arguments in `AbstractMCMC.sample`), it is necessary to initialize from a point \$\$x_0\$\$ that has a sensible norm \$\$\\lVert x_0 \\rVert > 0\$\$, otherwise, the chain will start from a pathological point in polar coordinates. This might even result in the sampler getting stuck in an infinite loop. (This can be prevented by setting `max_proposals`.) If \$\$\\lVert x_0 \\rVert \\leq 10^{-5}\$\$, the current implementation will display a warning. """ struct GibbsPolarSlice{W<:Real} <: AbstractMultivariateSliceSampling w::W