Statistics and Probability for Data Science

Chapter 13
Time-Series and Forecasting Analytics

Post Graduate Diploma in Data Science
Statistics and Probability for Data Science   •   Post Graduate

13. Time-Series and Forecasting Analytics

Introduction and Context

A time series is a sequence of observations recorded in chronological order. Examples include hourly electricity demand, daily website traffic, monthly sales, quarterly GDP, sensor readings, and annual rainfall. Time-series analytics differs from ordinary cross-sectional analysis because the order of observations contains information. A value observed today may depend on yesterday’s value, the time of year, a recent intervention, or a long-term change in the underlying system.

Forecasting analytics uses historical and contextual information to estimate future values and quantify uncertainty about those estimates. It supports inventory decisions, workforce planning, budgeting, maintenance, capacity management, financial risk assessment, and public policy. A forecast is not a prediction of an inevitable future; it is a conditional statement about what may occur if the data-generating conditions and assumptions remain reasonably stable.

Time-series work therefore combines statistical modelling, domain knowledge, data engineering, visual analysis, and evaluation. The analyst must establish the time scale, identify missing or irregular observations, distinguish signal from noise, avoid leakage from the future, and communicate uncertainty. This chapter develops the principal ideas of decomposition, smoothing, stationarity, autocorrelation, ARIMA modelling, forecast evaluation, anomaly detection, intervention analysis, and probabilistic forecasting.

Learning Objectives

By the end of this chapter, a student should be able to:

1. Explain the distinctive features of time-series data, including trend, seasonality, cycles, noise, and dependence.

2. Decompose a series and select appropriate smoothing and forecasting methods.

3. Assess stationarity using plots, transformations, autocorrelation, and formal tests.

4. Interpret autocorrelation and partial autocorrelation functions.

5. Build and diagnose ARIMA and seasonal ARIMA models.

6. Design time-aware validation schemes and evaluate forecasts using suitable measures.

7. Apply anomaly detection and intervention analysis responsibly.

8. Explain point forecasts, prediction intervals, and probabilistic forecasts.

9. Recognize practical, ethical, and organizational risks in forecasting projects.

The Structure of a Time Series

Let the observation at time t be denoted by y_t. A useful conceptual representation is

y_t = signal_t + irregular_t.

The signal may include several components. Trend is a persistent, long-run direction. It may be approximately linear, nonlinear, or subject to structural changes. Seasonality is a systematic pattern that repeats at a known and fixed frequency, such as higher retail demand every December or increased electricity consumption during working hours. Cyclical movement resembles long waves but does not necessarily have a fixed period; business cycles are a common example. Irregular variation consists of shocks, measurement error, and other movements not explained by the model.

Time series may be observed at regular intervals, such as every hour or month, or irregularly, as in some medical and financial records. Many standard methods require a regular time index. Resampling can create a regular grid, but aggregation may remove useful variation and imputation may introduce artificial dependence. These decisions should be documented rather than treated as routine preprocessing.

A series may also be univariate, containing only its own historical values, or multivariate, in which related variables are modelled jointly. A retailer may forecast sales using sales history, price, promotions, holidays, weather, and competitor information. Such models are often called dynamic regression or regression with errors that are themselves serially correlated.

Decomposition

In an additive decomposition,

y_t = T_t + S_t + R_t,

where T_t is trend, S_t is seasonal effect, and R_t is the remainder. This is appropriate when seasonal fluctuations have roughly constant magnitude. In a multiplicative decomposition,

y_t = T_t S_t R_t,

seasonal variation grows with the level of the series. Taking logarithms converts a multiplicative structure into an additive one:

log(y_t) = log(T_t) + log(S_t) + log(R_t).

Classical decomposition uses moving averages to estimate trend and seasonality. More flexible procedures, such as STL (Seasonal-Trend decomposition using Loess), allow smooth, evolving trend and seasonal components and are generally more robust to unusual observations. Decomposition is descriptive rather than automatically causal: a seasonal component indicates repetition, not the reason for it.

Smoothing and Baseline Forecasts

