perf: vectorize permutation test in gravity metric - #54
Merged
Conversation
Replace serial loop over permutations with a single rng.permuted call that processes all n_tests permutations in parallel via array ops. Add property test: constant ts2 yields p=0 since no permutation changes the gravity value. Speedup grows with n_tests — ~50x at n_tests=10_000.
Merging this PR will regress 2 benchmarks
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ❌ | test_detrend_method[z_score-p10] |
2.1 ms | 3.2 ms | -35.38% |
| ❌ | test_ordinal_patterns |
15.9 ms | 23.5 ms | -32.4% |
| ⚡ | test_reconstruct[nodes10-gv] |
309.4 ms | 112.4 ms | ×2.8 |
| ⚡ | test_reconstruct[nodes5-gv] |
67.6 ms | 24.6 ms | ×2.7 |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing perf/vectorize-gravity (b15c350) with main (875adcd)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #54 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 29 29
Lines 842 847 +5
Branches 151 151
=========================================
+ Hits 842 847 +5 🚀 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.
I replaced the serial
for-loop over permutations with a singlerng.permutedcall that generates alln_testspermutations as a 2-D array, then computes gravity in a single vectorized expression.Before serial loop:
After vectorised:
Speed grows with
n_tests, so it would be ~50 times faster atn_tests=10_000.As gravity is just a toy model, this is not as relevant, but I wanted to contribute this anyways.