Skip to content

P5: Markov chains, starting with the stationary distribution of a savings policy (aiyagari_jax) #47

Open

Description

@mmcky

#5's Phase 2 lists P5, Markov chains: stationary distributions and simulation, by sparse and dense linear algebra. This is P5's design, from lecture-jax's aiyagari_jax: the stationary distribution of a household savings policy, which the lecture's equilibrium needs at every price it tries. That is how a heterogeneous-agent model's equilibrium aggregates its households.

P5 has two sweeps, one for each way of computing the distribution (revised 2026-11-20, with Matt's agreement):

  • dense, the lecture's method. Howard policy iteration chooses savings on the asset grid, so the policy is an index. The lecture builds the transition matrix over every (assets, income) state as a dense array and solves (I − Pᵀ + 𝟏𝟏ᵀ) ψ = 𝟏 for the distribution.
  • sparse, Young's (2010) lottery. It also covers a policy off the grid, such as EGM's: each state sends its mass to the two grid points either side of its savings choice, so the transition has at most four nonzeros a row. The distribution is found by power iteration. This is the method at research scale, where a dense matrix is out of reach.

Why

  • P3 times one household solve. An equilibrium needs much more. In aiyagari_jax a bisection on the price calls the household solve and the stationary distribution 20 to 33 times, depending on its tolerance.
  • The distribution can dominate. In QuantEcon/project-econ-on-ai-hardware's runs at 1M savings points, the distribution ran in SciPy on the host. It took most of the equilibrium's time wherever the solve ran on a GPU, and it ran several times slower on the Threadripper 2990WX than on the M6, with or without NUMA pinning (QuantEcon/project-benchmarks-jax#46).
  • The suite has nothing like it. The sparse step is memory-bound and, in SciPy, single-threaded. The dense step is one large LU factorisation, the regime where a GPU's dense linear algebra should win. Neither is measured by P1, P3, P4 or P7.
  • The two sweeps meet in the middle. Where their sizes overlap, they show at what size the lecture's method should give way to the sparse one.

The problem

Both sweeps use aiyagari_jax's household and prices as the lecture sets them: log utility, β = 0.96, income z ∈ {0.1, 1.0} with Π = [[0.9, 0.1], [0.1, 0.9]], assets on an evenly spaced grid from 1e-10 to 20, r = 0.01 and w = 1.

dense sparse
Input, computed by NumPy outside the timed cell The lecture's Howard policy iteration at the cell's grid size: an index policy EGM's savings policy at the cell's grid size, solved to a change below 1e-8 (147 iterations in the prototype). In NumPy on the M6 that takes about 2 s at 1M points and about 20 s at 10M
What is timed Building the dense transition matrix from the policy, the lecture's dense solve, and the marginal distribution of assets Building the lottery's transition from the policy, power iteration from the uniform distribution until the L1 change is below 1e-10 in float64 or 1e-6 in float32, and the marginal distribution of assets
Sweep Assets 100, 200, 500, 1,000, 2,000 and 5,000: 200 to 10,000 states. The lecture runs 200. At 5,000 the matrix is 800 MB in float64, and a cell holds about three such arrays Assets 1,000, 10,000, 100,000, 1,000,000 and 10,000,000: 2,000 to 20 million states
Precision float64 and float32 float64 and float32, each to a tolerance it can reach
Iterations recorded 1 The power iteration's

float32 in the sparse sweep (revised 2026-11-20, with Matt's agreement). In a NumPy prototype on the M6, with the lottery built in float32, a tolerance of 1e-10 was out of float32's reach: plain iteration took 4,122 iterations at 1M points and hadn't converged after 5,000 at 10M, and renormalising each step left the L1 change at about 1e-7. Below that floor, whether and when the loop stops depends on rounding, so its time would measure the iteration cap. At 1e-6, float32 converged in 157 to 192 iterations from 1,000 to 10M points, against 266 to 299 for float64 at 1e-10. So each precision stops at a tolerance it can reach, as P1 runs in float64 only for the same reason.

Stacks

dense sparse
NumPy The matrix built in place, then scipy.linalg.solve (revised 2026-11-20, after review: np.linalg.solve computes in float64 even for float32 arrays, and the lecture's four-dimensional construction cost NumPy alone about 31% of its time at 2,000 assets) A scipy.sparse CSR matrix of the transition's transpose, and its products, which are single-threaded
Numba The matrix built in a loop, then np.linalg.solve inside njit The same CSR built in a loop, and a loop over its rows for each product, with prange, so numba-parallel splits the rows across cores
JAX The lecture's compute_asset_stationary, under jit, on the CPU and the GPU Each state's mass scattered to its four destinations with jax.ops.segment_sum, inside lax.while_loop, on the CPU and the GPU

Checks

As for P1, P3 and P7, but on the cumulative distribution of assets, since the distribution itself is mostly zeros beside one atom at the borrowing constraint: every stack's, at 64 fixed positions, against NumPy's in float64 at the same sweep and size, within a relative 1e-8 in float64 and 1e-3 in float32. On that measure, float32's answers were within about 2.4e-5 of float64's in development runs, in both sweeps. (An earlier figure here, about 2e-7 for the dense sweep, came from np.linalg.solve, which had computed in float64.)

Later

Once P3, P5 and P6 (bisection) are in, the whole equilibrium could be a composite problem, to show how a stack's parts add up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions