# Quantile Regression

Also known as: robust/Theil-Sen.
A Statistics concept (Regression & filtering) in the LuxAlgo Library, with 2 indicator implementations.

## What is Quantile Regression?

Quantile regression fits a line to a chosen quantile of the data instead of its mean. Ordinary [linear regression](https://www.luxalgo.com/library/concept/linear-regression/) minimizes squared errors and delivers the conditional mean; quantile regression minimizes a tilted absolute-error loss (the pinball or check loss) in which, for a target quantile q, points above the fit are weighted by q and points below by 1 minus q. Setting q to 0.5 yields median regression, the least-absolute-deviations fit; q of 0.05 and 0.95 yield outer lines with roughly 5% of the data beyond each, in-sample.

The method was formalized by Roger Koenker and Gilbert Bassett in a 1978 Econometrica paper, though the median-regression idea is far older, tracing to Boscovich's eighteenth-century geodesy and Laplace's refinements of it. The robust-slope relatives grouped with it have their own lineage: the Theil-Sen estimator, the median of slopes over all pairs of points, dates to Henri Theil in 1950 and Pranab Sen in 1968.

On charts it shows up mainly as robust fits and channels. A median-slope fit resists the outlier bars that drag least-squares lines around, and outer quantile fits form a channel calibrated to how often price actually closed outside it, rather than assuming normality the way sigma-multiple regression channels do. The Theil-Sen slope is a related robust choice wherever a trend estimate has to survive spikes, gaps, and bad prints.

For traders the draw is honesty about distribution shape. Returns are skewed and heavy-tailed, and tools built on standard deviations, from [z-scores](https://www.luxalgo.com/library/concept/z-score/) to sigma bands, quietly assume otherwise. Fitting several quantiles at once profiles the conditional distribution directly, complementing [distribution-of-returns profiling](https://www.luxalgo.com/library/concept/distribution-of-returns-profiling/) done on raw returns, and curved variants, spline or [polynomial](https://www.luxalgo.com/library/concept/polynomial-regression/) bases fitted under pinball loss, extend the same idea to nonlinear trends. The cost is computational and interpretive: quantile fits need enough data per quantile to be stable, especially in the tails.

## How to identify quantile regression tools on a chart

Quantile fits look like ordinary regression lines or channels; the giveaway is percentile-based, potentially asymmetric construction rather than mean-plus-sigma symmetry.

1. Check the indicator's settings for a quantile or percentile parameter (0.5, or a pair like 0.05/0.95) instead of a standard-deviation multiplier; that marks a quantile fit.
2. Compare the center line with a least-squares fit on the same window: after a large spike the quantile or Theil-Sen line holds its slope while least squares tilts toward the outlier.
3. Count closes outside the outer lines: a 5th/95th channel should have left roughly one bar in twenty beyond each line, in-sample.
4. Look for asymmetry: in skewed data the upper and lower lines sit at different distances from the median, something sigma channels never show.
5. Watch stability at the window's edge; quantile fits on short lookbacks can jump when single bars enter or leave the sample.

## How it's calculated

A fitted line that tracks a chosen quantile of price, such as the median or an upper quartile, rather than the average.

```
Choose a_tau, b_tau to minimize Σ_{i=1..n} rho_tau(y_i - (a_tau + b_tau × x_i))
rho_tau(u) = tau × u, if u >= 0
rho_tau(u) = (tau - 1) × u, if u < 0
Fitted line: Q_tau(x) = a_tau + b_tau × x
tau = 0.5 gives the median (least absolute deviations) line
Bands: refit with tau_lo and tau_hi (commonly 0.25 and 0.75, or 0.1 and 0.9)
Theil-Sen slope: b_TS = median( (y_j - y_i) / (x_j - x_i) ) over all pairs i < j
Theil-Sen intercept: a_TS = median( y_i - b_TS × x_i )

  y_i: price at bar i (typically the close)
  x_i: regressor, typically the bar index
  i, j: bar positions inside the window
  n: regression window length in bars
  tau: target quantile, between 0 and 1 (0.5 = median)
  rho_tau(u): pinball (check) loss applied to each residual
  u: a residual, y_i - (a_tau + b_tau × x_i)
  a_tau, b_tau: intercept and slope of the tau-quantile line
  Q_tau(x): fitted tau-quantile of y at x
  tau_lo, tau_hi: lower and upper band quantiles
  b_TS, a_TS: Theil-Sen slope and intercept
  median(...): middle value of the listed set
  Σ_{i=1..n}: sum over the n bars of the window
```

Introduced by Koenker and Bassett (1978); the fit has no closed form and is solved by linear programming.

Each tau targets a conditional quantile instead of the conditional mean, so the lines resist outliers better than ordinary least squares.

Theil-Sen is the related robust estimator: a single median-of-pairwise-slopes line, not a per-quantile fit.

## How traders use it

- Robust trend measurement: median (q = 0.5) or Theil-Sen fits give slope estimates a single news spike cannot hijack, useful on gappy or thinly traded instruments.
- Quantile channels: outer fits such as the 5th and 95th percentiles frame stretch zones whose in-sample exceedance rate is set by construction; out-of-sample that rate can drift, so excursions are context, not automatic fades.
- Distribution-aware projection: fitting several quantiles at once sketches the whole conditional distribution of price or returns, which matters when the tails drive the decision more than the average does.
- Tail-risk framing: a fitted lower quantile of returns acts as an empirical, value-at-risk-style line, and its distance from the median tracks how the downside tail widens or tightens.
- As an input to scenario tools: empirical quantile bands give [probability cones](https://www.luxalgo.com/library/concept/probability-cones/) and [Monte Carlo price paths](https://www.luxalgo.com/library/concept/monte-carlo-price-paths/) a data-driven alternative to normal-distribution widths.

## Quantile regression vs related statistical fits

- **Linear Regression** (https://www.luxalgo.com/library/concept/linear-regression/): Least squares estimates the conditional mean, and a single outlier bar moves the whole line. Quantile regression targets the median or any other quantile, bending far less to extremes and describing the distribution's edges as well as its center.
- **Polynomial Regression** (https://www.luxalgo.com/library/concept/polynomial-regression/): Polynomial regression changes the shape of the fit, curves instead of straight lines, while still targeting the mean. Quantile regression changes the target instead, and the two combine: curved bases fitted under pinball loss draw curved quantile channels.
- **Percentile Rank** (https://www.luxalgo.com/library/concept/percentile-rank/): Percentile rank scores where the latest value sits inside a rolling window's distribution, with no model of trend. Quantile regression fits trend and quantile jointly, describing where the distribution's edges run as a function of time rather than scoring one observation.

## FAQ

### How is quantile regression different from ordinary linear regression?

Ordinary least squares estimates the conditional mean by minimizing squared errors, which makes it outlier-sensitive and silent about the tails. Quantile regression estimates any chosen quantile by minimizing asymmetrically weighted absolute errors, so extreme bars pull on it far less, and fitting several quantiles (5th, 50th, 95th) describes the edges of the distribution as well as its center.

### What is the Theil-Sen estimator?

A robust slope estimate: compute the slope between every pair of points in the window and take the median. Because a median ignores extreme values, a handful of outlier bars barely moves the fit, whereas least squares lets large deviations dominate through squaring. It is a common drop-in wherever a trend slope needs to survive spikes, gaps, and bad prints.

### What quantiles are usually plotted on a chart?

The median (0.5) as the central trend, plus a symmetric outer pair such as 0.05/0.95 or 0.25/0.75. The outer choice sets the channel's meaning: quartile lines contain the middle half of closes, while 5th/95th lines frame rare-excursion territory. Some tools fit a ladder of quantiles and render it as a gradient fan.

### Does quantile regression assume a normal distribution?

No, and that is its main appeal on financial data. Each quantile line is estimated directly from the data through the pinball loss, with no assumption about the error distribution's shape. Skew and fat tails show up as asymmetric or wide bands instead of being averaged away.

### Why can fitted quantile lines cross each other?

Because each quantile is usually estimated independently, small samples can produce a 90th-percentile line dipping under the 80th, which is impossible for true quantiles. It is an estimation artifact; practical fixes include longer windows, joint fitting with non-crossing constraints, or treating crossed lines as a warning that the window is too short.

### Is a quantile channel better than a standard-deviation channel?

It answers a different question. Sigma channels are cheap and familiar, but their coverage claims lean on near-normal errors. Quantile channels set in-sample coverage by construction and handle skew naturally, at the cost of heavier computation and noisier tails. On heavy-tailed series the quantile version's stated probabilities are usually the more honest.

## Implementations in the Library

- Spline Quantile Regression Channel (LuxAlgo): https://www.luxalgo.com/library/indicator/spline-quantile-regression-channel/
- Bitcoin Expectile Model (LuxAlgo): https://www.luxalgo.com/library/indicator/bitcoin-expectile-model/

## Related concepts

- Linear Regression: https://www.luxalgo.com/library/concept/linear-regression/
- Polynomial Regression: https://www.luxalgo.com/library/concept/polynomial-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/quantile-regression/ (LuxAlgo Library, the encyclopedia of trading & technical analysis). Free to use with attribution: https://www.luxalgo.com/library/license/