From e79e7b9b0774607a83aec2996529fe5f9744d27f Mon Sep 17 00:00:00 2001 From: Xianda Sun Date: Wed, 23 Oct 2024 20:13:33 +0100 Subject: [PATCH 1/9] add optional argument of logdensity_function --- src/AbstractMCMC.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/AbstractMCMC.jl b/src/AbstractMCMC.jl index 8343bfa..dac49db 100644 --- a/src/AbstractMCMC.jl +++ b/src/AbstractMCMC.jl @@ -81,23 +81,27 @@ The `MCMCSerial` algorithm allows users to sample serially, with no thread or pr struct MCMCSerial <: AbstractMCMCEnsemble end """ - getparams(state[; kwargs...]) + getparams(state[, logdensity_function]) Retrieve the values of parameters from the sampler's `state` as a `Vector{<:Real}`. """ function getparams end """ - setparams!!(state, params) + setparams!!(state, params[, logdensity_function]) 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, an optional +`logdensity_function` can be provided. This is useful for samplers that need to evaluate the +log density at the new parameter values. """ function setparams!! end From a4d2f5d8d19fce122792b31edf0175e5bae9a3ea Mon Sep 17 00:00:00 2001 From: Xianda Sun Date: Wed, 23 Oct 2024 20:16:23 +0100 Subject: [PATCH 2/9] version bump --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c4ef9ae..19710e2 100644 --- a/Project.toml +++ b/Project.toml @@ -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" [deps] BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66" From 8d63557fba6022d3931497d7b784dc7f76dce0ec Mon Sep 17 00:00:00 2001 From: Xianda Sun Date: Fri, 25 Oct 2024 07:25:17 +0100 Subject: [PATCH 3/9] apply suggestions --- src/AbstractMCMC.jl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/AbstractMCMC.jl b/src/AbstractMCMC.jl index dac49db..c394b9a 100644 --- a/src/AbstractMCMC.jl +++ b/src/AbstractMCMC.jl @@ -81,14 +81,16 @@ The `MCMCSerial` algorithm allows users to sample serially, with no thread or pr struct MCMCSerial <: AbstractMCMCEnsemble end """ - getparams(state[, logdensity_function]) + getparams(model::AbstractModel, state) + getparams(state) Retrieve the values of parameters from the sampler's `state` as a `Vector{<:Real}`. """ function getparams end """ - setparams!!(state, params[, logdensity_function]) + setparams!!(model::AbstractModel, state, params) + setparams!!(state, params) Set the values of parameters in the sampler's `state` from a `Vector{<:Real}`. @@ -99,9 +101,8 @@ Although not enforced, it should hold that `setparams!!(state, getparams(state)) 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, an optional -`logdensity_function` can be provided. This is useful for samplers that need to evaluate the -log density at the new parameter values. +Sometimes, to maintain the consistency of the log density and parameter values, a `model::AbstractModel` +should be provided. This is useful for samplers that need to evaluate the log density at the new parameter values. """ function setparams!! end From 2d0292ccc4ceef19b79b61be6b888722d72dc6e0 Mon Sep 17 00:00:00 2001 From: Xianda Sun Date: Fri, 25 Oct 2024 07:30:33 +0100 Subject: [PATCH 4/9] add some default implementations --- src/AbstractMCMC.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/AbstractMCMC.jl b/src/AbstractMCMC.jl index c394b9a..4ac325c 100644 --- a/src/AbstractMCMC.jl +++ b/src/AbstractMCMC.jl @@ -82,14 +82,24 @@ struct MCMCSerial <: AbstractMCMCEnsemble end """ getparams(model::AbstractModel, state) + getparams(logdensity, state) getparams(state) 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 + +function getparams(model::AbstractModel, state) + return getparams(state) +end + """ setparams!!(model::AbstractModel, state, params) + setparams!!(logdensity, state, params) setparams!!(state, params) Set the values of parameters in the sampler's `state` from a `Vector{<:Real}`. @@ -103,9 +113,19 @@ and the vector representation of the parameter values. Sometimes, to maintain the consistency of the log density and parameter values, a `model::AbstractModel` 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). """ function setparams!! end +function setparams!!(logdensity, state, params) + return setparams!!(_model(logdensity), state, params) +end + +function setparams!!(model::AbstractModel, state, params) + return setparams!!(state, params) +end + include("samplingstats.jl") include("logging.jl") include("interface.jl") From 6855840dfa877fb72e2947f6ed5b582182bf7e2c Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Fri, 25 Oct 2024 22:27:49 +0800 Subject: [PATCH 5/9] Apply suggestions from code review Co-authored-by: Tor Erlend Fjelde --- src/AbstractMCMC.jl | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/AbstractMCMC.jl b/src/AbstractMCMC.jl index 4ac325c..83d7c94 100644 --- a/src/AbstractMCMC.jl +++ b/src/AbstractMCMC.jl @@ -81,18 +81,12 @@ The `MCMCSerial` algorithm allows users to sample serially, with no thread or pr struct MCMCSerial <: AbstractMCMCEnsemble end """ - getparams(model::AbstractModel, state) - getparams(logdensity, state) - getparams(state) + getparams([model::AbstractModel, ]state) 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 - function getparams(model::AbstractModel, state) return getparams(state) end @@ -111,17 +105,11 @@ Although not enforced, it should hold that `setparams!!(state, getparams(state)) 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` +Sometimes, to maintain the consistency of the log density and parameter values, a `model` 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). """ function setparams!! end -function setparams!!(logdensity, state, params) - return setparams!!(_model(logdensity), state, params) -end - function setparams!!(model::AbstractModel, state, params) return setparams!!(state, params) end From 4c508488aa3e15f1401d8a48fa0e5c1e1f053473 Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Fri, 25 Oct 2024 22:29:19 +0800 Subject: [PATCH 6/9] Update src/AbstractMCMC.jl Co-authored-by: Tor Erlend Fjelde --- src/AbstractMCMC.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/AbstractMCMC.jl b/src/AbstractMCMC.jl index 83d7c94..b7c35fb 100644 --- a/src/AbstractMCMC.jl +++ b/src/AbstractMCMC.jl @@ -92,9 +92,7 @@ function getparams(model::AbstractModel, state) end """ - setparams!!(model::AbstractModel, state, params) - setparams!!(logdensity, state, params) - setparams!!(state, params) + setparams!!([model::AbstractModel, ]state, params) Set the values of parameters in the sampler's `state` from a `Vector{<:Real}`. From b6fcf1166b15d0c7fa8ad1b1b38b16c0a3e555a3 Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Fri, 25 Oct 2024 22:29:31 +0800 Subject: [PATCH 7/9] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 19710e2..7d7e129 100644 --- a/Project.toml +++ b/Project.toml @@ -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.1" +version = "5.6.0" [deps] BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66" From 951dffef26bc99b49b4a2813e5aad8d7df96f859 Mon Sep 17 00:00:00 2001 From: Xianda Sun Date: Sun, 27 Oct 2024 10:47:22 +0000 Subject: [PATCH 8/9] add some documentation improvement --- docs/src/api.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/src/api.md b/docs/src/api.md index db4ce56..0e270f2 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -121,7 +121,13 @@ To make it a bit easier to interact with some arbitrary sampler state, we encour AbstractMCMC.getparams AbstractMCMC.setparams!! ``` -These methods can also be useful for implementing samplers which wraps some inner samplers, e.g. a mixture of samplers. +`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. If the `model` argument is not provided, the functions will be dispatched to the implementations without the `model` argument. + +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. + +The optional `model` argument to these functions allows sampler implementations to customize their behavior based on the model being used. This is useful for samplers that need to access or modify the model in order to extract or set the parameters. + +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`. ### Example: `MixtureSampler` From 1dc951b82976208c6ce2e153dce59dd4c31d935b Mon Sep 17 00:00:00 2001 From: Xianda Sun Date: Sun, 27 Oct 2024 10:49:15 +0000 Subject: [PATCH 9/9] improve added doc --- docs/src/api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/api.md b/docs/src/api.md index 0e270f2..da92fd3 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -121,11 +121,11 @@ To make it a bit easier to interact with some arbitrary sampler state, we encour AbstractMCMC.getparams AbstractMCMC.setparams!! ``` -`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. If the `model` argument is not provided, the functions will be dispatched to the implementations without the `model` argument. +`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. 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. -The optional `model` argument to these functions allows sampler implementations to customize their behavior based on the model being used. This is useful for samplers that need to access or modify the model in order to extract or set the parameters. +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. 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`.