Python for Business Analytics

Chapter 13
Time-Series Analytics and Forecasting

Bachelors Program in Business Analytics
Python for Business Analytics   •   Bachelor

Time-Series Analytics and Forecasting

Introduction

A time series is a sequence of observations indexed in chronological order. Examples include daily sales, monthly revenue, hourly website traffic, quarterly employment, and minute-by-minute inventory movements. Unlike ordinary cross-sectional data, time-series observations are often dependent: demand this week may be related to demand last week, and sales in December may differ systematically from sales in July.

Time-series analytics therefore addresses more than prediction. It asks how a variable changes over time, which patterns are persistent, how uncertainty should be represented, and how forecasts can support decisions about pricing, finance, staffing, and inventory.

In business analytics, a useful forecast is not necessarily the most mathematically sophisticated forecast. It is a forecast that is appropriate to the data-generating process, evaluated honestly, communicated clearly, and connected to an action.

Learning Objectives

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

  1. Distinguish trend, seasonality, cycles, irregular variation, and temporal dependence.
  2. Prepare time-series data in Python and identify common data-quality problems.
  3. Explain autocorrelation, stationarity, and the purpose of differencing.
  4. Construct naïve, seasonal-naïve, moving-average, exponential-smoothing, and regression-based forecasts.
  5. Interpret ARIMA and seasonal ARIMA models at an introductory bachelor level.
  6. Evaluate forecasts using appropriate rolling or expanding time-based validation.
  7. Quantify forecast uncertainty and translate forecasts into operational decisions.
  8. Recognize limitations, ethical risks, and governance requirements in forecasting systems.

1. The Structure of a Business Time Series

A commonly used conceptual decomposition represents an observation as the combination of systematic and unsystematic components:

yt = Tt + St + Ct + εt

Here, yt is the observed value at time t, Tt is the long-term trend, St is a seasonal component with a repeating calendar pattern, Ct is a longer-term cyclical component, and εt is irregular noise. This is an additive decomposition and is appropriate when the size of seasonal fluctuations is approximately constant over time.

A multiplicative decomposition is often more suitable when seasonal variation grows with the level of the series:

yt = Tt × St × Ct × It

where all components are usually positive and It is the irregular multiplicative factor. Taking logarithms converts multiplication into addition:

ln(yt) = ln(Tt) + ln(St) + ln(Ct) + ln(It)

Log transformation requires positive observations and should not be applied mechanically to series containing zeros or negative values.

Trend, seasonality, and cycles

A trend is a persistent increase or decrease in the level of a series. Seasonality is a pattern that repeats at a known and fixed frequency, such as higher retail sales every December or higher electricity demand every weekday afternoon. A cycle is a fluctuating movement whose duration is not fixed, such as the business cycle. Irregular variation includes shocks, recording errors, promotions, strikes, and other effects that are difficult to predict.

The distinction matters operationally. A retailer can schedule inventory for predictable December seasonality, but cannot treat an unexpected competitor closure as ordinary seasonal demand.

Level and change

The level is the current magnitude of the series. A series may also be analyzed through changes. The first difference is:

∆yt = yt − yt−1

For monthly data with annual seasonality, a seasonal difference is:

∆12 yt = yt − yt−12

Differencing can remove a trend or seasonal pattern, but excessive differencing can introduce unnecessary noise and make interpretation difficult.

2. Time-Series Data Preparation in Python

A reliable forecast begins with a correctly structured time index. Important preparation tasks include:

  • converting dates to a true datetime type;
  • sorting chronologically;
  • identifying duplicate timestamps;
  • checking missing periods rather than only missing values;
  • choosing an appropriate frequency;
  • distinguishing zero demand from missing demand;
  • documenting promotions, price changes, stockouts, and unusual events.

For example:

import pandas as pd

sales = pd.read_csv("monthly_sales.csv")
sales["date"] = pd.to_datetime(sales["date"])
sales = sales.sort_values("date").set_index("date")
sales = sales.asfreq("MS")  # Month-start frequency

print(sales.index.min(), sales.index.max())
print(sales.isna().sum())

