From c5d37b1d7b0bf0423846e22b5c25c998c3a9c738 Mon Sep 17 00:00:00 2001 From: "Documenter.jl" Date: Thu, 26 Sep 2024 10:17:34 +0000 Subject: [PATCH] build based on 1a5a693 --- previews/PR158/.documenter-siteinfo.json | 2 +- previews/PR158/api/index.html | 10 +- .../{c5430246.svg => 529abdd5.svg} | 62 +++++------ .../{a25a0c0d.svg => e62bf488.svg} | 102 +++++++++--------- .../{fb2693a2.svg => ecb99dd6.svg} | 72 ++++++------- previews/PR158/getting-started/index.html | 10 +- previews/PR158/index.html | 2 +- 7 files changed, 130 insertions(+), 130 deletions(-) rename previews/PR158/getting-started/{c5430246.svg => 529abdd5.svg} (86%) rename previews/PR158/getting-started/{a25a0c0d.svg => e62bf488.svg} (96%) rename previews/PR158/getting-started/{fb2693a2.svg => ecb99dd6.svg} (85%) diff --git a/previews/PR158/.documenter-siteinfo.json b/previews/PR158/.documenter-siteinfo.json index eaecb03..1e2eb88 100644 --- a/previews/PR158/.documenter-siteinfo.json +++ b/previews/PR158/.documenter-siteinfo.json @@ -1 +1 @@ -{"documenter":{"julia_version":"1.10.5","generation_timestamp":"2024-09-25T23:02:41","documenter_version":"1.7.0"}} \ No newline at end of file +{"documenter":{"julia_version":"1.10.5","generation_timestamp":"2024-09-26T10:17:30","documenter_version":"1.7.0"}} \ No newline at end of file diff --git a/previews/PR158/api/index.html b/previews/PR158/api/index.html index c4d4f01..160a9d0 100644 --- a/previews/PR158/api/index.html +++ b/previews/PR158/api/index.html @@ -1,15 +1,15 @@ API · MCMCTempering

API

Temper samplers

MCMCTempering.temperedFunction
tempered(sampler, inverse_temperatures; kwargs...)
 OR
-tempered(sampler, num_temps; swap_strategy=ReversibleSwap(), kwargs...)

Return a tempered version of sampler using the provided inverse_temperatures or inverse temperatures generated from num_temps and the swap_strategy.

Arguments

  • sampler is an algorithm or sampler object to be used for underlying sampling and to apply tempering to
  • The temperature schedule can be defined either explicitly or just as an integer number of temperatures, i.e. as:
    • inverse_temperatures containing a sequence of 'inverse temperatures' {β₀, ..., βₙ} where 0 ≤ βₙ < ... < β₁ < β₀ = 1 OR
    • num_temps, specifying the integer number of inverse temperatures to include in a generated inverse_temperatures

Keyword arguments

  • swap_strategy::AbstractSwapStrategy specifies the method for swapping inverse temperatures between chains
  • steps_per_swap::Integer steps are carried out between each attempt at a swap

See also

source
MCMCTempering.TemperedSamplerType
TemperedSampler <: AbstractMCMC.AbstractSampler

A TemperedSampler struct wraps a sampler upon which to apply the Parallel Tempering algorithm.

Fields

  • sampler: sampler(s) used to target the tempered distributions

  • chain_to_beta: collection of inverse temperatures β; β[i] correponds i-th tempered model

  • swapstrategy: strategy to use for swapping

  • adapt: boolean flag specifying whether or not to adapt

  • adaptation_states: adaptation parameters

source

Under the hood, MCMCTempering.TemperedSampler is actually just a "fancy" representation of a composition (represented using a MCMCTempering.CompositionSampler) of a MCMCTempering.MultiSampler and a MCMCTempering.SwapSampler.

Roughly speaking, the implementation of AbstractMCMC.step for MCMCTempering.TemperedSampler is basically

# 1. Construct the tempered models.
+tempered(sampler, num_temps; swap_strategy=ReversibleSwap(), kwargs...)