Smoothing methods reduce short-term noise to reveal level, trend, or seasonality. They are also forecasting models in their own right.

A moving average replaces each observation with the average of nearby observations. It is useful for visual exploration but can respond slowly to sudden changes. A weighted moving average gives greater weight to more recent observations.

Simple exponential smoothing (SES) is suitable for a series with no systematic trend or seasonality. Its updating equation is

l_t = αy_t + (1 − α)l_{t−1},

where l_t is the estimated level and 0 < α < 1. The next forecast is l_t. A large α reacts quickly to new information; a small α produces a smoother estimate.

Holt’s method extends exponential smoothing to trend by estimating level and slope. Damped-trend variants reduce the tendency of a linear trend to grow indefinitely. Holt–Winters methods add seasonality, using either additive or multiplicative seasonal effects. The choice depends on whether seasonal amplitude is approximately constant or increases with the level.

Simple benchmarks are essential. A naïve forecast sets the next value equal to the latest observation. A seasonal naïve forecast uses the observation from the corresponding previous season—for example, last December’s sales for this December. A sophisticated model should outperform a relevant benchmark, not merely produce an attractive fitted curve.

Stationarity and Dependence

A weakly stationary series has a constant mean, constant variance, and autocovariance that depends only on the lag between observations, not on calendar time. Stationarity does not mean that observations are independent. A stationary autoregressive process can exhibit strong serial dependence.

Trend and changing variance commonly violate stationarity. Transformations such as logarithms or the Box–Cox transformation can stabilize variance. Differencing removes changes in level:

Δy_t = y_t − y_{t−1}.

Seasonal differencing compares observations separated by a seasonal period m:

Δ_m y_t = y_t − y_{t−m}.

Over-differencing can destroy useful structure and create unnecessary noise, so differencing should be limited and justified.

The autocorrelation function (ACF) measures correlation between y_t and y_{t−k} at lag k. The partial autocorrelation function (PACF) measures the relationship at lag k after accounting for shorter lags. A slowly declining ACF may indicate non-stationarity or strong persistence. A sharp cutoff in the PACF can suggest an autoregressive structure, while a sharp cutoff in the ACF can suggest a moving-average structure; these are diagnostic clues, not mechanical rules.

Formal tests can supplement plots. The augmented Dickey–Fuller test uses a null hypothesis of a unit root, while the KPSS test typically uses stationarity as the null hypothesis. Their results must be interpreted with visual evidence and subject-matter understanding. A statistical test can be misleading in a short sample, in the presence of structural breaks, or when seasonal patterns have not been handled.

ARIMA and Seasonal ARIMA Models

The ARIMA(p,d,q) model applies an autoregressive moving-average model to a series differenced d times. In an autoregressive model, current values depend on previous values. In a moving-average model, current values depend on previous forecast errors. In lag-operator notation, a non-seasonal ARIMA model can be written as

φ(B)(1−B)^d y_t = c + θ(B)ε_t,

where B is the backshift operator, φ(B) represents autoregressive terms, θ(B) represents moving-average terms, and ε_t is an innovation or error.

The parameters p, d, and q describe the autoregressive order, differencing order, and moving-average order. Seasonal ARIMA, written SARIMA(p,d,q)(P,D,Q)_m, adds seasonal autoregressive, differencing, and moving-average terms for period m. For example, monthly data may require m = 12.

Model development generally proceeds through the following reasoning sequence:

1. Plot the series and inspect its time index, scale, outliers, trend, and seasonal behavior.

2. Establish a chronological training set and reserve later observations for evaluation.

3. Apply transformations or differencing only when justified.

4. Use ACF and PACF, domain knowledge, and information criteria to propose candidate models.

5. Estimate parameters using the training data.

6. Check residuals. They should have approximately zero mean, stable variance, little autocorrelation, and a distribution suitable for the intended use.

7. Compare with benchmarks and alternative models on a genuine future-like test period.

The Ljung–Box test can assess whether a group of residual autocorrelations is jointly inconsistent with white noise. However, a non-significant result is not proof that the model is correct. Residual plots, forecast performance, plausibility, and stability remain important.

