Skip to content

Residual-bootstrap uncertainty for subspaceid and AIC-based default horizons - #215

Open
baggepinnen wants to merge 5 commits into
masterfrom
fb/subspace-uncertainty
Open

baggepinnen wants to merge 5 commits into
masterfrom
fb/subspace-uncertainty

Conversation

@baggepinnen

@baggepinnen baggepinnen commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Adds opt-in uncertainty estimation to the time-domain subspaceid by a residual bootstrap, returns the ensemble of models as MonteCarloMeasurements.Particles, and changes the default horizons of subspaceid to a rule based on the AIC lag order of a VARX model.

sys, sysp = subspaceid(d, nx; uncertainty = Val(true), nboot = 200)
unsafe_comparisons(true)                 # required by the pivoting in freqresp
mag = bodev(sysp, w; balance = false)[1] # Vector{Particles}, one particle per bootstrap model

Uncertainty: residual bootstrap

  • The innovations of the estimated model are computed on the data, centered, and resampled with replacement. New outputs are simulated with the innovation model, the original input and the estimated initial state, and a model of the same order is estimated from each simulated data set with the same keyword arguments, including the horizons.
  • sysp is an N4SIDStateSpace whose A, B, C, D, K, Q, R, S, P and x have Particles entries. The particles are not transformed to a common state basis. Basis-invariant quantities (frequency response, poles, stability, simulations) are meaningful per particle; statistics of matrix entries and the mean model are not. This is documented.
  • Val(false) (default) returns the model only, unchanged. The residual bootstrap requires a stable model and throws an ArgumentError otherwise (only possible with stable = false).
  • The bootstrap relies on the estimated noise model K. With the previous default horizons this noise model was strongly biased for lightly damped systems (noise spectrum overestimated by a factor 2.5–9 near the resonance, not decreasing with the record length), and the bootstrap overestimated the uncertainty by up to 2.4. This is the finite-horizon bias of the state-sequence estimate of Q, S, R in find_PK; it vanishes as the horizons grow. Hence the change of the default horizons below.

Default horizons

  • r = s1 = s2 = clamp(2k, 2nx, N ÷ (6(m + p))), where k is the lag order of a VARX model selected by AIC (lags up to 50). All lag orders are evaluated from one QR factorization with the regressors ordered by lag (varx_aic_lag, subspace_horizon). The lag order reflects the memory of the predictor, and its penalty grows with the number of channels, so short data sets and many channels give shorter horizons. The upper bound limits the data matrices to approximately one row per three columns.
  • nx = :auto keeps the default min(N ÷ 20, 50), since the rule needs the model order.
  • If N ÷ (6(m + p)) < 2nx, the horizon is nx + 1. The log determinant in the AIC is computed from a QR factorization of the residual factor, and linearly dependent regressors (e.g. duplicated input channels) fall back to separate least-squares fits.
  • Monte Carlo comparison with the previous default 2nx + 10 (median relative error of the frequency response, 30 realizations): lightly damped third-order system 0.037 → 0.023 (N = 2000) and 0.019 → 0.010 (N = 8000); 2×2 MIMO 0.032 → 0.016 and 0.021 → 0.005; 3×3 MIMO with N = 300 fails with the previous default and gives 0.023; fast first-order and well-damped systems unchanged. The noise spectrum ratio estimate/true is 0.99–1.00 for N ≥ 2000 (previously 1.35–3).
  • Regression: for the unstable test system with a pole at 1.01 and focus = :simulation in test_subspace.jl, AIC selects a long lag and the relative prediction error increases from 0.10 to 0.14. The test now sets r = 2nx + 10 explicitly.

Calibration of the bootstrap with the new default horizons

Ratio of the RMS bootstrap standard deviation of |G| to the standard deviation over 100 noise realizations (fixed white-noise input, ω = 0.3, 1, 3, 10 rad/s): 0.85–1.13 for second- and third-order systems with measurement noise only and with process noise at N = 2000 and 8000, and for an undermodeled fourth-order system with process noise; 0.88–1.35 for the undermodeled system with measurement noise only.

