Linear Regression
A moving average only ever says "about the same as recently." Linear regression is braver: it draws a straight trend line through the prices and says "this is the direction things are heading, and here is where they will be next." Today you fit that line yourself, by hand, then let the maths find the perfect one.
The one idea
Plot the prices as dots, one per day. Now imagine laying a straight stick across them so it sits as close to all the dots as possible. That stick is your prediction machine: to guess a future day, just read where the stick is above that day. The gap between each dot and the stick is the error, and the best line is the one that makes those gaps smallest overall.
A residual is the vertical distance from a real dot to your line: how far that one prediction was off. Regression squares every residual (so big misses hurt more) and hunts for the line with the smallest total. That total is drawn live below as you drag.
Fit the line yourself
Drag the two gold handles up and down to tilt and shift the line through NCB's real prices. The coral sticks are your residuals, one per day. Watch the total error at the bottom. When you think you have it, press Snap to best fit and see how close you got to the line the maths would choose.
How to play
- Drag the left gold handle and the right gold handle to aim the line through the dots.
- Try to make the total coral error as small as you can.
- Press Snap to best fit. That is linear regression solving it in one step.
\(a\) is where the line starts, \(b\) is its slope (how much the price climbs per day). Regression picks the \(a\) and \(b\) with the smallest total squared error.
What it is great at, and where it slips
Things with a steady direction: a bill creeping up month after month, or a stock in a calm uptrend. It also tells you the rate of change.
The pattern bends. A straight line cannot follow a curve or a sudden turn, so it over- or under-shoots once the trend breaks.
If your electricity use has been climbing (hello, heatwave and more fans running), a trend line captures that rise in a way a plain average cannot. In the bonus light-bill challenge you will fit a regression line to your real bills and read off next month.
- Linear regression fits a straight trend line and reads the future off it.
- A residual is one dot's miss; the best fit has the smallest total squared residual.
- It captures a steady rise or fall, but cannot follow a curve or a sharp turn.
Next: ARIMA, the classic time-series workhorse that blends recent prices and recent misses.