- AFIRMA is a Windowed Weighted Moving Average that replaces standard linear weighting with weights derived from signal processing window functions (...
- **Similar:** [TSF](../../trends_FIR/tsf/Tsf.md), [LinReg](../../statistics/linreg/LinReg.md) | **Complementary:** Error metrics for accuracy | **Trading note:** Adaptive FIR Moving Average for forecasting; projects price using optimized FIR coefficients.
AFIRMA is a Windowed Weighted Moving Average that replaces standard linear weighting with weights derived from signal processing window functions (Hanning, Hamming, Blackman, Blackman-Harris). This approach achieves specific frequency response characteristics tailored to noise reduction.
The optional Least Squares mode fits a linear regression to recent bars and blends the fitted values with original data, producing a hybrid smoothed-predicted output.
## Historical Context
Moving averages traditionally use Simple (rectangular window), Weighted (triangular window), or Exponential (recursive) forms. The DSP community solved finite filter design decades ago using window functions to minimize spectral leakage (ringing artifacts at discontinuities).
AFIRMA applies these well-understood coefficients directly to price series. It is effectively an FIR filter where coefficients are determined solely by the chosen window function—no manual coefficient calculation required.
## Architecture & Physics
AFIRMA maintains a sliding window of the last $P$ prices and computes a weighted average using pre-calculated window coefficients.
### Window Functions
Instead of linear weights ($1, 2, 3...$), AFIRMA generates weights using cosine-sum series:
2.**Fit linear regression** to the most recent $n$ bars (lags 0 to $n-1$)
3.**Create hybrid buffer**: Use fitted values for lags $0$ to $n-1$, original values for lags $n$ to $P-1$
4.**Average the hybrid buffer**: Simple mean of all $P$ values
This produces a smoothed estimate that incorporates short-term trend extrapolation. The fitted portion projects recent momentum while the original portion anchors to historical context.
Note: Despite some references calling this "cubic polynomial fitting," the actual implementation uses **linear regression** (first-degree polynomial: $y = \text{intercept} + \text{slope} \times x$).
1.**Lag Increases with Smoothness**: Blackman-Harris has the best noise rejection but also the most lag. For fast signals, consider Hanning or even Rectangular (which degrades to SMA).
2.**Least Squares Is Not Magic**: LS mode adds trend extrapolation but can overshoot during reversals. It works best in trending markets, not choppy conditions.
3.**Large Periods Amplify Lag**: FIR filters have inherent delay of approximately $P/2$ bars. Period 50 means ~25 bars of lag regardless of window choice.
4.**isNew Parameter Matters**: When processing live ticks within the same bar, use `Update(value, isNew: false)`. When a new bar opens, use `isNew: true` (default). Incorrect usage corrupts internal state.
5.**NaN Handling**: Non-finite inputs (NaN, ±Infinity) are replaced with the last valid value. Consecutive NaN inputs maintain the last known good value. After `Reset()`, the first valid input establishes the baseline.
6.**Warmup Period**: AFIRMA requires `period` bars before `IsHot` becomes true. During warmup, it uses available data with proportionally adjusted weights.
## References
- Harris, F. J. (1978). "On the use of windows for harmonic analysis with the discrete Fourier transform." *Proceedings of the IEEE*, 66(1), 51-83.
- Nuttall, A. H. (1981). "Some windows with very good sidelobe behavior." *IEEE Transactions on Acoustics, Speech, and Signal Processing*, 29(1), 84-91.