Skip to content

docs: finish the math sweep and fix tutorial 3's dead Priors block #59

Closed

Description

@Jammy2211

Overview

The two follow-ups reported out of #57 / #58. A: #58 converted the unrenderable
\[ \] / \( \) math to $$ $$ / $ $ in tutorials 1-3 only, because the reader
scoped it there; tutorials 5, 6, 7 and the optional Bayesian formalism tutorial still
carry 13 display blocks and 55 inline spans that Jupyter and Colab render as raw LaTeX.
B: tutorial 3's __Priors__ block sets three priors on a model that every later fit
throws away, and two of the three are wrong — normalization Uniform(0, 10) excludes
the truth 25, and sigma Uniform(0, 10) puts the truth 10 on the boundary.

Plan

  • Convert every remaining \[ ... \] to $$ ... $$ and \( ... \) to $...$ across
    tutorials 5, 6, 7 and the optional Bayesian formalism tutorial.
  • Make tutorial 3's __Priors__ block actually take effect by removing the redundant
    model = af.Model(Gaussian) re-creations that discard it, and correct the two priors
    that exclude or clip the true solution.
  • Gate that on measurement: the uninitialised first MLE fit must still fail 10/10
    runs, because its failure is what the MLE section teaches. If a meaningful prior
    narrowing breaks that, keep the explored parameter space semantically equal to the
    config defaults and make the block honest rather than flaky.
  • Reconcile the surrounding prose with whatever the code ends up doing.
  • Regenerate notebooks + catalogue, run the smoke suite.
Detailed implementation plan

Affected Repositories

  • HowToFit (primary)

Branch Survey