Return a tempered version of sampler using the provided inverse_temperatures or inverse temperatures generated from num_temps and the swap_strategy.

Arguments

  • sampler is an algorithm or sampler object to be used for underlying sampling and to apply tempering to
  • The temperature schedule can be defined either explicitly or just as an integer number of temperatures, i.e. as:
    • inverse_temperatures containing a sequence of 'inverse temperatures' {β₀, ..., βₙ} where 0 ≤ βₙ < ... < β₁ < β₀ = 1 OR
    • num_temps, specifying the integer number of inverse temperatures to include in a generated inverse_temperatures

Keyword arguments

  • swap_strategy::AbstractSwapStrategy specifies the method for swapping inverse temperatures between chains
  • steps_per_swap::Integer steps are carried out between each attempt at a swap

See also

source
MCMCTempering.TemperedSamplerType
TemperedSampler <: AbstractMCMC.AbstractSampler

A TemperedSampler struct wraps a sampler upon which to apply the Parallel Tempering algorithm.

Fields

  • sampler: sampler(s) used to target the tempered distributions

  • chain_to_beta: collection of inverse temperatures β; β[i] correponds i-th tempered model

  • swapstrategy: strategy to use for swapping

  • adapt: boolean flag specifying whether or not to adapt

  • adaptation_states: adaptation parameters

source

Under the hood, MCMCTempering.TemperedSampler is actually just a "fancy" representation of a composition (represented using a MCMCTempering.CompositionSampler) of a MCMCTempering.MultiSampler and a MCMCTempering.SwapSampler.

Roughly speaking, the implementation of AbstractMCMC.step for MCMCTempering.TemperedSampler is basically

# 1. Construct the tempered models.
 multimodel = MultiModel([make_tempered_model(model, β) for β in tempered_sampler.chain_to_beta])
 # 2. Construct the samplers (can be the same one repeated multiple times or different ones)
 multisampler = MultiSampler([getsampler(tempered_sampler, i) for i = 1:numtemps])
 # 3. Step targeting `multimodel` using a compositoin of `multisampler` and `swapsampler`.
-AbstractMCMC.step(rng, multimodel, multisampler ∘ swapsampler, state; kwargs...)

which in this case is provided by repeated calls to MCMCTempering.make_tempered_model.

This should be overloaded if you have some custom model-type that does not support the LogDensityProblems.jl-interface. In the case where the model does support the LogDensityProblems.jl-interface, then the following will automatically be constructed

MCMCTempering.TemperedLogDensityProblemType
TemperedLogDensityProblem

A tempered log density function implementing the LogDensityProblem.jl interface.

Fields

  • logdensity: underlying log density; assumed to implement LogDensityProblems.jl interface

  • beta

source

In addition, for computation of the tempered logdensities, we have

MCMCTempering.compute_logdensitiesFunction
compute_logdensities(model[, model_other], state, state_other)

Return (logdensity(model, state), logdensity(model, state_other)).

The default implementation extracts the parameters from the transitions using MCMCTempering.getparams.

model_other can be provided to allow specializations that might be more efficient if we know that state_other is from model_other, e.g. in the case where the log-probability field is already present in state and state_other, and the only difference between logdensity(model, state_other) and logdensity(model_other, state_other) is an easily computable factor, then this can be exploited instead of re-computing the log-densities for both.

source

Swapping

Swapping is implemented using the somewhat special MCMCTempering.SwapSampler

Warning

This is a rather special sampler because, unlike most other implementations of AbstractMCMC.AbstractSampler, this is not a valid sampler on its own; for this to be sensible it needs to be part of composition (see MCMCTempering.CompositionSampler) with at least one other type of (an actually valid) sampler.

Different swap-strategies

A MCMCTempering.SwapSampler can be defined with different swapping strategies:

MCMCTempering.ReversibleSwapType
ReversibleSwap <: AbstractSwapStrategy

Stochastically attempt either even- or odd-indexed swap moves between chains.

See [SYED19] for more on this approach, referred to as SEO in their paper.

source
MCMCTempering.NonReversibleSwapType
NonReversibleSwap <: AbstractSwapStrategy

