Turing.jl: Default sampling behaviour

Created on 21 May 2018  路  8Comments  路  Source: TuringLang/Turing.jl

Maybe we can support a syntax like sample(model_f, num_iter) i.e. one doesn't specify the samplers. In this setting we ask Turing.jl to automatically split variables into continous and discrete variables and runing a composable Gibbs with HMC and PG on it.

doc

All 8 comments

In addition to this, it would also be useful to be able to sample from the prior more straightforwardly. For example:

@model gdemo(x) = begin
  s ~ InverseGamma(2,3)
  m ~ Normal(0,sqrt(s))
  x[1] ~ Normal(m, sqrt(s))
  x[2] ~ Normal(m, sqrt(s))
  return s, m
end

we can do this:

model = gdemo([5.0, 4.0])
model()

to generate (what I understand to be) samples from the prior, but it seems unnecessary to have to provide data to the prior to be able to sample from the model. It would be nice to be able to do

rand(gdemo)

or simply

gdemo()

(or something like this) and get samples from the prior.

It should be straightforward to support gdemo().

The rand(gdemo) interface would require the compiler to generate data types of models. This is possible but is more involved.

@willtebbutt In general, I'm not satisfied with the current compiler implementation. It was written incrementally and does not have a clear code structure. I think we should arrange a meeting to go through the code and see what can be done better.

The refactored compiler allows you to draw samples from the prior more directly.

Given the following model:

@model gdemo(x) = begin
  s ~ InverseGamma(2,3)
  m ~ Normal(0,sqrt(s))
  x ~ Normal(m, sqrt(s))
  return x
end

If we call:

model = gdemo()

all symbol at the left-hand side of ~聽will be handled as parameter.

Calling:

xprior = model()

will return a sample x drawn from the prior.
Calling gdemo(5.0) instead, will behave as usual and handle x as an observation.

@cpfiffer can we document this interface?

Yes, I think I'm going to be adding a general guide document and this is a good fit for that.

I'm getting an error trying to replicate this functionality when I call xprior = model():

UndefVarError: x not defined

Stacktrace:
 [1] macro expansion at /home/cameron/.julia/dev/Turing/src/core/compiler.jl:78 [inlined]
 [2] gdemo_model(::Turing.VarReplay.VarInfo, ::Turing.SampleFromPrior) at /home/cameron/.julia/packages/MacroTools/4AjBS/src/utils.jl:297 (repeats 2 times)
 [3] top-level scope at In[5]:10

This is a known issue. See #544.

Man, I'm a dummy. I watched that issue come in the other day and obviously did not internalize it.

Was this page helpful?
0 / 5 - 0 ratings