Skip to content

Fix ar()/grouped_ar() forward/inverse coefficient mismatch (partial fix for #232) - #235

Open
microprediction wants to merge 1 commit into
mainfrom
fix/ar-forward-inverse-coefficient-mismatch
Open

microprediction wants to merge 1 commit into
mainfrom
fix/ar-forward-inverse-coefficient-mismatch

Conversation

@microprediction

Copy link
Copy Markdown
Owner

Summary

Addresses part 1 of the fix plan laid out in the #232 discussion thread, plus an independent bug in _ar_spectral_radius identified there.

ar()/grouped_ar(): forward() emitted residuals using the raw, unconstrained RLS coefficients, while inverse_k() reconstructed the multi-step forecast using _ar_stationary(phi) (stationarity-projected). The leaf downstream learned errors from one predictor while forecasting used a different one -- a single ill-conditioned RLS update (e.g. a long zero run then a jump) could push phi non-stationary with nothing downstream any the wiser until inverse_k's reconstruction diverged.

Fixed by maintaining phi_used (re-projected after every RLS update) and using it consistently for BOTH the emitted residual and inverse_k's reconstruction; the raw RLS fit is untouched and still uses its own unconstrained prediction error.

Verified against the reproduction from the #232 thread:

from skaters.search import _build_from_recipe
f = _build_from_recipe(["ar(2)", "ar(5)", "pow(0.5)"], k=1)
state = None
for y in [0.0] * 2000 + [1e-8, 1.0, 0.0, 0.0]:
    ds, state = f(y, state)
print(ds[0].mean, ds[0].std)

Emitted std dropped from ~2.6e10 to ~0.31, matching the thread's own best-ablation number. On the original #232 FRED reproduction (NONFINGT80A2P2AMT, 6000 points), CRPS dropped from ~5.0e8 to ~400 -- now the same order of magnitude as laplace's own ~112 on the same (genuinely large-scale, spiky) series, rather than 6 orders of magnitude worse.

_ar_spectral_radius: the power-iteration branch (order >= 3) used a single all-ones seed, which can be exactly an eigenvector of a NON-dominant eigenvalue and never converge to the true spectral radius. Confirmed with the thread's exact counterexample: phi=[3,-2,0,0,0] (true radius 2) previously reported 1.0, and _ar_stationary then "damped" the coefficients to a result that was STILL non-stationary (true radius 1.998, not the intended 0.999 margin) -- the safety mechanism was silently failing on its own terms. Fixed with a second seed (alternating +-1) and taking the max; now correctly reports 2.0.

Fixed in Python, JS (docs/js/skaters/transform.mjs), and Rust (rust/src/transform.rs) -- rust/python's skaters_fast PyO3 backend picks the Rust fix up automatically, just needed a local rebuild (maturin develop --release -m rust/python/Cargo.toml) since its cross-backend parity test exercises ar/grouped_ar as part of laplace()'s standard candidate population.

NOT addressed here

Larger design changes flagged in the same thread, each needing its own scoping/discussion:

  • Replacing the naive L1/power-iteration stationarity certificate with a proper Schur/reflection-coefficient test (statsmodels-style).
  • Replacing the RLS covariance hard-reset with persistent regularization (a fading prior instead of a threshold reset).
  • search.py-level candidate health checks / quarantine / recovery, so a degenerate singleton survivor isn't an absorbing search configuration.

Testing

  • New regression tests: tests/test_ar_stability.py (4 tests, including the exact thread reproduction and the spectral-radius counterexample).
  • node parity/check.mjs passes (JS); cargo test --test parity passes (Rust).
  • Full Python suite: 1346 passed, 3 skipped.

🤖 Generated with Claude Code

https://claude.ai/code/session_011AwwectPicNyn6Wv5u43bf

…er-iteration blind spot in _ar_spectral_radius (partial fix for #232)

Addresses part 1 of the fix plan in the #232 discussion thread, plus an
independent bug in _ar_spectral_radius identified there:

- ar()/grouped_ar(): forward() emitted residuals using the raw, unconstrained
  RLS coefficients, while inverse_k() reconstructed the multi-step forecast
  using _ar_stationary(phi) (stationarity-projected). The leaf downstream
  learned errors from one predictor while forecasting used a different one.
  A single ill-conditioned RLS update (e.g. a long zero run then a jump) could
  push phi non-stationary with nothing downstream any the wiser until
  inverse_k's reconstruction diverged. Fixed by maintaining `phi_used`
  (re-projected after every RLS update) and using it consistently for BOTH
  the emitted residual and inverse_k's reconstruction; the raw RLS fit is
  untouched and still uses its own unconstrained prediction error.

  Verified against the reproduction from the #232 thread:
  `_build_from_recipe(["ar(2)", "ar(5)", "pow(0.5)"])` on
  `[0.0]*2000 + [1e-8, 1.0, 0.0, 0.0]` -- emitted std dropped from ~2.6e10 to
  ~0.31, matching the thread's own best-ablation number. On the original
  #232 FRED reproduction (NONFINGT80A2P2AMT, 6000 points), CRPS dropped from
  ~5.0e8 to ~400, now the same order of magnitude as laplace's own ~112 on
  the same (genuinely large-scale, spiky) series rather than 6 orders of
  magnitude worse.

- _ar_spectral_radius: the power-iteration branch (order >= 3) used a single
  all-ones seed, which can be exactly an eigenvector of a NON-dominant
  eigenvalue and never converge to the true spectral radius. Confirmed with
  the thread's exact counterexample: phi=[3,-2,0,0,0] (true radius 2)
  previously reported 1.0, and _ar_stationary then "damped" the coefficients
  to a result that was STILL non-stationary (true radius 1.998, not the
  intended 0.999 margin) -- the safety mechanism was silently failing on its
  own terms. Fixed with a second seed (alternating +-1) and taking the max;
  now correctly reports 2.0.

Fixed in Python, JS, and Rust (rust/python's skaters_fast PyO3 backend
picks the Rust fix up automatically on `maturin develop --release`, needed
to rebuild it locally since the fast-backend cross-parity test uses ar/
grouped_ar as part of laplace()'s standard candidate population).

NOT addressed here -- larger design changes flagged in the same thread,
each requiring its own scoping/discussion:
- Replacing the naive L1/power-iteration stationarity certificate with a
  proper Schur/reflection-coefficient test (statsmodels-style).
- Replacing the RLS covariance hard-reset with persistent regularization
  (a fading prior instead of a threshold reset).
- search.py-level candidate health checks / quarantine / recovery, so a
  degenerate singleton survivor isn't an absorbing search configuration.

Regression tests added (tests/test_ar_stability.py). Parity vectors
regenerated; node parity/check.mjs and cargo test --test parity both pass.
Full Python suite: 1346 passed, 3 skipped.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011AwwectPicNyn6Wv5u43bf

This branch has not been deployed

No deployments
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.

1 participant