Repository navigation
perf(markov): one linear solve in expectation_sensitivity -- 71x faster - #344
Merged
clonker merged 1 commit intoNov 9, 2026
Conversation
expectation_sensitivity looped over all states and called stationary_distribution_sensitivity for each one, which recomputed the stationary distribution and solved one least-squares problem per state. The sensitivity is linear in the observable, so compute the stationary distribution once and solve (T - I - 1 pi^T) phi = a once. Benchmark modelled on test_expectation_sensitivity: 245.08 ms -> 3.44 ms. Co-Authored-By: Aiden <aiden@weco.ai>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #344 +/- ##
==========================================
- Coverage 94.03% 94.03% -0.01%
==========================================
Files 141 141
Lines 11202 11201 -1
==========================================
- Hits 10534 10533 -1
Misses 668 668
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
TL;DR
expectation_sensitivityinmarkov/tools/analysis/dense/_sensitivity.pycompute its result without a per-state loop.test_expectation_sensitivity: 245.08 ms -> 3.44 ms, 98.6% faster (about 71x).python3 -m pytest tests/markov/tools/analysis/test_sensitivity.py tests/markov/tools/analysis/impl/dense/sensitivity_test.py -k test_expectation_sensitivitypasses (2 passed).What & Why
Before,
expectation_sensitivitycalledstationary_distribution_sensitivityonce for each of the M states. Each call computed the stationary distribution again and solved its own least-squares problem. Each call also built two M x M outer products, and the loop added them all.The sensitivity is linear in the observable
a. So the new code computespionce and solves(T - I - 1 pi^T) phi = aonce. Subtracting1 pi^Tmakes the matrix nonsingular, as in the fundamental matrix of the chain. The result is one outer product,outer(pi, -<pi, a> - phi). This is the same sum that the loop built, so the output stays identical up to rounding.stationary_distribution(T)still runs first, so a non-stochastic or disconnected matrix still raisesValueError.Results
Each row uses a random dense row-stochastic matrix and a random observable, timed on one pinned core with one BLAS thread.
Why this is safe and easy to maintain
The change replaces a 4-line loop with 3 short lines and a 2-line comment. The returned matrix has the same values, shape and dtype. The input checks in
stationary_distributionstill run.test_expectation_sensitivityin both sensitivity test files compares the result with reference values.Validation
python3 -m pytest tests/markov/tools/analysis/test_sensitivity.py tests/markov/tools/analysis/impl/dense/sensitivity_test.py -k test_expectation_sensitivitygives 2 passed. Both full files also pass (18 passed).Checklist
test_expectation_sensitivitytests already cover this function; no new test added)AUTHORSAI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/ISWekyKYwY6y8QfDIR-K2b1ICBB-cpJl