asfreq exposes missing calendar periods; it does not determine whether those periods should be filled with zero, interpolated, or left missing. A missing sales record caused by a system failure is not equivalent to a genuine month with zero sales.

Time zones are important for hourly or sub-daily data. A timestamp should represent the same physical time throughout the analysis, especially when daylight-saving changes occur. Resampling also requires care. Summing hourly transactions into daily sales is reasonable for transaction counts, whereas averaging hourly temperatures may be appropriate for a different variable.

Exploratory analysis

A time plot is usually the first diagnostic. Analysts should inspect:

  • changes in level and variance;
  • repeated peaks or troughs;
  • outliers and structural breaks;
  • periods of missingness;
  • possible calendar effects;
  • relationships with external variables.

Rolling statistics provide local summaries. For a window of width w, the rolling mean is:

mt = (1/w) × ∑i=0w−1 yt−i

The rolling standard deviation is calculated from the same window and indicates whether volatility changes over time. Rolling summaries are descriptive; they should not be mistaken for forecasts.

3. Autocorrelation and Stationarity

Autocorrelation

Autocorrelation measures the relationship between observations separated by a lag. The sample autocorrelation at lag k is commonly written as:

rk = [∑t=k+1n(yt − ȳ)(yt−k − ȳ)] ÷ [∑t=1n(yt − ȳ)2]

Here, n is the number of observations and ȳ is the sample mean. The numerator compares each observation with the observation k periods earlier. Autocorrelation is dimensionless and usually lies between −1 and 1, although finite-sample estimates can behave imperfectly near the boundaries.

Positive lag-one autocorrelation means high values tend to follow high values and low values tend to follow low values. A seasonal spike at lag 12 in monthly data suggests annual dependence. Negative autocorrelation may indicate alternating or corrective behavior.

The autocorrelation function (ACF) summarizes correlations at multiple lags. The partial autocorrelation function (PACF) measures the relationship at a lag after accounting for shorter lags. ACF and PACF are useful diagnostics, not automatic model-selection rules.

Stationarity

A weakly stationary series has a constant mean, constant variance, and autocovariance that depends on lag rather than calendar time. Many classical models assume stationarity or become simpler after transformation. A steadily growing sales series is generally not stationary in its original level.

Stationarity does not mean that observations are independent. A stationary autoregressive process can have stable statistical properties while retaining substantial autocorrelation.

The augmented Dickey–Fuller test examines a null hypothesis of a unit root. Its result should be interpreted with plots, subject-matter knowledge, and other diagnostics. A failure to reject the null is not proof that a series is permanently nonstationary, and a rejection can be misleading in the presence of structural breaks.

4. Forecasting Methods

4.1 Baseline forecasts

Baselines establish a minimum standard.

The naïve forecast sets the next value equal to the latest observation:

ŷt+h|t = yt

The notation means the forecast made at time t for horizon h. It is appropriate for series whose most recent level is informative.

The seasonal-naïve forecast uses the observation from the corresponding previous season:

ŷt+h|t = yt+h−m

where m is the seasonal period, such as 12 for monthly annual seasonality or 7 for daily weekly seasonality. Seasonal-naïve forecasts are especially strong benchmarks for seasonal business data.

4.2 Moving averages

A simple moving-average forecast uses recent observations:

ŷt+1|t = (1/w) × ∑i=0w−1 yt−i

The window length w controls responsiveness. A small window reacts quickly but is noisy; a large window is smoother but may lag behind a changing trend. A moving average should not be calculated using future observations.

4.3 Exponential smoothing

Simple exponential smoothing updates an estimated level:

ℓt = αyt + (1−α)ℓt−1

and forecasts future observations as:

ŷt+h|t = ℓt

The smoothing parameter α satisfies 0 < α ≤ 1. A larger value gives more weight to recent observations. Simple exponential smoothing is suitable when there is no systematic trend or seasonality.

Holt’s method extends exponential smoothing to trend. Holt–Winters methods add seasonality. In additive seasonality, the seasonal effect has approximately constant magnitude; in multiplicative seasonality, it is proportional to the level. Model selection should be based on validation performance and plausible residual behavior, not merely on a lower in-sample error.

