Repository navigation
bayes_nonconj: declare the truncated log-normal prior's (0, 1] support (numpyro 0.22) - #1065
Conversation
…t (numpyro 0.22) numpyro 0.22.0 validates distribution arguments by default. The prior was a TransformedDistribution(TruncatedNormal(high=0), ExpTransform()), whose declared support is (0, ∞), so NUTS initialisation could propose θ > 1 and dist.Binomial(n, θ) raised "BinomialProbs distribution got invalid probs parameter". Declaring interval(0, 1) keeps θ in range. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The change is minimal, localized to the prior construction, and directly addresses the reported NumPyro 0.22 support/initialization failure mode.
Review effort: Lite
Findings: None
What changed in this PR
This PR fixes execution failures in the bayes_nonconj lecture under NumPyro ≥ 0.22 by making the truncated log-normal prior correctly declare unit-interval support, so inference initialization doesn’t propose invalid binomial probabilities.
Changes:
- Update
truncated_lognormal()to return a smallTransformedDistributionsubclass that setssupport = constraints.interval(0.0, 1.0). - Add a brief inline comment explaining why the explicit support declaration is needed.
| File | Description |
|---|---|
| lectures/bayes_nonconj.md | Ensures the truncated log-normal prior advertises unit-interval support so NUTS/VI initialization won’t produce θ > 1 under NumPyro 0.22 validation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
📖 Netlify Preview Ready!Preview URL: https://pr-1065--sunny-cactus-210e3e.netlify.app Commit: 📚 Changed LecturesBuild Info
|
✅ Translation sync completed (zh-cn)Target repo: QuantEcon/lecture-python.zh-cn
|
Since numpyro 0.22.0 (PyPI, 2026-09-18),
bayes_nonconjfails wherever it is executed. The lecture installs numpyro unpinned, and 0.22.0 validates distribution arguments by default.truncated_lognormal()returned aTransformedDistribution(TruncatedNormal(high=0), ExpTransform()), whose declared support is (0, ∞) rather than (0, 1], so NUTS initialisation could propose θ > 1 anddist.Binomial(n, θ)raisedValueError: BinomialProbs distribution got invalid probs parameter.atmcmc_ln = run_nuts(binomial_model, prior_ln, k, n).This already broke this repo's weekly cache build: run 35557364600 (2026-09-21, the first after the release) failed with
bayes_nonconjas the only failing notebook. The 2026-09-22 publish still went green, presumably because it reused an earlier execution cache.Change.
truncated_lognormal()now returns a smallTransformedDistributionsubclass whosesupportisconstraints.interval(0.0, 1.0), with a two-line comment saying why. Nothing else in the lecture changes. Two alternatives were ruled out (details in the linked comment):init_strategy=init_to_mediangets NUTS through but the later SVI/AutoNormalcell fails with the same error, and pinningnumpyro<0.22in the!pip installcell would downgrade the shared environment for every later numpyro lecture in the build.Verification. I executed the whole lecture (jupytext →
jupyter nbconvert --execute) in clean Python 3.13 venvs with jax 0.11.2 and arviz 1.3.0:main(unfixed)BinomialProbs … invalid probs parameteratrun_nuts(…, prior_ln, …)main(unfixed)The log-normal posterior is statistically unchanged:
mcmc_lnθ mean 0.4506 / sd 0.101 before, 0.4532 / sd 0.102 after (sampling noise from the new unconstrained parameterisation), with no draw above 1. The fixed version gives identical draws under 0.21.0 and 0.22.0. The PR preview should show the posterior plots rendered.Found while diagnosing QuantEcon/actions#159; full write-up in this comment. This PR leaves the lecture's build time alone: that is QuantEcon/actions#196.
Assisted-by: Claude Code (Claude Opus 5.5)
🤖 Generated with Claude Code