perf: Replace statsmodels OLS+f_test with numpy/scipy in Granger F-test - #46
Merged
Conversation
- NODE_SIZES [5,15] (was [10,20,40,50]) - Remove SMALL_NODES and continuous_data_small (unified) - Unified _RECON_METRICS = lc/rc/gc/gv - ts_len 500 → 200 in isolated metric benchmarks - N_TESTS 5 → 2 (mi/te benchmarks) - Drop cop/random_patterns from isolated metrics - Drop continuous_data_20 fixture - Workers test simplified to [1, 2]
Signed-off-by: Carlson Büth <commit@cbueth.de>
Signed-off-by: Carlson Büth <commit@cbueth.de>
Merging this PR will regress 1 benchmark
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ❌ | test_ordinal_patterns |
9.4 ms | 23.1 ms | -59.4% |
| ⚡ | test_reconstruct[nodes10-gc] |
300.4 ms | 143.4 ms | ×2.1 |
| ⚡ | test_reconstruct[nodes5-gc] |
67.5 ms | 32.4 ms | ×2.1 |
| ⚡ | test_granger_f_test |
3.3 ms | 1.7 ms | +95.73% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing perf/fast-granger-f-test (a9c0fa6) with main (d9c47e1)
cbueth
force-pushed
the
perf/fast-granger-f-test
branch
from
July 29, 2026 15:51
19fa9b4 to
c2848ba
Compare
- Replace statsmodels OLS+f_test with numpy/scipy normal-equations F-test in granger.py - Remove _granger_fast.py (5 unused F-test variants + benchmark helpers + dead code) - Keep only the single optimal implementation (same API: gt_single_lag, gt_multi_lag) - Remove statsmodels as dependency from the Granger hot path (still used for design matrix) - Merge tests from test_fast_granger.py into test_granger.py, remove test_fast_granger.py - Keep public API fully stable, no user-facing changes required
cbueth
force-pushed
the
perf/fast-granger-f-test
branch
from
July 30, 2026 07:44
c2848ba to
a9c0fa6
Compare
Owner
Author
|
The only regression comes from runner changes, and the 2.1 speedup is underestimated, as the system calls on this other runner shadow the speedup. |
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.
This replaces the statsmodels OLS + F-test path in Granger causality with a numpy/scipy linear algebra implementation that produces identical p-values.
The old code called
statsmodels.OLS.fit()+f_test()for every lag of every pair. As example, that would be 51,450 OLS regressions for a 50-node network with 21 lags. Each statsmodels call has heavy overhead: class instantiation, data validation, result object tree construction. The new implementation computes the same F-statistic and p-value usingnp.linalg.solve/scipy.linalg.lstsqdirectly.71ed1a4 contains 6 variants of the F-test (normal equations, QR, Cholesky, scipy.lstsq, two-regression QR/Cholesky), all were benchmarked against statsmodels. The fastest variant (normal equations) gives about 8x speedup with p-values matching to machine epsilon 🥳 carrying over to the network reconstruction funcationality.
Statsmodels is still used for design matrix construction (
lagmat2ds) but the OLS fitting is gone from the central network reconstruction path. The oldgranger.pyis kept as-is, the new code lives in_granger_fast.pyand is wired in as the "gc" connectivity metric.Also includes a comprehensive test suite: p-value parity against statsmodels across grid of T/L values, causal lag detection, collinear data edge cases, constant time series, dispatch parity, and multi-lag optimal lag matching.