Skip to content

Commit e6d94cc

Browse files
authored
Compat for Turing v0.41 (#44)
* Compat for Turing v0.41 * Bump patch * use original vi (shouldn't make a difference) * Update comment * bump deps in docs/ and test/ as well * Format * That was a mistake; use getlogdensity instead of hardcoding the function * fix initial_params in tests * Fix docs and format * Fix more docs * Fix docstring
1 parent 837fe07 commit e6d94cc

File tree

8 files changed

+20
-23
lines changed

8 files changed

+20
-23
lines changed

.github/workflows/Docs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ jobs:
2424
steps:
2525
- name: Build and deploy Documenter.jl docs
2626
uses: TuringLang/actions/DocsDocumenter@main
27+
with:
28+
julia-version: 1.11
2729

2830
- name: Run doctests
2931
shell: julia --project=docs --color=yes {0}

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "SliceSampling"
22
uuid = "43f4d3e8-9711-4a8c-bd1b-03ac73a255cf"
3-
version = "0.7.8"
3+
version = "0.7.9"
44

55
[deps]
66
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
@@ -21,7 +21,7 @@ Distributions = "0.25"
2121
LinearAlgebra = "1"
2222
LogDensityProblems = "2"
2323
Random = "1"
24-
Turing = "0.40"
24+
Turing = "0.41"
2525
julia = "1.10"
2626

2727
[extras]

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ Random = "1"
2828
SliceSampling = "0.7.1"
2929
StableRNGs = "1"
3030
Statistics = "1"
31-
Turing = "0.37, 0.38, 0.39, 0.40"
31+
Turing = "0.41"
3232
julia = "1.10"

docs/src/gibbs_polar.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ end
6363
model = demo()
6464
6565
n_samples = 1000
66-
latent_chain = sample(model, externalsampler(LatentSlice(10)), n_samples; initial_params=ones(10))
67-
polar_chain = sample(model, externalsampler(GibbsPolarSlice(10)), n_samples; initial_params=ones(10))
66+
initial_params = InitFromParams((x = ones(10),))
67+
latent_chain = sample(model, externalsampler(LatentSlice(10)), n_samples; initial_params=initial_params)
68+
polar_chain = sample(model, externalsampler(GibbsPolarSlice(10)), n_samples; initial_params=initial_params)
6869
6970
l = @layout [a; b]
7071
p1 = Plots.plot(1:n_samples, latent_chain[:,1,:], ylims=[-10,10], label="LSS")

ext/SliceSamplingTuringExt.jl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,30 @@ end
3838
function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDensityFunction)
3939
n_max_attempts = 1000
4040

41-
model =.model
42-
vi = Turing.DynamicPPL.VarInfo(rng, model, Turing.SampleFromUniform())
43-
vi_spl = last(Turing.DynamicPPL.evaluate_and_sample!!(rng, model, vi, Turing.SampleFromUniform()))
44-
θ = vi_spl[:]
45-
ℓp = LogDensityProblems.logdensity(ℓ, θ)
41+
model, vi =.model, ℓ.varinfo
42+
vi_spl = last(
43+
Turing.DynamicPPL.init!!(rng, model, vi, Turing.DynamicPPL.InitFromUniform())
44+
)
45+
ℓp =.getlogdensity(vi_spl)
4646

4747
init_attempt_count = 1
4848
for attempts in 1:n_max_attempts
4949
if attempts == 10
5050
@warn "Failed to find valid initial parameters after $(init_attempt_count) attempts; consider providing explicit initial parameters using the `initial_params` keyword"
5151
end
5252

53-
# NOTE: This will sample in the unconstrained space.
54-
vi_spl = last(
55-
Turing.DynamicPPL.evaluate_and_sample!!(
56-
rng, model, vi, Turing.SampleFromUniform()
57-
),
58-
)
53+
# NOTE: This will sample in the unconstrained space if ℓ.varinfo is linked
54+
vi_spl = last(Turing.DynamicPPL.init!!(rng, model, vi, Turing.InitFromUniform()))
55+
ℓp =.getlogdensity(vi_spl)
5956
θ = vi_spl[:]
60-
ℓp = LogDensityProblems.logdensity(ℓ, θ)
6157

6258
if all(isfinite.(θ)) && isfinite(ℓp)
6359
return θ
6460
end
6561
end
6662

6763
@error "Failed to find valid initial parameters after $(n_max_attempts) attempts; consider providing explicit initial parameters using the `initial_params` keyword"
64+
θ = vi_spl[:]
6865
return θ
6966
end
7067

src/multivariate/gibbspolar.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ Gibbsian polar slice sampling algorithm by P. Schär, M. Habeck, and D. Rudolf [
1818
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.
1919
2020
!!! warning
21-
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.
22-
23-
!!! info
24-
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.
21+
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.
2522
"""
2623
struct GibbsPolarSlice{W<:Real} <: AbstractMultivariateSliceSampling
2724
w::W

test/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ MCMCTesting = "0.3"
1818
Random = "1"
1919
StableRNGs = "1"
2020
Test = "1"
21-
Turing = "0.37, 0.38, 0.39, 0.40"
21+
Turing = "0.41"
2222
julia = "1.10"

test/turing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
model,
5050
externalsampler(sampler),
5151
n_samples;
52-
initial_params=[1.0, 0.1],
52+
initial_params=InitFromParams((s=1.0, m=0.1)),
5353
progress=false,
5454
)
5555

0 commit comments

Comments
 (0)