Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 40 ยท Components and Decomposition

Trend and Seasonality by Regression

Model trend and seasonality with ordinary regression: linear and polynomial trends, seasonal dummy variables, Fourier terms, and how to extrapolate them for forecasts.

Decomposition describes the past. If you also want a forecast, one of the most transparent approaches is plain regression on time. You build columns for the trend and the seasonality, fit them with least squares, and extend the same columns into the future. The model is easy to explain, easy to extend with extra predictors, and a strong baseline against fancier methods.

Modelling the trend

Give the model a time counter t = 0, 1, 2, ... and fit it to the series.

  • Linear trend: y = a + b * t.
  • Polynomial trend: add t^2, t^3. Handy within the sample, but dangerous to extrapolate because a quadratic eventually shoots off.
  • Log-linear (exponential) trend: regress log(y) on t; the slope is roughly the growth rate per period.
Python Linear versus quadratic trend, and extrapolation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The quadratic fits the past better and, here, also forecasts better because the data truly curve. With real data, always judge a trend by its forecast error on held-out recent points, not by its fit.

Seasonal dummy variables

For a season of length m create m - 1 binary columns: "is it February?", "is it March?", and so on. January is the baseline (absorbed by the intercept). Each coefficient is the average difference between that month and the baseline, once the trend is accounted for.

Python Trend plus monthly dummies with pandas and statsmodels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The coefficient of t is about 0.4 (the true trend slope) and the month coefficients recover the shape we simulated, relative to January. Forecasting means building the same columns for future dates and calling fit.predict.

Fourier terms for seasonality

Dummies need m - 1 columns, which is wasteful for long periods (365 for daily data) and cannot be used for non-integer periods. Fourier terms describe a seasonal curve as a sum of sine and cosine waves:

season(t) = sum over k of [ a_k * sin(2*pi*k*t / m) + b_k * cos(2*pi*k*t / m) ]

Choose K pairs (k = 1..K). A small K gives a smooth seasonal shape; a larger K allows sharper features, up to m / 2 pairs, which is the same flexibility as dummies. Daily data with a yearly pattern often needs only K of 3 to 10, so 6 to 20 columns instead of 364.

Python Choosing K for Fourier terms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The residual error stops improving once K reaches the true complexity (3 here), and the AIC stops falling and starts rising for larger K. That is the signal to stop adding harmonics.

Forecasting with the fitted model

Because the columns depend only on the calendar, forecasting is mechanical: create t and the seasonal columns for the future dates and multiply by the fitted coefficients.

Python Forecast 12 months ahead with trend plus Fourier terms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Strengths and weaknesses

  • Strengths: transparent coefficients, handles several seasonal periods at once (add Fourier terms for 7 and 365.25), works with extra predictors such as price or promotions, trivial to forecast.
  • Weakness 1: the trend and seasonality are fixed forever. If the pattern drifts, errors grow.
  • Weakness 2: residuals are usually autocorrelated, which makes the usual standard errors and prediction intervals too optimistic. Fix this by modelling the residuals with ARIMA, which is the idea behind ARIMAX.
  • Polynomial trends must never be extrapolated far.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. With monthly data, how many dummy columns do you need for the seasonality when the model has an intercept?

    One month is the baseline absorbed by the intercept; using all 12 would make the columns perfectly collinear.
  2. 2. Why are Fourier terms useful for daily data with a yearly pattern?

    Smooth seasonal shapes can be captured with a handful of harmonics, and non-integer periods are allowed.
  3. 3. Which check best decides between a linear and a quadratic trend for forecasting?

    Flexible trends always fit the past better; only out-of-sample error shows forecasting skill.
  4. 4. What problem usually remains in the residuals of a trend-plus-season regression?

    Neighbouring residuals are typically correlated, so intervals are too narrow unless the residuals are modelled.

Summary

  • Regress on a time counter for the trend; prefer linear or log-linear for extrapolation.
  • Seasonal dummies use m - 1 columns; Fourier terms use 2K and scale to long or fractional periods.
  • Pick K by AIC or by out-of-sample error.
  • Forecasting means rebuilding the same columns for future dates.
  • The fit is rigid and its errors are autocorrelated, so treat it as a baseline or pair it with an ARIMA error model.

Next: Calendar Effects and Cycles