fix: warn on disconnected RVs from centered/noncentered prior drift#966
Open
AlexanderFengler wants to merge 3 commits into
Open
fix: warn on disconnected RVs from centered/noncentered prior drift#966AlexanderFengler wants to merge 3 commits into
AlexanderFengler wants to merge 3 commits into
Conversation
Adds two validation hooks around the bambi model build: * A targeted check that flags user-supplied Normal group-specific priors with a nested `mu` hyperprior under `noncentered=True`. Under non-centered, bambi reparameterizes the term as `offset * sigma` and never references `mu`, so the `mu` hyperprior is created in the PyMC graph but left disconnected from the likelihood. The check is scoped to user-supplied prior keys to avoid false alarms from HSSM defaults. * A general post-build graph walk (`find_disconnected_free_rvs`) that surfaces any free RV not reachable from the observed RVs, acting as a safety net for future name-convention drift. Both checks emit warnings via the existing `hssm` logger; model construction is unaffected. `noncentered` remains a `**kwargs` passthrough to `bmb.Model`. Includes a new tutorial, `docs/tutorials/centered_vs_noncentered_basic_logic.ipynb`, that walks through the two parameterizations, the node-naming difference, and the disconnected-`mu` footgun with two fixes (switch to centered; or move the location prior onto the common `Intercept`). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
On macOS, notebook kernels launched from non-terminal contexts (some IDE integrations, GUI launchers) inherit only /etc/paths and miss /opt/homebrew/bin, so pm.model_to_graphviz raises ExecutableNotFound even when Graphviz is installed. Tutorial now prepends common locations to PATH only if `dot` is missing — no-op otherwise. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…p mu Adds a second user-prior check next to the existing disconnected-node warning. It fires when a user-supplied group-specific Normal prior has a non-trivial mu (hyperprior or non-zero scalar) and the same formula also contains a common Intercept. The linear predictor sees only Intercept + mu, so the two parameters are non-identifiable from the data and the posterior has a ridge along their anti-diagonal. The warning fires under both centered and non-centered parameterizations. HSSM defaults already use mu=0 for group_intercept_with_common, so this only triggers on user-written priors that explicitly carry a non-trivial group-level location. Scoping reuses the _user_specified_prior_keys snapshot. Tutorial updates: * New note after Fix 1 in centered_vs_noncentered_basic_logic.ipynb explaining that switching to noncentered=False trades the disconnected-node problem for a statistical-level redundancy and flagging the new warning. * Rule-of-thumb section gains a bullet making mean-zero random effects explicit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
krishnbera
reviewed
May 21, 2026
| @@ -0,0 +1,1332 @@ | |||
| { | |||
Member
There was a problem hiding this comment.
krishnbera
approved these changes
May 21, 2026
| PriorMismatch( | ||
| parameter=param_name, | ||
| term=term_name, | ||
| reason=( |
Member
There was a problem hiding this comment.
we can explicitly suggest adding a prior on offset here?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a user supplies group-specific priors with nested hyperpriors (e.g. a Normal prior on
1|participant_idwhosemuis itself aPrior), HSSM/bambi can silently produce a PyMC graph with idle, disconnected nodes. Under the defaultnoncentered=True, bambi reparameterizes a group-specific Normal term asoffset * sigmaand never referencesmu— so a user-suppliedmuhyperprior is created as a free RV in the graph but never wired into the likelihood. Until now this happened silently.This PR adds two validation hooks and a tutorial.
src/hssm/param/parameterization_check.py::check_user_priors_against_parameterization): scoped to user-supplied prior keys, flags Normal group-specific priors with amuhyperprior when the effective parameterization is non-centered, and emits an actionable warning ("either passnoncentered=False, or move the location prior onto the commonIntercept…").find_disconnected_free_rvs): walks the PyMC graph aftermodel.build()and warns about any free RV that is not an ancestor of an observed RV — catches future name-convention drift, not just the known case.src/hssm/base.pyaround the existingbmb.Model(...)/self.model.build()block.noncenteredremains a**kwargspassthrough.docs/tutorials/centered_vs_noncentered_basic_logic.ipynbwalks through both parameterizations, the node-naming difference, the disconnected-node footgun, and two fixes.Severity: warn (not raise) — model still builds. The default
noncentered=Trueand HSSM behavior are otherwise unchanged.Test plan
uv run pytest tests/test_prior.py -v— 8/8 pass (5 new cases covering targeted warning, centered no-warning, defaults-skip, hand-built orphan detection, unit-level check)uv run pytest tests/ --ignore=tests/slow -x— 467/467 pass; no regressions from the new hook inHSSMBase.__init__uv run ruff checkclean on touched files (remaining ruff errors are pre-existing intests/test_prior.py)uv run mypy src/hssm/param/parameterization_check.py— clean🤖 Generated with Claude Code