Skip to content

Analytic and jackknife uncertainty estimates for tfest and coherence - #214

Open
baggepinnen wants to merge 6 commits into
fb/welch-segmentsfrom
fb/tfest-uncertainty
Open

baggepinnen wants to merge 6 commits into
fb/welch-segmentsfrom
fb/tfest-uncertainty

Conversation

@baggepinnen

@baggepinnen baggepinnen commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Summary

tfest and coherence return point estimates only. This PR adds opt-in estimates of the standard deviation of the estimates at each frequency, through the keyword argument uncertainty:

  • uncertainty = Val(:analytic): standard deviations computed from the distribution of the estimates under the assumption of Gaussian noise (and, for the coherence, Gaussian input). Available for method = :welch and method = :corr.
  • uncertainty = Val(:jackknife) (Val(true) is equivalent): delete-one jackknife over the Welch windows. Only for method = :welch.
  • uncertainty = Val(false) (default): unchanged return values.

Return values: H, N, σH = tfest(d; uncertainty = ...), where σH is a real FRD with the standard deviation of the complex estimate H, sqrt(Var(Re H) + Var(Im H)), of the same shape as H (also for multiple outputs); κ², σκ² = coherence(d; uncertainty = ...), also for multiple inputs (multiple coherence). Both work for the vector-of-segments methods of #211. The option is a type parameter, so the return types are inferred as Tuple{FRD, FRD} or Tuple{FRD, FRD, FRD}.

Analytic estimate

  • Transfer function: with the window transforms U_k, Y_k and Y_k = H U_k + V_k, the estimate Ĥ = Σ Y_k U_k* / Σ |U_k|² is linear in the noise and therefore complex Gaussian conditional on the input, with variance σ_V² uᴴCu / (uᴴu)². C[k,j] = ρ(s_k - s_j) exp(iω(s_k - s_j)) is the correlation of the noise transforms of overlapping windows starting at s_k, s_j, with ρ(Δ) = Σ w_t w_{t+Δ} / Σ w_t² (locally white noise). The noise power is estimated without bias as R / (K - q), with the residual R and q = uᴴCu / uᴴu (q = 1 without overlap). Windows of different segments are uncorrelated. For method = :corr, σ_H² = R / ((K_eff - 1) S_uu) with the equivalent number of independent windows K_eff = N / Σ_τ w_τ² of the lag window. Without overlap, |Ĥ - H|² / σ̂_H² follows F(2, 2(K-1)); this is documented.
  • Coherence: for jointly Gaussian input and noise, the squared coherence follows the distribution of Goodman (1963), a negative-binomial mixture of beta distributions with parameters m + j, K - m (m inputs). The moments are computed with the law of total variance on 1 - κ² (no cancellation at high coherence) and with bounded work (gamma approximation on a grid for large means). The signal-to-noise ratio is estimated from the data, without bias for independent windows before truncation at zero (the correction is not applied for the correlogram method, for which it overcorrects), overlapping windows enter through Welch's equivalent number of windows K_eff = K² / Σ ρ_kj², and the zero and Nyquist frequencies, where the transforms are real-valued, use half the degrees of freedom. Frequencies without input excitation or output power give NaN. A first version conditioned on the input (Poisson mixture, noncentral beta) underestimated the spread over repeated experiments with random input by about 18%, which motivated the unconditional distribution.

Jackknife estimate

With K Welch windows, K delete-one estimates are formed from the spectral sums with one window left out (via prefix and suffix sums, which avoids cancellation when one window dominates), and the variance is (K-1)/K Σ_k |θ_(k) - θ̄|². At least two windows are required for tfest, three for coherence, and m + 2 for the multiple coherence with m inputs.

Calibration

Ratio between the root-mean-square estimated standard deviation (square root of the mean predicted variance) and the standard deviation over 300 Monte Carlo realizations (new input and noise each time, 4000 samples, window length 256 unless stated, median over the interior frequencies, coherence between 0.07 and 0.93):

