Skip to content

bayes_nonconj: declare the truncated log-normal prior's (0, 1] support (numpyro 0.22) - #1065

Merged
mmcky merged 1 commit into
mainfrom
bayes-nonconj-numpyro-022
Oct 5, 2026
Merged

mmcky merged 1 commit into
mainfrom
bayes-nonconj-numpyro-022

Conversation

@mmcky

@mmcky mmcky commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Since numpyro 0.22.0 (PyPI, 2026-09-18), bayes_nonconj fails wherever it is executed. The lecture installs numpyro unpinned, and 0.22.0 validates distribution arguments by default. truncated_lognormal() returned a TransformedDistribution(TruncatedNormal(high=0), ExpTransform()), whose declared support is (0, ∞) rather than (0, 1], so NUTS initialisation could propose θ > 1 and dist.Binomial(n, θ) raised ValueError: BinomialProbs distribution got invalid probs parameter. at mcmc_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_nonconj as 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 small TransformedDistribution subclass whose support is constraints.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_median gets NUTS through but the later SVI/AutoNormal cell fails with the same error, and pinning numpyro<0.22 in the !pip install cell 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:

Lecture numpyro Result
main (unfixed) 0.22.0 fails: BinomialProbs … invalid probs parameter at run_nuts(…, prior_ln, …)
main (unfixed) 0.21.0 17/17 cells, 8 figures
this PR 0.21.0 17/17 cells, 8 figures, ~18 s
this PR 0.22.0 17/17 cells, 8 figures, ~18 s

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

…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>
Copilot AI lite review requested due to automatic review settings September 25, 2026 05:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 small TransformedDistribution subclass that sets support = 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.

@github-actions

Copy link
Copy Markdown

📖 Netlify Preview Ready!

Preview URL: https://pr-1065--sunny-cactus-210e3e.netlify.app

Commit: 8f5f08e

📚 Changed Lectures


Build Info

@mmcky

mmcky commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

✅ Translation sync completed (zh-cn)

Target repo: QuantEcon/lecture-python.zh-cn
Translation PR: QuantEcon/lecture-python.zh-cn#288
Files synced (1):

  • lectures/bayes_nonconj.md

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