Skip to content

Commit ce72bef

Browse files
committed
Deploy preview for PR 652
1 parent fdede4a commit ce72bef

File tree

41 files changed

+6245
-10688
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+6245
-10688
lines changed

pr-previews/652/developers/compiler/design-overview/index.ipynb

Lines changed: 114 additions & 311 deletions
Large diffs are not rendered by default.

pr-previews/652/developers/compiler/minituring-compiler/index.ipynb

Lines changed: 174 additions & 312 deletions
Large diffs are not rendered by default.

pr-previews/652/developers/compiler/minituring-contexts/index.ipynb

Lines changed: 126 additions & 316 deletions
Large diffs are not rendered by default.

pr-previews/652/developers/compiler/model-manual/index.ipynb

Lines changed: 47 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,58 @@
33
{
44
"cell_type": "markdown",
55
"metadata": {},
6-
"source": [
7-
"---\n",
8-
"title: Manually Defining a Model\n",
9-
"engine: julia\n",
10-
"aliases:\n",
11-
" - ../../../tutorials/dev-model-manual/index.html\n",
12-
"---\n",
13-
"\n",
14-
"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",
44-
" s², varinfo = DynamicPPL.tilde_assume!!(\n",
45-
" model.context, InverseGamma(2, 3), @varname(s²), varinfo\n",
46-
" )\n",
47-
"\n",
48-
" # Assume m has a Normal distribution.\n",
49-
" m, varinfo = DynamicPPL.tilde_assume!!(\n",
50-
" model.context, Normal(0, sqrt(s²)), @varname(m), varinfo\n",
51-
" )\n",
52-
"\n",
53-
" # Observe each value of x[i] according to a Normal distribution.\n",
54-
" for i in eachindex(x)\n",
55-
" _retval, varinfo = DynamicPPL.tilde_observe!!(\n",
56-
" model.context, Normal(m, sqrt(s²)), x[i], @varname(x[i]), varinfo\n",
57-
" )\n",
58-
" end\n",
59-
"\n",
60-
" # The final return statement should comprise both the original return\n",
61-
" # value and the updated varinfo.\n",
62-
" return nothing, varinfo\n",
63-
"end\n",
64-
"gdemo2(x) = DynamicPPL.Model(gdemo2, (; x))\n",
65-
"\n",
66-
"# Instantiate a Model object with our data variables.\n",
67-
"model2 = gdemo2([1.5, 2.0])\n",
68-
"```\n",
69-
"\n",
70-
"We can sample from this model in the same way:\n",
71-
"\n",
72-
"```{julia}\n",
73-
"chain = sample(model2, NUTS(), 1000; progress=false)\n",
74-
"```\n",
75-
"\n",
76-
"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:"
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": "chain = sample(model2, NUTS(), 1000; progress=false)"
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": "The subsequent pages in this section will show how the `@model` macro does this behind-the-scenes."
7843
}
7944
],
8045
"metadata": {
8146
"kernelspec": {
82-
"display_name": "Python 3",
83-
"language": "python",
84-
"name": "python3"
47+
"display_name": "Julia 1.11",
48+
"language": "julia",
49+
"name": "julia-1.11"
50+
},
51+
"language_info": {
52+
"file_extension": ".jl",
53+
"mimetype": "application/julia",
54+
"name": "julia",
55+
"version": "1.11.0"
8556
}
8657
},
8758
"nbformat": 4,
88-
"nbformat_minor": 4
59+
"nbformat_minor": 5
8960
}

0 commit comments

Comments
 (0)