Skip to content
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

Add getparams and setparams!! following AbstractMCMC v5.5 and v5.6 #103

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name = "AdvancedMH"
uuid = "5b7e9947-ddc0-4b3f-9b55-0d8042f74170"
version = "0.8.3"
version = "0.8.4"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -23,18 +24,19 @@ AdvancedMHMCMCChainsExt = "MCMCChains"
AdvancedMHStructArraysExt = "StructArrays"

[compat]
AbstractMCMC = "5"
AbstractMCMC = "5.5"
BangBang = "0.3.19, 0.4"
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
DiffResults = "1"
Distributions = "0.25"
FillArrays = "1"
ForwardDiff = "0.10"
LinearAlgebra = "1.6"
LogDensityProblems = "2"
MCMCChains = "6.0.4"
Random = "1.6"
Requires = "1"
StructArrays = "0.6"
julia = "1.6"
LinearAlgebra = "1.6"
Random = "1.6"

[extras]
DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
Expand Down
10 changes: 10 additions & 0 deletions src/AdvancedMH.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module AdvancedMH

# Import the relevant libraries.
using AbstractMCMC
using BangBang
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
using Distributions
using LinearAlgebra: I
using FillArrays: Zeros
Expand Down Expand Up @@ -140,6 +141,15 @@ function __init__()
end
end

# AbstractMCMC.jl interface
function AbstractMCMC.getparams(t::Transition)
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
return t.params
end

function AbstractMCMC.setparams!!(t::Transition, params)
return BangBang.setproperty!!(t, :params, params)
end

# Include inference methods.
include("proposal.jl")
include("mh-core.jl")
Expand Down
12 changes: 10 additions & 2 deletions src/MALA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MALA(d::RandomWalkProposal) = MALA{typeof(d)}(d)
MALA(d) = MALA(RandomWalkProposal(d))


struct GradientTransition{T<:Union{Vector, Real, NamedTuple}, L<:Real, G<:Union{Vector, Real, NamedTuple}} <: AbstractTransition
struct GradientTransition{T<:Union{Vector,Real,NamedTuple},L<:Real,G<:Union{Vector,Real,NamedTuple}} <: AbstractTransition
params::T
lp::L
gradient::G
Expand All @@ -20,6 +20,14 @@ end

logdensity(model::DensityModelOrLogDensityModel, t::GradientTransition) = t.lp

function AbstractMCMC.getparams(t::GradientTransition)
return t.params
end

function AbstractMCMC.setparams!!(t::GradientTransition, params)
return BangBang.setproperty!!(t, :params, params)
end
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved

propose(::Random.AbstractRNG, ::MALA, ::DensityModelOrLogDensityModel) = error("please specify initial parameters")
function transition(sampler::MALA, model::DensityModelOrLogDensityModel, params, accepted)
return GradientTransition(params, logdensity_and_gradient(model, params)..., accepted)
Expand Down Expand Up @@ -88,6 +96,6 @@ logdensity_and_gradient(::DensityModelOrLogDensityModel, ::Any)
function logdensity_and_gradient(model::AbstractMCMC.LogDensityModel, params)
check_capabilities(model)
return LogDensityProblems.logdensity_and_gradient(model.logdensity, params)
end
end


12 changes: 11 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AdvancedMH
using AbstractMCMC
using DiffResults
using Distributions
using ForwardDiff
Expand Down Expand Up @@ -33,6 +34,15 @@ include("util.jl")
LogDensityProblems.logdensity(::typeof(density), θ) = density(θ)
LogDensityProblems.dimension(::typeof(density)) = 2

@testset "getparams/setparams!! (AbstractMCMC interface)" begin
test_spl = StaticMH([Normal(0, 1), Normal(0, 1)])
t, _ = AbstractMCMC.step(Random.default_rng(), model, test_spl)
@test AbstractMCMC.getparams(t) == t.params
@test AbstractMCMC.setparams!!(t, AbstractMCMC.getparams(t)) == t
t_replaced = AbstractMCMC.setparams!!(t, (μ=1.0, σ=2.0))
@test t_replaced.params == (μ=1.0, σ=2.0)
end
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved

@testset "StaticMH" begin
# Set up our sampler with initial parameters.
spl1 = StaticMH([Normal(0,1), Normal(0, 1)])
Expand Down Expand Up @@ -69,7 +79,7 @@ include("util.jl")
@test mean(chain1.σ) ≈ 1.0 atol=0.1
@test mean(chain2.μ) ≈ 0.0 atol=0.1
@test mean(chain2.σ) ≈ 1.0 atol=0.1
@test mean(chain3.μ) ≈ 0.0 atol=0.1
@test mean(chain3.μ) ≈ 0.0 atol=0.15
@test mean(chain3.σ) ≈ 1.0 atol=0.1
end

Expand Down
Loading