In Python, statsmodels provides implementations:

from statsmodels.tsa.holtwinters import ExponentialSmoothing

model = ExponentialSmoothing(
    train["sales"], trend="add", seasonal="add", seasonal_periods=12
).fit()
forecast = model.forecast(len(test))

The seasonal period must match the data frequency. Twelve is appropriate for monthly annual seasonality, not for arbitrary weekly data.

4.4 Regression with time and calendar variables

A regression forecast can include a time trend and explanatory variables:

yt = β0 + β1t + β2DMon,t + β3Pt + εt

Here, t is a time index, DMon,t is a Monday indicator, Pt may represent price or promotion, and εt is an error term. Categorical variables require a reference category to avoid perfect multicollinearity. Forecasting also requires future values of predictors such as planned prices or promotions.

Regression residuals may remain autocorrelated. Ordinary least squares coefficient estimates can then be inefficient, and standard errors may be unreliable. Adding lagged terms, using generalized time-series errors, or combining regression with ARIMA errors can address this issue.

4.5 ARIMA models

An autoregressive model of order p uses prior values:

yt = c + φ1yt−1 + … + φpyt−p + εt

A moving-average error model of order q uses prior shocks:

yt = c + εt + θ1εt−1 + … + θqεt−q

An ARIMA(p, d, q) model applies differencing d times before modeling autoregressive and moving-average behavior. The errors are commonly assumed to have mean zero, finite variance, and no remaining autocorrelation after adequate fitting. Normality is not essential for point forecasts, but it helps when constructing conventional prediction intervals.

Seasonal ARIMA extends the model with seasonal autoregressive, differencing, and moving-average terms, commonly written ARIMA(p,d,q)(P,D,Q)_m. The subscript m is the seasonal period.

A fitted model should be diagnosed through residual plots, residual ACF, and tests such as Ljung–Box. A residual series that retains clear autocorrelation indicates that useful temporal structure remains unexplained.

5. Forecast Evaluation

Forecast accuracy must be evaluated on data not used to fit the model. Random train–test splitting is generally inappropriate because it allows the training set to contain observations later than some test observations.

A simple chronological split trains on the earlier period and tests on the later period. Rolling-origin evaluation is stronger: the model is repeatedly trained using an expanding or moving window, and forecasts are generated for a specified horizon.

Let actual values be yt and forecasts be ŷt. Common metrics include:

Mean absolute error:

MAE = (1/n) × ∑t=1n|yt − ŷt|

Root mean squared error:

RMSE = √[(1/n) × ∑t=1n(yt − ŷt)2]

Mean absolute percentage error:

MAPE = (100/n) × ∑t=1n|(yt − ŷt) ÷ yt|

MAE has the same units as the target and treats all absolute errors proportionally. RMSE has the same units but penalizes large errors more heavily. MAPE is easy to communicate but is undefined when yt = 0 and unstable when actual values are close to zero. For intermittent demand, MAE, RMSE, or scaled measures are generally preferable.

Mean absolute scaled error compares forecast error with a naïve benchmark. For a nonseasonal series, one common form is:

MASE = mean(|yt − ŷt|) ÷ mean(|yt − yt−1|)

The denominator should be calculated on the training data, and seasonal scaling may be appropriate for seasonal series. A MASE below 1 indicates improvement over the corresponding naïve benchmark under the chosen scaling.

Accuracy is not the sole criterion. A forecast with slightly higher MAE may be preferable if it is more stable, easier to explain, cheaper to maintain, or better calibrated for decisions.

6. Uncertainty and Prediction Intervals

A point forecast is only one possible future outcome. A prediction interval represents uncertainty about the future observation. A confidence interval for an estimated mean and a prediction interval for a future observation are not the same: prediction intervals are wider because they include both parameter uncertainty and future random variation.

For a simple forecast under strong assumptions of independent normally distributed errors with known standard deviation σ, an approximate interval is:

ŷ ± z1−α/2 × σ

