Fix ar()/grouped_ar() forward/inverse coefficient mismatch (partial fix for #232) - #235
Open
microprediction wants to merge 1 commit into
Open
microprediction wants to merge 1 commit into
microprediction wants to merge 1 commit into
Conversation
…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
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
Addresses part 1 of the fix plan laid out in the #232 discussion thread, plus an independent bug in
_ar_spectral_radiusidentified there.ar()/grouped_ar():forward()emitted residuals using the raw, unconstrained RLS coefficients, whileinverse_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 pushphinon-stationary with nothing downstream any the wiser untilinverse_k's reconstruction diverged.Fixed by maintaining
phi_used(re-projected after every RLS update) and using it consistently for BOTH the emitted residual andinverse_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:
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_stationarythen "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'sskaters_fastPyO3 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 exercisesar/grouped_aras part oflaplace()'s standard candidate population.NOT addressed here
Larger design changes flagged in the same thread, each needing its own scoping/discussion:
search.py-level candidate health checks / quarantine / recovery, so a degenerate singleton survivor isn't an absorbing search configuration.Testing
tests/test_ar_stability.py(4 tests, including the exact thread reproduction and the spectral-radius counterexample).node parity/check.mjspasses (JS);cargo test --test paritypasses (Rust).🤖 Generated with Claude Code
https://claude.ai/code/session_011AwwectPicNyn6Wv5u43bf