Alternatives that were evaluated and not adopted: a delete-one-block jackknife (overestimation by 1.3–4 in the heavy-tailed regime, and requires alignment of the state bases), a moving-block bootstrap of the data (no better calibrated, frequent alignment failures), a bootstrap of the simulation residuals in blocks (similar calibration, requires a block length), and the asymptotic prediction-error covariance at the subspace estimate (inherits the noise-model bias; well calibrated only after several Gauss–Newton iterations, i.e. for a prediction-error estimate, which belongs in newpem).

Fixes of existing behavior

  • find_PK divided the residual cross product by a constant determined by the matrix dimensions instead of by the number of data-matrix columns, so the estimated Q, R, S and P grew linearly with the record length (for a true R = 0.01: 0.10, 0.40, 1.60 at N = 1000, 4000, 16000; now approximately 0.012). K is unaffected.
  • stable = false was ignored; eigenvalue reflection of an unstable A was always applied.
  • The Riccati solution P was returned for the covariances scaled by the factor used for numerical conditioning in ared, and is now divided by that factor, so that P corresponds to the returned Q, R, S.

Other

  • Docs: sections "Choice of horizons" and "Uncertainty estimates for subspaceid" in ss.md (ensemble plot of the magnitude with mcplot, unsafe comparisons scoped with @unsafe, stability evaluated on the individual models); MonteCarloMeasurements added to the docs environment.
  • Tests: equality with the default estimate, data stored as vectors of samples, numeric stable margin (as passed by newpem), P against the Riccati solution of the returned covariances, short records, nearly dependent Float32 outputs, AIC lag against separate least-squares fits (also with a duplicated input), particle types and sizes, reproducibility with a seeded RNG, validity of the particle models, noise-covariance normalization, stable = false, forwarding of keyword arguments, error cases, MIMO, and a Monte Carlo calibration check.

Host: demeter2 · Session: f206e1d5-e03e-456f-aba8-1778b9dede55 · Working dir: ~/repos/CSI-subspace

🤖 Generated with Claude Code

baggepinnen and others added 4 commits December 2, 2026 21:21
subspaceid(data, nx; uncertainty = Val(:jackknife), nblocks) returns the
model together with a SubspaceJackknife object holding one estimate per
left-out block of data matrix columns, transformed to the state basis of
the full-data model through the extended observability matrix.
jackknife_std, jackknife_cov and pole_std compute standard deviations and
covariances from these replicates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Normalize the residual covariance in find_PK by the number of data matrix
  columns; Q, R, S and P previously grew linearly with the record length.
- Honor stable = false (eigenvalue reflection was applied unconditionally).
- Assign the samples before the first and after the last Hankel column to
  the first and last block, so that the blocks partition the record.
- Detect replicates whose observable subspace differs from the full-data
  model (smallest principal-angle cosine below 0.5), keep them in their own
  basis, warn, and record this in SubspaceJackknife.aligned;
  jackknife_cov(unc) throws in that case.
- Support covariance-type weight matrices in the B/D regression.
- Match poles by minimum total distance (Hungarian algorithm).
- State the observed calibration in the documentation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…orizons by AIC

The jackknife, its result type and helper functions are removed. With
uncertainty = Val(true), subspaceid returns the estimated model and an
N4SIDStateSpace with MonteCarloMeasurements.Particles entries, one particle
per model estimated from data simulated with resampled innovations.

The default horizon r = s1 = s2 for integer nx is twice the AIC lag order of
a VARX model, bounded by 2nx and N ÷ (6(m + p)). The previous default
2nx + 10 biased the estimated noise model for lightly damped systems.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@baggepinnen baggepinnen changed the title Jackknife uncertainty estimates for subspaceid Residual-bootstrap uncertainty for subspaceid and AIC-based default horizons Dec 3, 2026
- Accept a numeric stability margin (as passed by newpem).
- Convert vector-of-samples data before the horizon computation and the bootstrap.
- Return the Riccati solution P for the returned covariances.
- Compute the AIC log determinant from a QR factorization and fall back to
  separate least-squares fits for linearly dependent regressors.
- Use the horizon nx + 1 for records too short for 2nx; validate kmax.
- Keep only the particle fields of the bootstrap estimates.
- Informative errors for marginally stable models and insufficient horizons.
- Scope unsafe comparisons in the docs and evaluate stability per particle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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