Files
QuanTAlib/lib/trends_FIR/lanczos/Lanczos.md
T
Miha Kralj 90d5638008 Add new moving average implementations: LTMA, MCNMA, NLMA, NMA, NYQMA, RAIN, and TRAMA
- LTMA (Linear Trend Moving Average): Introduces a predictive moving average using dual cascaded EMAs for trend estimation.
- MCNMA (McNicholl EMA): Implements a zero-lag TEMA using a cascaded EMA structure for enhanced responsiveness.
- NLMA (Non-Lag Moving Average): Utilizes a damped cosine kernel to achieve reduced lag in moving averages.
- NMA (Natural Moving Average): Adapts smoothing based on volatility profiles using a square-root kernel.
- NYQMA (Nyquist Moving Average): Applies the Nyquist-Shannon theorem to prevent aliasing in cascaded moving averages.
- RAIN (Rainbow Moving Average): Combines multiple SMA layers with weighted averages for multi-scale smoothing.
- TRAMA (Trend Regularity Adaptive Moving Average): Adapts smoothing based on the frequency of new highs and lows in price data.
2026-02-20 21:40:32 -08:00

4.6 KiB

LANCZOS: Lanczos (Sinc) Window Moving Average

"Cornelius Lanczos used the sinc function to reconstruct band-limited signals from discrete samples. Apply it to price data and you get a moving average that respects the Nyquist limit while your competitors are still using SMAs."

LANCZOS applies the normalized sinc function \text{sinc}(x) = \sin(\pi x)/(\pi x) as a symmetric FIR window, producing a moving average with near-ideal low-pass frequency characteristics. The sinc function is the impulse response of the perfect brick-wall low-pass filter; windowing it to finite length trades sharp cutoff for practical realizability. The result is a smoother with minimal Gibbs phenomenon ringing and excellent passband flatness, at the cost of small negative sidelobe weights that can cause minor overshooting on sharp price discontinuities.

Historical Context

Cornelius Lanczos (1893-1974) was a Hungarian-American mathematician and physicist who made foundational contributions to applied mathematics, including the Lanczos algorithm for eigenvalue computation, the Lanczos tau method for differential equations, and the Lanczos sigma factor for reducing Gibbs phenomenon in Fourier series. His 1956 book Applied Analysis introduced the sinc-based window that bears his name.

The Lanczos window is the simplest sinc-kernel window: a single lobe of the sinc function, truncated to the filter length. Higher-order Lanczos kernels (Lanczos-2, Lanczos-3) multiply \text{sinc}(x) \cdot \text{sinc}(x/a) for sharper cutoff and are widely used in image resampling (e.g., the default resizer in FFmpeg and ImageMagick). For financial time series, the first-order Lanczos window provides a good balance between frequency selectivity and computational simplicity.

The key property distinguishing Lanczos from other window-based MAs is the sinc function's direct relationship to the ideal low-pass filter. While Hann, Hamming, and Blackman windows are ad-hoc designs optimized for sidelobe suppression, the Lanczos window starts from the theoretically optimal impulse response and truncates it, preserving the passband flatness that other windows sacrifice for sidelobe control.

Architecture & Physics

1. Weight Computation (One-Time)

For each position k \in [0, N-1], the normalized coordinate x = 2k/(N-1) - 1 maps to [-1, 1]. The Lanczos window value is:


w(k) = \text{sinc}(x) = \frac{\sin(\pi x)}{\pi x}, \quad w(0) = 1

The sinc function produces negative values for |x| > 1 in the general case, but within the [-1, 1] window, negative weights appear only near the edges where |x| approaches 1. These negative weights are retained (not clamped) for frequency-domain fidelity.

2. Normalization

Weights are normalized to sum to 1.0, ensuring the filter preserves constant (DC) signals exactly.

3. FIR Convolution

Standard weighted convolution over the circular buffer. O(N) per bar. The symmetric weight structure enables potential paired-multiplication optimization (summing symmetric buffer pairs before multiplying by the shared weight).

Mathematical Foundation

The Lanczos window for a filter of length N:


w[k] = \text{sinc}\!\left(\frac{2k}{N-1} - 1\right), \quad k = 0, 1, \ldots, N-1

where:


\text{sinc}(x) = \begin{cases} 1 & x = 0 \\ \frac{\sin(\pi x)}{\pi x} & x \neq 0 \end{cases}

Frequency response: The continuous sinc function has an ideal rectangular frequency response (brick-wall low-pass). Truncation introduces sidelobes at approximately -13 dB for the first sidelobe (comparable to the rectangular window), with subsequent sidelobes decaying as 1/f. The passband flatness is superior to most other windows of the same length.

Normalized output:


\text{LANCZOS}_t = \frac{\sum_{k=0}^{N-1} w[k] \cdot x_{t-k}}{\sum_{k=0}^{N-1} w[k]}

Default parameters: period = 14, minPeriod = 2.

Pseudo-code (streaming):

// One-time weight computation
for k = 0 to period-1:
    x = 2k/(N-1) - 1
    if |x| < 1e-10:
        w[k] = 1.0
    else:
        w[k] = sin(π·x) / (π·x)
normalize(w)

// Per-bar convolution
buffer.push(price)
if count < period: return price
return Σ buffer[j] * w[j]

Resources

  • Lanczos, C. (1956). Applied Analysis. Prentice-Hall. Reprinted by Dover, 1988.
  • Duchon, C.E. (1979). "Lanczos Filtering in One and Two Dimensions." Journal of Applied Meteorology, 18(8), 1016-1022.
  • Oppenheim, A.V. & Schafer, R.W. (2009). Discrete-Time Signal Processing, 3rd ed. Prentice Hall. Section 7.2: Properties of Commonly Used Windows.
  • Turkowski, K. (1990). "Filters for Common Resampling Tasks." In Graphics Gems I, Academic Press. pp. 147-165.