-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Super struct 'MCMCState' for MCMC sampling states, refactor…
… tuning
- Loading branch information
Showing
23 changed files
with
590 additions
and
460 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,102 @@ | ||
# This file is a part of BAT.jl, licensed under the MIT License (MIT). | ||
|
||
|
||
mutable struct HMCTunerState{A<:AdvancedHMC.AbstractAdaptor} <: AbstractMCMCTunerInstance | ||
struct HMCTrafoTunerState <: AbstractMCMCTunerState end | ||
|
||
mutable struct HMCProposalTunerState{A<:AdvancedHMC.AbstractAdaptor} <: AbstractMCMCTunerState | ||
tuning::HMCTuning | ||
target_acceptance::Float64 | ||
adaptor::A | ||
end | ||
|
||
function (tuning::HMCTuning)(mc_state::HMCState) | ||
θ = first(mc_state.samples).v | ||
adaptor = ahmc_adaptor(tuning, mc_state.proposal.hamiltonian.metric, mc_state.proposal.kernel.τ.integrator, θ) | ||
HMCTunerState(tuning, tuning.target_acceptance, adaptor) | ||
(tuning::HMCTuning)(chain_state::HMCState) = HMCProposalTunerState(tuning, chain_state), HMCTrafoTunerState() | ||
|
||
HMCTrafoTunerState(tuning::HMCTuning) = HMCTrafoTunerState() | ||
|
||
function HMCProposalTunerState(tuning::HMCTuning, chain_state::MCMCChainState) | ||
θ = first(chain_state.samples).v | ||
adaptor = ahmc_adaptor(tuning, chain_state.proposal.hamiltonian.metric, chain_state.proposal.kernel.τ.integrator, θ) | ||
HMCProposalTunerState(tuning, tuning.target_acceptance, adaptor) | ||
end | ||
|
||
BAT.create_trafo_tuner_state(tuning::HMCTuning, chain_state::MCMCChainState, iteration::Integer) = HMCTrafoTunerState(tuning) | ||
|
||
BAT.create_proposal_tuner_state(tuning::HMCTuning, chain_state::MCMCChainState, iteration::Integer) = HMCProposalTunerState(tuning, chain_state) | ||
|
||
BAT.mcmc_tuning_init!!(tuner::HMCTrafoTunerState, chain_state::HMCState, max_nsteps::Integer) = nothing | ||
|
||
function BAT.tuning_init!(tuner::HMCTunerState, mc_state::HMCState, max_nsteps::Integer) | ||
function BAT.mcmc_tuning_init!!(tuner::HMCProposalTunerState, chain_state::HMCState, max_nsteps::Integer) | ||
AdvancedHMC.Adaptation.initialize!(tuner.adaptor, Int(max_nsteps - 1)) | ||
nothing | ||
end | ||
|
||
BAT.tuning_postinit!(tuner::HMCTunerState, mc_state::HMCState, samples::DensitySampleVector) = nothing | ||
BAT.mcmc_tuning_reinit!!(tuner::HMCTrafoTunerState, chain_state::HMCState, max_nsteps::Integer) = nothing | ||
|
||
function BAT.tuning_reinit!(tuner::HMCTunerState, mc_state::HMCState, max_nsteps::Integer) | ||
function BAT.mcmc_tuning_reinit!!(tuner::HMCProposalTunerState, chain_state::HMCState, max_nsteps::Integer) | ||
AdvancedHMC.Adaptation.initialize!(tuner.adaptor, Int(max_nsteps - 1)) | ||
nothing | ||
end | ||
|
||
function BAT.tuning_update!(tuner::HMCTunerState, mc_state::HMCState, samples::DensitySampleVector) | ||
|
||
BAT.mcmc_tuning_postinit!!(tuner::HMCTrafoTunerState, chain_state::HMCState, samples::DensitySampleVector) = nothing | ||
|
||
BAT.mcmc_tuning_postinit!!(tuner::HMCProposalTunerState, chain_state::HMCState, samples::DensitySampleVector) = nothing | ||
|
||
|
||
BAT.mcmc_tune_post_cycle!!(tuner::HMCTrafoTunerState, chain_state::HMCState, samples::DensitySampleVector) = nothing | ||
|
||
function BAT.mcmc_tune_post_cycle!!(tuner::HMCProposalTunerState, chain_state::HMCState, samples::DensitySampleVector) | ||
max_log_posterior = maximum(samples.logd) | ||
accept_ratio = eff_acceptance_ratio(mc_state) | ||
accept_ratio = eff_acceptance_ratio(chain_state) | ||
if accept_ratio >= 0.9 * tuner.target_acceptance | ||
mc_state.info = MCMCStateInfo(mc_state.info, tuned = true) | ||
@debug "MCMC chain $(mc_state.info.id) tuned, acceptance ratio = $(Float32(accept_ratio)), integrator = $(mc_state.proposal.τ.integrator), max. log posterior = $(Float32(max_log_posterior))" | ||
chain_state.info = MCMCChainStateInfo(chain_state.info, tuned = true) | ||
@debug "MCMC chain $(chain_state.info.id) tuned, acceptance ratio = $(Float32(accept_ratio)), integrator = $(chain_state.proposal.τ.integrator), max. log posterior = $(Float32(max_log_posterior))" | ||
else | ||
mc_state.info = MCMCStateInfo(mc_state.info, tuned = false) | ||
@debug "MCMC chain $(mc_state.info.id) *not* tuned, acceptance ratio = $(Float32(accept_ratio)), integrator = $(mc_state.proposal.τ.integrator), max. log posterior = $(Float32(max_log_posterior))" | ||
chain_state.info = MCMCChainStateInfo(chain_state.info, tuned = false) | ||
@debug "MCMC chain $(chain_state.info.id) *not* tuned, acceptance ratio = $(Float32(accept_ratio)), integrator = $(chain_state.proposal.τ.integrator), max. log posterior = $(Float32(max_log_posterior))" | ||
end | ||
nothing | ||
end | ||
|
||
function BAT.tuning_finalize!(tuner::HMCTunerState, mc_state::HMCState) | ||
|
||
BAT.mcmc_tuning_finalize!!(tuner::HMCTrafoTunerState, chain_state::HMCState) = nothing | ||
|
||
function BAT.mcmc_tuning_finalize!!(tuner::HMCProposalTunerState, chain_state::HMCState) | ||
adaptor = tuner.adaptor | ||
proposal = mc_state.proposal | ||
proposal = chain_state.proposal | ||
AdvancedHMC.finalize!(adaptor) | ||
proposal.hamiltonian = AdvancedHMC.update(proposal.hamiltonian, adaptor) | ||
proposal.kernel = AdvancedHMC.update(proposal.kernel, adaptor) | ||
nothing | ||
end | ||
|
||
|
||
function BAT.mcmc_tune_transform!!( | ||
mc_state::MCMCState, | ||
tuner::HMCTunerState, | ||
BAT.tuning_callback(::HMCTrafoTunerState) = nop_func | ||
|
||
BAT.tuning_callback(::HMCProposalTunerState) = nop_func | ||
|
||
|
||
function BAT.mcmc_tune_post_step!!( | ||
tuner_state::HMCTrafoTunerState, | ||
chain_state::MCMCChainState, | ||
p_accept::Real | ||
) | ||
adaptor = tuner.adaptor | ||
proposal = mc_state.proposal | ||
return chain_state, tuner_state, chain_state.f_transform | ||
end | ||
|
||
function BAT.mcmc_tune_post_step!!( | ||
tuner_state::HMCProposalTunerState, | ||
chain_state::MCMCChainState, | ||
p_accept::Real | ||
) | ||
adaptor = tuner_state.adaptor | ||
proposal = chain_state.proposal | ||
tstat = AdvancedHMC.stat(proposal.transition) | ||
|
||
AdvancedHMC.adapt!(adaptor, proposal.transition.z.θ, tstat.acceptance_rate) | ||
proposal.hamiltonian = AdvancedHMC.update(proposal.hamiltonian, adaptor) | ||
proposal.kernel = AdvancedHMC.update(proposal.kernel, adaptor) | ||
tstat = merge(tstat, (is_adapt =true,)) | ||
|
||
return (tuner, mc_state.f_transform) | ||
return chain_state, tuner_state, chain_state.f_transform | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.