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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interpret probabilistic models from first principles. It is the teaching compani

- `scripts/` — Runnable Python tutorial scripts:
- `chapter_1_introduction/` — Models, fitting data, non-linear searches, results and samples, gradients and JAX autodiff, the details that make a fit succeed or fail, a prose-only scientific workflow closing tutorial, and an optional Bayesian formalism tutorial
- `chapter_3_graphical_models/` — Individual / graphical / hierarchical models, Expectation Propagation
- `chapter_advanced/` — Individual / graphical / hierarchical models, Expectation Propagation
- `simulators/` — Simulator scripts that generate the tutorial 1D datasets at runtime
- `notebooks/` — Jupyter versions, generated from `scripts/` (do not edit directly)
- `config/` — PyAutoFit configuration YAML
Expand Down
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# HowToFit

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/PyAutoLabs/HowToFit/blob/2026.9.14.1/start_here.ipynb)

[Start Here on Colab](https://colab.research.google.com/github/PyAutoLabs/HowToFit/blob/2026.9.14.1/start_here.ipynb) |
[Installation Guide](https://pyautofit.readthedocs.io/en/latest/installation/overview.html) |
[PyAutoFit readthedocs](https://pyautofit.readthedocs.io/en/latest/index.html) |
[Browse Chapter 1 With Images](markdown/README.md) |
Expand All @@ -23,27 +26,39 @@ they are concise, API-focused, and assume the concepts taught in **HowToFit** as

- `chapter_1_introduction` — Models, likelihoods, non-linear searches, why modeling is hard, and how to
interpret the results of a fit, ending with a short guide to building a scientific workflow.
- `chapter_3_graphical_models` — Fitting many datasets simultaneously with graphical models,
- `chapter_advanced` — Fitting many datasets simultaneously with graphical models,
hierarchical models, and Expectation Propagation.

Each chapter is organised into numbered tutorial files: `chapter_<N>_<name>/tutorial_<M>_<topic>.py`
(Python script) or the matching `.ipynb` in `notebooks/`. Tutorials build on each other within a
chapter and assume you have completed the earlier ones.
Each chapter is a folder of numbered tutorial files — `tutorial_<M>_<topic>.py` (Python script) or the
matching `.ipynb` in `notebooks/`. Tutorials build on each other within a chapter and assume you have
completed the earlier ones.

## Getting Started

You can run the tutorials on your own machine by following the
### Run in Google Colab (nothing to install)

Every tutorial opens in Google Colab in one click. There is nothing to install and no local Python
environment to set up — **PyAutoFit** installs itself in the notebook's first cell.

- **[Start here](https://colab.research.google.com/github/PyAutoLabs/HowToFit/blob/2026.9.14.1/start_here.ipynb)** — a one-page overview of the whole series.
- **[Chapter 1, Tutorial 1: Models](https://colab.research.google.com/github/PyAutoLabs/HowToFit/blob/2026.9.14.1/notebooks/chapter_1_introduction/tutorial_1_models.ipynb)** — the first tutorial.
- Every tutorial, chapter by chapter: **[chapter 1](scripts/chapter_1_introduction/README.md)** |
**[the advanced chapter](scripts/chapter_advanced/README.md)**

Model-fits run considerably faster on a GPU. In Colab, enable one via *Runtime* → *Change runtime type*
→ *Hardware accelerator* before running a notebook.

### Run on your own machine

Follow the
[PyAutoFit installation guide](https://pyautofit.readthedocs.io/en/latest/installation/overview.html),
then cloning this repository:
then clone this repository:

```bash
git clone https://github.com/PyAutoLabs/HowToFit.git
cd HowToFit
```

Alternatively, every tutorial can be opened directly in Google Colab via the links in each chapter's
`README.md`.

The tutorials are distributed as both Jupyter notebooks (`notebooks/`) and Python scripts (`scripts/`).
We recommend the notebooks for reading — figures render inline, and you can step through small code blocks
interactively. Use the Python scripts for actual **PyAutoFit** use.
Expand Down
16 changes: 8 additions & 8 deletions llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ AUTO-GENERATED by PyAutoHands — do not edit by hand; regenerate with generate.
- [Tutorial Optional: Bayesian Formalism](scripts/chapter_1_introduction/tutorial_optional_bayesian_formalism.py): Every tutorial in this chapter has been an exercise in Bayesian inference, and not one of them wrote down a single equation of probability theory. That was deliberate. The **HowToFit** lectures are built on the conviction that you can learn to fit models to data properly, and interpret the results correctly, without first sitting through a course on Bayesian statistics.
- Contents: Bayes Theorem, Data, The Model, The Likelihood, The Prior, The Posterior, Maximum Likelihood Vs Maximum A Posteriori, Marginalization, The Evidence, Gradients, Wrap Up

## chapter_3_graphical_models
## chapter_advanced

- [Tutorial 1: Individual Models](scripts/chapter_3_graphical_models/tutorial_1_individual_models.py): In many examples, we fit one model to one dataset. For many problems, we may have a large dataset and are not interested in how well the model fits each individual dataset. Instead, we want to know how the model fits the full dataset, so that we can determine "global" trends of how the model fits the data.
- [Tutorial 1: Individual Models](scripts/chapter_advanced/tutorial_1_individual_models.py): In many examples, we fit one model to one dataset. For many problems, we may have a large dataset and are not interested in how well the model fits each individual dataset. Instead, we want to know how the model fits the full dataset, so that we can determine "global" trends of how the model fits the data.
- Contents: Real World Example, Example Source Code (`af.ex`), Model, Data, Model Fits (one-by-one), Results, Estimating the Centre, Posterior Multiplication, Wrap Up
- [Tutorial 2: Graphical Models](scripts/chapter_3_graphical_models/tutorial_2_graphical_model.py): We have fitted a dataset containing 5 noisy 1D Gaussian which had a shared `centre` value. We estimated the `centre` by fitting each dataset individually and combining the value of the `centre` inferred by each fit into an overall estimate, using a weighted average.
- [Tutorial 2: Graphical Models](scripts/chapter_advanced/tutorial_2_graphical_model.py): We have fitted a dataset containing 5 noisy 1D Gaussian which had a shared `centre` value. We estimated the `centre` by fitting each dataset individually and combining the value of the `centre` inferred by each fit into an overall estimate, using a weighted average.
- Contents: Example Source Code (`af.ex`), Dataset, Analysis, Model, Analysis Factors, Factor Graph, Search, Result, Wrap Up
- [Tutorial 3: Graphical Benefits](scripts/chapter_3_graphical_models/tutorial_3_graphical_benefits.py): In the previous tutorials, we fitted a dataset containing 5 noisy 1D Gaussian which had a shared `centre` value and compared different approaches to estimate the shared `centre`. This included a simple approach fitting each dataset one-by-one and estimating the centre via a weighted average or posterior multiplication and a more complicated approach using a graphical model.
- [Tutorial 3: Graphical Benefits](scripts/chapter_advanced/tutorial_3_graphical_benefits.py): In the previous tutorials, we fitted a dataset containing 5 noisy 1D Gaussian which had a shared `centre` value and compared different approaches to estimate the shared `centre`. This included a simple approach fitting each dataset one-by-one and estimating the centre via a weighted average or posterior multiplication and a more complicated approach using a graphical model.
- Contents: The Model, Example Source Code (`af.ex`), Dataset, Analysis, Model (one-by-one), Model Fits (one-by-one), Centre Estimates (Weighted Average), Discussion, Model (Graphical), Analysis Factors, Factor Graph, Search, Result, Discussion, Posterior Multiplication, Wrap Up
- [Tutorial 4: Hierarchical](scripts/chapter_3_graphical_models/tutorial_4_hierachical_models.py): In the previous tutorial, we fitted a graphical model with the aim of determining an estimate of shared parameters, the `centre`'s of a dataset of 1D Gaussians. We did this by fitting all datasets simultaneously. When there are shared parameters in a model, this is a powerful and effective tool, but things may not always be so simple.
- [Tutorial 4: Hierarchical](scripts/chapter_advanced/tutorial_4_hierachical_models.py): In the previous tutorial, we fitted a graphical model with the aim of determining an estimate of shared parameters, the `centre`'s of a dataset of 1D Gaussians. We did this by fitting all datasets simultaneously. When there are shared parameters in a model, this is a powerful and effective tool, but things may not always be so simple.
- Contents: Example Source Code (`af.ex`), Dataset, Analysis, Model Individual Factors, Analysis Factors, Model, Factor Graph, Search, Result, Comparison to One-by-One Fits, Benefits of Graphical Model, Wrap Up
- [Tutorial 5: Expectation Propagation](scripts/chapter_3_graphical_models/tutorial_5_expectation_propagation.py): In the previous tutorials, we fitted graphical models to dataset comprising many noisy 1D Gaussians. These had a shared and global value of their `centre`, or assumed their centres were hierarchically drawn from a parent Gaussian distribution. This provides the basis of composing and fitting complex graphical models to large datasets.
- [Tutorial 5: Expectation Propagation](scripts/chapter_advanced/tutorial_5_expectation_propagation.py): In the previous tutorials, we fitted graphical models to dataset comprising many noisy 1D Gaussians. These had a shared and global value of their `centre`, or assumed their centres were hierarchically drawn from a parent Gaussian distribution. This provides the basis of composing and fitting complex graphical models to large datasets.
- Contents: Example Source Code (`af.ex`), Dataset, Analysis, Model, Analysis Factors, Factor Graph, Expectation Propagation, Cyclic Fitting, Result, Output, Results
- [Tutorial Optional: Hierarchical Expectation Propagation (EP)](scripts/chapter_3_graphical_models/tutorial_optional_hierarchical_ep.py): This optional tutorial gives an example of fitting a hierarchical model using EP.
- [Tutorial Optional: Hierarchical Expectation Propagation (EP)](scripts/chapter_advanced/tutorial_optional_hierarchical_ep.py): This optional tutorial gives an example of fitting a hierarchical model using EP.
- Contents: Example Source Code (`af.ex`), Dataset, Analysis, Model Individual Factors, Analysis Factors, Model, Factor Graph, Model Fit
- [Tutorial Optional: Hierarchical Individual](scripts/chapter_3_graphical_models/tutorial_optional_hierarchical_individual.py): In tutorial 4, we fit a hierarchical model using a graphical model, whereby all datasets are fitted simultaneously and the hierarchical parameters are fitted for simultaneously with the model parameters of each 1D Gaussian in each dataset.
- [Tutorial Optional: Hierarchical Individual](scripts/chapter_advanced/tutorial_optional_hierarchical_individual.py): In tutorial 4, we fit a hierarchical model using a graphical model, whereby all datasets are fitted simultaneously and the hierarchical parameters are fitted for simultaneously with the model parameters of each 1D Gaussian in each dataset.
- Contents: Example Source Code (`af.ex`), Dataset, Analysis, Model, Model Fits (one-by-one), Results, Overall Gaussian Parent Distribution, Model, Analysis + Search

## simulators
Expand Down
6 changes: 3 additions & 3 deletions llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ analysis?" below).
## The learning path (chapters in order)

- **Chapter 1 — Introduction** → [scripts/chapter_1_introduction/](scripts/chapter_1_introduction/): The foundations of model-fitting — defining models, fitting data (residuals / chi-squared / likelihood), the non-linear search, why modeling is hard, and reading results & samples.
- **Chapter 3 — Graphical Models** → [scripts/chapter_3_graphical_models/](scripts/chapter_3_graphical_models/): Fitting one model across a large dataset — individual fits vs graphical models, shared parameters, hierarchical models, and Expectation Propagation for scaling to many datasets.
- **Advanced Chapter — Graphical Models** → [scripts/chapter_advanced/](scripts/chapter_advanced/): Fitting one model across a large dataset — individual fits vs graphical models, shared parameters, hierarchical models, and Expectation Propagation for scaling to many datasets.

## I want to understand…

Expand All @@ -40,8 +40,8 @@ analysis?" below).
- **How does a search know which way is "up" / what is a gradient?** → [scripts/chapter_1_introduction/tutorial_6_gradients.py](scripts/chapter_1_introduction/tutorial_6_gradients.py): Gradients of the log likelihood, finite differencing, JAX autodiff, gradient vs non-gradient searches, errors from curvature.
- **My fit looks reasonable but the answer is wrong — what am I missing?** → [scripts/chapter_1_introduction/tutorial_7_the_details.py](scripts/chapter_1_introduction/tutorial_7_the_details.py): Parameterization and mirror solutions, assertions, plateaus, clipping, NaN diagnostics, the unit cube.
- **Where is the Bayesian maths behind all of this?** → [scripts/chapter_1_introduction/tutorial_optional_bayesian_formalism.py](scripts/chapter_1_introduction/tutorial_optional_bayesian_formalism.py): Bayes' theorem, likelihood, prior, posterior, marginalization and evidence, mapped onto what chapter 1 already computed.
- **I have many datasets with a shared parameter — what's the right tool?** → [scripts/chapter_3_graphical_models/tutorial_2_graphical_model.py](scripts/chapter_3_graphical_models/tutorial_2_graphical_model.py): Factor graphs and graphical models vs one-by-one fits.
- **What is a hierarchical model / Expectation Propagation?** → [scripts/chapter_3_graphical_models/tutorial_4_hierachical_models.py](scripts/chapter_3_graphical_models/tutorial_4_hierachical_models.py) then [tutorial_5_expectation_propagation.py](scripts/chapter_3_graphical_models/tutorial_5_expectation_propagation.py).
- **I have many datasets with a shared parameter — what's the right tool?** → [scripts/chapter_advanced/tutorial_2_graphical_model.py](scripts/chapter_advanced/tutorial_2_graphical_model.py): Factor graphs and graphical models vs one-by-one fits.
- **What is a hierarchical model / Expectation Propagation?** → [scripts/chapter_advanced/tutorial_4_hierachical_models.py](scripts/chapter_advanced/tutorial_4_hierachical_models.py) then [tutorial_5_expectation_propagation.py](scripts/chapter_advanced/tutorial_5_expectation_propagation.py).

## Ready for real analysis?

Expand Down
2 changes: 1 addition & 1 deletion markdown/chapter_1_introduction/start_here.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The HowToFit lectures cover an introductory chapter and an advanced chapter:
**Chapter 1: Introduction**: How to fit a model to data, perform statistical inference, and interpret the results
for scientific analysis, ending with an introduction to scientific workflows.

**Chapter 3: Graphical Models**: Simultaneous model fitting of large datasets, scaling up to models with tens of
**Advanced Chapter: Graphical Models**: Simultaneous model fitting of large datasets, scaling up to models with tens of
thousands of parameters.

After each chapter, it is advised that you apply what you've learned to your own model-fitting analysis based on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ For the corresponding Python implementation, follow the

__Wrap Up__

Apply the workflow to your own data before returning to chapter 3 for graphical and hierarchical models.
Apply the workflow to your own data before returning to the advanced chapter for graphical and hierarchical models.
14 changes: 7 additions & 7 deletions notebooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ The `scripts` folder contains **HowToFit** lectures, which teach a new user how
# Folders

- `chapter_1_introduction`: Introduction lectures describing how to compose and fit models in **PyAutoFit**, use gradients to fit them more efficiently and handle the details that make a fit succeed or fail, ending with a prose-only introduction to scientific workflows and an optional tutorial on the Bayesian formalism.
- `chapter_3_graphical_models`: How to compose and fit graphical models which fit many datasets simultaneously, including hierarchical models and Expectation Propagation.
- `chapter_advanced`: How to compose and fit graphical models which fit many datasets simultaneously, including hierarchical models and Expectation Propagation.
- `simulators`: Simulator scripts that generate the tutorial 1D datasets at runtime.

# Jupyter Notebooks
Expand All @@ -20,7 +20,7 @@ This makes them an ideal way for us to present the **HowToFit** lecture series.
`notebooks/` and are auto-generated from the Python scripts in `scripts/` — **edit the Python scripts,
not the notebooks.**

For actual **PyAutoFit** use we recommend the Python scripts. Chapter 3 onwards assumes that transition.
For actual **PyAutoFit** use we recommend the Python scripts. The advanced chapter assumes that transition.

# Code Style and Formatting

Expand All @@ -33,14 +33,14 @@ is auto-formatted with `black` — see <https://github.com/python/black>.

# How to Approach HowToFit

**HowToFit** consists of the introductory chapter 1 and advanced chapter 3. Chapter 1 will take a couple of
hours to work through. The concepts in chapter 3 are challenging, and familiarity with **PyAutoFit** and
**HowToFit** consists of the introductory chapter 1 and the advanced chapter. Chapter 1 will take a couple of
hours to work through. The concepts in the advanced chapter are challenging, and familiarity with **PyAutoFit** and
model fitting is desirable before tackling them.

We recommend that you complete chapter 1 and then apply what you've learnt to a model-fitting problem you
are interested in, building on the scripts found in the
[autofit_workspace](https://github.com/PyAutoLabs/autofit_workspace) repository. Once you're confident
with your use of **PyAutoFit**, return for chapter 3.
with your use of **PyAutoFit**, return for the advanced chapter.

# Overview of Chapter 1 (Beginner)

Expand All @@ -58,11 +58,11 @@ able to:
7. Use gradients of the log likelihood, computed with JAX automatic differentiation, to fit models with gradient based searches.
8. Recognise and fix the details that make a fit fail: degenerate parameterizations, plateaus, clipping and NaNs, and the unit cube.

# Overview of Chapter 3 (Advanced)
# Overview of the Advanced Chapter

**Fitting Graphical Models to Large Datasets**

Chapter 3 covers how to compose and fit graphical models to extremely large datasets. You'll learn:
The advanced chapter covers how to compose and fit graphical models to extremely large datasets. You'll learn:

1. Why fitting a model to many datasets one-by-one is suboptimal.
2. How to fit a graphical model to all datasets simultaneously and why this improves the model results.
Expand Down
Loading
Loading