Skip to content

Commit

Permalink
fixed stepsize bug
Browse files Browse the repository at this point in the history
  • Loading branch information
odunbar committed Oct 1, 2022
1 parent 5ef6f06 commit 805e8d7
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/MarkovChainMonteCarlo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -521,28 +521,38 @@ function optimize_stepsize(
max_iter = 20,
sample_kwargs...,
)
doubled = false
halved = false
increase = false
decrease = false
stepsize = init_stepsize
factor = [1.0]
step_history = [true, true]
_find_mcmc_step_log(mcmc)
for it in 1:max_iter
trial_chain = sample(rng, mcmc, N; stepsize = stepsize, sample_kwargs...)
acc_ratio = accept_ratio(trial_chain)
_find_mcmc_step_log(it, stepsize, acc_ratio, trial_chain)

change_step = true
if doubled && halved
stepsize = 0.75 * stepsize
doubled = false
halved = false
elseif acc_ratio < 0.15
stepsize = 0.5 * stepsize
halved = true
if acc_ratio < 0.15
decrease = true
elseif acc_ratio > 0.35
stepsize = 2.0 * stepsize
doubled = true
increase = true
else
change_step = false
end

if increase && decrease
factor[1] /= 2
increase = false
decrease = false
end

if acc_ratio < 0.15
stepsize *= 2^(-factor[1])
elseif acc_ratio > 0.35
stepsize *= 2^(factor[1])
end

if change_step
@printf "Set sampler to new stepsize: %.3g\n" stepsize
else
Expand Down

0 comments on commit 805e8d7

Please sign in to comment.