⚡️ Speed up function solve_discrete_riccati
by 17%
#3
+65
−28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 17% (0.17x) speedup for
solve_discrete_riccati
inquantecon/_matrix_eqn.py
⏱️ Runtime :
108 milliseconds
→92.3 milliseconds
(best of29
runs)📝 Explanation and details
The optimized version achieves a 17% speedup through several key computational optimizations:
1. Batch preprocessing for gamma candidate selection:
Z_array = [R + gamma * BB for gamma in candidates]
andcn_array = [np.linalg.cond(Z) for Z in Z_array]
upfrontvalid_idx = [i for i, cn in enumerate(cn_array) if cn * EPS < 1]
before expensive operations2. Reduced solve() calls through strategic reuse:
solve(Z, N + gamma * BTA)
,solve(Z, B.T)
, andsolve(Z, N)
multiple timessolve_Z_BTA
,solve_Z_BT
,solve_Z_N
and reuses themsolve_IGH0_A0
,solve_IHG0_A0T
, etc. are computed once and reused3. Optimized main iteration loop:
A0 @ solve(I + (G0 @ H0), A0)
into intermediate steps (GH0 = G0 @ H0
,I_GH0 = I_k + GH0
, etc.)4. Early candidate filtering and error handling:
cn_array
instead of recalculatingnp.linalg.cond(Z, np.inf)
The optimization is particularly effective for test cases with multiple candidates to evaluate (20-25% speedup in most basic/edge cases) and scales well to larger systems (55.6% speedup for 100x100 matrices), demonstrating that the computational savings compound with problem size.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-solve_discrete_riccati-mgfzrs5b
and push.