ARIMA models are useful when serial dependence is central and the future can reasonably be related to the past through a stable stochastic process. They may perform poorly after a major policy change, product launch, technological disruption, or other structural break. Dynamic regression with ARIMA errors can incorporate known predictors such as holidays, prices, weather, or interventions.

Forecast Evaluation and Validation

Forecast evaluation must reproduce the information available at the time forecasts would have been produced. Randomly shuffling time-series observations usually causes leakage because training data can contain information from the future. A simple holdout reserves the last segment as a test set. For limited data, rolling-origin evaluation repeatedly trains on an expanding or moving window and forecasts one or more future periods.

Common point-forecast measures include:

  • MAE: the mean absolute error, easy to interpret in the original unit.
  • MSE: the mean squared error, which penalizes large errors strongly.
  • RMSE: the square root of MSE, expressed in the original unit.
  • MAPE: mean absolute percentage error, which is unstable or undefined when actual values are zero or near zero.
  • sMAPE: a symmetric-looking percentage measure, although its interpretation also requires care.
  • MASE: mean absolute scaled error, which compares performance with a naïve benchmark and is useful across series of different scales.

No single metric is universally best. A hospital may penalize under-forecasting demand more than over-forecasting; a supply chain may use a cost-weighted loss; a public agency may value calibration of prediction intervals. Evaluation should therefore reflect the decision being supported.

A point forecast alone is incomplete. A 30-day demand forecast of 10,000 units might be accompanied by an 80% prediction interval of 9,400–10,700 units and a 95% interval of 9,000–11,200 units. Prediction intervals include uncertainty from future innovations and, depending on the method, parameter estimation. They are wider than confidence intervals for an estimated mean because they concern future observations.

Probabilistic forecasts describe a predictive distribution rather than only one expected value. They can be assessed through interval coverage, interval width, calibration, and proper scoring rules such as the logarithmic score and continuous ranked probability score. A well-calibrated 90% interval should contain the realized value approximately 90% of the time over many comparable forecasts, although sampling variability is expected.

Anomaly Detection and Intervention Analysis

An anomaly is an observation that is unusually inconsistent with an appropriate model or reference pattern. In time series, anomaly detection must account for trend, seasonality, changing variance, and autocorrelation. A high sales value during a seasonal peak may be normal, whereas a moderate value during an otherwise stable period may be anomalous.

Useful approaches include examining decomposition residuals, robust z-scores, prediction-interval violations, rolling medians, exponentially weighted control charts, and isolation-based machine-learning methods. The method should match the purpose. A fraud-monitoring system may prioritize rapid detection, while a maintenance system may prioritize low false-alarm rates.

An anomaly can be:

  • a point anomaly, affecting one observation;
  • a contextual anomaly, unusual only in a particular season or operating condition; or
  • a collective anomaly, where a sequence is unusual even though individual values appear ordinary.

An alert is not automatically an error. It may represent a genuine event, such as a campaign, outage, cyberattack, epidemic, or weather shock. Automatically deleting anomalies can remove the most valuable information in the series.

Intervention analysis explicitly models an event that changes a series. A pulse intervention represents a temporary shock; a step intervention represents a persistent level shift; a ramp or slope-change intervention represents gradual change. Intervention variables may enter a regression with ARIMA errors. This approach helps estimate the timing, magnitude, and persistence of an intervention while controlling for seasonality and serial dependence. Causal interpretation requires careful design: a before-and-after pattern alone does not prove that the intervention caused the change.

Applied Case: Forecasting Regional Pharmacy Demand

Background

This illustrative case concerns a regional pharmacy chain with 35 stores. The organization must forecast weekly demand for prescription medicines and selected over-the-counter products. Stockouts harm patients and reduce trust, while excessive inventory increases expiry and storage costs.

Situation and Problem

Historical data consisted of weekly unit demand for three years, together with store, product, price, promotion, holiday, and stock-availability fields. A preliminary dashboard showed a gradual upward trend, strong winter seasonality, occasional promotional spikes, and several weeks with recorded demand close to zero. The operations team had been using the previous week’s demand as its forecast.

