Your first model: forecasting sales with a straight line
Nakhla's question: “If we spend more on ads, how much will we sell?”. We have 8 months: ad spend (in thousands) and cartons sold (in hundreds). The simplest model is linear regression: draw the best straight line through the points.
The slope is how much sales rise per thousand riyals of ads. The intercept is the expected sales with zero ad spend. Move the two sliders and try to lower the average error, then let the machine compute the best line:
Try to lower the average error: the dashed lines are each month's errors.
The machine doesn't guess: a formula called least squares finds the line with the smallest sum of squared errors. Here is the same calculation in Python, with no libraries:
ads = [2, 3, 4, 5, 6, 7, 8, 9] # ad spend, thousand SAR sales = [52, 58, 63, 71, 74, 83, 87, 95] # cartons sold, hundreds n = len(ads) mx = sum(ads) / n my = sum(sales) / n top = 0 bottom = 0 for x, y in zip(ads, sales): top += (x - mx) * (y - my) bottom += (x - mx) ** 2 slope = top / bottom intercept = my - slope * mx print(f"sales = {intercept:.1f} + {slope:.2f} x ads") print(f"Forecast for 10k SAR: {intercept + slope * 10:.0f}")