Traditionally, models in Turing are defined using the @model macro:
usingTuring@modelfunctiongdemo(x)# Set priors. s² ~InverseGamma(2, 3) m ~Normal(0, sqrt(s²))# Observe each value of x. @. x ~Normal(m, sqrt(s²))endmodel =gdemo([1.5, 2.0])
The @model macro accepts a function definition and rewrites it such that call of the function generates a Model struct for use by the sampler.
However, models can be constructed by hand without the use of a macro. Taking the gdemo model above as an example, the macro-based definition can be implemented also (a bit less generally) with the macro-free version
# Create the model function.functiongdemo2(model, varinfo, context, x)# Assume s² has an InverseGamma distribution. s², varinfo = DynamicPPL.tilde_assume!!( context, InverseGamma(2, 3), Turing.@varname(s²), varinfo )# Assume m has a Normal distribution. m, varinfo = DynamicPPL.tilde_assume!!( context, Normal(0, sqrt(s²)), Turing.@varname(m), varinfo )# Observe each value of x[i] according to a Normal distribution.return DynamicPPL.dot_tilde_observe!!( context, Normal(m, sqrt(s²)), x, Turing.@varname(x), varinfo )endgdemo2(x) = Turing.Model(gdemo2, (; x))# Instantiate a Model object with our data variables.model2 =gdemo2([1.5, 2.0])