Brief
You will fit a trend to data you care about, such as daily steps, spending or temperature, and use it to forecast the next two weeks. The engine is least squares, the standard way to fit a model to data.
A model like has one equation per data point, far more equations than unknowns. No curve goes through every point, so you ask for the best one. Stack the model terms as columns of a design matrix and pick to make the residuals as small as possible in total:
There are two ways to solve this. The normal equations give a small square system you can already solve with BM-04's elimination. The second way is QR: Gram-Schmidt rewrites the columns of as perpendicular unit vectors, , and the fit becomes , a triangular system you solve from the bottom row up. Both give the same answer here. BM-06 shows when they stop agreeing.
DATA · any CSV time series
Objectives
Read-only. Log in to update progress.
VICTORY CONDITIONS
- Your QR coefficients match numpy.linalg.lstsq within 1e-8 for both the line and the quadratic, recorded with `python tools/mlarena.py record BM-05 "<largest difference>"`.
- No call to lstsq, solve, inv or qr from NumPy in your fitting code; only your own elimination and Gram-Schmidt.
- A chart with the data, both fits, the 14-day forecast, and a residual plot below it.
Side quests
Terms in plain words
- Least squares
- Pick the coefficients that make the sum of squared errors between the data and the model as small as possible.
- Residual
- One data point minus the model's prediction for it. Least squares minimises the sum of their squares.
- Design matrix
- A matrix with one row per data point and one column per term of the model, such as 1, t and t².
- Normal equations
- The square system XᵀX b = Xᵀy. Its solution is the least-squares fit.
- QR and Gram-Schmidt
- Gram-Schmidt turns the columns of X into perpendicular unit vectors Q, keeping the leftovers in an upper-triangular R, so X = QR.