Skip to content

Detrending

Remove a trend without confusing deviations with changes: known versus fitted trends, differencing, stationarity and historical information leakage.

Detrending removes a specified or estimated trend from a time series to study the deviations left behind. In quantitative finance, it can separate gradual movement in a measured variable from shorter-run variation. A price sitting below a fitted trend is only a model-relative deviation: it is not, by itself, evidence of undervaluation or a profitable buy signal.
For an additive deterministic trend, write
yt=m(t)+ut,y~t=yt−m^(t).y_t=m(t)+u_t, \qquad \widetilde y_t=y_t-\widehat m(t).

Here m(t)m(t) is the underlying trend and m^(t)\widehat m(t) is the reference actually removed. It may be known by assumption or estimated from observations. The detrended series retains the units of yty_t. Subtracting a fitted price trend produces price deviations, not percentage returns. If the model instead concerns log prices, its deviations are in log units.

What exactly was subtracted?

Take four synthetic index levels at equally spaced dates. Stipulate that the known trend is m(t)=5+2tm(t)=5+2t; these coefficients have not been fitted to the observations.
Observation time ttObserved level yty_tKnown trend 5+2t5+2tDetrended level yt−(5+2t)y_t-(5+2t)
0550
1871
289−1
311110

The two middle observations have the same level, yet lie on opposite sides of the rising reference path. Their detrended values answer “how far from the trend?” rather than “how much did the series change?”

Synthetic levels 5, 8, 8, 11 at times 0, 1, 2, 3 compared with known trend values 5, 7, 9, 11. Subtraction gives deviations 0, 1, minus 1, 0. Time differences instead give 3, 0, 3.
Synthetic levels 5, 8, 8, 11 at times 0, 1, 2, 3 compared with known trend values 5, 7, 9, 11. Subtraction gives deviations 0, 1, minus 1, 0. Time differences instead give 3, 0, 3.
Original synthetic illustration in index units. The dashed path is stipulated; solid markers are observations or their detrended values. Straight connectors guide the eye between equally spaced dates.
In real research, the trend is usually unknown. Linear detrending commonly estimates an intercept and slope by ordinary least squares, then subtracts the fitted line. For these same four observations, OLS gives
m^(t)=5.3+1.8t,(u^0,u^1,u^2,u^3)=(−0.3, 0.9, −0.9, 0.3).\widehat m(t)=5.3+1.8t, \qquad (\widehat u_0,\widehat u_1,\widehat u_2,\widehat u_3) =(-0.3,\ 0.9,\ -0.9,\ 0.3).
Those residuals differ from the known-trend deviations. “Detrended” is incomplete without the trend specification, fitting method and estimation window. Higher-order polynomials and smooth trend estimates are other possibilities, but a more flexible curve can absorb variation that the researcher hoped to explain. The statsmodels documentation makes its own convention explicit: its detrending function returns regression residuals for a chosen polynomial order.

Detrending and differencing change different things

A first difference is Δyt=yt−yt−1\Delta y_t=y_t-y_{t-1}. The example's differences are 3,0,33,0,3, leaving three changes from four observations. Trend subtraction leaves four deviations.

More fundamentally, the right transformation depends on the process. If yt=a+bt+uty_t=a+bt+u_t and utu_t is weakly stationary, removing the true line leaves utu_t. Differencing also gives a stationary series, but it changes the dynamics:

Δyt=b+ut−ut−1.\Delta y_t=b+u_t-u_{t-1}.
For independent, identically distributed utu_t with mean zero and positive finite variance σ2\sigma^2, the differenced disturbance has variance 2σ22\sigma^2 and lag-one correlation −1/2-1/2. That negative dependence comes from subtracting overlapping errors; it need not reflect an economic reversal mechanism. Brooks develops this distinction in Introductory Econometrics for Finance, second edition, §7.1.2, pp. 321–323.
Conversely, consider a random walk with drift, a fixed starting level and independent, zero-mean innovations:
yt=y0+bt+∑s=1tεs,Var(εs)=σ2>0.y_t=y_0+bt+\sum_{s=1}^{t}\varepsilon_s, \qquad \text{Var}(\varepsilon_s)=\sigma^2>0.

Even subtracting the true deterministic path y0+bty_0+bt leaves accumulated shocks with variance tσ2t\sigma^2. Detrending has not removed the unit root. Differencing this model instead leaves b+εtb+\varepsilon_t. A neat residual plot—or the four illustrative deviations above—cannot establish stationarity.

For the broader diagnosis, Stationarity and Unit Roots develops stochastic trends, spurious regression and unit-root tests. The full lesson requires membership; a public preview is available.

A historical fit can know the future

A trend fitted to the entire research sample uses later observations to define earlier deviations. A backtest that trades those earlier deviations therefore risks look-ahead bias. A centred smoother has the same problem whenever its window includes future observations.

For a signal used at a historical decision time, estimate the trend using only information available by that time, with a specified expanding or trailing window. Preserve the estimate that was available then; do not replace it later with a revised historical fit. If the signal uses a closing observation, its execution assumption must allow time to observe that close and calculate the signal.

This is the chronological discipline behind time-series cross-validation. Out-of-sample evaluation must cover the detrending step as well as the trading rule. Even a correctly timed estimate remains uncertain and may fail when the trend changes.

Sources

Last updated
  • detrending
  • time series
  • deterministic trend
  • stationarity
  • backtesting
← Back to the glossaryBack to top ↑