From 48d1876696d748df95d806da186e784036c306e7 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 11 Sep 2026 12:48:27 -0400 Subject: [PATCH 1/2] =?UTF-8?q?WIP:=20model=20figures=20phase=204=20?= =?UTF-8?q?=E2=80=94=20paused=20mid-implementation=202026-09-11=20(PyAutoF?= =?UTF-8?q?it#1616)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Snapshot taken at laptop shutdown; not tested/shipped. Resume from the active.md row of model-figures-graphical in PyAutoMind. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_0178yLU9v19GtACcRPFggjGa --- .../tutorial_2_graphical_model.py | 25 ++++++++++++++++--- .../tutorial_4_hierachical_models.py | 19 +++++++++++++- 2 files changed, 39 insertions(+), 5 deletions(-) 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..45f834b 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,25 @@ """ 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 every member of the plate pointing back at it, so the single shared parameter is drawn once rather than five +times. Each dataset's observed data enters as its own pill, coloured as an observation and therefore distinguishable +at a glance from a value we have fixed by hand. + +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 the `info` above prints the same `centre` prior once +per dataset either way. In the figure they are different pictures: one arrow into a hoisted card, or five separate +pills. 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..b5699f8 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 every dataset pointed at it: one prior object, one +number, shared. Here each dataset keeps its own `centre`, drawn as a `drawn` pill, and the arrow comes from the +hierarchical factor's card and lands on that 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__ From e62ac270fe7d5b26a34742b8bf293021ca3e45fe Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Sat, 12 Sep 2026 18:44:29 +0100 Subject: [PATCH 2/2] Add plate-notation figures to chapter 3 tutorials 2 and 4 (PyAutoFit#1616) Draw the shared graphical model (tutorial 2) and the hierarchical model (tutorial 4) with `af.ModelPlotter`, with prose checked against the figures the tutorials actually produce. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01AmyBwcVQBrWE4jSTqdATY6 --- .../tutorial_2_graphical_model.ipynb | 38 +++++++++++++++++-- .../tutorial_4_hierachical_models.ipynb | 30 ++++++++++++++- .../tutorial_2_graphical_model.py | 14 ++++--- .../tutorial_4_hierachical_models.py | 10 ++--- 4 files changed, 76 insertions(+), 16 deletions(-) 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 45f834b..ea080b1 100644 --- a/scripts/chapter_3_graphical_models/tutorial_2_graphical_model.py +++ b/scripts/chapter_3_graphical_models/tutorial_2_graphical_model.py @@ -221,14 +221,16 @@ 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 every member of the plate pointing back at it, so the single shared parameter is drawn once rather than five -times. Each dataset's observed data enters as its own pill, coloured as an observation and therefore distinguishable -at a glance from a value we have fixed by hand. +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 the `info` above prints the same `centre` prior once -per dataset either way. In the figure they are different pictures: one arrow into a hoisted card, or five separate -pills. If you can read that difference off the map, you can read a graphical model. +`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() 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 b5699f8..2d34118 100644 --- a/scripts/chapter_3_graphical_models/tutorial_4_hierachical_models.py +++ b/scripts/chapter_3_graphical_models/tutorial_4_hierachical_models.py @@ -218,11 +218,11 @@ 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 every dataset pointed at it: one prior object, one -number, shared. Here each dataset keeps its own `centre`, drawn as a `drawn` pill, and the arrow comes from the -hierarchical factor's card and lands on that 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. +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