Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 213 additions & 7 deletions notebooks/cookbooks/model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,27 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also draw the model, via `af.ModelPlotter`. The figure is the **map** of a model and the `info` above is its \n",
"**legend**: the map shows the structure, meaning which component owns which parameter and which of those parameters \n",
"are fixed, shared, related to one another or constrained, whereas the `info` lists the priors and values themselves.\n",
"\n",
"This first model is the simplest map there is, a single `Gaussian` card carrying one pill per free parameter, and it \n",
"is the baseline every customization below is drawn against."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -451,12 +472,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also draw the model, via `af.ModelPlotter`. The figure is the **map** of a model and the `info` above is its \n",
"**legend**: the map shows the structure, meaning which component owns which parameter and which of those parameters \n",
"are fixed, shared, related to one another or constrained, whereas the `info` lists the priors and values themselves.\n",
"\n",
"Customization is where the map earns its place, because linking a parameter to another or adding an assertion changes \n",
"a model's structure and not just its numbers. The `centre` pill states the expression `centre = normalization + sigma` \n",
"Customization is where the model figure earns its place, because linking a parameter to another or adding an \n",
"assertion changes a model's structure and not just its numbers. The `centre` pill states the expression `centre = normalization + sigma` \n",
"that defines it and the assertions are drawn as constraints, which the `info` above does not print at all."
]
},
Expand Down Expand Up @@ -685,6 +702,23 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The model loaded back from the `.json` file is a single `Gaussian` again, and its map is the one the file was written \n",
"from, which is the quickest check that a round trip through hard disk preserved the structure and not just the values."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -770,6 +804,24 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A `Collection` is drawn as one card per component, so the `Gaussian` and `Exponential` sit side by side and the footer \n",
"counts the six free parameters they have between them. Each card is titled by the key (`gaussian`, `exponential`) used \n",
"to access that component on an instance, which is the naming the list-based API further below gives up."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -939,6 +991,24 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fixing `normalization` and `centre` and adding an assertion changes the structure and not just the numbers, so the map \n",
"changes with it: the fixed parameters are greyed out on their cards and the assertion is drawn as a constraint on \n",
"the `Exponential`."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -965,6 +1035,23 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is the same model composed a different way, so it is the same map. The figure is a statement about the model and \n",
"not about the API used to build it, which makes it the quickest way to confirm two composition styles are equivalent."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1130,7 +1217,24 @@
"metadata": {},
"source": [
"model = af.Collection(gaussian_0=Gaussian, gaussian_1=Gaussian)\n",
"print(model.info)\n",
"print(model.info)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Two `Gaussian`'s given different keys are drawn as two cards titled `gaussian_0` and `gaussian_1`, whose parameters \n",
"are marked `independent`, meaning one prior per card."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()\n",
"\n",
"model_dict = {\"gaussian_0\": Gaussian, \"gaussian_1\": Gaussian}\n",
"model = af.Collection(**model_dict)\n",
Expand All @@ -1139,6 +1243,22 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The dictionary form builds exactly the same model, so the map is unchanged; only the code that made it moved."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1185,6 +1305,23 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing a list rather than keyword arguments leaves the components unnamed, and the map says so: the cards are titled \n",
"by their index in the list, which is why the `instance` below can only be accessed by indexing."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1277,6 +1414,23 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An `af.Array` is drawn as a single card whose pills are the elements of the array, titled by their index, so \n",
"a `shape=(2,2)` array is four pills and the footer counts the four free parameters they make."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1319,6 +1473,23 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Customizing the prior of individual elements does not change the shape of the array, so the map is unchanged; only \n",
"the legend moved."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1421,6 +1592,24 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here the customization is structural rather than numerical, and all three kinds of it appear on one card: the fixed \n",
"element is greyed out, the element set equal to another states that relation on its pill, and the assertion is drawn \n",
"as a constraint. The `info` above prints none of the three as structure."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1530,6 +1719,23 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Combining the two gives one card per kind of component, the `Gaussian` with its named pills beside the array with its \n",
"indexed ones, and the fixed `sigma` and `[0, 0]` element greyed out on each."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
43 changes: 40 additions & 3 deletions notebooks/cookbooks/multi_level_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,28 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also draw the model, via `af.ModelPlotter`. The figure is the **map** of a model and the `info` above is its \n",
"**legend**: the map shows the structure, meaning which component owns which parameter and which of those parameters \n",
"are fixed, shared, related to one another or constrained, whereas the `info` lists the priors and values themselves.\n",
"\n",
"A multi-level model is drawn as containment: the `higher_level_centre` sits on the outer `MultiLevelGaussians` card \n",
"whereas the two `Gaussian`'s sit inside the `gaussian_list` frame nested within it. That hierarchy is the reason to \n",
"use a multi-level model, and the `info` above can only express it as indentation."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -323,9 +345,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The model figure draws this hierarchy as containment. The figure is the **map** of a model and the `info` above is its \n",
"**legend**: the map shows the structure, meaning which component owns which parameter and which of those parameters \n",
"are fixed, shared, related to one another or constrained, whereas the `info` lists the priors and values themselves.\n",
"The model figure draws this hierarchy as containment.\n",
"\n",
"Each group's `higher_level_centre` sits in the outer `MultiLevelGaussians` card whereas its `Gaussian`s sit inside \n",
"the `gaussian_list` frame nested within it, which is exactly the reason to use a multi-level model, drawn rather than \n",
Expand Down Expand Up @@ -632,6 +652,23 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The model loaded back from the `.json` file is a single `Gaussian` again, and its map is the one the file was written \n",
"from, which is the quickest check that the round trip through hard disk preserved the structure."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"af.ModelPlotter(model).figure()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading
Loading