I5 · Machine Learning Foundations

Introduction to Time Series Forecasting

A time series is a sequence of measurements taken at regular intervals, such as daily rainfall or monthly sales. Forecasting predicts future values from past ones. Most series combine a trend (a long-term rise or fall), seasonality (a pattern that repeats with a fixed period, such as every 12 months) and noise. Simple forecasting methods include the naive forecast (repeat the last value), the seasonal naive forecast (repeat the value from one season ago), moving averages and exponential smoothing. Machine-learning models forecast by using lag features, past values of the series, as inputs. Time series must be validated in time order, never with random splits.

12 min Intermediate · Sublevel Intermediate Jump to the activity

What you need: The earlier topics of this module.

Definition

A sweet shop needs to know how much khoya to order for next month. Sales rise every year as the town grows, jump before Diwali and dip in the monsoon. Forecasting turns that history into a number for next month, and an honest range around it.

A time series is a sequence of measurements taken at regular intervals, such as daily rainfall or monthly sales. Forecasting predicts future values from past ones. Most series combine a trend (a long-term rise or fall), seasonality (a pattern that repeats with a fixed period, such as every 12 months) and noise. Simple forecasting methods include the naive forecast (repeat the last value), the seasonal naive forecast (repeat the value from one season ago), moving averages and exponential smoothing. Machine-learning models forecast by using lag features, past values of the series, as inputs. Time series must be validated in time order, never with random splits.

In simple words

To guess how many people will visit a temple next Sunday, you would look at recent Sundays, notice whether crowds are growing, and remember whether a festival is coming. Forecasting does exactly that with numbers: recent level, trend and seasonal pattern.

Overview

  • A time series is a sequence of measurements at regular intervals; forecasting predicts future values.
  • Most series combine trend, seasonality, cycles and noise.
  • Validation must respect time order: train on the past, test on later periods.
  • Naive and seasonal naive forecasts are baselines every model must beat.
  • Lag features let ordinary regression and boosting models forecast.
  • Forecasts should come with a range, because the future is uncertain.

Why it matters

Forecasts drive decisions everywhere: electricity companies plan generation, hospitals plan beds, farmers plan sowing with rainfall forecasts, shops plan stock and governments plan budgets. Ordinary machine learning assumes rows are independent, but time series rows are not, so forecasting needs its own methods and its own way of testing.

Key terms

Trend
The long-term upward or downward movement of a series.
Seasonality
A pattern that repeats with a fixed period, such as weekly or yearly.
Lag feature
A past value of the series used as an input, such as last month's sales.
Moving average
The average of the last k values, which smooths noise.
Exponential smoothing
A weighted average where recent values count more, controlled by α between 0 and 1.
Forecast horizon
How far ahead the forecast looks, such as 1 month or 12 months.

Types

Forecasting methods fall into several types, from simple rules to machine learning. These are the main ones.

1. Naive and seasonal naive baselines

Forecasting the last value, or the value from the same season last time.

  • How it works: They need no fitting; seasonal naive repeats last year's monthly pattern.
  • Example: In this module's example, naive gave MAE 16.8 and seasonal naive 29.8 because it ignored a year of growth.
  • When to use it: Use them as the first numbers to beat; a model that cannot beat them adds no value.

2. Moving averages and exponential smoothing

Forecasting from weighted averages of recent values.

  • How it works: A moving average treats the last k values equally; exponential smoothing gives recent values more weight; Holt adds a trend and Holt-Winters a seasonal term.
  • Example: Smoothing with α = 0.5 forecasting July sales of about 143 thousand rupees.
  • When to use it: Use them for business series with steady patterns; they are fast and robust.

3. ARIMA models

Statistical models built from past values (AR), differencing (I) and past errors (MA).

  • How it works: Differencing removes trend; AR and MA terms capture correlations with earlier points; SARIMA adds seasonal terms (Box and Jenkins, 1970).
  • Example: Forecasting monthly rainfall or electricity demand with SARIMA.
  • When to use it: Use them for single, fairly stable series where interpretability and prediction intervals matter.

4. Machine learning with lag features

Regression or gradient boosting using past values, calendar features and external drivers.

  • How it works: Each row holds lags (such as last week and last year), month or weekday and drivers like price or weather; the model predicts the next value.
  • Example: Trend, month and lag-12 features cut the sales forecast error to MAE 6.8.
  • When to use it: Use it when many related series exist or when external factors drive the target.

5. Deep-learning forecasters

Neural networks such as LSTMs, temporal convolutions and transformers for sequences.

  • How it works: They learn patterns across many series at once and can use long histories; pre-trained forecasting foundation models are also emerging.
  • Example: Forecasting demand for thousands of products in a retail chain.
  • When to use it: Use them for large collections of series with lots of data; check that they beat simpler methods.

Explained in detail

Components of a time series

Plot the series first. A rising line suggests a trend; regular peaks at the same time each year suggest seasonality; irregular jumps may be one-off events such as a lockdown or a strike. Decomposition splits the series into trend, seasonal and remainder parts, either added together (additive, when seasonal swings stay the same size) or multiplied (multiplicative, when swings grow as the level grows, as with many sales figures). Understanding these parts tells you which method to use. A series with no trend or seasonality is best forecast by a recent average; a series with strong yearly seasonality needs a model that knows about the calendar.

Forecasting methods from simple to advanced

