Repository navigation
Run NNS.seas's lag scan as R's own C++ (tides gist: ~50 min → 56 s) - #195
Merged
Merged
Conversation
nns_seas scored every lag up to n / 5 with a per-element Python loop, so the O(n^2) scan that R runs in compiled code took the interpreter about 50 minutes on the 222,040-point tides series in gists/timeseries/tides (R 14: 25 seconds). #193's exact-sum pass made it roughly twice as slow again. The scan is now R's seas_score from src/NNS_seas.cpp, copied verbatim into src/nns/_r14_seas.cpp and scored across lags with nns::parallel_for, as R splits it with RcppParallel. It builds into the NNS-core library target: nanobind compiles its module with -Os, which made the same loop three times slower than R's. It now matches R's speed (4.7 s single-threaded, 1.7 s on four threads for 60,000 points; R: 4.7 s and 1.7 s). The overlay is registered with its hashes in sync/nns_source.json. Without the native extension, a vectorised NumPy port of the same walk runs instead: one stable sort, then per lag every component's terms in ascending order, summed row by row from 0.0 as R's running sums are. Native and fallback scores match the previous implementation bit for bit (16,260 lag scores, at 1 and 3 threads), and the live R sweeps stay exact (72 NNS.seas and 32 seeded p-value cases, both modes). A new invariant test pins both paths to a line-by-line transcription of R's loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
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.
Summary
nns_seaswas far slower than R on long series. On the 222,040-point tides gist (gists/timeseries/tides), its seasonality step took about 50 minutes; R 14 takes 25 seconds. This PR runs the scan as R's own C++, so it now matches R's speed and still gives R's results bit for bit.Cause
R 14's
NNS.seasscores every lag up ton / 5. Each lag walks the whole sorted series, so the scan is O(n²). R does this in C++ (seas_scoreinsrc/NNS_seas.cpp), split across threads. The port, introduced in #192, did the same walk with a per-element Python loop: about 0.06 s per lag, times 44,408 lags, for the tides series. #193 added a second interpreted pass to reproduce R's exact sum, which roughly doubled the cost.Fix
src/nns/_r14_seas.cppcontains R'sSeasComponentandseas_score, copied verbatim. The lags are scored withnns::parallel_for, where R uses RcppParallel. Each lag writes its own output slot, so results never depend on the thread count. It is bound asseas_scores_r14and registered as an R14 overlay with its hashes insync/nns_source.json.nanobind_add_modulecompiles the module itself with-Os, which made the same loop 3× slower than R's (15.4 s vs 4.7 s single-threaded).Verification
NNS.seascases and 32 seeded p-value cases).NNS_DISABLE_NATIVE=1), 2,350 passed (NNS_R_CACHE_ONLY=1 tests/parity);ruffandmypyclean.Note on the gist READMEs
The tides and conformal READMEs show results from earlier versions. The tides one says nns 1.0.6, R² 0.965, and an "instant" seasonality step under the old algorithm. With R 14's algorithm, R and this port both give R² 0.943, and the scan costs about 25 s. Refreshing those READMEs is left for a separate change.
Generated by Claude Code