Repository Current Branch Dirty?
./HowToFit feature/howtofit-tutorials-1-3 (#58, pushed, green) clean

Suggested branch: feature/howtofit-tutorial-followups

Stacking. Both follow-ups touch files that #56 and #58 already modify
(#56 touches tutorial_7_the_details.py and tutorial_optional_bayesian_formalism.py;
#58 touches tutorial_3_non_linear_search.py), so this cannot branch from main
without conflicting. It is cut from origin/feature/howtofit-tutorials-1-3 and the PR
is based on it. Merge order: #56 -> #58 -> this.

Implementation Steps

  1. Follow-up A. In tutorial_5_results_and_samples.py (1 block),
    tutorial_6_gradients.py (4 display + 20 inline),
    tutorial_7_the_details.py (1 block) and
    tutorial_optional_bayesian_formalism.py (7 display + 35 inline), convert
    \[ ... \] -> $$ ... $$ (delimiters on their own lines) and \( ... \) -> $...$.
    Leave \begin{equation*} environments alone — MathJax does render those.
    Confirm each enclosing docstring is a raw string. Check for false positives: a
    \( inside a regex or a non-math string must not be touched.
  2. Follow-up B, measurement first. On the current code, run the uninitialised
    first MLE fit 10 times and record that it fails 10/10 (baseline). Then set
    normalization to UniformPrior(0.0, 50.0) and sigma to UniformPrior(0.0, 25.0),
    delete the redundant model = af.Model(Gaussian) lines that discard the block, and
    re-run the uninitialised fit 10 times.
    • 10/10 still fail -> keep this version; the overrides are now real and correct.
    • Any run succeeds -> revert to a parameter space semantically equal to the
      config defaults (centre Uniform(0, 100), sigma Uniform(0, 25),
      normalization LogUniform(1e-6, 1e6)), still made to actually apply, and say in
      the prose that these restate the defaults explicitly so the API is visible.
      Either way the dead-code bug is fixed: the block must take effect.
  3. Verify the initialised MLE, the MCMC and the nested-sampling fits all still
    recover the truth after the prior change, and that no number quoted in the prose
    went stale.
  4. Fix the prose claim "we are using UniformPriors in this tutorial due to their
    simplicity" if normalization ends up LogUniform.
  5. Regenerate: PYTHONPATH=../PyAutoHands/autohands python3 ../PyAutoHands/autohands/generate.py howtofit.
  6. python .github/scripts/run_smoke.py until green; black --check.

Key Files

  • scripts/chapter_1_introduction/tutorial_3_non_linear_search.py — the Priors block
  • scripts/chapter_1_introduction/tutorial_5_results_and_samples.py
  • scripts/chapter_1_introduction/tutorial_6_gradients.py
  • scripts/chapter_1_introduction/tutorial_7_the_details.py
  • scripts/chapter_1_introduction/tutorial_optional_bayesian_formalism.py
  • config/priors/gaussian.yaml — the defaults the fits actually use today (read-only reference)

Original Prompt

Click to expand starting prompt
# HowToFit follow-ups: finish the math sweep and fix tutorial 3's dead Priors block

Type: docs
Target: HowToFit
Repos:
- HowToFit
Themes:
- howtofit
- notebooks
- rendering
- priors
Difficulty: medium
Autonomy: safe
Priority: normal
Status: formalised
Consequence: judge
Review-minutes: 20
Unattended: ready
Filed: 2026-09-14
Parent: active/howtofit_tutorials_1_3_fixes.md

## User request (verbatim)

"do the follow ups"

— the two follow-ups reported out of HowToFit#57 / #58 (see that issue's
"Out of scope" section).

## Follow-up A: finish the math-delimiter sweep

`#58` converted `\[ ... \]` -> `$$ ... $$` and `\( ... \)` -> `$...$` in tutorials
1-3 only, because the reader scoped that task to tutorials 1-3. Neither `\[` nor
`\(` is a default MathJax delimiter in Jupyter, JupyterLab or Colab, so every
remaining occurrence renders as raw LaTeX. Remaining, measured on the `#58`
branch:

| File | `\[` | `\(` |
|---|---|---|
| `tutorial_5_results_and_samples.py` | 1 | 0 |
| `tutorial_6_gradients.py` | 4 | 20 |
| `tutorial_7_the_details.py` | 1 | 0 |
| `tutorial_optional_bayesian_formalism.py` | 7 | 35 |

Tutorial 5 was not in the original follow-up note but carries the same bug.
`chapter_advanced/` is clean.

## Follow-up B: tutorial 3's `__Priors__` block is dead *and* wrong

`scripts/chapter_1_introduction/tutorial_3_non_linear_search.py` sets

```python
model.centre = af.UniformPrior(lower_limit=0.0, upper_limit=100.0)
model.normalization = af.UniformPrior(lower_limit=0.0, upper_limit=10.0)
model.sigma = af.UniformPrior(lower_limit=0.0, upper_limit=10.0)

on a model that every subsequent fit discards by calling af.Model(Gaussian)
again before search.fit. The overrides therefore never apply, and the fits
silently run on the config/priors/gaussian.yaml defaults:

Parameter Truth Tutorial override Config default
centre 50.0 Uniform(0, 100) Uniform(0, 100) — identical
normalization 25.0 Uniform(0, 10) — excludes the truth LogUniform(1e-6, 1e6)
sigma 10.0 Uniform(0, 10) — truth sits on the boundary Uniform(0, 25)

So the block teaches a prior override that never applies and would break the fit
if it did. It also contradicts its own prose, which says "we are using
UniformPriors in this tutorial due to their simplicity" while every fit
actually runs a LogUniformPrior on normalization.

The trap that decides the design

Making the overrides apply changes the parameter space every search explores,
and the first, uninitialised MLE fit in this tutorial is supposed to fail —
that failure is the pedagogical hinge of the whole MLE section ("it can get stuck
in a local maximum"). Today it fails reliably because normalization is
LogUniform(1e-6, 1e6), so a random draw is essentially never near 25. Narrow
that to a Uniform band centred on plausible values and a random draw lands near
the truth often enough that the fit may start succeeding, which would make the
tutorial flaky in the opposite direction and silently destroy the lesson.

Decision rule (measured, not assumed): prefer the version that makes the
overrides real and meaningful, but only if the uninitialised first MLE fit still
fails 10 out of 10 runs. If it does not, keep the explored parameter space
semantically equal to the config defaults and make the block honest instead.


</details>

Activity

  1. Jammy2211 commented on Sep 14, 2026

    @Jammy2211
    ContributorAuthor

    Shipped

    Merged in #60.

    • Math sweep finished — tutorial 6 (4 display + 28 inline), tutorial 7 (1), optional Bayesian formalism (7 + 40). Tutorial 5's single \[ was correctly left: it is a LaTeX row-break inside a Python string, not prose.
    • Tutorial 3's __Priors__ block was dead and wrong — set on a model all five fits discarded, with normalization Uniform(0, 10) excluding the truth 25 and sigma Uniform(0, 10) putting the truth 10 on the boundary. Five redundant af.Model(Gaussian) re-creations removed so it now takes effect.
      The values deliberately restate the config defaults rather than narrowing them, because narrowing breaks the tutorial: af.LBFGS() initialises with InitializerBall(0.49, 0.51) — a tight ball on each prior's centre — so normalization = Uniform(0, 50) centres on exactly 25.0, the truth, and the fit that teaches 'MLE gets stuck' succeeded 10/10. Measured: config priors fail 10/10, narrowed candidate succeeds 10/10, restated defaults fail 10/10.
    • MCMC budget — the prose claimed the uninitialised Emcee fit succeeded; it did so 11 times in 20. Both fits now nwalkers=20, nsteps=500: 80/80, median 9.8s. Walkers, not steps, were the lever. CI cost unchanged (PYAUTO_TEST_MODE: "2" skips the sampler).
    • self.noise_map — Analysis.log_likelihood_function in tutorials 3, 4 and 5 read a module-level global; the class readers are told to copy raises NameError outside the tutorial.

    Smoke 18/18, CI green on all 7 checks. The PyAutoFit bug found while calibrating is fixed in PyAutoLabs/PyAutoFit#1629.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions