Skip to content

nns_var: forecast the horizon and report the regressors actually used (v2.1.0) - #143

Merged
OVVO-Financial merged 2 commits into
mainfrom
claude/trusting-allen-usj1cz
Oct 29, 2026
Merged

OVVO-Financial merged 2 commits into
mainfrom
claude/trusting-allen-usj1cz

Conversation

@OVVO-Financial

@OVVO-Financial OVVO-Financial commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Summary

nns_var now forecasts the horizon, and its multivariate estimate is a true multivariate fit on exactly the regressors listed in relevant_variables. Version bumped to 2.1.0.

The multivariate stage has two steps per variable:

  1. Selection. The nns_stack dimension reduction selects the relevant regressors. It runs Method 2 alone, which makes exactly the selection it makes inside a full stack (checked identical on 100 of 100 cases).
  2. Fit. nns_stack(stack=False) runs on exactly those regressors, so nns_reg uses each one as its own dimension. Before, they were collapsed into a single synthetic X* regressor, which a stack=True fit always does.

relevant_variables is that selection. If the blend gives nns_reg zero weight, it reports the regressors the dimension-reduction component carries instead. A single selected regressor is fit with method 1 alone, as R does.

This ports the matching R fix on OVVO-Financial/NNS branch claude/trusting-allen-usj1cz (commit 1aa7bdc, not yet merged). The regenerated NNS.VAR / NNS.stack cache entries come from that R build.

nns_var / nns_stack

  • nns_stack returns NNS.dim.red.variables (0-based predictor indices, ranked by |coefficient|) and NNS.dim.red.coefficients. With stack=True, regressors outside the selection cannot move stack (tested).
  • Forecast rows. The multivariate stage is evaluated on the h forecast rows of the lagged matrix, which carry the univariate forecasts. Before, it was evaluated on the last h training rows: on a straight line −2…17 it returned 15, 16, 17 instead of 18, 19, 20.
  • Objective. obj_fn (a callable) and objective now reach both nns_arma_optim and nns_stack. The MSE fallback for the default objective also covers the Method-2-only "No candidate produced a finite out-of-fold objective." case.
  • Input handling.
    • DataFrame column names are kept.
    • tau and h are validated up front; numpy integer tau no longer crashes.
    • h defaults to 1, as in R.
  • Missing values and seasonality.
    • Trailing NaNs are nns_arma_optim extrapolations.
    • Seasonal periods fall back to the unrestricted nns_seas periods, then lag 1, when ARMA rejects them; tau=12 on short series used to fail.
  • Output.
    • Variables never observed below zero keep non-negative outputs.
    • Own-lag weights map columns to variables exactly instead of parsing names.

Parity bugs fixed along the way

  • Gravity bin cap. Both the pure-Python and native gravity_exact capped the histogram at 4n bins; R does not. Small partition quadrants with an outlier therefore got a different regression point. The cap is now only a 1e6-bin memory guard. The full suite passes unchanged.
  • nns_stack with nns.caus / all. "nns.caus" used the outer NNS.caus seasonal-lag search, and could raise, where R's Uni.caus uses a fixed lag of 3 for "ts". "all" omitted R's equal-weight term from its average. Both now match R exactly.
  • Default VAR objective. It uses R's two-pass mean, so a constant candidate prediction scores Inf as in R; with objective="max" it used to be selected.

Tests

  • New parity tests on driven three-variable data (exact match with R): every dim_red_method, every tau form, own-lag weights, objective="max", interior plus trailing NaNs; plus stack selection parity for cor / nns.dep / nns.caus / all.
  • New invariants:
    • Every multivariate estimate is rebuilt as nns_stack(stack=False) on only the relevant columns, including a two-driver fixture where y selects both drivers.
    • Horizon forecasting, validation errors, obj_fn forwarding, non-negativity, seasonal fallback.
  • Degenerate fixture. The perfectly linear 20×2 fixture is degenerate for NNS.caus: R and Python normalize it about 1e-15 apart, which flips ties and can change how many regressors are kept. On that fixture only, each variable is compared exactly whenever R and Python selected the same regressors, and checked for validity otherwise. The driven-data tests hold nns.caus / all to exact parity everywhere.
  • Property test. test_nns_stack_pred_int_shape_invariants_hold now excludes two-valued responses, which switch nns_stack to classification. That was a pre-existing random failure; it reproduces on main.
  • Local runs. ruff check ., mypy, and pytest tests/invariants tests/parity tests/property tests/plotting tests/tools tests/docs with NNS_R_CACHE_ONLY=1 pass. The live-R macro VAR practical tests also pass against the fixed R.

Behaviour change

multivariate and ensemble values change for every nns_var call, because the multivariate stage now forecasts the horizon and fits the relevant regressors jointly. relevant_variables is ordered by importance rather than column order.

Version

2.0.0 → 2.1.0 in pyproject.toml, nns.__version__, the README version row, and uv.lock (regenerated with uv lock; only the ovvo-nns entry changes). scripts/check_version_consistency.py passes.

Follow-ups

  • sync/nns_source.json r_commit should be bumped once the R branch merges.
  • docs/benchmarks.md timings predate this change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Wws6NsSKm9v45EKYaXEdJJ

…used

Port of the matching R NNS fix (OVVO-Financial/NNS branch
claude/trusting-allen-usj1cz), keeping nns_var in exact parity with R:

- nns_stack returns NNS.dim.red.variables (0-based predictor indices) and
  NNS.dim.red.coefficients; nns_var reports that selection as
  relevant_variables, so it lists exactly the regressors behind each
  multivariate estimate.
- The multivariate stack is evaluated on the h forecast rows of the lagged
  matrix instead of the last h observed training rows.
- obj_fn (a callable) and objective now reach both nns_arma_optim and
  nns_stack; DataFrame column names are kept; tau / h are validated up front
  (numpy integer tau no longer crashes); h defaults to 1.
- Trailing NaNs are nns_arma_optim extrapolations; seasonal periods fall back
  to unrestricted nns_seas periods, then lag 1; never-negative variables keep
  non-negative outputs; own-lag weights map columns exactly.

Parity bugs found along the way:
- gravity (pure Python and the native gravity_exact) capped the histogram at
  4n bins; R does not, so small partition quadrants with an outlier got a
  different regression point. The cap is now a 1e6 memory guard only.
- nns_stack dim_red_method="nns.caus" used the outer NNS.caus seasonal lag
  search (and could raise) where R's Uni.caus uses a fixed lag of 3 for "ts";
  "all" omitted R's equal-weight term from the average.
- nns_var's default objective uses R's two-pass mean, so constant candidate
  predictions score Inf as in R (objective="max" used to pick them).

Tests: new parity tests on driven three-variable data for every
dim_red_method, tau form, own-lag weights, objective="max" and missing values
(all exact); stack selection parity for cor / nns.dep / nns.caus / all;
invariants proving regressors outside relevant_variables cannot move the
estimate. NNS.VAR caches regenerated from the fixed R; stack shards gain the
new-key entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NsSKm9v45EKYaXEdJJ
nns_var now forecasts the horizon and reports the regressors it actually
uses; nns_stack gains NNS.dim.red.variables / NNS.dim.red.coefficients.
uv.lock regenerated with `uv lock` (only the ovvo-nns entry changes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NsSKm9v45EKYaXEdJJ
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