Here, α is the total error probability and z1−α/2 is the corresponding standard-normal critical value. In actual time-series forecasting, forecast-error variance often grows with horizon, and errors may not be normal. Therefore, model-based intervals, bootstrap intervals, or empirically calibrated intervals may be preferable.

For operational use, intervals should be interpreted probabilistically and monitored for coverage. If nominal 90% intervals contain the actual outcome only 60% of the time, the uncertainty estimates are poorly calibrated.

7. Python Workflow

A practical workflow separates data preparation, modeling, validation, and decision use:

from sklearn.metrics import mean_absolute_error, mean_squared_error
from statsmodels.tsa.holtwinters import ExponentialSmoothing
import numpy as np

series = sales["sales"].dropna()
cut = int(len(series) * 0.8)
train, test = series.iloc[:cut], series.iloc[cut:]

fit = ExponentialSmoothing(
    train, trend="add", seasonal="add", seasonal_periods=12
).fit()
pred = fit.forecast(len(test))

mae = mean_absolute_error(test, pred)
rmse = np.sqrt(mean_squared_error(test, pred))
print({"MAE": mae, "RMSE": rmse})

This code is a demonstration, not a complete validation system. A professional workflow should compare against naïve and seasonal-naïve benchmarks, repeat evaluation across forecast origins, preserve a final untouched holdout period when feasible, and record model versions and data timestamps.

8. Applied Case: Illustrative Retail Inventory Forecast

Background

Consider an illustrative fictional company, Northstar Home Goods, which sells household appliances through stores and an online channel. Its monthly demand is higher in November and December, but promotions and occasional stockouts have created large fluctuations.

Problem

The inventory team must forecast demand three months ahead for purchasing. Underforecasting causes stockouts and lost sales; overforecasting creates storage costs and markdowns. Historical sales are observed, but sales during stockouts understate true customer demand.

Analysis

The analyst first verifies monthly timestamps, identifies missing months, and flags stockout periods. A time plot shows annual seasonality and an upward trend. The ACF has a notable spike at lag 12, supporting annual seasonal dependence. A seasonal-naïve model is established as the baseline.

Three candidate approaches are compared:

ModelStrengthRisk
Seasonal naïveTransparent benchmarkCannot respond to trend or promotions
Holt–WintersRepresents level, trend, and seasonalityCan be distorted by structural breaks
Regression with promotion variables and time-series errorsUses business knowledgeRequires reliable future promotion plans

The data are evaluated through rolling three-month forecasts. The regression model uses planned promotions, price reductions, and holiday indicators. Stockout periods are excluded from ordinary demand interpretation or modeled separately, because recorded sales are censored when inventory is unavailable.

Approach and solution

Suppose the seasonal-naïve model produces MAE of 1,240 units, Holt–Winters produces MAE of 1,080 units, and the regression model produces MAE of 940 units in rolling validation. The regression model is selected provisionally, subject to residual checks and operational review. The team also produces a 90% prediction interval rather than communicating only the point forecast.

For procurement, the upper interval may be used as an input to a service-level policy, but it should not automatically become the order quantity. Lead time, supplier minimums, holding cost, shortage cost, and current inventory must also be considered. If lead-time demand is forecast as μL with standard deviation σL, a simplified reorder point is:

ROP = μL + z × σL

Here, z is selected according to the desired cycle-service probability under an approximately normal lead-time-demand assumption. This formula is a simplified policy relationship; intermittent demand, supplier uncertainty, and non-normal distributions may require simulation or specialized inventory models.

Outcome

The model reduces validation error, but the principal business improvement comes from combining the forecast with stockout flags, promotion calendars, and an explicit review process. Forecasts are refreshed monthly, actual outcomes are compared with interval coverage, and large deviations trigger investigation rather than silent model replacement.

Learning points

  1. A sophisticated model should earn its complexity against a strong baseline.
  2. Observed sales may not equal unconstrained demand.
  3. Forecast intervals support risk-aware purchasing.
  4. Model performance and operational usefulness must be monitored together.

9. Applications Across Business Functions

Sales and marketing

