The Chain Rule and Computational Graphs

⚡ Key Takeaways

The chain rule computes the derivative of a composition of functions by multiplying the derivatives of the constituent functions. In words: to differentiate f(g(x))f(g(x)), multiply the rate of change of ff (at g(x)g(x)) by the rate of change of gg (at xx). It applies everywhere derivatives appear — single-variable calculus, multivariable analysis, differential geometry, and modern machine learning (where its computational-graph form is called backpropagation). First systematized by Leibniz (1676) via differential notation; the computational-graph perspective matured with reverse-mode automatic differentiation in the 1970s–80s.

💡 The Core Insight

A composition is a pipeline: x→u→yx \to u \to y. The chain rule says the overall sensitivity is the product of local sensitivities along the pipeline. On a graph with branches and merges, "product along a path" generalizes to "sum over all paths, each path being a product of edge derivatives." Reverse-mode differentiation is just an efficient order of summation that reuses shared subexpressions.

Formal Statement

DefinitionComposite Function

Given functions g:D⊆R→Rg : D \subseteq \mathbb{R} \to \mathbb{R} and f:E⊆R→Rf : E \subseteq \mathbb{R} \to \mathbb{R} with g(D)⊆Eg(D) \subseteq E, the composite f∘gf \circ g is defined by (f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x)). We call gg the inner function and ff the outer function. The intermediate value u=g(x)u = g(x) is the node variable connecting them.

TheoremChain Rule — Single Variable

Let gg be differentiable at x0x_0 and let ff be differentiable at u0=g(x0)u_0 = g(x_0). Then f∘gf \circ g is differentiable at x0x_0 and

1
(f∘g)′(x0)=f′(g(x0)) g′(x0).\quad (f \circ g)'(x_0) = f'\big(g(x_0)\big)\, g'(x_0).

Derivation hint: Differentiate ff at the point u0=g(x0)u_0=g(x_0), then multiply by the inner rate g′(x0)g'(x_0); see the proof below using the error-form of differentiability. In Leibniz notation, with y=f(u)y = f(u), u=g(x)u = g(x):

2
dydx=dydu⋅dudx.\quad \frac{dy}{dx} = \frac{dy}{du}\cdot\frac{du}{dx}.

Derivation hint: Treat dy/dudy/du and du/dxdu/dx as ratios of differentials; the cancellation of dudu is suggestive but must be justified by the limit argument in the proof.

TheoremChain Rule — Multivariable

Let g:Rn→Rm\mathbf{g} : \mathbb{R}^n \to \mathbb{R}^m be differentiable at x0\mathbf{x}_0 and f:Rm→Rk\mathbf{f} : \mathbb{R}^m \to \mathbb{R}^k differentiable at g(x0)\mathbf{g}(\mathbf{x}_0). Then the Jacobian of the composite is the matrix product of the Jacobians:

3
Jf∘g(x0)=Jf(g(x0)) Jg(x0).\quad J_{\mathbf{f}\circ\mathbf{g}}(\mathbf{x}_0) = J_{\mathbf{f}}\big(\mathbf{g}(\mathbf{x}_0)\big)\, J_{\mathbf{g}}(\mathbf{x}_0).

Derivation hint: Apply the linear-approximation definition of total differentiability to both maps; the composite's best linear approximation is the composition of the linear approximations, whose matrix is the product. Componentwise, for a scalar z=f(u1,…,um)z = f(u_1,\dots,u_m) where each uj=gj(x1,…,xn)u_j = g_j(x_1,\dots,x_n):

4
∂z∂xi=∑j=1m∂z∂uj ∂uj∂xi.\quad \frac{\partial z}{\partial x_i} = \sum_{j=1}^{m} \frac{\partial z}{\partial u_j}\,\frac{\partial u_j}{\partial x_i}.

Derivation hint: Expand the matrix product in [3][3] for a single output row and single input column; the sum runs over the shared intermediate variables uju_j — this is the "sum over paths through the uju_j layer."

