Optional Python Appendix — Empirical Forecasting
Optional Python Appendix — Empirical Forecasting Contrast
Status in this course
This appendix is not part of the matrix-first core. Use it only after completing the R matrix laboratory. It shows how classical assumptions are compressed inside a familiar empirical forecasting workflow and provides a software comparison, not a second conceptual spine.
The cells use Pyodide with numpy, pandas, matplotlib, and statsmodels. Each lab has one self-contained cell so it can be run independently and verified locally.
Shared teaching series
The labs simulate 72 monthly observations from January 2019 through December 2024:
This intentionally combines trend, fixed month effects, a scheduled January 2023 intervention, and stationary AR(1) errors. It is a transparent teaching series, not an empirical dataset.
The data-generating mechanism is transparent, but these pages emphasise plots, tests, fitted objects, and backtests. Return to the core modules whenever a software call hides the corresponding covariance, projection, or state-space equation.
Lab map
| Lab | Question | Output |
|---|---|---|
| 1. Explore | what must be explained before fitting? | evidence log |
| 2. Diagnose stationarity | which transformation matches the mechanism? | transformation memo |
| 3. Fit ARMA errors | which parsimonious dependence model survives diagnostics? | candidate ledger |
| 4. Forecast and backtest | does it beat seasonal naive and cover uncertainty? | forecast report |
Supplementary outcomes
You will be able to:
- preserve frequency and chronology with a
DatetimeIndex; - inspect seasonality, lag structure, and interventions;
- compare ADF/KPSS evidence across transformations;
- fit ARMA candidates with
statsmodels, inspect roots, and test residual autocorrelation; - evaluate a regression-with-AR-errors model at rolling origins;
- report MAE, MASE, interval coverage, and a monitoring trigger.
Working rule
After every run, record:
Observation:
Mechanism:
Competing explanation:
Next diagnostic or backtest:
Begin with Python Lab 1, or return to the required R matrix laboratory.
R Matrix Lab 4 — State Space and Kalman Filtering
Implement Kalman prediction and correction from matrix equations, handle missing observations, and express AR forecasts as state propagation.
Optional Python Lab 1 — Explore Before Modeling
Build a time plot, seasonal view, lag plot, and ACF from a transparent monthly demand series.