[multi_hyper] Conversion to JAX and stylesheet check - #657
Conversation
|
📖 Netlify Preview Ready! Preview URL: https://pr-657--sunny-cactus-210e3e.netlify.app (f4a251b) 📚 Changed Lecture Pages: multi_hyper |
|
🤖 Status note for a future session — from a maintainer investigation on 2026-07-08 into why open-PR previews 404. Context only, not instructions. Netlify preview: https://pr-657--sunny-cactus-210e3e.netlify.app/ currently returns 404. Why previews are down (repo-wide findings)1. This branch is stale — 166 commits behind 2. The arviz failure was a red herring — do NOT pin arviz or rewrite plotting. A 2026-07-07 rebuild also failed in Recommended first step for this PRUpdate this branch to This PR touches: |
Per the review on #657, JAX is not justified in this lecture. The sampling is a single call to np.random.Generator.multivariate_hypergeometric, a library primitive with no JAX equivalent, so the conversion hand-rolled it as a lax.scan over individual draws — the "rewriting a library call by hand" anti-pattern in the style guide. Measured on CPU, which the branch forced via jax_platform_name, that sampler is slower than NumPy at every size tested: 87x at 10k, 4.3x at 1M, and 2.7x at the 10M the lecture actually uses. Reimplementing the PMF with jax.scipy.special.gammaln also ran in float32, returning 0.079575970768929 for the first example where the prose two lines above states the exact value 0.079575596816976. The branch also added `!pip install jax`, which the style guide forbids because it can install jax[cpu] rather than the configuration we build with. Kept from the branch, on top of main's NumPy implementation: - vectorize the covariance matrix as Σ = n(N-n)/(N-1) · (diag(p) - outer(p, p)), replacing the nested loop and its trailing special case for the last diagonal entry; checked against the old code, max absolute difference 2.2e-16 - section headings to sentence case, the lecture title keeping title case - `{code-cell} python3` and `{code-cell} ipython` -> `ipython3` Co-Authored-By: HumphreyYang <humphreyyang30@gmail.com> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Thanks @HumphreyYang. I've reworked this branch rather than let it sit — here's the reasoning. Dropping the JAX conversion. The sampling in this lecture is a single call to The hand-rolled version is correct — sample means match theory and every row sums to
There was also a subtler consequence. Reimplementing the PMF in log space with where the prose two lines above states the exact value Separately, What I kept. Your vectorization of the covariance matrix is a real improvement and doesn't need JAX at all, so it's now applied on top of main's NumPy implementation: p = K_arr / N
Σ = n * (N - n) / (N - 1) * (np.diag(p) - np.outer(p, p))That replaces the nested loop and its easily-missed trailing Also kept: the section headings in sentence case, and I left the emphasis as it is on I also merged current |
📖 Netlify Preview Ready!Preview URL: https://pr-657--sunny-cactus-210e3e.netlify.app Commit: 📚 Changed LecturesBuild Info
|
|
Thanks @HumphreyYang for working on this. Comments above are by Claude. Modifications are by Claude after reading the latest style guide. I'm doing whatever it says because I'm scared of making it angry. I'll merge when green. |
✅ Translation sync completed (zh-cn)Target repo: QuantEcon/lecture-python.zh-cn
|
This PR converts the simulation code to JAX and perform stylesheet check!