Skip to content

Commit

Permalink
Add getparams and setparams!! following AbstractMCMC v5.5 and v…
Browse files Browse the repository at this point in the history
…5.6 (#103)

* add `getparams` and `setparams!!`

* add BangBang as dep, add functions for `GradientTransition`

* increase atol

* Update test/runtests.jl

Co-authored-by: Penelope Yong <[email protected]>

* update functions with `model` arguments

* fix test errors

* remove BangBang dep

---------

Co-authored-by: Penelope Yong <[email protected]>
  • Loading branch information
sunxd3 and penelopeysm authored Oct 31, 2024
1 parent 4442783 commit 98a1041
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AdvancedMH"
uuid = "5b7e9947-ddc0-4b3f-9b55-0d8042f74170"
version = "0.8.3"
version = "0.8.4"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand All @@ -23,18 +23,18 @@ AdvancedMHMCMCChainsExt = "MCMCChains"
AdvancedMHStructArraysExt = "StructArrays"

[compat]
AbstractMCMC = "5"
AbstractMCMC = "5.6"
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
14 changes: 14 additions & 0 deletions src/AdvancedMH.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ function __init__()
end
end

# AbstractMCMC.jl interface
function AbstractMCMC.getparams(t::Transition)
return t.params
end

# TODO (sunxd): remove `DensityModel` in favor of `AbstractMCMC.LogDensityModel`
function AbstractMCMC.setparams!!(model::DensityModelOrLogDensityModel, t::Transition, params)
return Transition(
params,
logdensity(model, params),
t.accepted
)
end

# Include inference methods.
include("proposal.jl")
include("mh-core.jl")
Expand Down
18 changes: 16 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,20 @@ end

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

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

function AbstractMCMC.setparams!!(model::DensityModelOrLogDensityModel, t::GradientTransition, params)
lp, gradient = logdensity_and_gradient(model, params)
return GradientTransition(
params,
lp,
gradient,
t.accepted
)
end

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 +102,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


22 changes: 21 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,25 @@ include("util.jl")
LogDensityProblems.logdensity(::typeof(density), θ) = density(θ)
LogDensityProblems.dimension(::typeof(density)) = 2

@testset "getparams/setparams!! (AbstractMCMC interface)" begin
t1, _ = AbstractMCMC.step(Random.default_rng(), model, StaticMH([Normal(0, 1), Normal(0, 1)]))
t2, _ = AbstractMCMC.step(Random.default_rng(), model, MALA(x -> MvNormal(x, I)); initial_params=ones(2))
for t in [t1, t2]
@test AbstractMCMC.getparams(model, t) == t.params

new_transition = AbstractMCMC.setparams!!(model, t, AbstractMCMC.getparams(model, t))
@test new_transition.lp == t.lp
@test new_transition.accepted == t.accepted
@test new_transition.params == t.params
if hasfield(typeof(t), :gradient)
@test new_transition.gradient == t.gradient
end

t_replaced = AbstractMCMC.setparams!!(model, t, [1.0, 2.0])
@test t_replaced.params == [1.0, 2.0]
end
end

@testset "StaticMH" begin
# Set up our sampler with initial parameters.
spl1 = StaticMH([Normal(0,1), Normal(0, 1)])
Expand Down Expand Up @@ -69,7 +89,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

4 comments on commit 98a1041

@sunxd3
Copy link
Member Author

@sunxd3 sunxd3 commented on 98a1041 Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Register Failed
@sunxd3, it looks like you are not a publicly listed member/owner in the parent organization (TuringLang).
If you are a member/owner, you will need to change your membership to public. See GitHub Help

@sunxd3
Copy link
Member Author

@sunxd3 sunxd3 commented on 98a1041 Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/118449

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.4 -m "<description of version>" 98a10418d0b43a9b16ddb3f1a265cd844edbbf23
git push origin v0.8.4

Please sign in to comment.