At every swap step taken, this strategy deterministically traverses first the odd chain indices, proposing swaps between neighbors, and then in the next swap step taken traverses even chain indices, proposing swaps between neighbors.

See [SYED19] for more on this approach, referred to as DEO in their paper.

source
MCMCTempering.SingleSwapType
SingleSwap <: AbstractSwapStrategy

At every swap step taken, this strategy samples a single chain index i and proposes a swap between chains i and i + 1.

This approach goes under a number of names, e.g. Parallel Tempering (PT) MCMC and Replica-Exchange MCMC.[PTPH05]

source
MCMCTempering.SingleRandomSwapType
SingleRandomSwap <: AbstractSwapStrategy

At every swap step taken, this strategy samples two chain indices i and 'j' and proposes a swap between the two corresponding chains.

This approach is shown to be effective for certain models in [1].

source
MCMCTempering.RandomSwapType
RandomSwap <: AbstractSwapStrategy

This strategy randomly shuffles all the chain indices to produce floor(numptemps(sampler)/2) pairs of random (not necessarily neighbouring) chain indices to attempt to swap

source
MCMCTempering.NoSwapType
NoSwap <: AbstractSwapStrategy

Mainly useful for debugging or observing each chain independently, this overrides and disables all swapping functionality.

source
MCMCTempering.swap_stepFunction
swap_step([strategy::AbstractSwapStrategy, ]rng, model, sampler, state)

Return a new state, with temperatures possibly swapped according to strategy.

If no strategy is provided, the return-value of swapstrategy called on sampler is used.

source

Other samplers

To make a sampler work with MCMCTempering.jl, the sampler needs to implement a few methods:

Other useful methods are:

Compositions of samplers

MCMCTempering.CompositionSamplerType
CompositionSampler <: AbstractMCMC.AbstractSampler

A CompositionSampler is a container for a sequence of samplers.

Fields

  • sampler_outer: The outer sampler

  • sampler_inner: The inner sampler

  • saveall: Whether to save all the transitions or just the last one

Examples

composed_sampler = sampler_inner ∘ sampler_outer # or `CompositionSampler(sampler_inner, sampler_outer, Val(true))`
-AbstractMCMC.step(rng, model, composed_sampler) # one step of `sampler_inner`, and one step of `sampler_outer`
source

This sampler also has its own transition- and state-type

Repeated sampler / composition with itself

Large compositions can have unfortunate effects on the compilation times in Julia.

To alleviate this issue we also have the MCMCTempering.RepeatedSampler:

MCMCTempering.RepeatedSamplerType
RepeatedSampler <: AbstractMCMC.AbstractSampler

A RepeatedSampler is a container for a sampler and a number of times to repeat it.

Fields

  • sampler: The sampler to repeat

  • num_repeat: The number of times to repeat the sampler

  • saveall: Whether to save all the transitions or just the last one

Examples

repeated_sampler = sampler^10 # or `RepeatedSampler(sampler, 10, Val(true))`
-AbstractMCMC.step(rng, model, repeated_sampler) # take 10 steps of `sampler`
source

In the case where MCMCTempering.saveall returns false, step for a MCMCTempering.RepeatedSampler simply returns the last transition and state; if it returns true, then the transition is of type MCMCTempering.SequentialTransitions and the state is of type MCMCTempering.SequentialStates.

This effectively allows you to specify whether or not the "intermediate" states should be kept or not.

Note

You will rarely see MCMCTempering.SequentialTransitions and MCMCTempering.SequentialStates as a user because AbstractMCMC.bundle_samples has been overloaded to these to return the flattened representation, i.e. we "un-roll" the transitions in every MCMCTempering.SequentialTransitions.

Multiple or product of samplers

MCMCTempering.MultiSamplerType
MultiSampler <: AbstractMCMC.AbstractSampler

A MultiSampler is a container for multiple samplers.

See also: MultiModel.

Fields

  • samplers: The samplers

Examples

# `sampler1` targets `model1`, `sampler2` targets `model2`, etc.
+AbstractMCMC.step(rng, multimodel, multisampler ∘ swapsampler, state; kwargs...)

