← All Base Form Math projects
BM-05NOT STARTEDBASE FORM · LINEAR ALGEBRAMATHPYTHON

Trend fitter

Fit straight lines and curves to your own data (steps, spending, temperature), forecast the next two weeks, and plot the error.

EST.5 h
REWARD+100 XP
AFTERoriginal

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
fitted coefficients
GOAL · match within 1e-8
numpy.linalg.lstsq—
no results yet
YOUR RESULTS

Your recorded results appear here.

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 y≈b0+b1t+b2t2y \approx b_0 + b_1 t + b_2 t^2 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 XX and pick bb to make the residuals y−Xby - Xb as small as possible in total:

min⁡b  ∥Xb−y∥2\min_b \; \|Xb - y\|^2

There are two ways to solve this. The normal equations X⊤Xb=X⊤yX^\top X b = X^\top y 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 XX as perpendicular unit vectors, X=QRX = QR, and the fit becomes Rb=Q⊤yRb = Q^\top y, a triangular system you solve from the bottom row up. Both give the same answer here. BM-06 shows when they stop agreeing.

VISUALFig. 1
day 0 day 13 day 20 forecast residual = data − fit
A line fitted to 14 days of data. The red segments are residuals; least squares minimises the sum of their squares. The dashed part is the forecast. Why it's here: Shows what the fit is actually minimising, and what your error plot in objective 6 measures.
HINT · OBJECTIVE 1
Stuck? Hints stay hidden until you ask.
HINT · OBJECTIVE 4
Stuck? Hints stay hidden until you ask.

DATA · any CSV time series

Objectives

0 / 6 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. 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>"`.
  2. No call to lstsq, solve, inv or qr from NumPy in your fitting code; only your own elimination and Gram-Schmidt.
  3. A chart with the data, both fits, the 14-day forecast, and a residual plot below it.

Side quests

Add a weekly pattern with sin(2πd/7) and cos(2πd/7) columns, where d is the day number, and see if the residuals shrink.
Hold out the last two weeks, fit on the rest, and measure how far off each model's forecast is.

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.