Skip to content

Run NNS.seas's lag scan as R's own C++ (tides gist: ~50 min → 56 s) - #195

Merged
OVVO-Financial merged 1 commit into
mainfrom
claude/nns-r14-parity-fidelity-rgag0e
Nov 9, 2026
Merged

OVVO-Financial merged 1 commit into
mainfrom
claude/nns-r14-parity-fidelity-rgag0e

Conversation

@OVVO-Financial

Copy link
Copy Markdown
Owner

Summary

nns_seas was 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.seas scores every lag up to n / 5. Each lag walks the whole sorted series, so the scan is O(n²). R does this in C++ (seas_score in src/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

  • Native kernel: src/nns/_r14_seas.cpp contains R's SeasComponent and seas_score, copied verbatim. The lags are scored with nns::parallel_for, where R uses RcppParallel. Each lag writes its own output slot, so results never depend on the thread count. It is bound as seas_scores_r14 and registered as an R14 overlay with its hashes in sync/nns_source.json.
  • Build placement: the kernel builds into the NNS-core library target. nanobind_add_module compiles the module itself with -Os, which made the same loop 3× slower than R's (15.4 s vs 4.7 s single-threaded).
  • NumPy fallback: when the extension isn't available, a vectorised port of the same walk runs instead. It sorts once, then per lag sums each component's terms in ascending order, row by row from 0.0, as R's running sums are.

Verification

  • Exactness: native and fallback scores are bit-identical to the previous implementation, which was verified against R, on 16,260 lag scores at 1 and 3 threads.
  • Live R sweeps: still exact with and without the extension (72 NNS.seas cases and 32 seeded p-value cases).
  • New invariant test: pins both paths to a line-by-line Python transcription of R's loop.
  • Speed, 60,000 points: 4.68 s single-threaded and 1.68 s on 4 threads (R: 4.70 s and 1.66 s).
  • Tides gist end to end: 56 s in total (seasonality 26 s, forecast 29 s). Its selected periods, objective, R² and all 4,040 forecast values and interval bounds are identical to R 14's on the same data.
  • Test suites: 3,232 passed (native), 3,208 passed (NNS_DISABLE_NATIVE=1), 2,350 passed (NNS_R_CACHE_ONLY=1 tests/parity); ruff and mypy clean.

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

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
@OVVO-Financial
OVVO-Financial merged commit 9a81422 into main Nov 9, 2026
4 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