which in this case is provided by repeated calls to MCMCTempering.make_tempered_model.

This should be overloaded if you have some custom model-type that does not support the LogDensityProblems.jl-interface. In the case where the model does support the LogDensityProblems.jl-interface, then the following will automatically be constructed

MCMCTempering.TemperedLogDensityProblemType
TemperedLogDensityProblem

A tempered log density function implementing the LogDensityProblem.jl interface.

Fields

  • logdensity: underlying log density; assumed to implement LogDensityProblems.jl interface

  • beta

source

In addition, for computation of the tempered logdensities, we have

MCMCTempering.compute_logdensitiesFunction
compute_logdensities(model[, model_other], state, state_other)

Return (logdensity(model, state), logdensity(model, state_other)).

The default implementation extracts the parameters from the transitions using MCMCTempering.getparams.

model_other can be provided to allow specializations that might be more efficient if we know that state_other is from model_other, e.g. in the case where the log-probability field is already present in state and state_other, and the only difference between logdensity(model, state_other) and logdensity(model_other, state_other) is an easily computable factor, then this can be exploited instead of re-computing the log-densities for both.

source

Swapping

Swapping is implemented using the somewhat special MCMCTempering.SwapSampler

Warning

This is a rather special sampler because, unlike most other implementations of AbstractMCMC.AbstractSampler, this is not a valid sampler on its own; for this to be sensible it needs to be part of composition (see MCMCTempering.CompositionSampler) with at least one other type of (an actually valid) sampler.

Different swap-strategies

A MCMCTempering.SwapSampler can be defined with different swapping strategies:

MCMCTempering.ReversibleSwapType
ReversibleSwap <: AbstractSwapStrategy

Stochastically attempt either even- or odd-indexed swap moves between chains.

See [SYED19] for more on this approach, referred to as SEO in their paper.

source
MCMCTempering.NonReversibleSwapType
NonReversibleSwap <: AbstractSwapStrategy

At every swap step taken, this strategy deterministically traverses first the odd chain indices, proposing swaps between neighbors, and then in the next swap step taken traverses even chain indices, proposing swaps between neighbors.

See [SYED19] for more on this approach, referred to as DEO in their paper.

source
MCMCTempering.SingleSwapType
SingleSwap <: AbstractSwapStrategy

At every swap step taken, this strategy samples a single chain index i and proposes a swap between chains i and i + 1.

This approach goes under a number of names, e.g. Parallel Tempering (PT) MCMC and Replica-Exchange MCMC.[PTPH05]

source
MCMCTempering.SingleRandomSwapType
SingleRandomSwap <: AbstractSwapStrategy

At every swap step taken, this strategy samples two chain indices i and 'j' and proposes a swap between the two corresponding chains.

This approach is shown to be effective for certain models in [1].

source
MCMCTempering.RandomSwapType
RandomSwap <: AbstractSwapStrategy

This strategy randomly shuffles all the chain indices to produce floor(numptemps(sampler)/2) pairs of random (not necessarily neighbouring) chain indices to attempt to swap

source
MCMCTempering.NoSwapType
NoSwap <: AbstractSwapStrategy

Mainly useful for debugging or observing each chain independently, this overrides and disables all swapping functionality.

source
MCMCTempering.swap_stepFunction
swap_step([strategy::AbstractSwapStrategy, ]rng, model, sampler, state)

Return a new state, with temperatures possibly swapped according to strategy.

If no strategy is provided, the return-value of swapstrategy called on sampler is used.

source

Other samplers

To make a sampler work with MCMCTempering.jl, the sampler needs to implement a few methods:

Other useful methods are:

Compositions of samplers

MCMCTempering.CompositionSamplerType
CompositionSampler <: AbstractMCMC.AbstractSampler

A CompositionSampler is a container for a sequence of samplers.

Fields

  • sampler_outer: The outer sampler

  • sampler_inner: The inner sampler

  • saveall: Whether to save all the transitions or just the last one

Examples

