Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/src/freq.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,65 @@ k = coherence(ds; n = 1000)
nothing # hide
```

### Uncertainty estimates
[`tfest`](@ref) and [`coherence`](@ref) optionally return an estimate of the standard deviation of the estimates at each frequency, requested with the keyword argument `uncertainty`. Two estimates are available:
- `uncertainty = Val(:analytic)` computes the standard deviation from the distribution of the estimates under the assumption that the input and the noise are Gaussian processes. The transfer-function estimate is linear in the noise, and is therefore complex Gaussian conditional on the input, with a variance that is computed from the input spectrum and the residual noise power. The correlation between overlapping Welch windows is taken into account. The squared coherence follows the distribution of Goodman (1963) for independent windows, which is applied approximately with the equivalent number of independent windows for overlapping windows and for the lag window of the correlogram method. This estimate is available for both `method = :welch` and `method = :corr`.
- `uncertainty = Val(:jackknife)` (or `Val(true)`) computes a delete-one jackknife estimate over the Welch windows, i.e., the spread of the estimates obtained when one window at a time is left out. It does not rely on the Gaussian assumption, but treats the windows as independent, and is only available for `method = :welch`.
```@example npfreq
G, N, σG = tfest(dn; method = :welch, n = 1000, uncertainty = Val(:analytic))
k, σk = coherence(dn; n = 1000, uncertainty = Val(:analytic))
inds = 2:length(G.w) # exclude the zero frequency from the logarithmic axis
mag = abs.(G.r[inds])
plot(G.w[inds], mag, ribbon = (min.(2σG.r[inds], 0.99mag), 2σG.r[inds]), xscale = :log10, yscale = :log10, lab = "|G| ± 2σ_G", layout = 2)
plot!(G.w[inds], abs.(vec(freqresp(c2d(sys, h), G.w[inds]))), sp = 1, lab = "True system", l = (:black, :dash))
plot!(k.w[inds], k.r[inds], ribbon = 2σk.r[inds], xscale = :log10, yscale = :identity, ylims = (0, 1), sp = 2, lab = "κ² ± 2σ")
DisplayAs.PNG(current()) # hide
```
Here, `σG` is the standard deviation of the complex-valued estimate, ``\sqrt{\operatorname{Var}(\operatorname{Re} G) + \operatorname{Var}(\operatorname{Im} G)}``, which is an upper bound on the standard deviation of the magnitude ``|G|`` to first order. The band in the left figure thus indicates the total error of the estimate, including the phase error. A band around the coherence is also drawn by `coherenceplot(dn, (; n = 1000, uncertainty = Val(:analytic)))`.

#### Comparison of the analytic and jackknife estimates
To compare the two estimates with the actual variability of the estimates, we repeat the experiment 200 times with new realizations of the input and the noise, and compute the standard deviation of the estimates over the realizations. A shorter experiment with a window length of 2000 samples is used, which gives only 19 windows, such that the difference between the two estimates is visible. The figure shows the ratio between the uncertainty estimates and the Monte Carlo standard deviation. A ratio of one indicates a correct estimate. Each uncertainty estimate is computed from a single experiment. The lines show the ratio between the root-mean-square uncertainty estimate (the square root of the mean predicted variance) and the Monte Carlo standard deviation, and the bands the 10% and 90% quantiles of the ratios of the individual uncertainty estimates, i.e., how much the uncertainty estimate obtained from a single experiment varies. The transfer-function variance is computed conditional on the input; since the estimate is unbiased given the input, the mean conditional variance equals the variance over realizations with new inputs.
```@example npfreq
using Statistics
Tc = 20000
function experiment()
u = randn(1, Tc)
y = lsim(sys, u, 1:Tc)[1] .+ lsim(sysn, randn(1, Tc), 1:Tc)[1]
iddata(y, u, h)
end
kw = (; method = :welch, n = 2000)
res = map(1:200) do _
di = experiment()
G, _, σa = tfest(di; kw..., uncertainty = Val(:analytic))
_, _, σj = tfest(di; kw..., uncertainty = Val(:jackknife))
k, σka = coherence(di; kw..., uncertainty = Val(:analytic))
_, σkj = coherence(di; kw..., uncertainty = Val(:jackknife))
(; G = G.r, k = k.r, σa = σa.r, σj = σj.r, σka = σka.r, σkj = σkj.r)
end
w = tfest(experiment(); kw...)[1].w
inds = 2:length(w)
Gs = reduce(hcat, [r.G for r in res])
σG_mc = vec(sqrt.(mean(abs2, Gs .- mean(Gs, dims = 2), dims = 2)))
σk_mc = vec(std(reduce(hcat, [r.k for r in res]), dims = 2))
function band!(sp, field, σ_mc, lab, color)
M = reduce(hcat, [getfield(r, field) for r in res])[inds, :] ./ σ_mc[inds]
q10, q90 = (mapslices(x -> quantile(x, q), M, dims = 2)[:] for q in (0.1, 0.9))
rms = sqrt.(mean(abs2, M, dims = 2)[:])
plot!(w[inds], rms, ribbon = (rms .- q10, q90 .- rms), sp = sp, lab = lab, c = color, fillalpha = 0.3)
end
plot(layout = (1, 2), size = (800, 350), xscale = :log10, ylims = (0, 2), xlabel = "Frequency [rad/s]", ylabel = "Estimate / Monte Carlo", title = ["σ_G" "σ_κ²"])
band!(1, :σa, σG_mc, "Analytic", 1)
band!(1, :σj, σG_mc, "Jackknife", 2)
band!(2, :σka, σk_mc, "Analytic", 1)
band!(2, :σkj, σk_mc, "Jackknife", 2)
hline!([1], c = :black, l = :dash, lab = "", sp = 1)
hline!([1], c = :black, l = :dash, lab = "", sp = 2)
DisplayAs.PNG(current()) # hide
```
In this example, the root-mean-square values of both estimates of `σ_G` agree with the Monte Carlo standard deviation, and the analytic estimate obtained from a single experiment varies less than the jackknife estimate (the 10%–90% range of the ratio is approximately 15% narrower). For `σ_κ²`, the jackknife estimate agrees with the Monte Carlo standard deviation, while the analytic estimate is approximately 10%–20% too large on average. The analytic estimate evaluates the distribution of the coherence at the estimated signal-to-noise ratio, and the error in this estimate increases the predicted standard deviation on average, in particular where the coherence is high and the number of windows small. The analytic estimate accounts for the correlation between overlapping windows, which the jackknife estimate neglects: for an overlap of 75%, the jackknife estimates are approximately 30% too small, while the analytic estimates are not affected.

The analytic estimate is preferable when its assumptions hold approximately, i.e., when the noise is approximately Gaussian and stationary, and the input is either a Gaussian process (for the coherence) or known (for the transfer function). The jackknife estimate is more robust to violations of these assumptions, for example to noise that is not stationary over the duration of the experiment. Neither estimate accounts for bias, such as the bias caused by spectral leakage at sharp resonances, or by noise that is correlated with the input in closed-loop experiments. At frequencies where the true coherence is close to zero, both estimates overestimate the standard deviation of the coherence, by approximately 20%. The jackknife estimate of the coherence uncertainty requires at least three windows, and at least `m + 2` windows for `m` inputs.

See the [example notebooks](https://github.com/JuliaControl/ControlExamples.jl?files=1) for more details.

## Parametric estimation
Expand Down
Loading
Loading