Skip to content

Commit bc760b0

Browse files
sunxd3torfjelde
andauthored
Add a model argument to getparams and setparams!! functions (#150)
* add optional argument of logdensity_function * version bump * apply suggestions * add some default implementations * Apply suggestions from code review Co-authored-by: Tor Erlend Fjelde <[email protected]> * Update src/AbstractMCMC.jl Co-authored-by: Tor Erlend Fjelde <[email protected]> * Update Project.toml * add some documentation improvement * improve added doc --------- Co-authored-by: Tor Erlend Fjelde <[email protected]>
1 parent 467b076 commit bc760b0

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

Diff for: Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
33
keywords = ["markov chain monte carlo", "probabilistic programming"]
44
license = "MIT"
55
desc = "A lightweight interface for common MCMC methods."
6-
version = "5.5.0"
6+
version = "5.6.0"
77

88
[deps]
99
BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66"

Diff for: docs/src/api.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ To make it a bit easier to interact with some arbitrary sampler state, we encour
121121
AbstractMCMC.getparams
122122
AbstractMCMC.setparams!!
123123
```
124-
These methods can also be useful for implementing samplers which wraps some inner samplers, e.g. a mixture of samplers.
124+
`getparams` and `setparams!!` provide a generic interface for interacting with the parameters of a sampler's state, regardless of how that state is represented internally.
125+
126+
This allows generic code to be written that works with any sampler implementing this interface. For example, a generic ensemble sampler could use `getparams` to extract the parameters from each of its component samplers' states, and `setparams!!` to initialize each component sampler with a different set of parameters.
127+
128+
The optional `model` argument to these functions allows sampler implementations to customize their behavior based on the model being used. For example, some samplers may need to evaluate the log density at new parameter values when setting parameters, which requires access to the model. If access to `model` is not needed, the sampler only needs to implement the version without the `model` argument - the default implementations will then call those methods directly.
129+
130+
These methods are particularly useful for implementing samplers which wrap some inner samplers, such as a mixture of samplers. In the next section, we will see how `getparams` and `setparams!!` can be used to implement a `MixtureSampler`.
125131

126132
### Example: `MixtureSampler`
127133

Diff for: src/AbstractMCMC.jl

+15-4
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,37 @@ The `MCMCSerial` algorithm allows users to sample serially, with no thread or pr
8181
struct MCMCSerial <: AbstractMCMCEnsemble end
8282

8383
"""
84-
getparams(state[; kwargs...])
84+
getparams([model::AbstractModel, ]state)
8585
8686
Retrieve the values of parameters from the sampler's `state` as a `Vector{<:Real}`.
8787
"""
8888
function getparams end
8989

90+
function getparams(model::AbstractModel, state)
91+
return getparams(state)
92+
end
93+
9094
"""
91-
setparams!!(state, params)
95+
setparams!!([model::AbstractModel, ]state, params)
9296
9397
Set the values of parameters in the sampler's `state` from a `Vector{<:Real}`.
9498
9599
This function should follow the `BangBang` interface: mutate `state` in-place if possible and
96100
return the mutated `state`. Otherwise, it should return a new `state` containing the updated parameters.
97101
98-
Although not enforced, it should hold that `setparams!!(state, getparams(state)) == state`. In another
99-
word, the sampler should implement a consistent transformation between its internal representation
102+
Although not enforced, it should hold that `setparams!!(state, getparams(state)) == state`. In other
103+
words, the sampler should implement a consistent transformation between its internal representation
100104
and the vector representation of the parameter values.
105+
106+
Sometimes, to maintain the consistency of the log density and parameter values, a `model`
107+
should be provided. This is useful for samplers that need to evaluate the log density at the new parameter values.
101108
"""
102109
function setparams!! end
103110

111+
function setparams!!(model::AbstractModel, state, params)
112+
return setparams!!(state, params)
113+
end
114+
104115
include("samplingstats.jl")
105116
include("logging.jl")
106117
include("interface.jl")

0 commit comments

Comments
 (0)