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 a model argument to getparams and setparams!! functions #150

Merged
merged 9 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
keywords = ["markov chain monte carlo", "probabilistic programming"]
license = "MIT"
desc = "A lightweight interface for common MCMC methods."
version = "5.5.0"
version = "5.5.1"
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved

[deps]
BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66"
Expand Down
31 changes: 28 additions & 3 deletions src/AbstractMCMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,51 @@ The `MCMCSerial` algorithm allows users to sample serially, with no thread or pr
struct MCMCSerial <: AbstractMCMCEnsemble end

"""
getparams(state[; kwargs...])
getparams(model::AbstractModel, state)
getparams(logdensity, state)
getparams(state)
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved

Retrieve the values of parameters from the sampler's `state` as a `Vector{<:Real}`.
"""
function getparams end

function getparams(logdensity, state)
return getparams(_model(logdensity), state)
end

sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
function getparams(model::AbstractModel, state)
return getparams(state)
end

"""
setparams!!(model::AbstractModel, state, params)
setparams!!(logdensity, state, params)
setparams!!(state, params)
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved

Set the values of parameters in the sampler's `state` from a `Vector{<:Real}`.

This function should follow the `BangBang` interface: mutate `state` in-place if possible and
return the mutated `state`. Otherwise, it should return a new `state` containing the updated parameters.

Although not enforced, it should hold that `setparams!!(state, getparams(state)) == state`. In another
word, the sampler should implement a consistent transformation between its internal representation
Although not enforced, it should hold that `setparams!!(state, getparams(state)) == state`. In other
words, the sampler should implement a consistent transformation between its internal representation
and the vector representation of the parameter values.

Sometimes, to maintain the consistency of the log density and parameter values, a `model::AbstractModel`
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
should be provided. This is useful for samplers that need to evaluate the log density at the new parameter values.
If `model` is not an `AbstractMCMC.AbstractModel`, by default, it is assumed to be a log density function following
the `LogDensityProblems.jl` interface, and will be wrapped with [`AbstractMCMC.LogDensityModel`](@ref).
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
"""
function setparams!! end
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved

function setparams!!(logdensity, state, params)
return setparams!!(_model(logdensity), state, params)
end

sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
function setparams!!(model::AbstractModel, state, params)
return setparams!!(state, params)
end

include("samplingstats.jl")
include("logging.jl")
include("interface.jl")
Expand Down
Loading