Random Forests
A random forest is a machine-learning method that builds many decision trees on random samples of the data and averages their predictions. Each tree alone is noisy and over-fits, but the average of many different trees is stable and usually accurate. In supply chain it is used for demand forecasting, lead-time prediction, delivery-time estimates and classification tasks such as predicting late shipments.
How it works
- Bootstrap sampling (bagging): each tree is trained on a random sample of the rows, drawn with replacement.
- Random feature selection: at each split, a tree considers only a random subset of the features, so trees differ from one another.
- Grow many trees (hundreds is common).
- Combine: for regression, average the trees’ predictions; for classification, take a majority vote.
A tiny worked example
Five trees predict next week’s demand for a product given the features (price, promotion flag, day of week, last week’s sales): 118, 126, 110, 122 and 124.
Forecast = (118 + 126 + 110 + 122 + 124) ÷ 5 = 600 ÷ 5 = 120 units
The spread of the individual predictions (110 to 126) also shows uncertainty. A wide spread signals a prediction to treat with caution.
Typical features for demand forecasting
| Group | Examples |
|---|---|
| Calendar | Day of week, month, holiday flags, week of year |
| Price and promotion | Price, discount, promotion type, competitor price |
| History (lags) | Sales last week, same week last year, rolling average |
| External | Weather, events, economic indicators |
| Product and location | Category, store, region |
In Python
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor(n_estimators=300, random_state=1)
model.fit(X_train, y_train)
forecast = model.predict(X_next)
print(model.feature_importances_)
Strengths and limits
| Strengths | Limits |
|---|---|
| Captures non-linear effects and interactions without manual specification | Cannot extrapolate beyond the range of training data, so strong trends are underestimated unless handled (use differences or detrending) |
| Handles many features and mixed data types | Less interpretable than a simple model, although feature importance helps |
| Resistant to over-fitting compared with a single tree | Needs enough data and careful time-based validation |
| Few settings to tune | Does not understand time order by itself; lag features must be created |
Validation: use time, not random splits
For forecasting, always train on earlier data and test on later data (a walk-forward or rolling-origin test). Randomly shuffled splits leak the future into the training set and make accuracy look better than it is. Compare against simple benchmarks such as moving averages and Holt-Winters: a machine-learning model must beat them to justify its complexity.
Common mistakes
- Data leakage: using features that would not be known at forecast time.
- Random train/test splits for time series.
- Skipping a simple baseline comparison.
- Reading feature importance as cause and effect.
Key takeaways
- A random forest averages many randomized trees for a stable prediction.
- It captures non-linear patterns but cannot extrapolate trends.
- Validate with time-ordered tests and compare with simple baselines.
Frequently asked questions
What is a random forest?
A random forest is a machine-learning method that trains many decision trees on random samples and features and averages their predictions to give a stable, accurate result.
Can random forests forecast time series?
Yes, if you build features such as lags, calendar variables and drivers. They cannot extrapolate trends beyond the training range, so detrend or difference the data when trends are strong.
What is feature importance?
A measure of how much each input feature contributes to the model’s predictions. It indicates predictive value, not cause and effect.
How do I validate a forecasting model?
Train on earlier data and test on later data using a rolling or walk-forward approach, and compare against simple baselines.
Is a random forest better than ARIMA?
Not always. It can capture non-linear effects and many drivers, but simpler statistical models can win on short or smooth series. Compare both on held-out data.