Analysis

The analytics team first separated true low demand from stockout-censored demand. A week with zero recorded sales while the product was unavailable was not treated as evidence of zero customer need. The team aggregated demand by product-store-week, created a regular weekly index, and marked public holidays and promotions.

Plots and seasonal summaries showed that respiratory medicines had winter peaks, while some chronic medicines exhibited a stable trend with modest seasonality. A logarithmic transformation was considered for products whose variance increased with demand. Seasonal naïve forecasts were used as a benchmark because the annual pattern was important.

For a chronic medicine, a dynamic regression with holiday and availability variables and ARIMA errors performed better than the benchmark. For a highly seasonal respiratory product, a seasonal exponential-smoothing model produced more accurate short-horizon forecasts. A product with intermittent demand was handled separately; an ordinary ARIMA model was unsuitable because most weeks had zero demand.

The team used rolling-origin validation. At each forecast origin, only information available up to that week was used. MAE was reported for operational interpretability, while a cost-weighted metric penalized under-forecasting more heavily. Prediction intervals were translated into reorder policies rather than presented as purely statistical outputs.

Approach and Solution

The final system used a model-selection layer based on product characteristics, benchmark comparisons, and recent validation performance. Forecasts were generated weekly and reviewed for implausible negative values, abrupt unexplained changes, and unusually wide intervals. Promotion calendars were supplied in advance where available. Anomaly alerts were routed to pharmacists and planners, who could annotate events rather than silently override the model.

Outcome

The organization achieved a more transparent forecasting process and could distinguish demand changes from data-quality problems. The principal improvement was not simply a lower average error; it was better alignment between forecast uncertainty and inventory decisions. Managers could identify products requiring additional safety stock and could investigate events that models could not explain.

Learning Points

The case illustrates five principles: data quality precedes model sophistication; seasonal benchmarks are necessary; validation must respect time; business costs should influence evaluation; and human review is most valuable when it explains exceptional events rather than replacing the model arbitrarily.

Technology, Automation, and Contemporary Practice

Time-series workflows are increasingly implemented through reproducible pipelines. Data platforms can automate ingestion, calendar joins, feature creation, model training, backtesting, monitoring, and alert delivery. Libraries for statistical forecasting, gradient-boosted trees, state-space models, and deep learning allow analysts to compare diverse methods.

Machine-learning models can use lagged values, rolling statistics, calendar variables, prices, weather, and other predictors. Gradient boosting and neural networks may perform well with many related series and nonlinear effects, but they require careful feature construction and time-aware validation. Deep sequence models can be powerful when large, stable datasets are available; they are not automatically superior to simpler statistical models.

A growing practice is global forecasting, in which one model is trained across many related series while learning series-specific behavior through identifiers or embeddings. Forecast reconciliation ensures that forecasts at different organizational levels add up consistently—for example, product forecasts should aggregate to store and regional totals. Automated forecasting should include monitoring for data drift, forecast bias, interval miscalibration, missing updates, and changes in the data-generating process.

Risks, Limitations, and Professional Governance

Forecasts inherit weaknesses in the data. Missing values, revised historical records, changes in product definitions, survivorship bias, and stockout censoring can distort conclusions. A model may appear accurate during a stable period and fail during a crisis. Structural breaks, policy changes, and competitor actions are difficult to infer from historical patterns alone.

Common mistakes include random train-test splitting, leakage through future-derived features, excessive differencing, ignoring seasonality, selecting models only by in-sample fit, reporting MAPE with zero-valued data, and presenting narrow intervals as certainty. Another mistake is to treat an anomaly as an error without investigating its operational meaning.

Forecasting also involves governance. Sensitive applications such as staffing, credit, healthcare, and public safety require attention to privacy, fairness, accountability, and the consequences of error. A forecast that systematically underestimates demand in a disadvantaged community can produce unequal service. Users should know the forecast horizon, data cutoff, model version, uncertainty range, and appropriate override procedure. Decisions with material consequences should not rely on an unreviewed automated forecast.

Best-Practice Workflow

A defensible forecasting project usually follows this sequence:

