---
title: "Derivatives & the Chain Rule"
description: "Bring secants to a tangent, multiply local rates, and check a small-change prediction."
source: https://vizlearn.app/ai/classical-ml/derivatives-chain-rule
---

# Derivatives & the Chain Rule

Derivatives and the chain rule tell you how a small input error reaches an output. If one
stage locally scales changes by three and the next by four, their combined local rate is
twelve. For differentiable functions `y = f(g(x))`, the derivative is
`dy/dx = f′(g(x)) · g′(x)`.

Think of a measurement that is slightly high: the second stage receives the change already
scaled by the first. With a local rate of twelve, an input error of 0.01 predicts an output
change of 0.12. That prediction tells you the sensitivity near this input; evaluating the
function tells you its error. This scalar calculation underlies
[backpropagation](https://vizlearn.app/ai/neural-networks/backpropagation), which combines local derivatives in a
network, and [gradient descent](https://vizlearn.app/ai/neural-networks/gradient-descent), which uses derivatives
to choose update directions. Neither procedure is performed here.

## How Derivatives Measure Local Change

A derivative measures a function's local rate of change. To find it, start with a secant
joining two points on the curve. Its slope is the output change divided by the input
change. Starting at `x₀`, a right-hand probe at `x₀ + h` gives
`[H(x₀ + h) − H(x₀)] / h`. A left-hand probe at `x₀ − h` divides by `−h`, because
that input change is negative. Here `h` is a positive distance, never zero.

As `h` approaches zero, the two slopes must approach the same finite value for the derivative
to exist. That value is the tangent slope `H′(x₀)`. A small finite `h` estimates the limit;
it does not turn the difference quotient into an exact derivative.

For the square function, expanding `(u + h)²` gives the secant slope `2u + h`.
Letting `h` approach zero leaves `f′(u) = 2u`. For an affine function `g(x) = ax + b`,
the difference quotient is exactly `a` at every nonzero probe distance.

## How the Chain Rule Composes Changes

Write the computation in two stages: `u = g(x)`, then `y = f(u)`. Near `x₀`,
`Δu ≈ g′(x₀)Δx`. Near the corresponding `u₀ = g(x₀)`,
`Δy ≈ f′(u₀)Δu`. Combining those local approximations gives
`Δy ≈ f′(u₀)g′(x₀)Δx`.

The chain rule makes the coefficient exact: `H′(x₀) = f′(g(x₀))g′(x₀)`, provided
`g` is differentiable at `x₀` and `f` at `g(x₀)`. The finite-change prediction remains an
approximation. The notation `dy/du · du/dx` records how the rates compose; treating `du` as
an ordinary number to cancel is not a proof.

The outer derivative must be evaluated at the **inner output**. With `g(0) = 2`, use
`f′(2)`, even though the original input was zero. The outer function never received zero.
Forgetting the inner stage loses a factor of three in the default. Adding the rates gives
seven instead of twelve: the second stage scales the first stage's change, not the original
change independently.

## Derivatives & the Chain Rule Step by Step

The default uses `g(x) = 3x + 2`, `f(u) = u²`, and `x₀ = 0`.

1. **Fix the base point.** The inner output is `u₀ = 2`; the final output is `y₀ = 4`.
2. **Measure both sides.** At `h = 1`, the left and right secant slopes are 3 and 21.
   Reducing `h` keeps the base point fixed:

   | h | Left slope | Right slope |
   | --- | --- | --- |
   | 0.5 | 7.5 | 16.5 |
   | 0.1 | 11.1 | 12.9 |
   | 0.01 | 11.91 | 12.09 |

3. **Identify the limit.** Expanding `H(x) = (3x + 2)² = 9x² + 12x + 4` gives
   `H′(0) = 12`. The measurements approach that slope from opposite sides.
4. **Find the two local rates.** The inner rate is 3. At `u₀ = 2`, the outer rate is
   `2 × 2 = 4`; it acts on changes in `u`, not directly on changes in `x`.
5. **Pass one change through both stages.** For `Δx = 0.1`, the affine stage gives
   `Δu = 3 × 0.1 = 0.3` exactly. The outer stage receives that 0.3 and predicts
   `Δy ≈ 4 × 0.3 = 1.2`.
6. **Collect the factors.** The two scalings give `4 × (3 × Δx) = (4 × 3)Δx`.
   The chain-rule derivative is `4 × 3 = 12`, agreeing with the composite curve.
7. **Check the function.** The new output is `2.3² = 5.29`, so the actual change is
   `5.29 − 4 = 1.29`. The prediction error is 0.09.
8. **Reduce the change.** At `Δx = 0.01`, the prediction is 0.12 and the actual change is
   `2.03² − 4 = 0.1209`. The error falls to 0.0009.

The moving probes keep the original point pinned while the secants approach its tangent.
Later, the same change passes through the two function links. The error bar measures the
absolute gap between predicted and actual output change; it has its own scale, separate from
the curve's axes. The first error stays beside the smaller-step error, so you can compare
0.09 with 0.0009 without remembering the previous frame.

## How Accurate Is the Derivative Prediction?

For the square outer function, expanding the actual change gives
`Δy = 2u₀(aΔx) + (aΔx)²`. The chain-rule prediction is the first term. Its error is
exactly `(aΔx)²`, so dividing `Δx` by ten divides this example's error by one hundred.
That quadratic error rate is a property of this function, not a promise for every
differentiable function.

Change only `a` from 3 to 1 while keeping `b = 2` and `x₀ = 0`. The base output and outer
slope stay fixed, but the composite derivative falls from 12 to 4. Set `a = −3` instead:
the derivative becomes −12. A negative rate reverses the direction of the response.

The input panel retains the default comparison for these cases: the inner rate changes while
`u₀ = 2` and the outer rate 4 stay fixed. Use the derivative to estimate sensitivity, then check
the finite change against the function. A zero derivative alone does not guarantee an
unchanged output.

## Edge Cases: Zero Slope Is Not the Same as No Derivative

- **Flat outer slope:** with `a = 3`, `b = 0`, and `x₀ = 0`, the square composite has
  derivative zero. Yet `Δx = 0.1` produces `Δy = 0.09`. Zero first-order change does not
  imply zero finite change.
- **Absolute-value corner:** `g(x) = x`, `f(u) = |u|` at zero has left slope −1 and right
  slope +1. There is no derivative, even though a symmetric difference quotient is zero.
- **Constant through a corner:** `g(x) = 0` makes `|g(x)|` constant. Its derivative is zero
  by direct evaluation, although `f′(0)` is undefined. The usual chain-rule hypothesis fails;
  that alone does not decide whether the composite is differentiable.
- **Absolute value away from zero:** the outer derivative is +1 or −1. The linear prediction
  is exact while the perturbation stays on the same affine segment; crossing the corner can
  break that finite-step equality.

The numeric control bounds keep plots legible. They are display limits, not restrictions on
the chain rule.

## Common Mistakes: Adding Rates or Treating a Prediction as Exact

- Using `f′(x₀)` instead of `f′(g(x₀))`: the outer stage receives `u₀`.
- Adding local derivatives or omitting the inner factor: sequential changes scale one another.
- Substituting `h = 0` into a difference quotient: the derivative is a limit, not division by zero.
- Replacing an undefined derivative with zero: a corner and a horizontal tangent make different claims.
- Treating `H′(x₀)Δx` as the exact finite change: evaluate the function to check it.
