From c1ae480eb261eb10d76079bf8e816541b730ad8c Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 11 Sep 2026 12:48:25 -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 --- scripts/features/graphical_models.py | 96 ++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 5 deletions(-) diff --git a/scripts/features/graphical_models.py b/scripts/features/graphical_models.py index 8fbfbc27..b1037140 100644 --- a/scripts/features/graphical_models.py +++ b/scripts/features/graphical_models.py @@ -32,9 +32,9 @@ - **Analysis**: Create Analysis objects for each dataset with a log likelihood function. - **Model**: Compose a graphical model with a shared prior across multiple model components. - **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**: Create a non-linear search and fit the factor graph. -- **Hierarchical Models**: Discuss how shared parameters can be drawn from a common parent distribution. +- **Hierarchical Models**: Compose a model where each dataset's `centre` is drawn from a common parent distribution. - **Expectation Propagation**: Introduce the EP framework for scaling graphical models to high dimensionality. __Example Source Code (`af.ex`)__ @@ -220,9 +220,7 @@ class we are therefore associating it with a unique `log_likelihood_function`. that make up our model (e.g. the three `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: - -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. @@ -232,6 +230,24 @@ class we are therefore associating it with a unique `log_likelihood_function`. """ factor_graph = af.FactorGraphModel(*analysis_factor_list) +""" +To inspect the model, we print `factor_graph.global_prior_model.info`. +""" +print(factor_graph.global_prior_model.info) + +""" +We can also draw this global model, via `af.ModelPlotter`. The figure is the **map** of the model and the `info` above +is its **legend**: the map shows the structure, meaning which dataset gets which component, which parameters are shared +between them and where the data enters, whereas the `info` lists the priors and values themselves. + +A graphical model is where the map says the most, because the three datasets collapse into a single dashed plate +badged with the number of datasets instead of three repeated cards, the shared `centre` is hoisted into its own card +above that plate with every member of the plate pointing back at it, and the observed data enters as its own green +`observed` pill, which is visibly a different kind of thing from the grey pills used for fixed values. The `info` +above can only list the same `centre` prior once per dataset and cannot show the data at all. +""" +af.ModelPlotter(factor_graph.global_prior_model).figure() + """ __Search__ @@ -257,6 +273,76 @@ class we are therefore associating it with a unique `log_likelihood_function`. Fitting the datasets simultaneously enables better estimate of this global hierarchical distribution. +We compose one below, on the same three datasets fitted above. Every `Gaussian` is now given its own `centre` prior, +because the centres are no longer assumed to be the same number as one another. +""" +model_list = [] + +for model_index in range(len(data_list)): + gaussian = af.Model(af.ex.Gaussian) + + gaussian.centre = af.TruncatedGaussianPrior( + mean=50.0, sigma=20.0, lower_limit=0.0, upper_limit=100.0 + ) + gaussian.normalization = af.LogUniformPrior(lower_limit=1e-6, upper_limit=1e6) + gaussian.sigma = af.UniformPrior(lower_limit=0.0, upper_limit=25.0) + + model_list.append(gaussian) + +""" +Each model is paired with its `Analysis` at an `AnalysisFactor`, exactly as it was for the graphical model above. +""" +analysis_factor_list = [] + +for model, analysis in zip(model_list, analysis_list): + analysis_factor = af.AnalysisFactor(prior_model=model, analysis=analysis) + + analysis_factor_list.append(analysis_factor) + +""" +The `HierarchicalFactor` is the parent distribution that the individual `centre`'s are assumed to be drawn from. Its +own `mean` and `sigma` are free parameters of the fit, which is what makes the model hierarchical rather than shared. +""" +hierarchical_factor = af.HierarchicalFactor( + af.GaussianPrior, + mean=af.TruncatedGaussianPrior( + mean=50.0, sigma=10, lower_limit=0.0, upper_limit=100.0 + ), + sigma=af.TruncatedGaussianPrior( + mean=10.0, sigma=5.0, lower_limit=0.0, upper_limit=100.0 + ), +) + +""" +We now add each individual `Gaussian`'s `centre` to the `hierarchical_factor`, which declares that each was drawn +from the parent distribution. +""" +for model in model_list: + hierarchical_factor.add_drawn_variable(model.centre) + +""" +The factor graph is composed from the `AnalysisFactor`'s and the hierarchical factor, which was not passed in for +the graphical model above. +""" +factor_graph = af.FactorGraphModel(*analysis_factor_list, hierarchical_factor) + +print(factor_graph.global_prior_model.info) + +""" +Putting the two models side by side is the quickest way to understand what a hierarchical model is. In the shared +graphical model above there is one `centre`: one prior object, one number, and the link into the hoisted card means +the three datasets use literally the same value. Here there are three distinct `centre`'s, one per dataset, each +drawn from a parent distribution whose own `mean` and `sigma` are the parameters we are fitting for, so the arrow +means "these came from a common population", not "these are the same number". + +The figure states this directly: each `centre` is drawn as a `drawn` pill with the arrow from the hierarchical +factor's card landing on the pill itself, and there is no shared badge anywhere on the figure. The footer counts the +two hyper-parameters separately from the parameters belonging to each dataset, which is the accounting the `info` +above cannot express. +""" +af.ModelPlotter(factor_graph.global_prior_model).figure() + +""" __Expectation Propagation__ For large datasets, a graphical model may have hundreds, thousands, or *hundreds of thousands* of parameters. The From b85bbac8fbcdece7a1dda4c63a67f12b93fd03fe Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Sat, 12 Sep 2026 18:44:22 +0100 Subject: [PATCH 2/2] Add graphical-model figures to graphical_models.py (PyAutoFit#1616) Draw the shared graphical model and a new hierarchical model with `af.ModelPlotter`, with prose checked against the figures the script actually produces. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01AmyBwcVQBrWE4jSTqdATY6 --- notebooks/features/graphical_models.ipynb | 184 +++++++++++++++++++++- scripts/features/graphical_models.py | 14 +- 2 files changed, 186 insertions(+), 12 deletions(-) diff --git a/notebooks/features/graphical_models.ipynb b/notebooks/features/graphical_models.ipynb index 0994c8a1..4d3f0861 100644 --- a/notebooks/features/graphical_models.ipynb +++ b/notebooks/features/graphical_models.ipynb @@ -37,9 +37,9 @@ "- **Analysis**: Create Analysis objects for each dataset with a log likelihood function.\n", "- **Model**: Compose a graphical model with a shared prior across multiple model components.\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**: Create a non-linear search and fit the factor graph.\n", - "- **Hierarchical Models**: Discuss how shared parameters can be drawn from a common parent distribution.\n", + "- **Hierarchical Models**: Compose a model where each dataset's `centre` is drawn from a common parent distribution.\n", "- **Expectation Propagation**: Introduce the EP framework for scaling graphical models to high dimensionality.\n", "\n", "__Example Source Code (`af.ex`)__\n", @@ -350,9 +350,7 @@ "that make up our model (e.g. the three `Gaussian` classes) and how their parameters are linked or shared (e.g. that\n", "each `Gaussian` has its own unique `normalization` and `sigma`, but a shared `centre` parameter.\n", "\n", - "This is what our factor graph looks like: \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", @@ -370,6 +368,46 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To inspect the model, we print `factor_graph.global_prior_model.info`." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "print(factor_graph.global_prior_model.info)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also draw this global model, via `af.ModelPlotter`. The figure is the **map** of the model and the `info` above\n", + "is its **legend**: the map shows the structure, meaning which dataset gets which component, which parameters are shared\n", + "between them and where the data enters, whereas the `info` lists the priors and values themselves.\n", + "\n", + "A graphical model is where the map says the most, because the three datasets collapse into a single dashed plate\n", + "badged with the number of datasets instead of three repeated cards, the shared `centre` is hoisted into its own card\n", + "above that plate with the plate linked back to it, and the observed data enters as its own green `observed` pill,\n", + "which is visibly a different kind of thing from the white pills used for the free priors. The `info` above can only\n", + "say the `centre` is shared by grouping it under a `0 - 2` heading, and it cannot show the data at all." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(factor_graph.global_prior_model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -409,6 +447,142 @@ "\n", "Fitting the datasets simultaneously enables better estimate of this global hierarchical distribution.\n", "\n", + "We compose one below, on the same three datasets fitted above. Every `Gaussian` is now given its own `centre` prior,\n", + "because the centres are no longer assumed to be the same number as one another." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "model_list = []\n", + "\n", + "for model_index in range(len(data_list)):\n", + " gaussian = af.Model(af.ex.Gaussian)\n", + "\n", + " gaussian.centre = af.TruncatedGaussianPrior(\n", + " mean=50.0, sigma=20.0, lower_limit=0.0, upper_limit=100.0\n", + " )\n", + " gaussian.normalization = af.LogUniformPrior(lower_limit=1e-6, upper_limit=1e6)\n", + " gaussian.sigma = af.UniformPrior(lower_limit=0.0, upper_limit=25.0)\n", + "\n", + " model_list.append(gaussian)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each model is paired with its `Analysis` at an `AnalysisFactor`, exactly as it was for the graphical model above." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "analysis_factor_list = []\n", + "\n", + "for model, analysis in zip(model_list, analysis_list):\n", + " analysis_factor = af.AnalysisFactor(prior_model=model, analysis=analysis)\n", + "\n", + " analysis_factor_list.append(analysis_factor)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `HierarchicalFactor` is the parent distribution that the individual `centre`'s are assumed to be drawn from. Its\n", + "own `mean` and `sigma` are free parameters of the fit, which is what makes the model hierarchical rather than shared." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "hierarchical_factor = af.HierarchicalFactor(\n", + " af.GaussianPrior,\n", + " mean=af.TruncatedGaussianPrior(\n", + " mean=50.0, sigma=10, lower_limit=0.0, upper_limit=100.0\n", + " ),\n", + " sigma=af.TruncatedGaussianPrior(\n", + " mean=10.0, sigma=5.0, lower_limit=0.0, upper_limit=100.0\n", + " ),\n", + ")" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We now add each individual `Gaussian`'s `centre` to the `hierarchical_factor`, which declares that each was drawn\n", + "from the parent distribution." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "for model in model_list:\n", + " hierarchical_factor.add_drawn_variable(model.centre)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The factor graph is composed from the `AnalysisFactor`'s and the hierarchical factor, which was not passed in for\n", + "the graphical model above." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "factor_graph = af.FactorGraphModel(*analysis_factor_list, hierarchical_factor)\n", + "\n", + "print(factor_graph.global_prior_model.info)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Putting the two models side by side is the quickest way to understand what a hierarchical model is. In the shared\n", + "graphical model above there is one `centre`: one prior object, one number, and the link into the hoisted card means\n", + "the three datasets use literally the same value. Here there are three distinct `centre`'s, one per dataset, each\n", + "drawn from a parent distribution whose own `mean` and `sigma` are the parameters we are fitting for, so the arrow\n", + "means \"these came from a common population\", not \"these are the same number\".\n", + "\n", + "The figure states this directly: inside the plate the `centre` is now a `drawn` pill, with the violet arrow from the\n", + "hierarchical factor's card landing on the pill itself, and there is no shared badge anywhere on the figure. The\n", + "footer counts the two hyper-parameters separately from the parameters belonging to each dataset, which is an\n", + "accounting the `info` above leaves you to do yourself." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(factor_graph.global_prior_model).figure()" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "__Expectation Propagation__\n", "\n", "For large datasets, a graphical model may have hundreds, thousands, or *hundreds of thousands* of parameters. The\n", diff --git a/scripts/features/graphical_models.py b/scripts/features/graphical_models.py index b1037140..40de59f2 100644 --- a/scripts/features/graphical_models.py +++ b/scripts/features/graphical_models.py @@ -242,9 +242,9 @@ class we are therefore associating it with a unique `log_likelihood_function`. A graphical model is where the map says the most, because the three datasets collapse into a single dashed plate badged with the number of datasets instead of three repeated cards, the shared `centre` is hoisted into its own card -above that plate with every member of the plate pointing back at it, and the observed data enters as its own green -`observed` pill, which is visibly a different kind of thing from the grey pills used for fixed values. The `info` -above can only list the same `centre` prior once per dataset and cannot show the data at all. +above that plate with the plate linked back to it, and the observed data enters as its own green `observed` pill, +which is visibly a different kind of thing from the white pills used for the free priors. The `info` above can only +say the `centre` is shared by grouping it under a `0 - 2` heading, and it cannot show the data at all. """ af.ModelPlotter(factor_graph.global_prior_model).figure() @@ -335,10 +335,10 @@ class we are therefore associating it with a unique `log_likelihood_function`. drawn from a parent distribution whose own `mean` and `sigma` are the parameters we are fitting for, so the arrow means "these came from a common population", not "these are the same number". -The figure states this directly: each `centre` is drawn as a `drawn` pill with the arrow from the hierarchical -factor's card landing on the pill itself, and there is no shared badge anywhere on the figure. The footer counts the -two hyper-parameters separately from the parameters belonging to each dataset, which is the accounting the `info` -above cannot express. +The figure states this directly: inside the plate the `centre` is now a `drawn` pill, with the violet arrow from the +hierarchical factor's card landing on the pill itself, and there is no shared badge anywhere on the figure. The +footer counts the two hyper-parameters separately from the parameters belonging to each dataset, which is an +accounting the `info` above leaves you to do yourself. """ af.ModelPlotter(factor_graph.global_prior_model).figure()