Skip to content

nns_seas: score every component series of each lag (book ch25) - #170

Closed
gitRasheed wants to merge 2 commits into
rasheed/vendor-corefrom
rasheed/book-seas-all-phases
Closed

gitRasheed wants to merge 2 commits into
rasheed/vendor-corefrom
rasheed/book-seas-all-phases

Conversation

@gitRasheed

@gitRasheed gitRasheed commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

nns_seas scores only three reverse-stride subsamples per lag, ending at n, n-1 and n-2, averages them unweighted, accepts ties with <= and falls back to 1/|acf1| only when the mean is exactly 0. Book ch25 (ch25:183, 257-284) tests lag m on all m component series with a length-weighted mean CV, kept when strictly below CV(X). We mirror OVVO-Financial/NNS#91, which makes R follow the book, including its round-2 fixes. That PR changes the method, so the R author may close it; if so, this slice should be dropped too.

Stacked on #169; merge with a merge commit.

Changes

  • Each lag m in 1..n/2 is scored on all m components x[j::m], weighted by length, and kept when strictly below CV(X). When |mean(x)| <= sqrt(eps) * sd(x), the sd replaces the CV. Each component's sums are added in R's order, so the scores are bit-identical to R's.
  • A constant component scores 0, so rep([0, 10], 10) keeps periods 2 4 6 8, and a near-zero-mean component is capped at 1/sqrt(eps). A constant series goes from periods 1..9 to 1 (no seasonality).
  • AirPassengers: best period goes from 50 to 12, and 18 periods to 3 (12, 13, 6). The holdout MAPE of nns_arma(AirPassengers[:132], h=12) goes from 0.2434 to 0.0389, and the forecast equals R's to 1e-15.
  • We remove the dead acf1 and subsample helpers. The ARMA equal-weights fallback was already in nns_arma.

nns_seas at n = 20000 goes from 0.05 s to 2.2 s, because the rule is O(n^2). R takes 0.91 s, and the R PR lists the cost under its tradeoffs.

Tests

A new invariant pins the scores to a transcription of the C++ loop, and new parity cases cover the constant-component and AirPassengers cases. The 15 NNS.seas, 11 NNS.ARMA, 2 NNS.caus and 11 NNS.VAR entries these tests read predate the R change and are refilled; NNS.caus with tau = "ts" picks its lag from NNS.seas. The parity cache encodes R 13.3 plus the fixes in OVVO-Financial/NNS#73-#92.

Nine NNS.VAR tests are strict xfails here and in the next three slices. Their R values also depend on OVVO-Financial/NNS#88, and #174 removes the marks.

Mirrors OVVO-Financial/NNS#91
Part of OVVO-Financial/NNS#69

Update

This push mirrors the seasonality part of OVVO-Financial/NNS#99. A constant series or component scores 0 by exact equality. Before, a constant 0.1 series had a rounding-level spread and reported period 4. The best period for rep(0.1, 20) is now 1, and for rep(c(0.1, 1), 10) it is 2.

Review

GPT-6.1 Sol reviewed this slice and found nothing. It compared 200 new inputs against R.

…ses)

Mirrors R's seas-all-phases branch (NNS.seas), including its round-2
fixes. NNS.seas scored only three reverse-stride subsamples per lag
(ending at n, n-1 and n-2), averaged them unweighted, accepted ties with
<= and fell back to 1/|acf1| when the mean was exactly 0. The book
(ch. 25) defines the test on all m component series of lag m with a
length-weighted mean CV, kept when strictly below CV(X).

Now each lag m in 1..n/2 is scored on all m components x[j::m], weighted
by length, and kept when strictly below CV(X). When |mean(x)| <=
sqrt(eps) * sd(x) the sd replaces the CV. A constant component scores 0,
and a component with a near-zero mean has its CV capped at 1/sqrt(eps).
Each component's sums are added in R's order, so the scores are
bit-identical to R's (new invariant against a transcription of the C++
loop). The dead acf1/subsample helpers go. R's ARMA.seas.weighting
equal-weights fallback was already in nns_arma.

Before -> after (= R):
  AirPassengers best period   50 -> 12; periods 18 -> 3 (12, 13, 6)
  nns_arma(AirPassengers[:132], h=12) holdout MAPE   0.2434 -> 0.0389
  rep([0, 10], 10)            periods 2 4 6 8, CV 0
  constant series             periods 1..9 -> 1 (no seasonality)
  AirPassengers[:24], seasonal_factor=False, h=4
                              122 102 162 213.67 -> 120 153 149 115
nns_seas at n = 20000 takes 2.2 s (was 0.05 s; the rule is O(n^2)).

Refilled from R: the 15 NNS.seas, 11 NNS.ARMA, 2 NNS.caus and 11 NNS.VAR
entries these tests read (all predate the R change; NNS.caus with
tau="ts" picks its lag from NNS.seas). The nine NNS.VAR
tests also need R's optim-pi-from-returned-model change in
NNS.ARMA.optim, so they are strict xfails until that is ported. The
airline smoke values are updated from R.
…irror of the R fix)

A repeated 0.1 does not centre to exactly 0, so the sd of a constant series
or component was rounding noise and nns_seas(np.full(20, 0.1)) reported
period 4. As R now does, a series or component whose raw values are all equal
has sd 0: that series gives best period 1 and np.tile([0.1, 1.0], 10) gives 2.
@gitRasheed
gitRasheed force-pushed the rasheed/book-seas-all-phases branch from b392514 to 0f3610d Compare November 2, 2026 23:09
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Incorporated through NNS R 14.0 integration #192, merged as bd19c3303222af46da79d8c97f258248eb5674d2. Verified exact head 0f3610d9e0e261b56901f89255129736767d080c is an ancestor of main; original commits and authorship are preserved. The integration includes the subsequent R14 compatibility corrections and passed the complete GitHub Python 3.11-3.14 matrix. This supersedes the separate landing, not the work or review history.

@github-actions github-actions Bot closed this Nov 7, 2026
@github-actions
github-actions Bot deleted the rasheed/book-seas-all-phases branch November 7, 2026 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant