Define the kernel and the envelope error
For an auditable Gaussian example, let P[t] be the close, N the trailing window, h a positive bandwidth in bars, M the error window and k a nonnegative band multiplier. The definitions below specify our demonstration variant. They do not claim that every script bearing the Nadaraya-Watson name uses the same error window, initialization or kernel.
- The Gaussian normalization constant cancels in the ratio. Larger h spreads relative weight across more lags; N remains a separate truncation limit.
- A[t] averages residuals against the center that was available at each earlier bar. Averaging all prices against the current center, or refitting old centers before measuring errors, gives a different band.
- Require N valid closes for the first center and M valid residuals for the first envelope: N + M − 1 observations in total. Missing data makes the affected output unavailable; do not substitute zero.
- Reject h ≤ 0, nonpositive window lengths and k < 0. A constant finite input produces zero error and coincident bands. A very small positive h approaches the current price; a very large h approaches equal weighting within N.
| Quantity | Definition |
|---|---|
| Lag weight | w[i] = exp(−i² / (2h²)), for i = 0, …, N − 1 |
| Causal center | C[t] = Σ w[i]P[t−i] / Σ w[i] |
| Endpoint residual | e[t] = |P[t] − C[t]| |
| Trailing absolute error | A[t] = (e[t] + … + e[t−M+1]) / M |
| Envelope | Lower[t] = C[t] − kA[t]; Upper[t] = C[t] + kA[t] |