Configuration σH analytic σH jackknife σκ² analytic σκ² jackknife
Hann, 50% overlap 1.00 0.99 1.02 1.02
Hamming, 50% overlap (default) 1.00 0.97 1.01 1.00
Rectangular, no overlap 1.00 1.04 1.02 1.08
Hann, 75% overlap 1.00 0.73 1.01 0.74
Hann, n = 1000 (7 windows) 1.00 1.07 1.04 1.15
Rectangular, n = 1000, no overlap (4 windows) 1.00 1.19 1.02 1.23
Correlogram (σ = 0.05, about 6 equivalent windows) 1.04 – 1.0–1.4 –
Two inputs, Hamming 50% (multiple coherence) – – 0.99 0.99

The analytic σH is calibrated in all configurations and varies less between experiments than the jackknife estimate (relative spread 0.13 vs 0.17 for Hann 50%). The analytic σκ² evaluates the Goodman distribution at the estimated signal-to-noise ratio; at high coherence with few windows the error in that estimate makes it too large on average (10–20% in the documentation example with 19 windows and coherence above 0.95, where the jackknife is calibrated). For the correlogram method, the coherence distribution is approximated with the equivalent number of windows of the lag window and overestimates the standard deviation at high coherence. At zero coherence, both estimates overestimate the standard deviation of κ² by about 20%.

Other changes

  • The Welch noise spectrum N is normalized by the number of windows times the window energy Σ w² rather than by the number of samples. Previously, N depended on samples outside every window and on the scaling of the window function, and differed from the units of the correlogram estimate (for a Hamming window with 50% overlap by a factor of about 0.8). White noise of variance σ² now gives N ≈ σ² for every window function. wcfft returns this normalization as a fourth value.
  • Negative values of the residual spectrum caused by rounding errors are replaced by zero (previously sqrt(N) could throw for a single window), and the coherence is bounded to [0, 1].
  • method is validated, and coherence with multiple inputs rejects method = :corr, which was previously replaced by the Welch method without notice.
  • tfest and coherence throw an ArgumentError for frequency-domain data (InputOutputFreqData), which was previously windowed across frequency bins as if it were a time series.
  • The multiple-output branch of tfest(d::AbstractIdData) did not forward method, n, noverlap and window to the per-output estimates; it now does.
  • wcfft is implemented in terms of a new internal foreach_window, shared with wcfft_windows (per-window periodograms) and the analytic estimate.
  • wcfft_mimo is replaced by wcfft_mimo_windows, used by all paths of the multiple coherence. The previous implementation applied rfft to the windowed input matrix without a dimension argument, i.e., it also transformed across the input channels; this is equivalent for the multiple coherence in exact arithmetic but not numerically for inputs of very different scales. window = nothing is now supported for multiple inputs.
  • coherenceplot(d, (; uncertainty = Val(:analytic), ...)) draws a band of ± two standard deviations.
  • coherenceplot accepts a vector of data segments and plots the pooled Welch coherence (previously it threw an ArgumentError).

This PR is based on #211 and targets its branch.

Tests (test/test_frd.jl)

  • Estimates are unchanged by the option; return types.
  • Jackknife: equality with a manual delete-one computation; Monte Carlo calibration; dominant window; Float32 data; minimum window counts.
  • Analytic: closed form without overlap, null distribution of the coherence, Monte Carlo calibration for Welch with 50% and 75% overlap and for the correlogram method; multiple outputs, segments and inputs.
  • Normalization of N for white noise, independence of N from unused samples, non-negative N and bounded coherence for a single window, rejection of frequency-domain data.

Documentation

Subsection "Uncertainty estimates" in docs/src/freq.md with a plot of |G| ± 2σ and κ² ± 2σ, and a Monte Carlo comparison of the analytic and jackknife estimates.


Host: demeter2, session f206e1d5-e03e-456f-aba8-1778b9dede55, working directory /home/baggepinnen/repos/ControlSystemIdentification.jl