Forecasts support quota setting, campaign planning, and revenue budgeting. Analysts should distinguish causal uplift from correlation: a sales increase after a campaign may also reflect seasonality or a competitor’s shortage.

Finance

Cash-flow and revenue forecasts inform liquidity planning. Scenario forecasts are often more useful than a single number because interest rates, exchange rates, and customer payment behavior may change.

Staffing

Hourly arrival forecasts support shift scheduling. Forecasts should account for day-of-week and holiday effects while respecting labor regulations, employee well-being, and minimum staffing requirements.

Inventory and supply chain

Demand forecasts feed replenishment, safety-stock, and capacity decisions. Forecast error should be evaluated at the level at which decisions are made: a highly accurate national forecast may not prevent shortages in a particular store.

10. Limitations, Governance, and Responsible Use

Forecasts can fail after structural breaks, policy changes, pandemics, competitor entry, product launches, or changes in measurement systems. Historical relationships are not guaranteed to persist.

Important governance practices include:

  • documenting data sources, transformations, assumptions, and forecast horizons;
  • preventing leakage of future information into training features;
  • retaining model and forecast versions;
  • monitoring accuracy, bias, interval coverage, and drift;
  • assigning human accountability for consequential decisions;
  • protecting personal and commercially sensitive data;
  • explaining limitations to decision makers.

Forecasting systems can also reproduce unequal treatment. For example, staffing forecasts based on historical service patterns may allocate fewer resources to locations whose demand was previously suppressed by poor service. Analysts should investigate whether the target reflects need, access, or past organizational decisions.

Artificial intelligence and machine-learning models can incorporate nonlinearities, promotions, weather, text, and high-dimensional predictors. They may improve predictive performance, but complexity does not eliminate leakage, confounding, drift, or accountability requirements. Time-aware validation, interpretable diagnostics, and a strong benchmark remain necessary.

11. Best-Practice Checklist

Before deploying a forecast, ask:

  1. Is the time index complete, correctly ordered, and at the right frequency?
  2. Are zeros, missing values, stockouts, and outliers correctly interpreted?
  3. What are the trend and seasonal periods?
  4. Does the model outperform naïve and seasonal-naïve benchmarks?
  5. Was evaluation chronological and consistent with the intended horizon?
  6. Are residuals plausibly unstructured?
  7. Are prediction intervals calibrated?
  8. Are future explanatory variables genuinely available?
  9. What action will the forecast trigger, and what is the cost of error?
  10. Who reviews, monitors, and is accountable for the system?

Chapter Summary

Time-series analytics studies observations ordered through time, where trend, seasonality, cycles, irregular shocks, and autocorrelation may coexist. Additive and multiplicative decompositions provide useful conceptual frameworks, while differencing can help transform nonstationary data. Autocorrelation and stationarity diagnostics guide model selection but do not replace judgment.

Naïve and seasonal-naïve forecasts establish essential benchmarks. Moving averages, exponential smoothing, Holt–Winters, regression with calendar or business variables, and ARIMA-family models provide progressively richer representations. Models must be evaluated using chronological or rolling-origin validation. MAE, RMSE, MAPE, and MASE answer different questions and have different limitations.

Forecast uncertainty is as important as the point forecast. Prediction intervals, residual diagnostics, calibration checks, and scenario analysis support responsible decisions. In business settings, the best forecast is one that is accurate enough, operationally relevant, explainable, monitored, and connected to costs and consequences. Ethical governance is required because historical data may reflect constraints, bias, or unequal access rather than true underlying demand.

Key Terms

Time series: Observations recorded in chronological order.

Trend: A persistent long-term movement in a series.

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

Cycle: A longer-term fluctuation whose duration is not fixed.

Irregular component: Unpredictable or unexplained variation.

Additive decomposition: A representation in which components are added.

Multiplicative decomposition: A representation in which components are multiplied.

Autocorrelation: Correlation between observations separated by a time lag.

Lag: The number of periods separating two observations.

Stationarity: Stability of key statistical properties over time.

Differencing: Subtracting a previous or seasonal observation to remove persistent structure.

Naïve forecast: A forecast equal to the latest observed value.

