11.1 Limits, Derivatives, and the Chain Rule: Describing Local Change
How much does a prediction change after a small parameter adjustment? What is the instantaneous velocity along a continuous position curve? Both questions begin with behavior near a point, followed by the limits of that local approximation.
Learning Objectives
- Use limits to understand derivatives, rather than just memorizing derivative rules;
- Distinguish between continuity, differentiability, and the appearance of smoothness in numerical data;
- Break down composite functions using the product and chain rules;
- Understand the boundaries between finite differences, symbolic differentiation, and automatic differentiation.
1. Limits Describe Behavior Near a Point
When
Limits focus on behavior near a point, without requiring that
For example,
A function is continuous at
is defined; exists; - These two values are equal.
Discrete computers can only sample a finite number of points. A smooth-looking graph does not imply mathematical continuity, sampling might miss sharp corners, discontinuities, or high-frequency oscillations.
2. The Derivative Is a Local Linear Approximation
The derivative is defined as the limit of a difference quotient:
Geometrically, it represents the slope of the tangent line; in modeling terms, it provides a first-order approximation to small perturbations:
This requires differentiability at the point. More precisely, the remainder divided by
Common rules include:
More generally, the derivative of
Apply the power rule where the function and right-hand expression are defined. Integer
Differentiability implies continuity, but continuity does not imply differentiability. For example,
3. Composite Systems Require the Chain Rule
If
For example:
the derivative of the outer square is
Software systems are often composed of multiple layered functions. In neural networks, a computation graph represents the operations, forward propagation evaluates its nodes, and backpropagation repeatedly applies the chain rule from output to input. Its key advantage isn't simply "computing one derivative"; it's the ability to reuse intermediate results, enabling efficient gradient computation of a scalar output with respect to a large number of parameters.
Writing the example as two intermediate nodes lets us inspect one backward pass. bar_v means the output derivative with respect to v; contributions through repeated uses of the same variable must be added:
x = 2.0
v = 3 * x + 1
y = v * v
bar_y = 1.0
bar_v = bar_y * (v + v) # Both inputs of the product depend on v.
bar_x = bar_v * 3
print(y, bar_x) # 49.0 42.0This Python 3 code writes the local derivatives explicitly; it is not a general autodiff engine. A framework must also retain needed intermediate values, manage the graph, and handle unsupported operations.
You should also master the product rule:
Miswriting the product as
4. Multivariate Functions, Partial Derivatives, and Gradients
For a function
The gradient is formed by collecting all partial derivatives:
For differentiable functions, the directional derivative along a unit vector
The gradient points in the direction of steepest ascent; the negative gradient points in the direction of steepest descent. The term "steepest" depends on the Euclidean norm; altering the metric of the space changes what is considered the steepest direction.
When the gradient is zero, every first-order directional derivative is zero, so there is no unique steepest direction. Existence of partial derivatives alone does not imply differentiability: define
5. Finite differences are approximations, not automatic differentiation
The central difference formula is:
from math import isfinite
def f(x: float) -> float:
return x**3 - 2 * x
def central_difference(x: float, h: float) -> float:
if not isfinite(h) or h <= 0:
raise ValueError("h must be positive and finite")
return (f(x + h) - f(x - h)) / (2 * h)
x = 1.5
exact = 3 * x**2 - 2
for h in [1e-1, 1e-3, 1e-5, 1e-8, 1e-12, 1e-20]:
estimate = central_difference(x, h)
print(f"{h:.0e} {estimate:.8f} {abs(estimate - exact):.3e}")As f(x+h) and f(x-h) become extremely close, and floating-point rounding and cancellation errors begin to dominate. Therefore, "make
For this cubic, exact-arithmetic central differences equal h=1e-20 makes x+h == x-h == x, so the estimate is zero and its error is 4.75. Last digits for other steps can vary by environment.
Three methods of differentiation must be distinguished:
| Method | Result | Typical Use Case |
|---|---|---|
| Symbolic differentiation | Operates on expressions to derive a new formula | Algebraic derivation, simplification |
| Finite differences | Approximates derivatives using function values | Gradient verification, black-box functions |
| Automatic differentiation | Applies the chain rule to basic operations | Model training, differentiable programs |
Automatic differentiation propagates derivatives of elementary operations through the chain rule without a finite-difference step. It still uses floating-point arithmetic and does not guarantee machine-precision final derivative error for every problem. Overflow, cancellation, ill conditioning, and nondifferentiable-point conventions affect results. See PyTorch’s autograd notes, including which branches and operations actually enter the computation.
Common Misconceptions
- The derivative is simply the function value divided by the input: The derivative is the limit of the ratio of local increments, not a direct division of output by input.
- Continuity implies a smooth slope: A function can be continuous at a point and still have a sharp corner, continuity does not guarantee differentiability.
- The gradient points to the global optimum: It only indicates the direction of first-order change at the current point; it does not guarantee a path to the global best solution.
- Smaller
in numerical differentiation yields greater accuracy: There's a trade-off between truncation error and rounding error, using an extremely small can actually degrade precision.
Exercise
- Use the definition of the derivative to derive the derivative of
. - Apply the chain rule to differentiate
. - Compute the gradient of
. - Run the central difference example to estimate the value of
that minimizes error in the current environment.
Summary
Limits give a precise meaning to approaching a point, and derivatives provide a local linear model for functions. The chain rule breaks down complex compositions into products of local derivatives. Automatic differentiation systematizes these rules, but it doesn't decide whether a function is suitable for differentiation or how to handle points where the derivative doesn't exist.
The next lesson explores the other half of calculus: how local changes accumulate into totals, and how computers approximate analytical integrals.