Skip to content

perf(markov): one linear solve in expectation_sensitivity -- 71x faster - #344

Merged
clonker merged 1 commit into
deeptime-ml:mainfrom
dexhunter:perf/expectation-sensitivity-single-solve
Nov 9, 2026
Merged

clonker merged 1 commit into
deeptime-ml:mainfrom
dexhunter:perf/expectation-sensitivity-single-solve

Conversation

@dexhunter

@dexhunter dexhunter commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR

  • This PR makes expectation_sensitivity in markov/tools/analysis/dense/_sensitivity.py compute its result without a per-state loop.
  • It computes the stationary distribution once and solves one square linear system with the observable as right-hand side.
  • Benchmark modelled on 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_sensitivity passes (2 passed).

What & Why

Before, expectation_sensitivity called stationary_distribution_sensitivity once 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 computes pi once and solves (T - I - 1 pi^T) phi = a once. Subtracting 1 pi^T makes 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 raises ValueError.

Results

Situation Before After Change
1 state 0.19 ms 0.16 ms no slowdown
2 states 0.41 ms 0.17 ms 2.4x faster
10 states 2.32 ms 0.18 ms 13x faster
30 states 10.94 ms 0.22 ms 50x faster
100 states 162.94 ms 0.53 ms 309x faster

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_distribution still run. test_expectation_sensitivity in both sensitivity test files compares the result with reference values.

Validation

  • Native tests: python3 -m pytest tests/markov/tools/analysis/test_sensitivity.py tests/markov/tools/analysis/impl/dense/sensitivity_test.py -k test_expectation_sensitivity gives 2 passed. Both full files also pass (18 passed).
  • Benchmark: chains of 30 to 100 states, the test matrices and edge cases (one state, zero observable, non-stochastic and disconnected input). The median time went from 245.08 ms to 3.44 ms. Three repeated runs gave time ratios of 0.0140, 0.0140 and 0.0136. Each run also checked that the outputs match the unchanged code to 1e-7 relative tolerance.
  • Reproduce one row of the table:
import numpy as np, timeit
from deeptime.markov.tools.analysis import expectation_sensitivity
T = np.random.rand(100, 100) + 0.1
T /= T.sum(axis=1, keepdims=True)
a = np.random.rand(100)
print(min(timeit.repeat(lambda: expectation_sensitivity(T, a), number=1, repeat=7)))

Checklist

  • Make sure to include one or more tests for your change (existing test_expectation_sensitivity tests already cover this function; no new test added)
  • Add yourself to AUTHORS

AI 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

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

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.03%. Comparing base (2a57dcd) to head (fb8afe3).

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              
Flag Coverage Δ
decomposition 48.84% <0.00%> (+<0.01%) ⬆️
markov 69.19% <100.00%> (-0.01%) ⬇️
misc 65.70% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@clonker clonker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice!

@clonker
clonker merged commit 6dd775b into deeptime-ml:main Nov 9, 2026
76 checks passed
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