DefinitionComputational Graph

A computational graph for an expression is a directed acyclic graph G=(V,E)G = (V, E) where each vertex vv holds a variable computed by an elementary operation ϕv\phi_v of its parent vertices pa(v)\text{pa}(v): v=ϕv({p:p∈pa(v)})v = \phi_v\big(\{p : p \in \text{pa}(v)\}\big). Source vertices are inputs; a designated sink is the output. Each edge (p→v)(p \to v) carries the local partial derivative ∂v/∂p\partial v / \partial p, evaluated at the current values. The full derivative of the output with respect to an input is obtained by applying the chain rule across the graph.

Figure
Computational graph for y=sin⁡(x1x2)+x1y = \sin(x_1 x_2) + x_1 with edge-local derivatives; backward pass accumulates ∂y/∂xi\partial y/\partial x_i by summing over paths.
x₁ x₂ u=x₁x₂ v=sin u y=v+x₁ ∂u/∂x₁=x₂ ∂u/∂x₂=x₁ ∂v/∂u=cos u ∂y/∂v=1 ∂y/∂x₁ = 1 (direct path)

Why It Holds / Derivation

proof[Single-Variable Chain Rule via the Error Form] Step 1 — Differentiability as linear approximation with controlled error. Because ff is differentiable at u0u_0, there is a function ε\varepsilon with lim⁡k→0ε(k)=0=ε(0)\lim_{k\to 0}\varepsilon(k) = 0 = \varepsilon(0) such that for all small kk:

