Repository navigation
Analytic and jackknife uncertainty estimates for tfest and coherence - #214
Open
baggepinnen wants to merge 6 commits into
Open
baggepinnen wants to merge 6 commits into
baggepinnen wants to merge 6 commits into
Conversation
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]>
- 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
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
tfestandcoherencereturn point estimates only. This PR adds opt-in estimates of the standard deviation of the estimates at each frequency, through the keyword argumentuncertainty: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 formethod = :welchandmethod = :corr.uncertainty = Val(:jackknife)(Val(true)is equivalent): delete-one jackknife over the Welch windows. Only formethod = :welch.uncertainty = Val(false)(default): unchanged return values.Return values:
H, N, σH = tfest(d; uncertainty = ...), whereσHis a real FRD with the standard deviation of the complex estimateH,sqrt(Var(Re H) + Var(Im H)), of the same shape asH(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 asTuple{FRD, FRD}orTuple{FRD, FRD, FRD}.Analytic estimate
U_k, Y_kandY_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 ats_k, s_j, withρ(Δ) = Σ w_t w_{t+Δ} / Σ w_t²(locally white noise). The noise power is estimated without bias asR / (K - q), with the residualRandq = uᴴCu / uᴴu(q = 1without overlap). Windows of different segments are uncorrelated. Formethod = :corr,σ_H² = R / ((K_eff - 1) S_uu)with the equivalent number of independent windowsK_eff = N / Σ_τ w_τ²of the lag window. Without overlap,|Ĥ - H|² / σ̂_H²followsF(2, 2(K-1)); this is documented.m + j, K - m(minputs). The moments are computed with the law of total variance on1 - κ²(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 windowsK_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 giveNaN. 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
KWelch windows,Kdelete-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 fortfest, three forcoherence, andm + 2for the multiple coherence withminputs.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):
σ = 0.05, about 6 equivalent windows)The analytic
σHis 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
Nis normalized by the number of windows times the window energyΣ w²rather than by the number of samples. Previously,Ndepended 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 givesN ≈ σ²for every window function.wcfftreturns this normalization as a fourth value.sqrt(N)could throw for a single window), and the coherence is bounded to[0, 1].methodis validated, andcoherencewith multiple inputs rejectsmethod = :corr, which was previously replaced by the Welch method without notice.tfestandcoherencethrow anArgumentErrorfor frequency-domain data (InputOutputFreqData), which was previously windowed across frequency bins as if it were a time series.tfest(d::AbstractIdData)did not forwardmethod,n,noverlapandwindowto the per-output estimates; it now does.wcfftis implemented in terms of a new internalforeach_window, shared withwcfft_windows(per-window periodograms) and the analytic estimate.wcfft_mimois replaced bywcfft_mimo_windows, used by all paths of the multiple coherence. The previous implementation appliedrfftto 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 = nothingis now supported for multiple inputs.coherenceplot(d, (; uncertainty = Val(:analytic), ...))draws a band of ± two standard deviations.coherenceplotaccepts a vector of data segments and plots the pooled Welch coherence (previously it threw anArgumentError).This PR is based on #211 and targets its branch.
Tests (test/test_frd.jl)
Float32data; minimum window counts.Nfor white noise, independence ofNfrom unused samples, non-negativeNand bounded coherence for a single window, rejection of frequency-domain data.Documentation
Subsection "Uncertainty estimates" in
docs/src/freq.mdwith 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