diff --git a/notebooks/chapter_3_graphical_models/tutorial_2_graphical_model.ipynb b/notebooks/chapter_3_graphical_models/tutorial_2_graphical_model.ipynb index ac7cb70..18e2c65 100644 --- a/notebooks/chapter_3_graphical_models/tutorial_2_graphical_model.ipynb +++ b/notebooks/chapter_3_graphical_models/tutorial_2_graphical_model.ipynb @@ -34,7 +34,7 @@ "- **Analysis**: Create Analysis objects for each dataset.\n", "- **Model**: Set up the graphical model with a shared prior for the centre parameter.\n", "- **Analysis Factors**: Pair each model with its corresponding Analysis class at factor graph nodes.\n", - "- **Factor Graph**: Combine the Analysis Factors into a factor graph representing the graphical model.\n", + "- **Factor Graph**: Combine the Analysis Factors into a factor graph and draw the graphical model.\n", "- **Search**: Configure and run the non-linear search to fit the factor graph.\n", "- **Result**: Inspect and compare the graphical model results to the individual fits.\n", "- **Wrap Up**: Summary and discussion of the benefits of graphical models." @@ -325,9 +325,7 @@ "model components that make up our model (e.g. the individual `Gaussian` classes) and how their parameters are linked or \n", "shared (e.g. that each `Gaussian` has its own unique `normalization` and `sigma`, but a shared `centre` parameter).\n", "\n", - "This is what our factor graph looks like (visualization of graphs not implemented yet): \n", - "\n", - "The factor graph above is made up of two components:\n", + "A factor graph is made up of two components:\n", "\n", "- Nodes: these are points on the graph where we have a unique set of data and a model that is made up of a subset of \n", "our overall graphical model. This is effectively the `AnalysisFactor` objects we created above. \n", @@ -365,6 +363,38 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also draw the model, via `af.ModelPlotter`. The figure is the **map** of the model and the `info` printed above\n", + "is its **legend**: the map shows the structure, meaning which dataset gets which component and which parameters are\n", + "shared between them, whereas the `info` lists the priors and values themselves.\n", + "\n", + "Look for three things in the figure. The five datasets do not appear as five repeated cards: because every dataset is\n", + "fitted by the same `Gaussian` model they collapse into one dashed plate badged with the number of datasets, which is\n", + "the picture of \"this structure repeats\". The shared `centre` is lifted out of that plate into its own card above it,\n", + "with the plate linked back to it, so the single shared parameter is drawn once rather than five times. The observed\n", + "data enters as its own pill, coloured as an observation and therefore distinguishable at a glance from the free\n", + "priors beside it.\n", + "\n", + "This is worth dwelling on, because it is exactly the claim the text struggles to make. Written down, \"a shared\n", + "`centre`\" and \"a `centre` per dataset\" are three words apart, and in the `info` above the whole distinction is carried\n", + "by an index range, the shared `centre` being printed once under a `0 - 4` heading instead of once per dataset. In the\n", + "figure they are two different pictures: a hoisted card the plate points back at, or a `centre` pill sitting inside the\n", + "plate exactly as `normalization` and `sigma` do. If you can read that difference off the map, you can read a\n", + "graphical model." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(factor_graph.global_prior_model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_3_graphical_models/tutorial_4_hierachical_models.ipynb b/notebooks/chapter_3_graphical_models/tutorial_4_hierachical_models.ipynb index f12bcc8..7472d0a 100644 --- a/notebooks/chapter_3_graphical_models/tutorial_4_hierachical_models.ipynb +++ b/notebooks/chapter_3_graphical_models/tutorial_4_hierachical_models.ipynb @@ -32,7 +32,7 @@ "- **Model Individual Factors**: Set up individual Gaussian models with independent priors.\n", "- **Analysis Factors**: Compose Analysis Factors pairing models with Analysis objects.\n", "- **Model**: Create a HierarchicalFactor with a parent Gaussian distribution for the centres.\n", - "- **Factor Graph**: Compose the factor graph including the hierarchical factor.\n", + "- **Factor Graph**: Compose the factor graph including the hierarchical factor and draw the hierarchical model.\n", "- **Search**: Configure and run the non-linear search for the hierarchical model.\n", "- **Result**: Inspect the inferred hierarchical distribution parameters.\n", "- **Comparison to One-by-One Fits**: Compare the hierarchical model results to simpler individual fits.\n", @@ -374,6 +374,34 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Drawing this model with `af.ModelPlotter` and putting it next to the figure from tutorial 2 is the fastest way to see\n", + "what changed, because the two models say opposite things with almost the same words.\n", + "\n", + "In tutorial 2 the `centre` was hoisted into a card of its own and the plate pointed back at it: one prior object, one\n", + "number, shared. Here every dataset keeps its own `centre`, so it stays inside the plate as a `drawn` pill badged with\n", + "the hierarchical factor it came from, and the violet arrow comes from that factor's card and lands on the pill. There\n", + "is no shared badge anywhere on this figure. The arrow is making the weaker and more realistic claim: these centres\n", + "were drawn from a common population, they are not the same number.\n", + "\n", + "The footer makes the accounting explicit too, counting the parent distribution's `mean` and `sigma` as\n", + "hyper-parameters separately from the parameters belonging to each dataset. That is the split that matters when you\n", + "ask what a hierarchical model actually buys you: the hyper-parameters are the handful of numbers the whole sample\n", + "informs, and the figure is where you can see how few of them there are." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(factor_graph.global_prior_model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/scripts/chapter_3_graphical_models/tutorial_2_graphical_model.py b/scripts/chapter_3_graphical_models/tutorial_2_graphical_model.py index 5eed016..ea080b1 100644 --- a/scripts/chapter_3_graphical_models/tutorial_2_graphical_model.py +++ b/scripts/chapter_3_graphical_models/tutorial_2_graphical_model.py @@ -29,7 +29,7 @@ - **Analysis**: Create Analysis objects for each dataset. - **Model**: Set up the graphical model with a shared prior for the centre parameter. - **Analysis Factors**: Pair each model with its corresponding Analysis class at factor graph nodes. -- **Factor Graph**: Combine the Analysis Factors into a factor graph representing the graphical model. +- **Factor Graph**: Combine the Analysis Factors into a factor graph and draw the graphical model. - **Search**: Configure and run the non-linear search to fit the factor graph. - **Result**: Inspect and compare the graphical model results to the individual fits. - **Wrap Up**: Summary and discussion of the benefits of graphical models. @@ -194,9 +194,7 @@ model components that make up our model (e.g. the individual `Gaussian` classes) and how their parameters are linked or shared (e.g. that each `Gaussian` has its own unique `normalization` and `sigma`, but a shared `centre` parameter). -This is what our factor graph looks like (visualization of graphs not implemented yet): - -The factor graph above is made up of two components: +A factor graph is made up of two components: - Nodes: these are points on the graph where we have a unique set of data and a model that is made up of a subset of our overall graphical model. This is effectively the `AnalysisFactor` objects we created above. @@ -215,6 +213,27 @@ """ print(factor_graph.global_prior_model.info) +""" +We can also draw the model, via `af.ModelPlotter`. The figure is the **map** of the model and the `info` printed above +is its **legend**: the map shows the structure, meaning which dataset gets which component and which parameters are +shared between them, whereas the `info` lists the priors and values themselves. + +Look for three things in the figure. The five datasets do not appear as five repeated cards: because every dataset is +fitted by the same `Gaussian` model they collapse into one dashed plate badged with the number of datasets, which is +the picture of "this structure repeats". The shared `centre` is lifted out of that plate into its own card above it, +with the plate linked back to it, so the single shared parameter is drawn once rather than five times. The observed +data enters as its own pill, coloured as an observation and therefore distinguishable at a glance from the free +priors beside it. + +This is worth dwelling on, because it is exactly the claim the text struggles to make. Written down, "a shared +`centre`" and "a `centre` per dataset" are three words apart, and in the `info` above the whole distinction is carried +by an index range, the shared `centre` being printed once under a `0 - 4` heading instead of once per dataset. In the +figure they are two different pictures: a hoisted card the plate points back at, or a `centre` pill sitting inside the +plate exactly as `normalization` and `sigma` do. If you can read that difference off the map, you can read a +graphical model. +""" +af.ModelPlotter(factor_graph.global_prior_model).figure() + """ __Search__ diff --git a/scripts/chapter_3_graphical_models/tutorial_4_hierachical_models.py b/scripts/chapter_3_graphical_models/tutorial_4_hierachical_models.py index 28a8fe2..2d34118 100644 --- a/scripts/chapter_3_graphical_models/tutorial_4_hierachical_models.py +++ b/scripts/chapter_3_graphical_models/tutorial_4_hierachical_models.py @@ -27,7 +27,7 @@ - **Model Individual Factors**: Set up individual Gaussian models with independent priors. - **Analysis Factors**: Compose Analysis Factors pairing models with Analysis objects. - **Model**: Create a HierarchicalFactor with a parent Gaussian distribution for the centres. -- **Factor Graph**: Compose the factor graph including the hierarchical factor. +- **Factor Graph**: Compose the factor graph including the hierarchical factor and draw the hierarchical model. - **Search**: Configure and run the non-linear search for the hierarchical model. - **Result**: Inspect the inferred hierarchical distribution parameters. - **Comparison to One-by-One Fits**: Compare the hierarchical model results to simpler individual fits. @@ -214,6 +214,23 @@ """ print(factor_graph.global_prior_model.info) +""" +Drawing this model with `af.ModelPlotter` and putting it next to the figure from tutorial 2 is the fastest way to see +what changed, because the two models say opposite things with almost the same words. + +In tutorial 2 the `centre` was hoisted into a card of its own and the plate pointed back at it: one prior object, one +number, shared. Here every dataset keeps its own `centre`, so it stays inside the plate as a `drawn` pill badged with +the hierarchical factor it came from, and the violet arrow comes from that factor's card and lands on the pill. There +is no shared badge anywhere on this figure. The arrow is making the weaker and more realistic claim: these centres +were drawn from a common population, they are not the same number. + +The footer makes the accounting explicit too, counting the parent distribution's `mean` and `sigma` as +hyper-parameters separately from the parameters belonging to each dataset. That is the split that matters when you +ask what a hierarchical model actually buys you: the hyper-parameters are the handful of numbers the whole sample +informs, and the figure is where you can see how few of them there are. +""" +af.ModelPlotter(factor_graph.global_prior_model).figure() + """ __Search__