🤖 Generated with Claude Code

baggepinnen and others added 4 commits December 2, 2026 14:12
Add the keyword argument `uncertainty::Val = Val(false)` to `tfest` and
`coherence` (single dataset, vector of data segments, and multiple-input
coherence). With `Val(true)`, the delete-one jackknife estimate of the
standard deviation over the Welch windows is returned as an additional FRD.
Only `method = :welch` is supported.

The multiple-output branch of `tfest(d::AbstractIdData)` now forwards
`method`, `n`, `noverlap` and `window` to the per-output estimates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Form delete-one sums from prefix and suffix sums instead of subtracting
  each window from the total, which avoids cancellation when one window
  dominates.
- Require at least three windows for the coherence uncertainty, and
  m + 2 windows for the multiple coherence with m inputs; with fewer,
  every delete-one coherence equals one.
- Compute the multiple coherence from transforms along the time axis in
  both paths, and support window = nothing for multiple inputs.
- Form per-window periodogram products in the precision of the transforms,
  as in wcfft.
- Draw a ±2σ band in coherenceplot when uncertainty = Val(true) is passed.
- Document the interpretation of σG and the behavior of the coherence
  uncertainty near zero coherence.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Normalize the Welch noise spectrum N by the number of windows times
  the window energy instead of by the number of samples, so that N is
  a spectral density in the same units as for method = :corr and does
  not depend on samples outside every window or on the window scaling.
  wcfft, wcfft_windows and foreach_window return this normalization.
- Replace negative values of the residual spectrum by zero and bound
  the coherence to [0, 1].
- Reject frequency-domain data (InputOutputFreqData) in tfest and
  coherence.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add uncertainty = Val(:analytic) next to Val(:jackknife) (Val(true) is
equivalent to Val(:jackknife)).

- The standard deviation of the transfer-function estimate is computed
  from its conditional distribution given the input under Gaussian noise,
  including the correlation between overlapping Welch windows, with the
  noise power estimated without bias from the residual.
- The standard deviation of the squared (multiple) coherence is computed
  from the distribution of Goodman (1963) for jointly Gaussian input and
  noise, as a negative binomial mixture of beta distributions, with the
  equivalent number of windows of Welch (1967) for overlapping windows.
- The analytic estimate is available for method = :corr, using the
  equivalent number of independent windows of the lag window, for
  multiple data segments, and for multiple inputs.
- foreach_window passes the start of each window on a common time axis.
- Documentation section comparing the analytic and jackknife estimates
  with Monte Carlo simulations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@baggepinnen baggepinnen changed the title Jackknife uncertainty estimates for tfest and coherence Analytic and jackknife uncertainty estimates for tfest and coherence Dec 2, 2026
baggepinnen and others added 2 commits December 2, 2026 19:05
- Compute the mixture moments of the coherence distribution with the law
  of total variance on the complement 1 - κ², which avoids cancellation at
  high coherence, and with bounded work (gamma approximation on a grid
  for large means); skip the coherence computation in tfest.
- Compute the Welch residual directly from the fit, and estimate the
  signal-to-noise ratio with the bias correction for independent windows
  (real-valued bins included).
- Return NaN at frequencies without input excitation or output power
  instead of aborting the estimate.
- Validate `method`, reject method = :corr for multiple inputs (it was
  silently replaced by the Welch method), and require an equivalent
  number of windows larger than the number of inputs.
- Correlogram: include the number of products per lag in the equivalent
  number of windows, reject lag windows that span too few lags, and
  document the approximations.
- Document F(1, K - 1) at real-valued bins, correct the coherence formula
  in the extended help, and compare the mean predicted variance with the
  empirical variance in the documentation and tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
coherenceplot accepts a vector of data segments and plots the Welch
coherence pooled over the windows of all segments, one series per output,
with the uncertainty band when `uncertainty` is passed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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