You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"Traditionally, models in Turing are defined using the `@model` macro:\n",
15
-
"\n",
16
-
"```{julia}\n",
17
-
"using Turing\n",
18
-
"\n",
19
-
"@model function gdemo(x)\n",
20
-
" # Set priors.\n",
21
-
" s² ~ InverseGamma(2, 3)\n",
22
-
" m ~ Normal(0, sqrt(s²))\n",
23
-
"\n",
24
-
" # Observe each value of x.\n",
25
-
" x .~ Normal(m, sqrt(s²))\n",
26
-
"\n",
27
-
" return nothing\n",
28
-
"end\n",
29
-
"\n",
30
-
"model = gdemo([1.5, 2.0])\n",
31
-
"```\n",
32
-
"\n",
33
-
"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.\n",
34
-
"\n",
35
-
"However, models can be constructed by hand without the use of a macro.\n",
36
-
"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\n",
37
-
"\n",
38
-
"```{julia}\n",
39
-
"using DynamicPPL\n",
40
-
"\n",
41
-
"# Create the model function.\n",
42
-
"function gdemo2(model, varinfo, x)\n",
43
-
" # Assume s² has an InverseGamma distribution.\n",
"The subsequent pages in this section will show how the `@model` macro does this behind-the-scenes."
77
-
]
6
+
"source": "Traditionally, models in Turing are defined using the `@model` macro:"
7
+
},
8
+
{
9
+
"cell_type": "code",
10
+
"execution_count": null,
11
+
"metadata": {},
12
+
"outputs": [],
13
+
"source": "using Turing\n\n@model function gdemo(x)\n # Set priors.\n s² ~ InverseGamma(2, 3)\n m ~ Normal(0, sqrt(s²))\n\n # Observe each value of x.\n x .~ Normal(m, sqrt(s²))\n\n return nothing\nend\n\nmodel = gdemo([1.5, 2.0])"
14
+
},
15
+
{
16
+
"cell_type": "markdown",
17
+
"metadata": {},
18
+
"source": "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.\n\nHowever, models can be constructed by hand without the use of a macro.\nTaking the `gdemo` model above as an example, the macro-based definition can be implemented also (a bit less generally) with the macro-free version"
19
+
},
20
+
{
21
+
"cell_type": "code",
22
+
"execution_count": null,
23
+
"metadata": {},
24
+
"outputs": [],
25
+
"source": "using DynamicPPL\n\n# Create the model function.\nfunction gdemo2(model, varinfo, x)\n # Assume s² has an InverseGamma distribution.\n s², varinfo = DynamicPPL.tilde_assume!!(\n model.context, InverseGamma(2, 3), @varname(s²), varinfo\n )\n\n # Assume m has a Normal distribution.\n m, varinfo = DynamicPPL.tilde_assume!!(\n model.context, Normal(0, sqrt(s²)), @varname(m), varinfo\n )\n\n # Observe each value of x[i] according to a Normal distribution.\n for i in eachindex(x)\n _retval, varinfo = DynamicPPL.tilde_observe!!(\n model.context, Normal(m, sqrt(s²)), x[i], @varname(x[i]), varinfo\n )\n end\n\n # The final return statement should comprise both the original return\n # value and the updated varinfo.\n return nothing, varinfo\nend\ngdemo2(x) = DynamicPPL.Model(gdemo2, (; x))\n\n# Instantiate a Model object with our data variables.\nmodel2 = gdemo2([1.5, 2.0])"
26
+
},
27
+
{
28
+
"cell_type": "markdown",
29
+
"metadata": {},
30
+
"source": "We can sample from this model in the same way:"
0 commit comments