Regression by worked example
Five houses, one formula, and every error metric calculated by hand. By the end you will know exactly what MAE, RMSE and R² on a model card are telling you.
The question: predict a number
Regression is what you use when the answer is a quantity: a price, a wait time, a monthly cost. Our question: given a house's size, what will it sell for?
The data: five houses
One feature (size in m²) and the target we want to predict (sale price in SAR):
| House | Size (m²) | Sold for (SAR) |
|---|---|---|
| A | 100 | 410,000 |
| B | 120 | 470,000 |
| C | 150 | 610,000 |
| D | 80 | 330,000 |
| E | 200 | 790,000 |
The formula the model learned
Training (lesson 1's guess–measure–adjust loop) lands on the straight line that fits these points best:
price = 4,000 × size + 10,000
Read it as: every square meter adds 4,000 SAR, on top of a 10,000 SAR base. Walk house A through it: 4,000 × 100 + 10,000 = 410,000 SAR — exactly what it sold for. House B: 4,000 × 120 + 10,000 = 490,000 SAR — but B actually sold for 470,000. The model is 20,000 SAR off on that one.
How wrong is it? MAE and RMSE by hand
Run all five houses through the formula and compare:
| House | Actual (SAR) | Predicted (SAR) | Error (SAR) |
|---|---|---|---|
| A | 410,000 | 410,000 | 0 |
| B | 470,000 | 490,000 | +20,000 |
| C | 610,000 | 610,000 | 0 |
| D | 330,000 | 330,000 | 0 |
| E | 790,000 | 810,000 | +20,000 |
(0 + 20,000 + 0 + 0 + 20,000) ÷ 5 = 8,000
"On a typical house, this model is about 8,000 SAR off." Same units as the price — the most readable error number there is.
(0² + 20,000² + 0² + 0² + 20,000²) ÷ 5 = 160,000,000 → √160,000,000 ≈ 12,650
Squaring makes big misses count extra: RMSE lands at about 12,650 SAR — noticeably above the 8,000 MAE, because two 20,000-SAR misses weigh more once squared. A wide gap between MAE and RMSE is a hint that a model is usually fine but occasionally badly wrong.
R²: better than guessing the average?
Errors in SAR tell you how far off the model is. R² answers a different question: how much better is it than the laziest possible model — one that predicts the average price (522,000 SAR) for every house?
Always-guess-the-average racks up squared errors of 131,680 million SAR². Our model's squared errors total just 800 million SAR².
R² = 1 − 800 ÷ 131,680 ≈ 0.994
The model explains 99.4% of the price differences between these houses. 1.0 would be perfect; 0 would mean "no better than the average"; below 0 means worse than the average.
What Bayanii does beyond this example
Real data has many features, and a straight line is only one family of rule. Bayanii trains several model families in parallel — straight lines, decision-tree ensembles, gradient boosting — scores each with the rotating exams from lesson 1 (cross-validation), and keeps the winner. The scores you just computed by hand are exactly the ones on the model card, measured on data the model never saw.
This lesson describes Bayanii's real pipeline in simplified terms. It is updated by hand when the pipeline changes, so wording may occasionally lag the code.