Seasonal-naïve forecast: A forecast using the value from the corresponding previous season.

Exponential smoothing: A method that assigns declining weights to older observations.

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

Residual: The difference between an observed value and its fitted or forecast value.

MAE: Mean absolute error, measured in the target’s units.

RMSE: Root mean squared error, which penalizes large errors more strongly.

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

Forecast leakage: Use of information unavailable at the time a forecast would have been made.

Review Questions

Short Answer Questions

  1. What is the difference between trend and seasonality?
  2. Why is a seasonal-naïve forecast an important benchmark?
  3. What does positive autocorrelation at lag one indicate?
  4. Why can MAPE be unsuitable when actual values are zero?
  5. Distinguish a prediction interval from a point forecast.

Long Answer Questions

  1. Explain additive and multiplicative time-series decomposition, including when each is appropriate.
  2. Compare simple exponential smoothing, Holt–Winters methods, and ARIMA models.
  3. Describe a time-aware forecast-validation procedure and explain why random splitting is usually inappropriate.
  4. Explain how stockouts can bias demand forecasting and propose analytical responses.
  5. Discuss how forecast uncertainty should influence inventory, staffing, or financial decisions.

Analytical / Application Questions

  1. A monthly series has strong spikes at lag 12 and an increasing level. Propose an exploratory and modeling strategy, including at least two benchmarks and one seasonal model.
  2. A model has lower RMSE than its benchmark but poor 90% interval coverage. What might this indicate, and what actions should an analyst take?
  3. A retailer must choose between a transparent seasonal-naïve forecast and a complex machine-learning model with only a small validation improvement. Develop a decision framework that considers accuracy, cost, explainability, data availability, and operational risk.

Multiple Choice Questions with Answers

Question 1

Which forecast is defined by setting the future value equal to the most recent observation?

A. Naïve forecast

B. Seasonal-naïve forecast

C. Holt–Winters forecast

D. Regression forecast

Correct Answer: A. Naïve forecast

Question 2

For monthly data with annual seasonality, which lag commonly represents the seasonal period?

A. 4

B. 7

C. 12

D. 52

Correct Answer: C. 12

Question 3

Which metric has the same units as the forecast target and averages absolute errors?

A. MAPE

B. MAE

C. ACF

D. MASE

Correct Answer: B. MAE

Question 4

Why is random train–test splitting generally inappropriate for time-series forecasting?

A. It always reduces the sample size

B. It can allow future information to influence model training

C. It prevents calculation of RMSE

D. It removes seasonality from the data

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

Question 5

What does a strong positive autocorrelation at lag 12 in monthly data suggest?

A. Annual temporal dependence

B. That all observations are independent

C. A guaranteed structural break

D. A negative trend

Correct Answer: A. Annual temporal dependence

Question 6

Which method is designed to represent level, trend, and seasonality through smoothing components?

A. Simple random sampling

B. Holt–Winters exponential smoothing

C. A confusion matrix

D. Principal components analysis

Correct Answer: B. Holt–Winters exponential smoothing

Question 7

Why can MAPE be undefined?

A. When the forecast is positive

B. When the sample contains a trend

C. When an actual value equals zero

D. When observations are monthly

Correct Answer: C. When an actual value equals zero

Question 8

What is the main purpose of a strong naïve benchmark?

A. To guarantee the most accurate forecast

B. To provide a minimum standard for judging more complex models

C. To remove all autocorrelation

D. To estimate causal effects

Correct Answer: B. To provide a minimum standard for judging more complex models

Question 9

In the simplified reorder-point formula ROP = μ_L + z × σ_L, what does μ_L represent?

A. Mean lead-time demand

B. Monthly price change

C. Forecast bias

D. Seasonal period

Correct Answer: A. Mean lead-time demand

Question 10

What does forecast leakage mean?

A. A missing calendar period

B. A forecast interval that is too wide

C. Using information unavailable when the forecast would have been made

D. A model with no trend component

Correct Answer: C. Using information unavailable when the forecast would have been made

Python for Business Analytics
Chapter 13 of 15