MethodForecastGood for
NaiveLast observed valueA baseline every model must beat
Seasonal naiveValue from the same season last yearStrongly seasonal data
Moving averageMean of the last k valuesNoisy series without trend
Exponential smoothing (Holt-Winters)Weighted level, plus trend and seasonal termsMost business series
ARIMAModel built from past values and past errorsSeries that become stable after differencing
Machine learning with lagsRegression or boosting on lag, calendar and external featuresMany related series; extra drivers such as price or weather

Testing forecasts honestly

Never split time series randomly: a random split lets the model see June when predicting May, which is leakage. Instead, train on the past and test on the most recent period, or use rolling-origin evaluation (TimeSeriesSplit in scikit-learn), where the model is repeatedly trained up to a point and tested on the next block. Compare every model with the naive and seasonal naive baselines, using MAE or the mean absolute percentage error (MAPE). When building lag features, make sure each one is available at forecast time: to forecast 12 months ahead, last month's value is not yet known, but the value from 12 months earlier is. Report a range, such as an 80 per cent prediction interval, not just a single number, because the future is uncertain. In the code below, the seasonal naive forecast (MAE 29.8) does worse than the plain naive one (16.8) because it ignores a year of growth, while a regression using trend, month and lag-12 features cuts the error to 6.8.

How it works

  1. Plot the series and look for trend, seasonality and unusual events.
  2. Hold out the most recent period as a test set.
  3. Compute naive and seasonal naive baselines.
  4. Fit smoothing or a regression with lag and calendar features.
  5. Compare MAE with the baselines and report a forecast range.

The maths

Moving average forecast
Simple exponential smoothing
Smoothing weight between 0 and 1; higher reacts faster
Mean absolute error

Monthly sales from January to June are 120, 130, 125, 140, 150 and 145 (in thousand rupees). The naive forecast for July is the last value, 145. The 3-month moving average is (140 + 150 + 145) ÷ 3 = 145. With exponential smoothing and α = 0.5, start the level at 120; then 0.5 × 130 + 0.5 × 120 = 125; 0.5 × 125 + 0.5 × 125 = 125; 0.5 × 140 + 0.5 × 125 = 132.5; 0.5 × 150 + 0.5 × 132.5 = 141.25; and 0.5 × 145 + 0.5 × 141.25 ≈ 143.1. So smoothing forecasts about 143. None of these methods knows about the trend, which is why Holt's method adds a trend term.

In code

forecast_sales.py
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

# The worked example: exponential smoothing with alpha = 0.5
level = 120
for y in [130, 125, 140, 150, 145]:
    level = 0.5 * y + 0.5 * level
print("Smoothing forecast for July:", round(level, 1))

# Four years of monthly sales with trend, a festive peak in October-November and noise
rng = np.random.default_rng(4)
months = pd.date_range("2021-01-01", periods=48, freq="MS")
season = {1: -8, 2: -10, 3: -4, 4: 0, 5: 2, 6: -6, 7: -12, 8: -10, 9: 4, 10: 30, 11: 38, 12: 6}
sales = pd.Series([200 + 2.5 * t + season[m.month] + rng.normal(0, 6) for t, m in enumerate(months)], index=months)
train, test = sales[:36], sales[36:]

forecasts = {
    "naive (last value)": np.repeat(train.iloc[-1], 12),
    "3-month moving average": np.repeat(train.iloc[-3:].mean(), 12),
    "seasonal naive (last year)": train.iloc[-12:].values,
}
df = pd.DataFrame({"y": sales, "t": np.arange(48), "lag12": sales.shift(12)})
df = df.join(pd.get_dummies(df.index.month, prefix="m", drop_first=True).set_index(df.index).astype(int))
fit_rows = df.iloc[12:36]
X_cols = [c for c in df.columns if c != "y"]
reg = LinearRegression().fit(fit_rows[X_cols], fit_rows["y"])
forecasts["regression: trend + month + lag-12"] = reg.predict(df.iloc[36:][X_cols])

for name, f in forecasts.items():
    print(f"{name:<36} MAE = {np.mean(np.abs(test.values - f)):.1f}")

Output

Smoothing forecast for July: 143.1
naive (last value)                   MAE = 16.8
3-month moving average               MAE = 20.9
seasonal naive (last year)           MAE = 29.8
regression: trend + month + lag-12   MAE = 6.8

Try it yourself

Honest forecasting workflow

Put the steps in order.

  1. 1Compute naive and seasonal naive baselines
  2. 2Keep the latest months aside as a test period
  3. 3Fit a model with lag and calendar features on earlier data
  4. 4Compare errors on the test period and report a range
  5. 5Plot the series and spot trend and seasonality

Trend, seasonality or one-off event?

Classify each pattern in a city's electricity demand.

  • Demand drops during a three-day citywide strike

  • Demand grows about 5% each year as the city expands

  • Demand peaks every summer afternoon

  • Demand is lower every Sunday

Common mistakes

  • Splitting time series randomly instead of by time.
  • Using lag features that would not be known at forecast time.
  • Skipping the naive and seasonal naive baselines.

Check yourself

Question 1

1. Why must time series not be split randomly into train and test?

Question 2

2. Sales were 90, 100 and 110 in the last three months. What is the 3-month moving-average forecast?

Question 3

3. The seasonal naive forecast for next October is…

Summary

  • Time series combine trend, seasonality and noise; plot and decompose them first.
  • Start with naive, seasonal naive, moving averages and smoothing; add lag features for machine learning.
  • Validate in time order, beat the baselines and report a forecast range.

Finished reading?

Sources: Hyndman and Athanasopoulos, Forecasting: Principles and Practice (free online book) · scikit-learn: TimeSeriesSplit