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
184 changes: 179 additions & 5 deletions notebooks/features/graphical_models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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": {},
Expand Down Expand Up @@ -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",
Expand Down
96 changes: 91 additions & 5 deletions scripts/features/graphical_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`)__
Expand Down Expand Up @@ -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.
Expand All @@ -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 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()

"""
__Search__

Expand All @@ -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: 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()

"""
__Expectation Propagation__

For large datasets, a graphical model may have hundreds, thousands, or *hundreds of thousands* of parameters. The
Expand Down
Loading