Skip to content

fix(probas,#8081): PyMC-6-Debugging §7 — exercise debug loop + make 'correct' model correct - #8366

Merged
jsboige merged 1 commit into
mainfrom
fix/pymc6-debug-loop-8081
Jul 24, 2026
Merged

jsboige merged 1 commit into
mainfrom
fix/pymc6-debug-loop-8081

Conversation

@jsboige

@jsboige jsboige commented Jul 24, 2026

Copy link
Copy Markdown
Owner

fix(probas,#8081): PyMC-6-Debugging §7 — exercise the debug loop + make the "correct" model actually correct

Grain: MED/notebook-python — lane myia-po-2025:CoursIA — prev: MED/notebook-dotnet (Z3-06-Csharp Prong-B #8353).

Firsthand audit of the PyMC twin of Infer-6 (whose §7 gap was backfilled by po-2026 in #8349). Same gap-class, two defects in PyMC-6-Debugging.ipynb §7 "Exemple guide : Debugger un Modèle":

Defect A — the "correct" example model was itself broken (doc-honesty)

Cell model_corrige ("Modele d'estimation bien specifie / déjà corrigé") used observed=50 — a single scalar observation. With n=1, sigma is unidentified → Neal's funnel → the committed output literally showed 573 divergences + R-hat > 1.01 + ESS < 100, while the prose (cell 33) claimed it showed "comment spécifier correctement un modèle". A debugging notebook teaching that 573 divergences = "well-specified" is an integrity bug.

Firsthand-verified (coursia-ml-training kernel, PyMC 5.28.5, random_seed=42, determinism confirmed): the single-obs model reproduces divergences (49 at 500 draws; 573 at the notebook's 2000/4). A well-specified version with n=30 observations samples clean: 0 divergences, R-hat 1.0, ESS 4684, moyenne 25.14 (hdi [22.9, 27.3]), sigma 6.44. Fix: cell 34 now uses n=30 simulated data → the "bien spécifié" claim is true.

Defect B — the debug loop was described, not exercised

Cell 36 had the buggy model commented out ("# DECOMMENTEZ POUR VOIR LES PROBLEMES") and immediately ran the corrected version — so the student never sees the buggy model fail. The debug loop (cassé → diagnostic → fix) was prose, not demonstrated.

Fix: cell 36 now runs the buggy model and shows its real symptom, then the corrected model, side by side:

Rappel : les donnees (n=30) ont une moyenne empirique de 25.1.

-- Modele bugge (3 erreurs) --
  Posterior de la moyenne : m ~ 0.001
    --> CONTRADICTION : la donnee vaut ~25.1, mais la posterior reste a ~0.
        Le prior Normal(0, sigma=0.01) est si concentre qu'il ECRASE la donnee.
  Divergences NUTS : 0  -- attention, 0 divergence ne signifie pas 'modele correct' !

-- Modele corrige --
  Posterior de la moyenne : moyenne ~ 25.1  (recovere la donnee)
  Divergences : 0

=> Le reflexe debug : comparer la posterior a la DONNEE, pas seulement aux divergences.

The deeper pedagogical finding (mirrors Infer-6 #8349): the bug is NOT caught by divergences — both models have 0 — but by comparing the posterior to the data (m≈0 vs data≈25). A hyper-concentrated prior is a silent trap: the sampler runs cleanly yet the posterior ignores the data. This is the reflex the §7 debugging section exists to teach.

Validation

  • Firsthand re-executed via nbconvert --execute (kernel coursia-ml-training, PyMC 5.28.5, matching the notebook's committed env): 0 errors, real captured outputs (stdout+stderr interleaved correctly).
  • H.3 validate_pr_notebooks.py origin/main <path>: 1/1 PASS (13 code cells).
  • Minimal diff (C.3): hybrid reconstruct from origin/main baseline + surgical replace of cells 33-37 (intro md + cell34 code + challenge md + cell36 code + analysis md). Verified by-id that only cells [33,34,35,36,37] changed — §8/§9 exercises + summary intact. ec preserved (34=11, 36=12; the pre-existing ec anomaly on cells 15/18 is in origin/main, untouched).
  • C.1: 0 violations (raise/assert False/1/0 scan clean).
  • LF + round-trip stable json.dumps(indent=1).
  • Diff: +133/−158, 1 file (the net negative = the old 573-divergence output + commented buggy model replaced by the exercised debug loop — legitimate §7 content replacement, not a regression; no exercise/solution stripped).

Context

PyMC twin of Infer-6 (#8349). Part of the #8081 fidelity audit — PyMC-6 verdict: gap was localized to §7 (backfilled here); the rest (§2.2 divergences, §3 NUTS-vs-ADVI, §4 ArviZ diagnostics, §5 priors, §6 checklist) is faithful and rich. See #8081, See #8052.

…l actually correct

Cell model_corrige used observed=50 (single obs) → sigma unidentified → committed output showed 573 divergences while prose claimed 'bien specifie'. Now n=30 obs: 0 divergences, R-hat 1.0, ESS 4684. Cell 36 had buggy model commented out ('DECOMMENTEZ POUR VOIR') → debug loop was prose not exercised. Now runs buggy model (shows prior crushes data, m~0 vs data~25, 0 div) then corrected. Deeper finding (mirrors Infer-6 #8349): bug NOT caught by divergences (both 0) but by comparing posterior to data. Hybrid reconstruct from origin/main, only cells 33-37 changed, H.3 1/1 PASS, C.1 0 violations.

Refs #8081. See #8052, #8349 (twin Infer-6 class).

Co-Authored-By: Claude-Code <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Golden-Set Execution (H.7 P3)

✅ 8/8 notebooks passed (certified reproducible)

Notebook Status Time
2.1-Workflow-ML.ipynb ✅ SUCCESS 4.9s
2.2-Descente-de-gradient.ipynb ✅ SUCCESS 4.4s
2.3-Regression-lineaire-logistique.ipynb ✅ SUCCESS 4.1s
2.4-Arbres-Forets-Ensembles.ipynb ✅ SUCCESS 5.0s
Search-1-StateSpace.ipynb ✅ SUCCESS 4.0s
SL-1-LogicalLearning.ipynb ✅ SUCCESS 3.1s
rl_4_multi_armed_bandits.ipynb ✅ SUCCESS 25.7s
GameTheory-4c-NashExistence-Python.ipynb ✅ SUCCESS 3.6s

Pinned lockfile: scripts/notebook_tools/golden_set.lock.txt (H.7 P3, axe A #4208)

@github-actions

Copy link
Copy Markdown
Contributor

Notebook PR Validation: PASS

  • Notebooks checked: 1
  • Code cells validated: 13
  • Result: All passed

Checks: H.1 (no errors), H.3 (execution_count), C.1 (no banned patterns)
Non-Python kernels (.NET/Lean): C.1 + errors only (execution_count advisory)
QuantConnect notebooks: C.1 + errors only (require QC Cloud for execution)

@clusterManager-Myia clusterManager-Myia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NanoClaw] structural review (glm-5.2 budget: file-list + 1 load-bearing notebook, base↔head content-hash diff + full read of the 5 replaced cells, no full patch)

CoursIA #8366 fix(probas,#8081): PyMC-6-Debugging §7 — exercise debug loop + make 'correct' model correct
head 3dc173a53c86, base f79034711e75, +133/-158, 1f PyMC-6-Debugging.ipynb (Python/PyMC). Author jsboige ≠ my identity → 0 self-review conflict. 0 secret, 0 leak, 0 CJK (whole-notebook scan clean).

LGTM deep — this fixes a real latent bug AND upgrades the pedagogy. Two things in one PR, both right:

1. The title's claim is accurate: the base "correct" model was actually broken. Compared the 5 removed base cells vs the 5 new head cells (content-hash diff, cell count 43→43):

  • BASE[34] (the "modèle corrigé" reference) used observed=50 — a single scalar, not the n=30 array the surrounding prose described. Estimating both moyenne and sigma from one observation makes σ under-identified → exactly the funnel/NUTS-divergence pathology the notebook's own §2.2 warns against. The base markdown BASE[37] even (incorrectly) framed this as fine: "σ estimé à partir d'une seule observation." That's teaching the wrong reflex.
  • HEAD[34] fixes it: observed=donnees where donnees = rng_obs.normal(loc=25, scale=8, size=30) — 30 observations → σ identifiable. With an explicit in-code comment explaining why n=30 (single obs → σ under-determined → funnel). Output recovers the data-generating params: moyenne 25.136±1.173, σ 6.443 (slight downward bias vs true 8 = expected n=30 sampling variance), 0 divergences, R̂=1.0, ESS 4684. ✓ Sound.

2. The debug exercise is made observable. BASE[36] had the buggy model commented out ("DECOMMENTEZ POUR VOIR") — so a student running the notebook never saw the failure. HEAD[36] makes the buggy model actually run and print its symptom: m ~ 0.001 (the Normal(0, sigma=0.01) prior crushes the data which is ~25) while reporting 0 divergences — the "silent trap" the closing lesson needs. Then it runs the corrected model showing moyenne ~ 25.1. The pedagogical pivot ("compare posterior to the DATA, not to divergences") lands because both models genuinely execute.

Verification: ec 11→12→13 (the PR's cells) clean and monotonic; the ec=5 gap and ec=6 duplicate earlier in the notebook are pre-existing (cells [2]-[31] unchanged by this PR), not introduced here. No new ec-doublon. HalfNormal-vs-Normal for σ reasoning correct (σ>0 constraint). Prior-shrinkage mechanics correct (Normal(0,0.01) is ~±0.02 → data at 25 is ~1250σ away, posterior stays at prior). rule 6 respected (the 5 replaced cells are the changed region; surrounding 38 cells byte-identical).

▼ NIT — chain-count inconsistency in the debug demo. HEAD[36] corrected model runs chains=2 (draws=2000) while the HEAD[34] reference runs chains=4. PyMC prints the "We recommend running at least 4 chains" warning, visible in the output of [36]. Minor (the debug point doesn't need 4 chains), but a student copying the corrected snippet gets a noisier R̂. Consider chains=4 in [36] too, or a one-line note that the debug demo deliberately uses fewer chains for speed. Non-blocking.

△ Cosmetic: the corrected-model code block is duplicated between [34] (reference) and [36] (debug-then-fix) — intentional for a self-contained exercise cell, just noting the repetition in case a helper refactor is ever wanted.

Net: a genuine correctness fix (single-scalar observed → n=30 array) wrapped in a better exercise (silent-failure made visible). The kind of change that earns its LGTM. Mergeable.

@jsboige
jsboige merged commit 9f41c6b into main Jul 24, 2026
28 of 30 checks passed
jsboige added a commit that referenced this pull request Jul 24, 2026
…clear fleet-red gate) (#8372)

The Twin parity audit gate (#8185 = BLOCKING) was fleet-RED on every open PR
because 15 registered twin pairs drifted on origin/main: merged PRs that modify
a registered twin notebook (citations, Prong-B enrichment, determinism seed,
metadata.cost rollout, nbformat cell-id fix) did not rebaseline their
last_audit.{python,csharp}_sha, so the recorded SHA lagged the merged blob.

G.8 audited resync (NOT rubber-stamped) — every drift traces to a NAMED merged
PR (verified via git log -- <notebook> between recorded and current blob):
  - Coordinated twin updates (parallel PRs both sides): Z3-06 (#8328+#8353),
    ML-7 (#8284 both), Probas-6 (#8366+#8349), Probas-8 (#8330+#8355),
    Probas-11/#8325 both, Probas-13 (#8344+#8360), Probas-15/16/17 citation twins.
  - Framework-specific infra (asymmetric, benign): Probas-3/4/12 C#-only =
    #8323 metadata.cost rollout (PyMC twins have no such frontmatter yet);
    CSP-3 PY-only = #8252 OR-Tools CpSolver seed (C# twin uses Choco Solver,
    deterministic default search — different lib, no parity break, spot-checked);
    Search-3 PY-only = #8324 IDA* prose reframe + #7252 FR accents.
Independently re-confirms the G.1 firsthand claim (jsboige c.03:58, po-2023
c.04:08 merge-sim) on the CURRENT origin/main state (8743caf): all 15 are
stale-sha-only, ZERO content drift that broke semantic parity.

Surgical method (NOT check_twin_parity.py --update): --update yaml.safe_dump's
the ENTIRE curated YAML -> 1350-line churn + strips the header comment block
(measured). Instead: context-free exact string replacement of each old->new
40-char git blob SHA (all 25 old shas unique, verified -> unambiguous). Result:
+25/-25 = 50 lines, ONLY python_sha/csharp_sha fields changed; header comments,
known_differences prose, indentation, LF line-ending byte-preserved (GATE A-C).

Proof: check_twin_parity.py --check -> OK=39 DRIFT=0 exit 0 (CI-green).

See #8264 (resync of existing drifts; 'going-forward enforcement' = documented
precedent #8057, separate concern). See #8057, #8185.

Co-authored-by: Claude <noreply@anthropic.com>
@jsboige
jsboige deleted the fix/pymc6-debug-loop-8081 branch September 2, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants