Overview
In “Are Transformers Effective for Time Series Forecasting?” Zeng et al. showed that a single linear layer from the look-back window to the horizon outperformed several Transformer models on standard long-horizon benchmarks. DLinear adds one twist: a moving average splits the input into a trend and a remainder, and each part gets its own linear map.
NLinear, its sibling, subtracts the last value before the linear layer and adds it back, which helps when the series shifts level. These models train in seconds, their weights are directly interpretable, and they belong in every long-horizon benchmark.
How it works
For an input window \(x\in\mathbb R^{L}\) and moving-average kernel \(k\):
\[x^{t}=\mathrm{MA}_k(x),\qquad x^{s}=x-x^{t}\] \[\hat y=W_t\,x^{t}+W_s\,x^{s}+b,\qquad W_t,W_s\in\mathbb R^{H\times L}\]For multivariate data the same maps are applied per channel (or shared across channels). NLinear uses \(\hat y=W(x-x_L)+x_L\).
When to use it
Use it for
- Long-horizon benchmarks (ETT, electricity, traffic, weather)
- Fast, explainable forecasts at scale
- Sanity-checking complex deep models
Avoid it for
- Strongly non-linear dynamics or regime changes
- Short horizons where boosting on lags may do better
Key settings
| Setting | What it controls | Typical value |
|---|---|---|
input_size (L) | Look-back window | 96–720 |
moving average k | Trend smoothing window | 25 |
individual | Separate weights per channel | True for heterogeneous channels |
Python example
import numpy as np import pandas as pd from sklearn.linear_model import Ridge URL = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-min-temperatures.csv" y = pd.read_csv(URL, index_col="Date", parse_dates=True)["Temp"].to_numpy() L, H, K = 90, 14, 25 # look-back, horizon, moving-average kernel def decompose(windows, k=K): """Trend = centred moving average (edges padded), remainder = window - trend.""" pad = np.pad(windows, ((0, 0), (k // 2, k // 2)), mode="edge") trend = np.stack([np.convolve(w, np.ones(k) / k, mode="valid") for w in pad]) return trend, windows - trend idx = np.arange(len(y) - L - H + 1) X = np.stack([y[i:i + L] for i in idx]) # inputs: last L days Y = np.stack([y[i + L:i + L + H] for i in idx]) # targets: next H days split = int(0.8 * len(idx)) trend, rem = decompose(X) # DLinear = one linear map for the trend + one for the remainder (here with a little ridge penalty) lin_t = Ridge(alpha=1.0).fit(trend[:split], Y[:split]) lin_r = Ridge(alpha=1.0).fit(rem[:split], Y[:split] - lin_t.predict(trend[:split])) pred = lin_t.predict(trend[split:]) + lin_r.predict(rem[split:]) mae_by_h = np.abs(pred - Y[split:]).mean(axis=0) print("MAE by horizon (days 1, 7, 14):", mae_by_h[[0, 6, 13]].round(2)) print("seasonal-naive MAE (same day last year):", np.abs(y[-365 * 2:-365] - y[-365:]).mean().round(2))
Tips & pitfalls
- Longer look-back windows usually help linear models, unlike many Transformers.
- Plot the rows of W: they show which past lags drive each horizon.
- The example fits DLinear in closed form with ridge regression, no deep-learning framework needed.
References
- Zeng, A., Chen, M., Zhang, L., & Xu, Q. (2023). Are Transformers Effective for Time Series Forecasting? Proceedings of the AAAI Conference on Artificial Intelligence, 37(9), 11121–11128.