composed_sampler = sampler_inner ∘ sampler_outer # or `CompositionSampler(sampler_inner, sampler_outer, Val(true))`
+AbstractMCMC.step(rng, model, composed_sampler) # one step of `sampler_inner`, and one step of `sampler_outer`
source

This sampler also has its own transition- and state-type

Repeated sampler / composition with itself

Large compositions can have unfortunate effects on the compilation times in Julia.

To alleviate this issue we also have the MCMCTempering.RepeatedSampler:

MCMCTempering.RepeatedSamplerType
RepeatedSampler <: AbstractMCMC.AbstractSampler

A RepeatedSampler is a container for a sampler and a number of times to repeat it.

Fields

  • sampler: The sampler to repeat

  • num_repeat: The number of times to repeat the sampler

  • saveall: Whether to save all the transitions or just the last one

Examples

repeated_sampler = sampler^10 # or `RepeatedSampler(sampler, 10, Val(true))`
+AbstractMCMC.step(rng, model, repeated_sampler) # take 10 steps of `sampler`
source

In the case where MCMCTempering.saveall returns false, step for a MCMCTempering.RepeatedSampler simply returns the last transition and state; if it returns true, then the transition is of type MCMCTempering.SequentialTransitions and the state is of type MCMCTempering.SequentialStates.

This effectively allows you to specify whether or not the "intermediate" states should be kept or not.

Note

You will rarely see MCMCTempering.SequentialTransitions and MCMCTempering.SequentialStates as a user because AbstractMCMC.bundle_samples has been overloaded to these to return the flattened representation, i.e. we "un-roll" the transitions in every MCMCTempering.SequentialTransitions.

Multiple or product of samplers

MCMCTempering.MultiSamplerType
MultiSampler <: AbstractMCMC.AbstractSampler

A MultiSampler is a container for multiple samplers.

See also: MultiModel.

Fields

  • samplers: The samplers

Examples

# `sampler1` targets `model1`, `sampler2` targets `model2`, etc.
 multi_model = model1 × model2 × model3 # or `MultiModel((model1, model2, model3))`
 multi_sampler = sampler1 × sampler2 × sampler3 # or `MultiSampler((sampler1, sampler2, sampler3))`
 # Target the joint model.
-AbstractMCMC.step(rng, multi_model, multi_sampler)
source

where the tempered models are represented using a MCMCTempering.MultiModel

The step for a MCMCTempering.MultiSampler and a [MCMCTempering.MultiModel] is a transition of type MCMCTempering.MultipleTransitions and a state of type MCMCTempering.MultipleStates

  • SYED19Syed, S., Bouchard-Côté, Alexandre, Deligiannidis, G., & Doucet, A., Non-reversible Parallel Tempering: A Scalable Highly Parallel MCMC Scheme, arXiv:1905.02939, (2019).
  • SYED19Syed, S., Bouchard-Côté, Alexandre, Deligiannidis, G., & Doucet, A., Non-reversible Parallel Tempering: A Scalable Highly Parallel MCMC Scheme, arXiv:1905.02939, (2019).
  • PTPH05Earl, D. J., & Deem, M. W., Parallel tempering: theory, applications, and new perspectives, Physical Chemistry Chemical Physics, 7(23), 3910–3916 (2005).
  • 1Malcolm Sambridge, A Parallel Tempering algorithm for probabilistic sampling and multimodal optimization, Geophysical Journal International, Volume 196, Issue 1, January 2014, Pages 357–374, https://doi.org/10.1093/gji/ggt342
+AbstractMCMC.step(rng, multi_model, multi_sampler)source

where the tempered models are represented using a MCMCTempering.MultiModel

MCMCTempering.MultiModelType
MultiModel <: AbstractMCMC.AbstractModel

A MultiModel is a container for multiple models.

See also: MultiSampler.

Fields

  • models: The models
source

The step for a MCMCTempering.MultiSampler and a [MCMCTempering.MultiModel] is a transition of type MCMCTempering.MultipleTransitions and a state of type MCMCTempering.MultipleStates

