brian-j-smith / Mamba.jl

Markov chain Monte Carlo (MCMC) for Bayesian analysis in julia
Other
254 stars 52 forks source link

Faulty results using mcmc(::ModelChains, ...) as continuation of simulation #72

Closed axsk closed 8 years ago

axsk commented 8 years ago

Consider following example:

using Mamba
m = Model(
  x = Stochastic(1,
    ()->Truncated(Flat(), 0, 1)), 
  samplers = [AMM([:x],[.001]'')])

mc = mcmc(m, Dict{Symbol,Any}(), [Dict{Symbol,Any}(:x=>[0.5])], 1000, verbose=false);
for i=1:9
  # save mc to file
  mc = mcmc(mc,1000, verbose=false)
end
draw(plot(mc))

I actually use similar code to save the results to a file while computing them.

Unfortunately something goes wrong with the continuation using mcmc(::ModelChains, ...). The qualitative behaviour of the random walk switches randomly on some of the 1000th's iterations, as one can see in the following example plot: image

brian-j-smith commented 8 years ago

I see. It looks like a change is needed in the reset! function in initialization.jl. Something along the lines of

function reset!(m::Model, iter::Integer)
  m.iter = iter
  if iter == 0
    for s in m.samplers
      s.tune["sampler"] = nothing
    end
  end
  m
end
axsk commented 8 years ago

I don't really see a reason for the reset! function at all. It is not exported and called only from setstate!. All it does is setting the m.iter field, which I think belongs directly to setstate!, and resets the samplers on the first run, but they are initialized with the sampler field set to nothing anyhow.

To me it is also not clear what resetting a model could mean semantically.

As I mentioned in #66 I would avoid using the tune["sampler"] field in favor of a sampler specific tune type. That way one could define a reset!(::SpecificSampler) method which basically does what setting tune["sampler"]=nothing does now implicitly, it would just be more explicit and, in my eyes, Julian.

If resetting all model samplers is still desired one could define resetsamplers! which just dispatches to each's block reset!(::Sampler) function.

brian-j-smith commented 8 years ago

The mcmc continuation issue is fixed in the latest Mamba release (0.7.3). I'll respond to the sampler comment in the other thread in which it was brought up.