-
Notifications
You must be signed in to change notification settings - Fork 230
Add elliptical slice sampling algorithm #1000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
584d78a
Add elliptical slice sampling algorithm
devmotion 2de7d8b
Allow nonzero mean and update according to comments
devmotion 82a04ff
Update error message
devmotion 7393aad
Merge branch 'master' into ess
devmotion 7887f7c
Merge branch 'master' into ess
devmotion bb4d1ea
Add _getvns methods and remove static parameters
devmotion 2d9bc42
Remove Nothing sampler again
devmotion 249d505
Update implementation of elliptical slice sampling
devmotion 9464654
Remove more nothing
devmotion 695dc1c
Fix tests
devmotion 21a7158
Merge branch 'master' into ess
devmotion 4225e15
Remove some Unicode characters
devmotion 2b57d6e
Merge branch 'master' into ess
devmotion 946a6f5
Overload tilde and dot_tilde and test dot notation
devmotion e2f9d44
Fix error
devmotion 94e1e6f
Fix test errors on Julia 1.0
devmotion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| """ | ||
| ESS | ||
|
|
||
| Elliptical slice sampling algorithm. | ||
|
|
||
| # Examples | ||
| ```jldoctest; setup = :(Random.seed!(1)) | ||
| julia> @model gdemo(x) = begin | ||
| m ~ Normal() | ||
| x ~ Normal(m, 0.5) | ||
| end | ||
| gdemo (generic function with 2 methods) | ||
|
|
||
| julia> sample(gdemo(1.0), ESS(), 1_000) |> mean | ||
| Mean | ||
|
|
||
| │ Row │ parameters │ mean │ | ||
| │ │ Symbol │ Float64 │ | ||
| ├─────┼────────────┼──────────┤ | ||
| │ 1 │ m │ 0.824853 │ | ||
| ``` | ||
| """ | ||
| struct ESS{space} <: InferenceAlgorithm end | ||
|
|
||
| ESS() = ESS{()}() | ||
| ESS(space::Symbol) = ESS{(space,)}() | ||
|
|
||
| mutable struct ESSState{V<:VarInfo} <: AbstractSamplerState | ||
| vi::V | ||
| end | ||
|
|
||
| function Sampler(alg::ESS, model::Model, s::Selector) | ||
| # sanity check | ||
| vi = VarInfo(model) | ||
| space = getspace(alg) | ||
| vns = _getvns(vi, s, Val(space)) | ||
| length(vns) == 1 || | ||
| error("[ESS] does only support one variable ($(length(vns)) variables specified)") | ||
| for vn in vns[1] | ||
| dist = getdist(vi, vn) | ||
| isgaussian(dist) || | ||
| error("[ESS] only supports Gaussian prior distributions") | ||
| end | ||
|
|
||
| state = ESSState(vi) | ||
| info = Dict{Symbol, Any}() | ||
|
|
||
| return Sampler(alg, info, s, state) | ||
| end | ||
|
|
||
| isgaussian(dist) = false | ||
| isgaussian(::Normal) = true | ||
| isgaussian(::NormalCanon) = true | ||
| isgaussian(::AbstractMvNormal) = true | ||
|
|
||
| # always accept in the first step | ||
| function step!(::AbstractRNG, model::Model, spl::Sampler{<:ESS}, ::Integer; kwargs...) | ||
| return Transition(spl) | ||
| end | ||
|
|
||
| function step!( | ||
| rng::AbstractRNG, | ||
| model::Model, | ||
| spl::Sampler{<:ESS}, | ||
| ::Integer, | ||
| ::Transition; | ||
| kwargs... | ||
| ) | ||
| # obtain mean of distribution | ||
| vi = spl.state.vi | ||
| vns = _getvns(vi, spl) | ||
| μ = mapreduce(vcat, vns[1]) do vn | ||
| dist = getdist(vi, vn) | ||
| vectorize(dist, mean(dist)) | ||
| end | ||
|
|
||
| # obtain previous sample | ||
| f = vi[spl] | ||
|
|
||
| # recompute log-likelihood in logp | ||
| if spl.selector.tag !== :default | ||
| runmodel!(model, vi, spl) | ||
| end | ||
|
|
||
| # sample log-likelihood threshold for the next sample | ||
| threshold = getlogp(vi) - randexp(rng) | ||
|
|
||
| # sample from the prior | ||
| set_flag!(vi, vns[1][1], "del") | ||
| runmodel!(model, vi, spl) | ||
| ν = vi[spl] | ||
|
|
||
| # sample initial angle | ||
| θ = 2 * π * rand(rng) | ||
| θmin = θ - 2 * π | ||
| θmax = θ | ||
|
|
||
| while true | ||
| # compute proposal and apply correction for distributions with nonzero mean | ||
| sinθ, cosθ = sincos(θ) | ||
| a = 1 - (sinθ + cosθ) | ||
| vi[spl] = @. f * cosθ + ν * sinθ + μ * a | ||
|
|
||
| # recompute log-likelihood and check if threshold is reached | ||
| runmodel!(model, vi, spl) | ||
| if getlogp(vi) > threshold | ||
| break | ||
| end | ||
|
|
||
| # shrink the bracket | ||
| if θ < 0 | ||
| θmin = θ | ||
| else | ||
| θmax = θ | ||
| end | ||
|
|
||
| # sample new angle | ||
| θ = θmin + rand(rng) * (θmax - θmin) | ||
| end | ||
|
|
||
| return Transition(spl) | ||
| end | ||
|
|
||
| function tilde(ctx::DefaultContext, sampler::Sampler{<:ESS}, right, vn::VarName, inds, vi) | ||
| if vn in getspace(sampler) | ||
| return tilde(LikelihoodContext(), SampleFromPrior(), right, vn, inds, vi) | ||
| else | ||
| return tilde(ctx, SampleFromPrior(), right, vn, inds, vi) | ||
| end | ||
| end | ||
|
|
||
| function tilde(ctx::DefaultContext, sampler::Sampler{<:ESS}, right, left, vi) | ||
| return tilde(ctx, SampleFromPrior(), right, left, vi) | ||
| end | ||
|
|
||
| function dot_tilde(ctx::DefaultContext, sampler::Sampler{<:ESS}, right, left, vn::VarName, inds, vi) | ||
| if vn in getspace(sampler) | ||
| return dot_tilde(LikelihoodContext(), SampleFromPrior(), right, left, vn, inds, vi) | ||
| else | ||
| return dot_tilde(ctx, SampleFromPrior(), right, left, vn, inds, vi) | ||
| end | ||
| end | ||
|
|
||
| function dot_tilde(ctx::DefaultContext, sampler::Sampler{<:ESS}, right, left, vi) | ||
| return dot_tilde(ctx, SampleFromPrior(), right, left, vi) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using Turing, Random, Test | ||
|
|
||
| dir = splitdir(splitdir(pathof(Turing))[1])[1] | ||
| include(dir*"/test/test_utils/AllUtils.jl") | ||
|
|
||
| @testset "ESS" begin | ||
| @model demo(x) = begin | ||
| m ~ Normal() | ||
| x ~ Normal(m, 0.5) | ||
| end | ||
| demo_default = demo(1.0) | ||
|
|
||
| @model demodot(x) = begin | ||
| m = Vector{Float64}(undef, 2) | ||
| @. m ~ Normal() | ||
| x ~ Normal(m[2], 0.5) | ||
| end | ||
| demodot_default = demodot(1.0) | ||
|
|
||
| @turing_testset "ESS constructor" begin | ||
| Random.seed!(0) | ||
| N = 500 | ||
| s1 = ESS() | ||
| s2 = ESS(:m) | ||
| s3 = Gibbs(ESS(:m), MH(:s)) | ||
|
|
||
| c1 = sample(demo_default, s1, N) | ||
| c2 = sample(demo_default, s2, N) | ||
| c3 = sample(demodot_default, s1, N) | ||
| c4 = sample(demodot_default, s2, N) | ||
| c5 = sample(gdemo_default, s3, N) | ||
| end | ||
|
|
||
| @numerical_testset "ESS inference" begin | ||
| Random.seed!(1) | ||
| chain = sample(demo_default, ESS(), 5_000) | ||
| check_numerical(chain, [:m], [0.8], atol = 0.1) | ||
|
|
||
| Random.seed!(1) | ||
| chain = sample(demodot_default, ESS(), 5_000) | ||
| check_numerical(chain, ["m[1]", "m[2]"], [0.0, 0.8], atol = 0.1) | ||
|
|
||
| Random.seed!(100) | ||
| alg = Gibbs( | ||
| CSMC(15, :s), | ||
| ESS(:m)) | ||
| chain = sample(gdemo(1.5, 2.0), alg, 10_000) | ||
| check_numerical(chain, [:s, :m], [49/24, 7/6], atol=0.1) | ||
|
|
||
| # MoGtest | ||
| Random.seed!(125) | ||
| alg = Gibbs( | ||
| CSMC(15, :z1, :z2, :z3, :z4), | ||
| ESS(:mu1), ESS(:mu2)) | ||
| chain = sample(MoGtest_default, alg, 6000) | ||
| check_MoGtest_default(chain, atol = 0.1) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.