1. Define the decision, horizon, frequency, units, and cost of forecast errors.

2. Audit timestamps, missingness, duplicates, revisions, outliers, availability, and aggregation choices.

3. Visualize the series and investigate trend, seasonality, variance, breaks, and relationships with external variables.

4. Establish naïve and seasonal naïve benchmarks.

5. Create chronological validation sets and select metrics linked to the decision.

6. Fit a small set of interpretable candidate models before considering more complex alternatives.

7. Diagnose residuals and inspect forecast stability and plausibility.

8. Produce prediction intervals or full predictive distributions.

9. Monitor accuracy, bias, calibration, drift, and operational outcomes after deployment.

10. Record assumptions, interventions, model versions, and human decisions.

Current Developments and Emerging Directions

Important developments include probabilistic forecasting at scale, conformal prediction for distribution-free uncertainty calibration, hierarchical and grouped forecast reconciliation, causal forecasting around interventions, and models that combine statistical structure with machine learning. Foundation models and large neural architectures are being explored for time-series representation and cross-domain transfer, but their value depends on data quality, calibration, interpretability, and computational cost.

Another important direction is decision-focused forecasting, where models are judged by the quality of decisions they support rather than by error metrics alone. Forecasts may be integrated with optimization systems for inventory, staffing, energy dispatch, or transport planning. This integration increases the importance of transparent assumptions and sensitivity analysis: an uncertainty range should be connected to a decision rule, not displayed as decorative information.

Chapter Summary

Time-series analytics studies observations whose chronological order and dependence carry information. Trend, seasonality, cycles, and irregular variation can be explored through decomposition and smoothing. Stationarity is important for many stochastic models and may be approached through transformations and differencing. ACF and PACF describe serial dependence and help guide ARIMA and SARIMA specification, while residual diagnostics and benchmark comparisons remain essential.

Forecast evaluation must imitate real information flow through holdout or rolling-origin validation. MAE, RMSE, MASE, and other measures should be selected according to the decision context. Forecast uncertainty is represented through prediction intervals or predictive distributions and assessed through calibration and proper scoring rules. Anomaly detection must distinguish errors from meaningful events, and intervention analysis can represent temporary or permanent changes without automatically establishing causality. Effective professional practice combines statistical modelling with data governance, domain expertise, monitoring, and responsible communication.

Key Terms

Time series: Chronologically ordered observations of one or more variables.

Trend: Persistent long-term movement in a series.

Seasonality: A pattern that repeats at a known calendar or operational frequency.

Cycle: A long-term fluctuation without necessarily fixed periodic length.

Decomposition: Separation of a series into trend, seasonal, and remainder components.

Stationarity: Stability of distributional properties, especially mean, variance, and autocovariance, over time.

Differencing: Subtracting an earlier observation from a later one to remove level changes.

Autocorrelation: Correlation between observations separated by a specified lag.

PACF: Correlation at a lag after shorter-lag relationships have been controlled.

Exponential smoothing: Forecasting based on weighted averages that emphasize recent observations.

ARIMA: A model combining autoregression, differencing, and moving-average error structure.

SARIMA: ARIMA extended with seasonal autoregressive, differencing, and moving-average terms.

Residual: The observed value minus the fitted or forecast value.

Naïve forecast: A forecast that uses a recent observation, commonly the last value.

Rolling-origin evaluation: Repeated time-ordered training and forecasting using successive forecast origins.

Prediction interval: A range intended to contain a future observation with stated probability.

Anomaly: An observation or sequence inconsistent with an appropriate expected pattern.

Intervention analysis: Modelling the effect of a specified event or policy change on a time series.

Forecast reconciliation: Adjusting forecasts so that related hierarchical series aggregate consistently.

Calibration: Agreement between stated predictive probabilities or interval coverage and observed frequencies.

Review Questions

Short Answer Questions

1. Distinguish trend, seasonality, cyclical movement, and irregular variation.

2. When is a multiplicative decomposition more appropriate than an additive decomposition?

3. What is the purpose of differencing in an ARIMA model?

