1. Project Goal
Forecast monthly retail sales six months ahead so inventory planners can compare a simple baseline with a seasonal model, understand every evaluation value and monitor when the time series changes.
2. What You Will Build
3. Skills / Prerequisites
Review unfamiliar foundations before implementing the project. Each link points to an existing lesson used by the workflow.
4. Dataset or Inputs
The example generates one sales total for the first day of each month. In a real file, each row needs a timestamp, the amount being forecast and only covariates that would be known before the forecast is issued.
Understand every project input
5. Architecture / Workflow
Order observations by timestamp and resolve missing periods.
Reserve the newest horizon as test and the preceding horizon as validation.
Fit baselines and stronger candidates without using future observations.
Refit the selected approach through validation, forecast test, diagnose residuals and monitor new errors.
Project figure
Forecasting respects time: past trains, future tests
Why must the split preserve chronological order?
6. Environment / Dependencies
- Python 3.10+
- NumPy and pandas
- scikit-learn metrics
- statsmodels for Holt-Winters
7. Data or Input Understanding
Plot the series before modeling. Trend is the long-term direction; seasonality is a pattern repeating at a known period; noise is remaining variation.
A random train/test split destroys the forecasting question because future months can teach the model about earlier months. Every transformation, lag and rolling value must use only information available at that timestamp.
8. Baseline / Simplest Working Approach
The naive forecast repeats the last observed value. A seasonal naive baseline repeats the value from 12 months earlier. These are hard to beat when the series is persistent or strongly seasonal.
Holt-Winters adds level, trend and seasonality. It is stronger than a flat baseline while remaining interpretable and consistent with existing dependencies.
Project figure
Actual sales versus two reproducible forecasts
Does the stronger model improve on the naive last-value baseline?
Worked Example: Calculate MAE and RMSE for four months
Given: Actual sales are [102, 108, 111, 115] and forecasts are [100, 111, 110, 119]. Residuals actual − forecast are [2, −3, 1, −4].
- 1Absolute errors are [2, 3, 1, 4]; their sum is 10.
- 2MAE = 10 / 4 = 2.50 sales units.
- 3Squared errors are [4, 9, 1, 16]; their sum is 30.
- 4RMSE = √(30 / 4) = √7.5 = 2.74 sales units.
Interpretation: Both metrics use the same four future errors. RMSE is larger because squaring gives the four-unit miss extra weight.
9. Step-by-Step Implementation
Step 1: Generate and split the series
The trend adds 1.5 units each month; the sine term repeats every 12 months; the seeded noise adds realistic variation.
import numpy as np
import pandas as pd
rng = np.random.default_rng(42)
months = pd.date_range("2023-01-01", periods=36, freq="MS")
trend = np.arange(36) * 1.5
season = 12 * np.sin(2 * np.pi * np.arange(36) / 12)
sales = 120 + trend + season + rng.normal(0, 3, 36)
series = pd.Series(sales, index=months, name="sales")
train = series.iloc[:24]
validation = series.iloc[24:30]
test = series.iloc[30:]
# Naive forecast: the last observed training value for every validation month.
naive_validation = np.repeat(train.iloc[-1], len(validation))Step 2: Fit the seasonal candidate
The model estimates level, additive trend and a 12-month seasonal pattern from train only.
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from sklearn.metrics import mean_absolute_error, mean_squared_error
# 12 means one yearly season contains 12 monthly observations.
model = ExponentialSmoothing(train, trend="add", seasonal="add", seasonal_periods=12)
fit = model.fit(optimized=True)
forecast = fit.forecast(len(validation))
mae = mean_absolute_error(validation, forecast)
rmse = mean_squared_error(validation, forecast) ** 0.5
print(f"validation MAE={mae:.2f}, RMSE={rmse:.2f}")Step 3: Refit only after choosing
After validation selects a method, combine train and validation, refit once and forecast exactly six untouched test months.
development = pd.concat([train, validation])
final_fit = ExponentialSmoothing(
development, trend="add", seasonal="add", seasonal_periods=12
).fit()
test_forecast = final_fit.forecast(6)10. Expected Results / Outputs
Read the output as evidence, not decoration
11. Evaluation
Evaluate at the exact business horizon and report units. MAE = 4.9 means the forecast misses by about 4.9 sales units per validation month on average.
Plot residuals by time and season. Runs above zero mean under-forecasting; increasing spread suggests changing variance; one large error may correspond to a promotion, stock-out or data issue.
Project figure
Residuals reveal when the forecast is systematically wrong
Are errors centered near zero, or do they form a pattern?
12. Failure Analysis
Failure mode → diagnosis → response
13. Improve the Project
14. Production Considerations
What changes beyond the notebook
15. Summary / Key Takeaways
- A forecasting dataset is ordered evidence, so development and testing must preserve time.
- The forecast horizon determines how data is split and which errors matter to the business.
- Last-value and seasonal-naive forecasts are essential baselines, not throwaway demos.
- Holt-Winters adds interpretable level, trend and seasonal components without future leakage.
- MAE gives the average absolute miss, while RMSE emphasizes larger misses.
- Residual plots reveal bias, missing events and structural change that one metric can hide.
- Production forecasting needs scheduled data checks, horizon-specific monitoring, uncertainty and a reliable fallback.