Skip to content

Problems: P5, the stationary distribution of a savings policy (#47) - #71

Merged
mmcky merged 2 commits into
mainfrom
2026-11-20-p5-stationary
Nov 20, 2026
Merged

mmcky merged 2 commits into
mainfrom
2026-11-20-p5-stationary

Conversation

@mmcky

@mmcky mmcky commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Phase 2's first problem type, for #5: P5, the stationary distribution of a Markov chain, as designed in #47 (revised and approved on 2026-11-20).

What it adds

  • problems/stationary.py: the household and prices of lecture-jax's aiyagari_jax, in NumPy, Numba and JAX, with two sweeps.
    • dense, the lecture's method: Howard policy iteration's index policy, the transition over every (assets, income) state built as a dense matrix, and the lecture's solve of (I − Pᵀ + 𝟏𝟏ᵀ) ψ = 𝟏. Assets 100 to 5,000.
    • sparse, Young's lottery over an EGM policy: each state sends its mass to the grid points either side of its savings, and power iteration runs until the L1 change is below 1e-10 in float64 or 1e-6 in float32. Assets 1,000 to 10,000,000.
  • problems/spec.py: P5's entry, between P4 and P7, with its sizes, memory estimate and the usual check against NumPy's float64, applied to the cumulative distribution of assets (stationary.sample).
  • problems/README.md: P5's row in the problems table and a note on what it times. The note on P7's optional sweeps no longer says a default run is Phase 1's, since a default run now includes P5.

Each cell's policy is computed by NumPy in float64 before the cell starts, so the cell times building the transition, finding the distribution and summing out income.

dense sparse
NumPy The matrix built in place, then scipy.linalg.solve, since np.linalg.solve computes in float64 even for float32 arrays A scipy.sparse CSR matrix of the transition's transpose
Numba The matrix built in place in a prange loop, then np.linalg.solve The same CSR, with unsigned indices, built by a counting sort, and a prange loop over its rows. The L1 change is summed in float64, since a float32 sum of millions of tiny terms stops changing
JAX The lecture's compute_asset_stationary, under jit jax.ops.segment_sum inside lax.while_loop

An independent review found five defects in the first commit, all fixed in e59ded5; the comment below lists them.

Checked on qe-mini1, CPU backends only

These were development runs, not measurements, so their rows aren't committed.

  • --quick: every cell ran on numpy, numba, numba-parallel and jax-cpu, in both precisions, and summarise.py found every answer within tolerance of NumPy's float64.
  • Dense 2,000 and sparse 1,000,000, on the same four backends in both precisions: every answer agreed, and every stack took the same number of iterations in each precision.
  • The largest sizes, dense 5,000 in float64 and sparse 10,000,000 in both precisions, on numpy and jax-cpu: both ran well within the 900 s cell limit, and agreed.
  • After the fixes, quick sizes, dense 2,000 and sparse 1,000,000 on all four backends in both precisions, and dense 5,000 in float32 on numpy, numba and jax-cpu: every float64 answer was within 1e-12 of NumPy's, and every float32 answer within 2.4e-5 on the new check, against its tolerance of 1e-3.

jax-gpu hasn't run, since qe-mini1 has no NVIDIA card. Its first run will be with Phase 2's measured runs, once P2, P6 and P8 are in, as planned.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WT8bMZbJB4j8pmPEYyJ8SG

mmcky and others added 2 commits November 20, 2026 21:58
Phase 2's first problem type, for #5, as designed in #47. The household
and prices of lecture-jax's aiyagari_jax, with two sweeps: `dense`, the
lecture's dense solve over every (assets, income) state, at 100 to 5,000
assets; and `sparse`, Young's lottery over an EGM policy, by power
iteration, at 1,000 to 10,000,000 assets. Each cell's policy is computed
by NumPy before the cell starts; the cell times building the transition,
finding the distribution and summing out income. Power iteration stops
at an L1 change of 1e-10 in float64 and 1e-6 in float32.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZbJB4j8pmPEYyJ8SG
- NumPy's dense cells solve with scipy.linalg.solve: np.linalg.solve
  computes in float64 even for float32 arrays, so NumPy's float32
  column was float64 arithmetic. M is built in place, as Numba's is,
  rather than through the lecture's four-dimensional construction.
- NumPy's sparse cells import scipy.sparse in setup, not in the timed
  function, so their cold time no longer includes the import.
- Answers are checked as the cumulative distribution of assets: the
  distribution is mostly zeros beside one atom at the borrowing
  constraint, so errors in its interior barely registered.
- Numba's CSR indices are unsigned, which spares a negative-index check
  on every access, and the first loop of its build runs in parallel.
- spec.py's sparse memory estimate covers NumPy in float32.

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

mmcky commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

An independent review of 572c168, and the fixes in e59ded5. Copilot couldn't review this PR, so a fresh reviewer read the diff against #47's design and the lecture, with no view of my reasoning. It ran small CPU sizes on qe-mini1. It found the algorithms correct: in both sweeps the three stacks compute the same distribution, and the float64 answers agree to 1e-14 or better. It also found five defects, all fixed in e59ded5:

  1. NumPy's float32 dense cells solved in float64. np.linalg.solve computes in double even for float32 arrays, then rounds the answer back to float32, so the dtype check couldn't see it. I confirmed that its float32 answer equals the float64 solve rounded. Fixed: scipy.linalg.solve, which calls LAPACK's float32 routine. At 2,000 assets NumPy's float32 cell now takes about a third of its float64 time, where before the two were about equal.
  2. NumPy's sparse cold time included import scipy.sparse. The import was inside the timed function, while Numba's and JAX's imports are recorded apart from the cold time. Fixed: the import is in setup. At 1,000 assets NumPy's cold time fell from about 0.06 s to 0.002 s.
  3. The answer check was mostly blind. The distribution of assets is mostly zeros beside one atom at the borrowing constraint, so errors in its interior barely registered against the atom. Fixed: stationary.sample takes the cumulative distribution, which runs from 0 to 1.
  4. Numba's sparse step was slowed by signed 64-bit indices, which Numba checks for negative values on every access, and its CSR build ran serially even in numba-parallel. Fixed: unsigned indices, and the build's first loop runs in parallel. Building M in place for NumPy's dense cells (point 1) also removes a cost the other stacks didn't pay: the lecture's four-dimensional construction took about 31% of NumPy's time at 2,000.
  5. spec.py's memory estimate was slightly low for NumPy's sparse cells in float32. Fixed: the estimate is raised.

Two findings concern code already on main, so they're left out of this PR, to be raised separately: summarise.py compares float32 iteration counts with NumPy's float64 count, so every float32 cell here is flagged by design, and P7's NumPy float32 cells also call np.linalg.solve, so they compute in float64 too.

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.

1 participant