Objective
Learning objectives
- Explain constant rate of change as equal steps right always producing equal steps up (or down)
- Compute slope from two points as rise over run
- Read
m and b directly from the equation y = mx + b and explain what each controls - Derive a full linear equation from two real data points and use it to predict a third
Hook
If every 1,000 tokens costs the same fixed amount — no bulk discount, no surge pricing, just a flat rate — what does that guarantee about the shape of the graph? Not the numbers on it, the shape. Take a second before reading on: "the same fixed amount every time" is a claim about consistency, and consistency has a specific, visible signature on a graph.
See it
The signature is this: pick any two points on the graph that are the same horizontal distance apart, and they'll always be the same vertical distance apart too. Move 2,000 tokens to the right, cost always rises by the same $0.004 — whether you start at 0 tokens or 6,000. That "always the same rise for the same run" is exactly what makes a graph a straight line, and it's the visual definition of constant rate of change: the rate never speeds up or slows down anywhere along the line.
This is the test for "is this actually linear?"
If you only have a table of values (no graph drawn yet), you can test for a straight line without plotting anything: compute rise-over-run between several different pairs of points. If every pair gives the same number, it's linear. If even one pair disagrees, it isn't — some of the rate is changing, which Lesson 04 picks up directly.
Name it
Slope is that constant rate, computed precisely as rise over run: slope = (y2 - y1) / (x2 - x1), using any two points on the line. "Rise" is how far up (vertical change), "run" is how far right (horizontal change) — a positive slope climbs left to right, a negative slope drops.
Every straight-line function can be written y = mx + b, where m is the slope (steepness and direction) and b is the y-intercept (where the line crosses the y-axis, the value when x = 0). Changing m visibly tilts the line — larger m means steeper, negative m means it drops instead of climbs. Changing b slides the whole line up or down without changing its tilt at all. These two numbers are the entire description of a straight line — nothing else about it can vary.
Code it
def slope(x1, y1, x2, y2):
return (y2 - y1) / (x2 - x1)
# cost(tokens) = (tokens / 1000) * 0.002, using two of its real points
print(slope(2000, 0.004, 8000, 0.016))
# h(x) = -x + 4 from Lesson 02, using its two intercepts
print(slope(0, 4, 4, 0))
2e-06
-1.0
Both results are exact matches for what the formulas already told us: cost(tokens)'s rate is $0.002 per 1,000 tokens, which is $0.000002 per single token — precisely 2e-06. And h(x) = -x + 4 has m = -1 sitting right in the equation, which is exactly what slope() independently computed from just two points on its graph, no formula lookup required. Two completely different ways of asking "how steep is this" — reading m off the equation, and computing rise-over-run from points — agree exactly, which is the whole reason y = mx + b is trustworthy as a model in the first place.
m = slope(1, 3, 4, 12)
b = 3 - m * 1
print(m, b)
print(m * 10 + b)
slope(1, 3, 4, 12): rise = 12 - 3 = 9, run = 4 - 1 = 3, so m = 9/3 = 3.0. Solving y = mx + b using the point (1, 3): 3 = 3(1) + b, so b = 0.0. Predicting at x = 10: y = 3(10) + 0 = 30.0. Same three-step process as the worked example above — compute m from two points, solve for b using either point, then use the full equation to predict anywhere else on the line.