MCMCTempering.MultipleTransitionsType
MultipleTransitions

A container for multiple transitions.

See also: MultipleStates.

Fields

  • transitions: The transitions
source
MCMCTempering.MultipleStatesType
MultipleStates

A container for multiple states.

See also: MultipleTransitions.

Fields

  • states: The states
source
diff --git a/previews/PR158/getting-started/c5430246.svg b/previews/PR158/getting-started/529abdd5.svg similarity index 86% rename from previews/PR158/getting-started/c5430246.svg rename to previews/PR158/getting-started/529abdd5.svg index 3b04070..76e7421 100644 --- a/previews/PR158/getting-started/c5430246.svg +++ b/previews/PR158/getting-started/529abdd5.svg @@ -1,46 +1,46 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/previews/PR158/getting-started/a25a0c0d.svg b/previews/PR158/getting-started/e62bf488.svg similarity index 96% rename from previews/PR158/getting-started/a25a0c0d.svg rename to previews/PR158/getting-started/e62bf488.svg index 33ddde1..a81aba5 100644 --- a/previews/PR158/getting-started/a25a0c0d.svg +++ b/previews/PR158/getting-started/e62bf488.svg @@ -1,70 +1,70 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + diff --git a/previews/PR158/getting-started/fb2693a2.svg b/previews/PR158/getting-started/ecb99dd6.svg similarity index 85% rename from previews/PR158/getting-started/fb2693a2.svg rename to previews/PR158/getting-started/ecb99dd6.svg index 331c781..96fa747 100644 --- a/previews/PR158/getting-started/fb2693a2.svg +++ b/previews/PR158/getting-started/ecb99dd6.svg @@ -1,51 +1,51 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/previews/PR158/getting-started/index.html b/previews/PR158/getting-started/index.html index d080815..a04d3e0 100644 --- a/previews/PR158/getting-started/index.html +++ b/previews/PR158/getting-started/index.html @@ -10,7 +10,7 @@ components[3] (prior = 0.2000): Distributions.Normal{Float64}(μ=20.0, σ=1.5)

This is a simple 1-dimensional distribution, so let's visualize it:

using StatsPlots
 figsize = (800, 400)
-plot(target_distribution; components=false, label=nothing, size=figsize)
Example block output

We can convert a Distribution from Distributions.jl into something we can pass to sample for many different samplers by implementing the LogDensityProblems.jl interface:

using LogDensityProblems: LogDensityProblems
+plot(target_distribution; components=false, label=nothing, size=figsize)
Example block output

We can convert a Distribution from Distributions.jl into something we can pass to sample for many different samplers by implementing the LogDensityProblems.jl interface:

using LogDensityProblems: LogDensityProblems
 
 struct DistributionLogDensity{D}
     d::D
@@ -57,9 +57,9 @@
       Symbol   Float64   Float64   Float64   Float64   Float64
 
            x   -5.6359   -3.2669   -1.4454    2.5327    5.3961
-
plot(chain; size=figsize)
Example block output

This doesn't look quite like what we're expecting.

plot(target_distribution; components=false, linewidth=2)
+
plot(chain; size=figsize)
Example block output

This doesn't look quite like what we're expecting.

plot(target_distribution; components=false, linewidth=2)
 density!(chain)
-plot!(size=figsize)
Example block output

Notice how chain has zero probability mass in the left-most component of the mixture!

Let's instead try to use a tempered version of RWMH. But before we do that, we need to make sure that AdvancedMH.jl is compatible with MCMCTempering.jl.

To do that we need to implement two methods. First we need to tell MCMCTempering how to extract the parameters, and potentially the log-probabilities, from a AdvancedMH.Transition:

MCMCTempering.getparams_and_logprobFunction
getparams_and_logprob([model, ]state)

Return a vector of parameters from the state.

See also: setparams_and_logprob!!.

source

And similarly, we need a way to update the parameters and the log-probabilities of a AdvancedMH.Transition:

MCMCTempering.setparams_and_logprob!!Function
setparams_and_logprob!!([model, ]state, params)