5
f(u0+k)−f(u0)=(f′(u0)+ε(k)) k.\quad f(u_0 + k) - f(u_0) = \big(f'(u_0) + \varepsilon(k)\big)\,k.

Derivation hint: Define ε(k)=f(u0+k)−f(u0)k−f′(u0)\varepsilon(k) = \frac{f(u_0+k)-f(u_0)}{k} - f'(u_0) for k≠0k\neq 0 and ε(0)=0\varepsilon(0)=0; differentiability forces ε(k)→0\varepsilon(k)\to 0.

Step 2 — Set the inner increment. Let hh be a small increment in xx, and define k=g(x0+h)−g(x0)k = g(x_0 + h) - g(x_0). By differentiability of gg, k→0k \to 0 as h→0h \to 0.

Step 3 — Substitute. Then

6
f(g(x0+h))−f(g(x0))=(f′(u0)+ε(k)) (g(x0+h)−g(x0)).\quad f(g(x_0+h)) - f(g(x_0)) = \big(f'(u_0) + \varepsilon(k)\big)\,\big(g(x_0+h) - g(x_0)\big).

Derivation hint: Replace u0+ku_0 + k by g(x0+h)g(x_0+h) in [5][5]; this remains valid even when k=0k = 0 because the right side then vanishes too.

Step 4 — Divide by hh and take the limit.

7
f(g(x0+h))−f(g(x0))h=(f′(u0)+ε(k)) g(x0+h)−g(x0)h.\quad \frac{f(g(x_0+h)) - f(g(x_0))}{h} = \big(f'(u_0) + \varepsilon(k)\big)\,\frac{g(x_0+h)-g(x_0)}{h}.

Derivation hint: As h→0h\to 0: ε(k)→0\varepsilon(k)\to 0 (since k→0k\to 0), and the difference quotient of gg tends to g′(x0)g'(x_0); multiply the limits. Taking h→0h \to 0 gives (f∘g)′(x0)=f′(u0) g′(x0)=f′(g(x0)) g′(x0)(f\circ g)'(x_0) = f'(u_0)\,g'(x_0) = f'(g(x_0))\,g'(x_0). ■\blacksquare

⚠️ Why the naïve "cancel dudu" proof fails

The seductive argument ΔyΔx=ΔyΔu⋅ΔuΔx\frac{\Delta y}{\Delta x} = \frac{\Delta y}{\Delta u}\cdot\frac{\Delta u}{\Delta x} followed by taking limits breaks when Δu=g(x0+h)−g(x0)=0\Delta u = g(x_0+h)-g(x_0) = 0 for some sequence of h→0h \to 0 (e.g. gg locally constant or oscillating). Division by Δu\Delta u is then illegal. The error-form proof above avoids any division by Δu\Delta u, which is exactly why it is the correct argument.

GW
Gottfried Wilhelm Leibniz
1646–1716

Leibniz, co-inventor of calculus, gave us the differential notation dy/dxdy/dx in which the chain rule appears as the almost-tautological cancellation dydx=dydududx\frac{dy}{dx} = \frac{dy}{du}\frac{du}{dx}. His manuscripts from November 1676 already manipulate composite differentials. While the notation makes the rule memorable, it obscures the subtlety (see the warning above) — a gap rigorously closed only with the 19th-century ε\varepsilon–δ\delta definition of the limit by Cauchy and Weierstrass. Leibniz's deeper gift was the operational viewpoint: differentiation as a symbolic process that propagates along the structure of an expression — the conceptual seed of computational graphs three centuries later.

Building Intuition

🧠 Intuition

Think of nested gears. If gear AA drives gear BB at ratio 3:13{:}1 (B turns 3× as fast), and BB drives CC at ratio 2:12{:}1, then AA drives CC at 6:16{:}1 — ratios multiply. The derivative g′(x)g'(x) is the local gear ratio from xx to uu; f′(u)f'(u) is the ratio from uu to yy; the composite ratio is their product. Now add branching. If xx influences yy through two independent gear trains (paths), the total effect is the sum of the two products — small pushes from each route add up. That is exactly [4][4]: sum over paths, multiply along each. The computational graph is the literal gear diagram, and backpropagation is the disciplined way to total up all the ratios without recomputing shared gears.

Worked Examples

ExampleSingle-Variable: a nested trig–power function

Given: y=(sin⁡(3x))4y = \big(\sin(3x)\big)^4. Find dydx\frac{dy}{dx} at x=π/6x = \pi/6.

Step 1 — Identify the layers. Outer: f(u)=u4f(u)=u^4. Middle: u=sin⁡(w)u = \sin(w). Inner: w=3xw = 3x. Three nested functions.

Step 2 — Local derivatives.

  • dydu=4u3\dfrac{dy}{du} = 4u^3
  • dudw=cos⁡w\dfrac{du}{dw} = \cos w
  • dwdx=3\dfrac{dw}{dx} = 3

Step 3 — Multiply (chain rule for a 3-chain).

8
dydx=4(sin⁡3x)3⋅cos⁡(3x)⋅3=12 sin⁡3(3x)cos⁡(3x).\quad \frac{dy}{dx} = 4\big(\sin 3x\big)^3 \cdot \cos(3x)\cdot 3 = 12\,\sin^3(3x)\cos(3x).

Derivation hint: Apply [2][2] twice: dydx=dydududwdwdx\frac{dy}{dx}=\frac{dy}{du}\frac{du}{dw}\frac{dw}{dx}; substitute w=3xw=3x, u=sin⁡3xu=\sin 3x.

Step 4 — Evaluate at x=π/6x=\pi/6. Here 3x=π/23x = \pi/2, so sin⁡(3x)=1\sin(3x)=1, cos⁡(3x)=0\cos(3x)=0.

dydx∣π/6=12⋅13⋅0=0.\frac{dy}{dx}\Big|_{\pi/6} = 12\cdot 1^3 \cdot 0 = 0.
dydx∣x=π/6=0  (dimensionless)\boxed{\dfrac{dy}{dx}\Big|_{x=\pi/6} = 0 \ \ (\text{dimensionless})}

The zero makes sense: at 3x=π/23x=\pi/2, sin⁡(3x)\sin(3x) is at its maximum, so sin⁡4(3x)\sin^4(3x) momentarily stops changing.

ExampleMultivariable / Graph: reverse-mode pass

Given: y=sin⁡(x1x2)+x1y = \sin(x_1 x_2) + x_1 at (x1,x2)=(2,π4)(x_1,x_2) = (2, \tfrac{\pi}{4}). Compute ∂y/∂x1\partial y/\partial x_1 and ∂y/∂x2\partial y/\partial x_2 by a forward pass then a backward pass.

Forward pass (compute node values).

  • u=x1x2=2⋅π4=π2≈1.5708u = x_1 x_2 = 2\cdot \tfrac{\pi}{4} = \tfrac{\pi}{2} \approx 1.5708
  • v=sin⁡u=sin⁡(π2)=1v = \sin u = \sin(\tfrac{\pi}{2}) = 1
  • y=v+x1=1+2=3y = v + x_1 = 1 + 2 = 3

Backward pass (seed yˉ≡∂y/∂y=1\bar y \equiv \partial y/\partial y = 1, propagate adjoints).

9
vˉ=yˉ ∂y∂v=1⋅1=1,uˉ=vˉ ∂v∂u=1⋅cos⁡u=cos⁡π2=0.\quad \bar v = \bar y\,\frac{\partial y}{\partial v} = 1\cdot 1 = 1,\qquad \bar u = \bar v\,\frac{\partial v}{\partial u} = 1\cdot \cos u = \cos\tfrac{\pi}{2} = 0.

Derivation hint: Each adjoint nˉ=∂y/∂n\bar n = \partial y/\partial n; multiply the downstream adjoint by the local edge derivative.

Accumulate to inputs (sum over all incoming paths).

10
∂y∂x1=uˉ ∂u∂x1⏟via u+yˉ ∂y∂x1⏟direct=0⋅x2+1⋅1=1.\quad \frac{\partial y}{\partial x_1} = \underbrace{\bar u\,\frac{\partial u}{\partial x_1}}_{\text{via }u} + \underbrace{\bar y\,\frac{\partial y}{\partial x_1}}_{\text{direct}} = 0\cdot x_2 + 1\cdot 1 = 1.

Derivation hint: x1x_1 reaches yy by two paths — through uu and directly via the +x1+x_1 term; [4][4] tells us to add them.

11
∂y∂x2=uˉ ∂u∂x2=0⋅x1=0.\quad \frac{\partial y}{\partial x_2} = \bar u\,\frac{\partial u}{\partial x_2} = 0\cdot x_1 = 0.

Derivation hint: x2x_2 reaches yy only through uu; one path, so a single product.

∇y=(∂y∂x1, ∂y∂x2)=(1, 0)  (dimensionless)\boxed{\nabla y = \Big(\tfrac{\partial y}{\partial x_1},\ \tfrac{\partial y}{\partial x_2}\Big) = (1,\ 0) \ \ (\text{dimensionless})}

Note: a single backward pass produced both partials — the efficiency that powers backpropagation.

Common Misconceptions

⚠️ Three classic errors

1. Evaluating the outer derivative at xx instead of at u=g(x)u=g(x). f′f' must be evaluated at g(x0)g(x_0), not x0x_0. Writing (sin⁡(x2))′=cos⁡(x)⋅2x(\sin(x^2))' = \cos(x)\cdot 2x is wrong; it is cos⁡(x2)⋅2x\cos(x^2)\cdot 2x. 2. Forgetting the inner derivative entirely. (sin⁡(x2))′≠cos⁡(x2)(\sin(x^2))' \neq \cos(x^2). The factor 2x2x is mandatory. 3. Treating dy/dudy/du as a genuine fraction. It is notation for a limit. The cancellation works as a mnemonic but is not a proof, and it fails to generalize cleanly to partial derivatives, where ∂z/∂x≠1/(∂x/∂z)\partial z/\partial x \ne 1/(\partial x/\partial z) in general.

CounterexampleA composite that is differentiable while neither factor's quotient is "tame"

Let g(x)=x2sin⁡(1/x)g(x) = x^2 \sin(1/x) for x≠0x\neq 0, g(0)=0g(0)=0. Then gg is differentiable at 00 with g′(0)=0g'(0)=0, but g′(x)g'(x) oscillates wildly near 00 and Δu=g(x0+h)−g(0)\Delta u = g(x_0+h)-g(0) equals 00 infinitely often as h→0h\to 0. With f(u)=uf(u)=u (so f∘g=gf\circ g = g), the naïve "divide by Δu\Delta u" proof is invalid on those sequences — yet the chain rule still holds because the error-form proof ([5][5]–[7][7]) never divides by Δu\Delta u. This is the canonical example showing why rigor matters.

Connections

connection

The chain rule is the connective tissue of differential calculus. It powers Implicit Differentiation (differentiate F(x,y(x))=0F(x,y(x))=0 and solve for y′y'), Related Rates (link dV/dtdV/dt to dr/dtdr/dt through dV/drdV/dr), and The Substitution Rule for Integration (which is the chain rule read backwards). In several variables it underlies the Gradient and Directional Derivative and the Jacobian Matrix. Its graph form is precisely Backpropagation in neural networks, and the abstract version — composition of linear approximations — is the statement of the Total Derivative in Differential Geometry (the functoriality of the tangent map: T(f∘g)=Tf∘TgT(f\circ g) = Tf \circ Tg).

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- 1676 — Leibniz manipulates composite differentials in unpublished manuscripts, foreshadowing dydx=dydududx\frac{dy}{dx}=\frac{dy}{du}\frac{du}{dx}.
- 1684 — Leibniz publishes his first calculus paper (Nova Methodus), establishing differential notation that encodes the chain rule.
- 1690s — The Bernoullis and L'Hôpital apply chain-rule reasoning to curves and nested expressions across Europe.
- 1820s — Cauchy's Cours d'analyse (1821) introduces limit-based rigor; the chain rule gets its first careful, if still informal, treatment.
- 1860s–1870s — Weierstrass's ε\varepsilon–δ\delta framework enables the modern error-form proof, closing the "divide by Δu\Delta u" gap.
- 1870s — The multivariable / Jacobian chain rule is formalized in the work of Jacobi's successors on transformations and partial differentiation.
- 1964 — Wengert publishes a method for the automatic evaluation of derivatives — an early forward-mode automatic differentiation.
- 1970 — Seppo Linnainmaa describes the reverse-mode accumulation of derivatives (the algorithmic chain rule on a graph) in his master's thesis.
- 1986 — Rumelhart, Hinton & Williams popularize backpropagation for neural networks, igniting modern deep learning.

Subtleties & Limits

open-problem · Boundaries of the rule

The chain rule requires differentiability; at points where the inner or outer function is merely continuous (e.g. ∣x∣|x| at 00), the composite may fail to be differentiable or require subgradients — leading to the calculus of Clarke subdifferentials and questions about which "nonsmooth chain rules" hold for compositions like ReLU\text{ReLU} networks, where backpropagation is used in practice despite kink points. A related modern theme is numerical stability: repeated multiplication of edge derivatives causes vanishing or exploding gradients in deep graphs; understanding when the product of many Jacobians stays well-conditioned (via residual connections, normalization, orthogonal initialization) remains an active research area. Finally, in infinite-dimensional and stochastic settings, generalizations (Fréchet-derivative chain rule, Itô's formula as a "stochastic chain rule" with an extra second-order term) show that the simple product law is only the smooth, deterministic special case of a richer family of composition rules.

See Also

Product Rule · Quotient Rule · Jacobian Matrix · Gradient and Directional Derivative · Backpropagation · Automatic Differentiation · Faà di Bruno's Formula · Itô's Lemma · Total Derivative