The maths you need · lesson 6 of 12
Parts in this lesson
A(f(x + h) - f(x)) / ha slope from a small nudge
B(f(x + h) - f(x - h)) / (2 * h)the central difference, a better estimate

Rates of change

Last updated

Training a model means nudging its numbers so its mistakes shrink. To know which way to nudge, you need to know how fast the mistake changes when a number changes: its rate of change, the derivative. This lesson reads a derivative as the slope of a curve at one point, gives the four rules you'll use most, and shows how to check any derivative by nudging, which the course does every time it works one out by hand.

By the end of this lesson you can

  • Read a derivative as the slope of a curve at a point
  • Use the power, exponential, log and sum rules
  • Estimate a derivative by nudging, with the central difference
  • Explain why a step that's too small gives a worse answer, not a better one

Slope of a line

Lesson 1.1 met the slope of a line: how much y goes up for each step of 1 in x. Between any two points on a curve, the same idea gives the slope of the straight line joining them, the chord:

slope=f(x2)−f(x1)x2−x1

In words: the change in height divided by the change in x, "rise over run". For f(x)=x2 between x=1 and x=3: 9−13−1=4.

slope.ipynb

Slope at a point

A curve's slope changes as you move along it. To get the slope at one point, bring the second point closer and closer. The slider below does this: shrink h, the gap between the two points, and watch the dashed chord turn into the pink line that just touches the curve, the tangent.

Tangent slidery = x²

At x = 1, the curve’s slope (pink) is 2: the derivative of x² is 2x. The chord to x + h (dashed) has slope (4 − 1) ÷ 1 = 3. Make h smaller and it gets closer.

The same thing in numbers, for x2 at x=2. As h shrinks, the chord's slope closes in on 4:

slope.ipynb

The derivative

The number those slopes close in on is the derivative of f at x. It has two common names: f′(x), said "f prime of x", and dfdx, said "d f by d x".

f′(x)=limh→0⁡f(x+h)−f(x)h

In words: nudge x by a tiny h, see how much f changes, divide by h, and ask what that ratio settles on as h shrinks towards 0 (lim⁡ means "the limit").

What it tells you: if f′(x)=4, then nudging x up by a small amount makes f go up by about 4 times that amount. A positive derivative means "uphill to the right", negative means "downhill to the right", and 0 means the curve is flat there, as at the bottom of a valley. Gradient descent (Stage 3) uses exactly this to decide which way to move a model's weights.

Rules: power, eˣ, ln x and sums

You almost never take the limit by hand. A few rules cover nearly everything in this course:

FunctionDerivativeIn words
xnnxn−1bring the power down, lower it by 1
exexits own derivative
ln⁡x1xone over x
c⋅f(x)c⋅f′(x)a constant multiplier stays
f(x)+g(x)f′(x)+g′(x)the derivative of a sum is the sum

So the derivative of 3x2+2x is 3×2x+2=6x+2, which is 14 at x=2. Switch the slider above to ex and see the slope always equal the height. The chain rule, in the next lesson, handles functions inside functions.

Checking by nudging

You can check any derivative without knowing the rules, by nudging. The central difference nudges both ways and is much more accurate than nudging one way:

f′(x)≈f(x+h)−f(x−h)2h

In words: step a little to the right and a little to the left, and divide the change in f by the distance between them. In NumPy: (f(x + h) - f(x - h)) / (2 * h), with h around 1e-5. Here it checks each rule:

nudge.ipynb

Each pair agrees to about 10 decimal places. This is gradient checking, and the course does it after every derivative it works out by hand. If your formula and the nudge disagree past the sixth decimal place or so, the formula is wrong.

Wrong way: a step so tiny that rounding wins

If a smaller h is better, surely the tiniest is best? Run it:

tiny.ipynb

The answer gets better down to about h=10−8 (lesson 1.1's e notation: 1e-08 is 10−8), then worse: 4.00036 at 10−12, 3.55 at 10−15, and 0.0 at 10−17. A float64 keeps only about 16 significant digits. When h is tiny, f(2+h) and f(2) agree in nearly all of them, so subtracting leaves mostly rounding error, and dividing by tiny h blows that error up. At 10−17, 2+h rounds to exactly 2 (the last cell), so the difference is 0.

So use h around 10−5 with the central difference. That's the default in every slope function in this course.

Check what you learned

What is the derivative of x3?
Predict, then check with the central difference in step 5: what is the derivative of ln⁡x at x=4?
Your hand-worked derivative gives 7.3; the central difference with h=10−5 gives 6.0000000001. What should you suspect?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning