# Savitzky-Golay Filter

Also known as: SG filter, Savitzky-Golay smoothing, polynomial smoothing filter.
A Trend concept (Digital filters & smoothers) in the LuxAlgo Library, with 1 indicator implementation.

## What is a Savitzky-Golay Filter?

A Savitzky-Golay filter smooths a series by fitting a low-degree polynomial, by least squares, to a sliding window of points and taking the fitted value at the window's center. Abraham Savitzky and Marcel Golay published it in Analytical Chemistry in 1964 to clean noisy spectra. Their key observation was that, for evenly spaced data, the fit collapses into a fixed set of weights: the filter is a weighted moving average whose coefficients depend only on the window length and the polynomial degree.

That is the source of its reputation for preserving peaks. A centered [SMA](https://www.luxalgo.com/library/concept/sma/) is the degree-zero (and degree-one) case and flattens every peak it spans; a quadratic or quartic fit can bend with the data, so turning points keep more of their height and width at the same window length, at the cost of passing more noise. On a price chart the centered form needs bars on both sides, so its newest values are provisional and revise as bars arrive, the end-point problem it shares with [LOESS](https://www.luxalgo.com/library/concept/loess-smoothing/) and the [Whittaker–Henderson smoother](https://www.luxalgo.com/library/concept/whittaker-henderson-smoother/).

## How it's calculated

A centered least-squares polynomial fit, equivalent to a fixed weighted average over the window.

```
y_hat_t = Σ c_j × y_(t+j), summed over j = -m to m
c = first row of (A^T × A)^(-1) × A^T, with A_(j,k) = j^k
Window 5, degree 2 or 3: c = (-3, 12, 17, 12, -3) / 35
Window 7, degree 2 or 3: c = (-2, 3, 6, 7, 6, 3, -2) / 21

  t: bar index of the point being smoothed
  y_t: input value at bar t (typically the close)
  y_hat_t: smoothed value at bar t
  m: half-window; the window spans 2m + 1 bars
  j: offset from the center bar, -m to m
  c_j: filter weight for offset j (c is the vector of weights)
  p: polynomial degree (commonly 2 to 4, below 2m + 1)
  A: (2m + 1) × (p + 1) matrix of powers j^k; A^T is its transpose
  k: polynomial power, 0 to p
```

Degrees 2 and 3 give identical center weights, as do degrees 0 and 1 (the plain centered average).

The centered form uses m future bars, so the newest m values revise; evaluating the fit at the newest bar of a trailing window is causal, and with degree 1 that endpoint fit is the LSMA.

Other weight sets from the same fit return smoothed first and second derivatives.

## How traders use it

- As a peak-preserving smoother for analysis, where the height and timing of past swings matter, such as cycle studies and swing labeling.
- As a derivative estimator: smoothed slope and curvature from the same fit feed momentum and [trend acceleration](https://www.luxalgo.com/library/concept/trend-acceleration-inflection/) reads with less noise than raw differences.
- As an oscillator pre-filter: a short, low-degree window removes jitter while keeping turning points near where they occurred, provided the end-point revisions are handled.

## Savitzky-Golay filter vs related smoothers

- **LSMA** (https://www.luxalgo.com/library/concept/lsma/): The LSMA fits a straight line to a trailing window and plots its endpoint. Savitzky-Golay usually fits a curve and plots the center; its causal endpoint form with degree one is the LSMA.
- **LOESS Smoothing** (https://www.luxalgo.com/library/concept/loess-smoothing/): LOESS refits a distance-weighted local polynomial at every point, which suits uneven spacing and, in its robust form, outliers. Savitzky-Golay uses an unweighted fit on evenly spaced data, which collapses into one fixed set of coefficients.
- **Windowed FIR Smoothing** (https://www.luxalgo.com/library/concept/windowed-fir-smoothing/): Both are fixed-weight symmetric FIR filters. Tapered windows keep every weight positive and smooth harder; from degree 2 up, Savitzky-Golay weights turn negative near the edges, which is what lets them preserve peaks.

## FAQ

### Does the Savitzky-Golay filter repaint?

The centered version does on its newest m bars, because each smoothed point uses m bars after it. Causal endpoint versions do not repaint but give up much of the peak preservation and tend to overshoot at turns.

### How do I choose the window and degree?

The window sets how much noise is averaged away; the degree sets how much curvature survives. A longer window with a higher degree keeps peaks while smoothing more; a degree near the window size barely smooths at all.

### Is a Savitzky-Golay filter better than a moving average?

For describing past swings, often yes: it keeps peaks and turning points sharper at the same window length. For live trading the right edge decides, and there a centered fit revises while a causal fit overshoots, so it does not remove the lag-versus-noise trade-off every smoother faces.

## Implementations in the Library

- Savitzky-Golay Filter (LuxAlgo): https://www.luxalgo.com/library/indicator/savitzky-golay-filter/

## Related concepts

- Gaussian Filter: https://www.luxalgo.com/library/concept/gaussian-filter/
- Butterworth Filter: https://www.luxalgo.com/library/concept/butterworth-filter/
- Chebyshev Filters: https://www.luxalgo.com/library/concept/chebyshev-filters/
- Elliptic Filter: https://www.luxalgo.com/library/concept/elliptic-filter/
- Bessel Filter: https://www.luxalgo.com/library/concept/bessel-filter/
- Ehlers SuperSmoother: https://www.luxalgo.com/library/concept/ehlers-supersmoother/
- Ehlers Instantaneous Trendline: https://www.luxalgo.com/library/concept/ehlers-instantaneous-trendline/
- Ultimate Smoother: https://www.luxalgo.com/library/concept/ultimate-smoother/
- Swiss Army Knife Filter: https://www.luxalgo.com/library/concept/swiss-army-knife-filter/
- Windowed FIR Smoothing: https://www.luxalgo.com/library/concept/windowed-fir-smoothing/

---

Source: https://www.luxalgo.com/library/concept/savitzky-golay-filter/ (LuxAlgo Library, the encyclopedia of trading & technical analysis). Free to use with attribution: https://www.luxalgo.com/library/license/