Set the parameters in the state to params, possibly mutating if it makes sense.

See also: getparams_and_logprob.

source

Luckily, implementing these is quite easy:

using MCMCTempering
+plot!(size=figsize)
Example block output

Notice how chain has zero probability mass in the left-most component of the mixture!

Let's instead try to use a tempered version of RWMH. But before we do that, we need to make sure that AdvancedMH.jl is compatible with MCMCTempering.jl.

To do that we need to implement two methods. First we need to tell MCMCTempering how to extract the parameters, and potentially the log-probabilities, from a AdvancedMH.Transition:

MCMCTempering.getparams_and_logprobFunction
getparams_and_logprob([model, ]state)

Return a vector of parameters from the state.

See also: setparams_and_logprob!!.

source

And similarly, we need a way to update the parameters and the log-probabilities of a AdvancedMH.Transition:

MCMCTempering.setparams_and_logprob!!Function
setparams_and_logprob!!([model, ]state, params)

Set the parameters in the state to params, possibly mutating if it makes sense.

See also: getparams_and_logprob.

source

Luckily, implementing these is quite easy:

using MCMCTempering
 
 MCMCTempering.getparams_and_logprob(transition::AdvancedMH.Transition) = transition.params, transition.lp
 function MCMCTempering.setparams_and_logprob!!(transition::AdvancedMH.Transition, params, lp)
@@ -136,7 +136,7 @@
 end
 density!(chain_tempered_all[1], color="green", size=figsize)
 plot!(size=figsize)

Works like a charm!

But we're recomputing both the logdensity and the gradient of the logdensity upon every MCMCTempering.setparams_and_logprob!! above! This seems wholly unnecessary in the tempering case, since

\[\pi_{\beta_1}(x) = \pi(x)^{\beta_1} = \big( \pi(x)^{\beta_2} \big)^{\beta_1 / \beta_2} = \pi_{\beta_2}^{\beta_1 / \beta_2}\]

i.e. if model in the above is tempered with $\beta_1$ and the params are coming from a model with $\beta_2$, we can could just compute it as

(β_1 / β_2) * logprob

and similarly for the gradient! Luckily, it's possible to tell MCMCTempering that this should be done by overloading the MCMCTempering.state_from method. In particular, we'll specify that when we're working with two models of type MCMCTempering.TemperedLogDensityProblem and two states of type AdvancedHMC.HMCState, then we can just re-use scale the logdensity and gradient computation from the MCMCTempering.state_from to get the quantities we want, thus avoiding unnecessary computations:

MCMCTempering.state_fromFunction
state_from(model_source, state_target, state_source)
-state_from(model_source, model_target, state_target, state_source)

Return a new state similar to state_target but updated from state_source, which could be a different type of state.

source
using AbstractMCMC: AbstractMCMC
+state_from(model_source, model_target, state_target, state_source)

Return a new state similar to state_target but updated from state_source, which could be a different type of state.

source
using AbstractMCMC: AbstractMCMC
 
 function MCMCTempering.state_from(
     # AdvancedHMC.jl works with `LogDensityModel`, and by default `AbstractMCMC` will wrap
@@ -183,4 +183,4 @@
     density!(chain_tempered, color="green", alpha=inv(sqrt(length(chain_tempered_all))))
 end
 density!(chain_tempered_all[1], color="green", size=figsize)
-plot!(size=figsize)
+plot!(size=figsize) diff --git a/previews/PR158/index.html b/previews/PR158/index.html index 73f89a6..cfd235e 100644 --- a/previews/PR158/index.html +++ b/previews/PR158/index.html @@ -1,2 +1,2 @@ -Home · MCMCTempering

MCMCTempering.jl

Tempering methods and more for Markov chain Monte Carlo methods.

MCMCTempering provides implementations of different ways to define tempered samplers and models, in addition to other ways of composing and mixing samplers.

+Home · MCMCTempering

MCMCTempering.jl

Tempering methods and more for Markov chain Monte Carlo methods.

MCMCTempering provides implementations of different ways to define tempered samplers and models, in addition to other ways of composing and mixing samplers.