# Polynomial Regression

A Statistics concept (Regression & filtering) in the LuxAlgo Library, with 4 indicator implementations.

## What is Polynomial Regression?

Polynomial regression fits a curved line to price by least squares: instead of a straight [linear regression](https://www.luxalgo.com/library/concept/linear-regression/), the model adds squared, cubed, and higher powers of the bar index, then solves for the coefficients that minimize the squared distance between curve and price over a lookback window. Degree controls flexibility: degree 1 is a straight line, degree 2 a parabola with one bend, degree 3 allows two bends. Chart implementations usually draw the fitted curve through the window, often with bands offset by a multiple of the residual standard deviation, in the spirit of a [standard-error channel](https://www.luxalgo.com/library/concept/standard-error-channel/).

The appeal is that real swings curve, and a straight fit misrepresents them; a low-degree polynomial can trace an arcing trend and expose acceleration or rollover in its right-edge slope. The costs are just as structural. Higher degrees chase noise and oscillate hardest near the window's edges, exactly where decisions are made; the entire curve is refit on every bar, so drawn history revises; and extrapolating a polynomial beyond its window is fragile, with small coefficient changes swinging the projected path widely.

The edge instability has a name in numerical analysis: the Runge phenomenon, the tendency of high-degree polynomial fits to oscillate wildly near the ends of their interval even when they behave in the middle. It is why careful implementations center and rescale the bar index before fitting, or work in an orthogonal polynomial basis, choices that stabilize the arithmetic without changing the statistical trade-off. The degrees of freedom are worth counting too: a degree-d fit estimates d+1 coefficients, so on short windows a high degree leaves the curve nearly free to reproduce the data, fit without meaning.

The Library shows the design space. LuxAlgo's Polynomial Regression Extrapolation draws the fitted curve and its forward extension, making the scenario-path use explicit; Daily Polynomial Regressions re-anchors a fresh fit to each session so the curve becomes an evolving intraday mean; and the Spline Quantile Regression Channel replaces the single global polynomial with piecewise fits through chosen quantiles, trading global curvature for local flexibility. Across all of them the honest workflow is the same: read the curve for shape, standardize the residual before comparing stretch across regimes, and treat any projection as an assumption made visible, in the spirit of [dominant cycle](https://www.luxalgo.com/library/concept/dominant-cycle-measurement/) and other estimated-structure tools.

## How to calculate a polynomial regression

The fit is ordinary least squares with extra columns for the curvature terms.

1. Choose a window N and a degree d. On charts, degree 2 or 3 covers most useful curvature; higher degrees mostly model noise.
2. Build the predictors: the bar index and its powers up to d, usually centered or rescaled first for numerical stability.
3. Solve the least-squares problem for the d+1 coefficients that minimize the sum of squared residuals between curve and price.
4. Draw the fitted curve across the window, optionally adding bands at multiples of the residual standard deviation, and refit as each new bar arrives.
5. Standardize the residual, a [z-score](https://www.luxalgo.com/library/concept/z-score/) against its own recent spread, when comparing stretch readings across windows, symbols, or volatility regimes.

## How it's calculated

A least-squares fit of a degree-k polynomial to price over a lookback window, producing a curved trend line through recent data.

```
y_hat_t = b_0 + b_1 × t + b_2 × t^2 + ... + b_k × t^k
The coefficients b_0..b_k minimize SSE = Σ over t = 1..n of (P_t - y_hat_t)^2
b = (X^T × X)^(-1) × X^T × y, with row t of X equal to [1, t, t^2, ..., t^k]
RMSE = sqrt(SSE / n)

  P_t: source price at bar t (typically the close)
  y_hat_t: fitted polynomial value at bar t
  t: bar index inside the window, 1..n
  n: lookback window length in bars (commonly 100)
  k: polynomial degree (commonly 2 to 4; k = 1 is linear regression)
  b_0..b_k: fitted coefficients
  b: coefficient vector [b_0, ..., b_k]
  SSE: sum of squared residuals over the window
  X: n × (k + 1) design (Vandermonde) matrix of the powers of t
  X^T: transpose of X
  y: column vector of the n source prices
  RMSE: root mean squared error of the fit
```

Charting versions draw a channel by offsetting the fitted curve by a multiple of RMSE (commonly 2) or by the maximum deviation; some use the unbiased divisor n - k - 1 in RMSE.

High degrees oscillate near the window edges, and the whole curve repaints as the window slides forward.

Solvers usually center or rescale t before fitting to avoid numerical instability.

## How traders use it

- As a curved trend baseline: the right-edge slope and its change give a read on trend direction and [acceleration or inflection](https://www.luxalgo.com/library/concept/trend-acceleration-inflection/) that a straight fit smooths away.
- As a mean-reversion frame: distance from the fitted curve, measured in residual standard deviations, flags stretched excursions, with band tags treated as fade candidates in ranging conditions rather than automatic signals.
- As a detrender: subtracting the fitted polynomial isolates the residual wiggle, a common preprocessing step before cycle or oscillator analysis.
- As a projection: some tools extend the curve forward as a scenario path. Treat it as a sketch of what happens if current curvature persists, not a forecast; reliability decays quickly outside the fitted window.
- As a session-anchored mean: refitting the polynomial from each day's open turns it into an evolving intraday baseline with bands, a curved alternative to session averages for framing the day's stretch.

## Polynomial Regression vs related concepts

- **Linear Regression** (https://www.luxalgo.com/library/concept/linear-regression/): Linear regression is the degree-1 special case: one slope, no curvature. The polynomial's extra terms track arcing moves better inside the window, but they add overfitting and edge-instability risk the straight line does not have.
- **LOESS Smoothing** (https://www.luxalgo.com/library/concept/loess-smoothing/): LOESS fits many small local regressions and stitches them together, so its shape is driven by neighborhoods of data. A polynomial regression fits one global curve, so a shock at one end of the window bends the fit everywhere.
- **Polynomial Regression Band** (https://www.luxalgo.com/library/concept/polynomial-regression-band/): The band is the packaged application: the same fitted curve plus envelopes at a multiple of the residual deviation. The regression itself is the estimator; the band adds the volatility casing used for tag-and-fade or breakout reads.

## FAQ

### What degree should a polynomial regression use on charts?

Low. Degree 2 or 3 captures the curvature of most swings; each added degree buys flexibility at the cost of fitting noise and wilder behavior near the window edges. Degree is a bias-variance dial rather than a quality setting, so increase it only when residuals show clear systematic shape the current degree cannot express.

### Does polynomial regression repaint?

As usually drawn, yes: the whole curve is refit each bar, so plotted history bends as new data arrives, and a touch that appeared on the curve earlier may vanish. The live right-edge value is what a system actually had at the time, so signal logic should be built on that, evaluated bar by bar ([repaint-safe engineering](https://www.luxalgo.com/library/concept/repaint-safe-engineering/)).

### Can a polynomial regression be projected into the future?

Mechanically it extrapolates, and some overlays draw the extension. Statistically it is the weakest use: outside the fitted window polynomial paths diverge quickly, and refitting one bar later can swing the projection substantially. Treat an extension as a visual scenario that assumes current curvature persists, and expect it to be revised.

### What is the Runge phenomenon and why does it matter here?

A classical result about polynomial fits: as degree rises, the curve can develop large oscillations near the ends of its interval even while fitting the middle well. On charts the interval end is the live edge, so the pathology lands exactly where trades are decided. It is the mathematical reason chart fits stay at low degrees and why implementations center or orthogonalize the predictors before solving.

### How do polynomial fits compare with splines?

A polynomial is one global curve: every data point influences the whole shape, and flexibility comes only from raising the degree. Splines split the window into segments with low-degree pieces joined smoothly, so flexibility is local and edge behavior is tamer. Spline and quantile-spline channels on charts trade the single readable equation for that local control, converging in spirit toward LOESS as segments shrink.

### How should the channel width around the curve be set?

The common convention offsets by multiples of the residual standard deviation, which assumes the residuals are roughly stable across the window. Volatility shifts and fat tails break that symmetry, leaving bands too tight in wild regimes and too loose in quiet ones. Alternatives include quantile fits through chosen [percentile ranks](https://www.luxalgo.com/library/concept/percentile-rank/) of the residuals, which set asymmetric, distribution-aware envelopes at the cost of extra estimation.

## Implementations in the Library

- Polynomial Regression Extrapolation (LuxAlgo): https://www.luxalgo.com/library/indicator/polynomial-regression-extrapolation/
- Daily Polynomial Regressions (LuxAlgo): https://www.luxalgo.com/library/indicator/daily-polynomial-regressions/
- Spline Quantile Regression Channel (LuxAlgo): https://www.luxalgo.com/library/indicator/spline-quantile-regression-channel/
- Cubic Bézier Curve Extrapolation (LuxAlgo): https://www.luxalgo.com/library/indicator/cubic-b-zier-curve-extrapolation/

## Related concepts

- Linear Regression: https://www.luxalgo.com/library/concept/linear-regression/
- Quantile Regression: https://www.luxalgo.com/library/concept/quantile-regression/
- Kalman Filter: https://www.luxalgo.com/library/concept/kalman-filter/
- Hodrick-Prescott Filter: https://www.luxalgo.com/library/concept/hodrick-prescott-filter/
- Wavelet Decomposition: https://www.luxalgo.com/library/concept/wavelet-decomposition/
- FFT/spectral Analysis: https://www.luxalgo.com/library/concept/fft-spectral-analysis/
- Maximum-entropy Spectrum: https://www.luxalgo.com/library/concept/maximum-entropy-spectrum/
- Hilbert Transform: https://www.luxalgo.com/library/concept/hilbert-transform/
- Exponential Smoothing Forecasts: https://www.luxalgo.com/library/concept/exponential-smoothing-forecasts/
- LOESS Smoothing: https://www.luxalgo.com/library/concept/loess-smoothing/

---

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