4. Why is a seasonal naïve forecast a useful benchmark?

5. Define a prediction interval and distinguish it from a confidence interval.

Long Answer Questions

1. Explain the process of developing and diagnosing an ARIMA or SARIMA model.

2. Discuss how stationarity can be assessed using plots, transformations, autocorrelation, and formal tests.

3. Compare simple exponential smoothing, Holt’s method, and Holt–Winters methods.

4. Explain why time-series forecast evaluation should use chronological holdout or rolling-origin validation.

5. Discuss the professional and ethical risks of deploying automated forecasts in healthcare, staffing, or public services.

Analytical / Application Questions

1. A monthly sales series has increasing variance, strong annual seasonality, and a gradual upward trend. Propose a modelling workflow and justify the transformation, decomposition, benchmark, validation design, and evaluation measures.

2. A demand-monitoring system flags a value as anomalous during a holiday week. Explain how you would determine whether it is a data error, a contextual anomaly, or a genuine demand event.

3. A retailer introduces a permanent price reduction and observes a level shift in sales. Design an intervention-analysis strategy and identify what evidence would be needed before making a causal claim.

22. Multiple Choice Questions with Answers

Question 1

Which situation most strongly supports using a multiplicative decomposition?

A. Seasonal fluctuations remain approximately constant as the series level changes

B. Seasonal fluctuations become larger as the level of the series increases

C. The series contains no seasonal pattern

D. All observations are independent

Correct Answer: B. Seasonal fluctuations become larger as the level of the series increases

Question 2

What is the main purpose of differencing a non-stationary time series?

A. To create additional observations

B. To remove all random variation

C. To reduce level-related non-stationarity

D. To guarantee normally distributed residuals

Correct Answer: C. To reduce level-related non-stationarity

Question 3

A seasonal naïve forecast for monthly sales in March would normally use:

A. The average of all previous months

B. The sales value from the previous March

C. The sales value from February only

D. The median sales value for the entire year

Correct Answer: B. The sales value from the previous March

Question 4

Which residual pattern most clearly suggests that a fitted forecasting model is inadequate?

A. Residuals fluctuate around zero with little autocorrelation

B. Residuals have a stable variance and no visible structure

C. Residuals show repeated autocorrelation at seasonal lags

D. Residuals are expressed in the original measurement units

Correct Answer: C. Residuals show repeated autocorrelation at seasonal lags

Question 5

Why is random shuffling generally inappropriate when creating training and test sets for time-series forecasting?

A. It always reduces the sample size

B. It can allow future information to enter model training

C. It prevents the calculation of MAE

D. It guarantees that seasonal patterns disappear

Correct Answer: B. It can allow future information to enter model training

Question 6

Which measure is especially problematic when actual values can be zero or near zero?

A. MAE

B. RMSE

C. MASE

D. MAPE

Correct Answer: D. MAPE

Question 7

A single unusually high observation that is abnormal only because it occurred outside the normal seasonal period is best described as a:

A. Contextual anomaly

B. Seasonal naïve forecast

C. Unit root

D. Damped trend

Correct Answer: A. Contextual anomaly

Question 8

What does intervention analysis primarily attempt to model?

A. The effect of a specified event or policy change on a series

B. The random ordering of observations

C. The removal of all seasonal components

D. The conversion of a univariate series into a cross-sectional dataset

Correct Answer: A. The effect of a specified event or policy change on a series

Question 9

If a forecasting system reports a 90% prediction interval, calibration would require that:

A. Every future value must fall inside the interval

B. Approximately 90% of comparable future observations fall inside intervals over time

C. The interval width must remain constant

D. The point forecast must equal the lower bound

Correct Answer: B. Approximately 90% of comparable future observations fall inside intervals over time

Question 10

Which practice best protects against forecasting leakage?

A. Using randomly shuffled observations for validation

B. Using features calculated with data available only after the forecast origin

C. Constructing features using only information available at each forecast origin

D. Selecting the model with the best in-sample fit only

Correct Answer: C. Constructing features using only information available at each forecast origin

Statistics and Probability for Data Science
Chapter 13 of 15