Skip to content

Problem types: Phase 1's harness, in NumPy, Numba and JAX - #24

Merged
mmcky merged 2 commits into
mainfrom
problems-phase1
Nov 1, 2026
Merged

mmcky merged 2 commits into
mainfrom
problems-phase1

Conversation

@mmcky

@mmcky mmcky commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Phase 1 of #5's harness, in a new problems/ folder: P1, P3, P4 and P7, each written in NumPy, Numba and JAX and timed as used across problem sizes. The design is recorded on #5. Nothing has run on a measured machine yet. The platform runs wait for qe-wharton1-gpu to be free and for a plan for the M6 to be agreed.

Problem Model, from Sweep
P1 vfi, value function iteration lecture-jax's opt_savings_1 wealth grid, 100 to 1,500, float64
P3 egm, the endogenous grid method lecture-jax's ifp_egm_jax savings grid, 200 to 50,000
P4 wealth, Monte Carlo lecture-jax's wealth_dynamics_jax households, 1,000 to 10,000,000
P7 kalman, a sequential recursion precision/'s Kalman filter 1 to 10,000 filters batched with vmap; 2 to 200 states

The backends are numpy, numba (parallel=False), numba-parallel, jax-cpu and jax-gpu.

What's in it

File What it does
problems/spec.py The problems, sweeps, sizes, precisions, memory estimates and answer checks. It imports neither JAX nor Numba
problems/vfi.py, egm.py, wealth.py, kalman.py Each problem's shared inputs, one builder per stack, and the sample of the answer that is compared between cells
problems/run_cell.py Times one cell in a fresh process, importing only its own stack: cold time (for JAX the backend start, compile and first run; for Numba the first call), then warm runs, with the result dtype asserted
problems/run_suite.py The driver, built like precision/run_matrix.py. Each size runs every backend before the next size starts, and once a backend times out its larger sizes are skipped
problems/summarise.py Markdown tables of warm and cold time by size and backend, with the fastest of each. It flags answers that differ from NumPy's in float64, and iteration counts that differ
problems/constraints.txt JAX 0.12.0.1, NumPy 2.5.4, SciPy 1.18.1, Numba 0.68.1, llvmlite 0.50.0
precision/env_info.py Now also records numba, llvmlite and NUMBA_ settings

Checked on qe-mini1, as development runs rather than measurements

  • Every CPU backend in both precisions at the quick sizes, 36 cells: every answer agrees with NumPy's in float64. P1 and P3 match exactly, with the same iteration counts (572 and 2,192), P7 matches to rounding, and P4's shares are within their binomial error.
  • A pilot at larger sizes set the sweeps, so that the full CPU suite takes about 1.5 h on the M6. On qe-wharton1-gpu, with the card, I estimate 2.5 to 3 h, since its single-threaded cells will be slower.
  • Three things the pilot found, now fixed:
    • Numba promotes a float32 array to float64 when it meets a Python float, and @ then fails on the mixed dtypes. Each Numba version now takes its constants in the cell's precision.
    • P3's parallel Numba version opened a parallel region for every income state.
    • Numba's scalar np.interp searches the grid afresh for every point, and ran 7× slower than NumPy at 20,000 points. It now interpolates each column in one call, as NumPy does.

Not tested: jax-gpu, since there's no GPU here. It runs the same JAX code as jax-cpu, under precision/'s GPU environment settings, and its first run will be on the TITAN RTX.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WT8bMbJUx63ms5Evcjx1Un

A new problems/ folder: P1 (value function iteration), P3 (the endogenous
grid method), P4 (Monte Carlo) and P7 (a Kalman filter), each with a
lecture's model and written in NumPy, Numba and JAX, timed as used in a
fresh process per cell on numpy, numba, numba-parallel, jax-cpu and
jax-gpu. Each solve runs to the lecture's tolerance, records its
iterations, and is checked against NumPy's answer in float64.
P7 sweeps the work in each step: filters batched with vmap, and the
state size.

precision/env_info.py now records Numba's version and NUMBA_ settings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMbJUx63ms5Evcjx1Un

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

An unresolved finding remains, flagging the captured T_START timestamp in problems/run_cell.py as unused dead code that should be removed to avoid confusion.

1 open issue
What changed in this PR

This PR adds Phase 1 of the problem-type benchmark harness from issue #5, in a new problems/ folder. It implements four QuantEcon problem types (P1 value function iteration, P3 endogenous grid method, P4 Monte Carlo wealth dynamics, P7 Kalman filter), each written idiomatically in NumPy, Numba and JAX, and timed across problem sizes and backends (numpy, numba, numba-parallel, jax-cpu, jax-gpu). The design closely mirrors the existing precision/ harness: one fresh process per cell, cold/warm times kept apart, result dtype asserted, and every answer checked against the NumPy float64 baseline. No measured runs are included yet.

Changes:

  • New problems/ suite: spec.py (JAX/Numba-free problem catalog), one module per problem (vfi, egm, wealth, kalman) with shared inputs + per-stack builders, plus common.py (Tauchen chain, answer sampling).
  • New driver/runtime: run_cell.py (times one cell in isolation), run_suite.py (per-size backend sweeps with timeout/memory skipping), summarise.py (markdown tables + disagreement/iteration flags), and pinned constraints.txt.
  • Documentation and env plumbing: problems/README.md, a top-level README entry, and precision/env_info.py extended to record numba/llvmlite versions and NUMBA_ flags.

I verified that the three stacks for each problem are mathematically consistent (VFI Bellman indexing, EGM interpolation/expectation, Kalman gain/log-likelihood shapes, wealth Kesten update), that dtype is preserved across float32/float64 paths, and that the harness wires model/BUILDERS/sample and result-row keys together correctly. The only concrete issue found is minor (dead code).

File Description
problems/​spec.py Problem catalog (sizes, sweeps, precisions, memory estimates, answer checks); JAX/Numba-free for planning.
problems/​common.py Shared Tauchen discretisation and fixed-position answer sampling.
problems/​vfi.py P1 value function iteration in NumPy/Numba/JAX.
problems/​egm.py P3 endogenous grid method; column-wise interp in Numba to match NumPy.
problems/​wealth.py P4 Monte Carlo wealth dynamics; shared aggregate path, per-stack shocks.
problems/​kalman.py P7 batched Kalman filter log-likelihood; batch/state sweeps.
problems/​run_cell.py Times one cell in a fresh process; asserts result dtype. Unused T_START leftover flagged.
problems/​run_suite.py Driver: per-size backend sweeps, memory/timeout skipping, JSONL output.
problems/​summarise.py Markdown tables of warm/cold times; flags answer and iteration disagreements.
problems/​constraints.txt Pinned jax/numpy/scipy/numba/llvmlite releases for cross-platform comparability.
problems/​README.md Documents problems, backends, running, method and output.
README.md Adds problems/ to the repo structure and a category section.
precision/​env_info.py Records numba/llvmlite versions and NUMBA_ env flags.

Review effort: Balanced

Comment thread problems/run_cell.py Resolved
Copilot's review: import_s is timed around the stack's import, and the
driver's process_s covers the whole process, so the start time was unused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMbJUx63ms5Evcjx1Un
@mmcky
mmcky merged commit 5fbf033 into main Nov 1, 2026
@mmcky
mmcky deleted the problems-phase1 branch November 1, 2026 21:53
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