SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons

Level 2, Single-variable calculus 2.3 Differentiation rules Lesson 3 of 7

ConceptLesson 2.3.3 in Level 2, Single-variable calculus. About 30 minutes.

The chain rule

A function inside a function

(3x+1)5(3x + 1)^5 is not a power of xx. It is a power of the expression 3x+13x + 1, so the power rule of lesson 2.3.1 does not apply to it directly. It is a composition (lesson 1.1.3): an inner function g(x)=3x+1g(x) = 3x + 1 fed into an outer function f(u)=u5f(u) = u^5.

The letter uu names the inner function's output, so u=3x+1u = 3x + 1 and y=u5y = u^5.

To spot the two parts, evaluate the function at a number. At x=2x = 2, you first work out 3×2+1=73 \times 2 + 1 = 7 (the inner function), then 757^5 (the outer one). The last step is the outer function.

functioninner uuouter, in terms of uu
(3x+1)5(3x + 1)^53x+13x + 1u5u^5
x2+1\sqrt{x^2 + 1}x2+1x^2 + 1u=u1/2\sqrt{u} = u^{1/2}
1x2+1\dfrac{1}{x^2 + 1}x2+1x^2 + 11u=u−1\dfrac{1}{u} = u^{-1}
(5−2x)3(5 - 2x)^35−2x5 - 2xu3u^3

You could expand (3x+1)5(3x + 1)^5 into six terms and use lesson 2.3.1, but it is long. x2+1\sqrt{x^2 + 1} cannot be expanded at all. The chain rule handles both in one line.

Slopes multiply

Rates that feed into each other multiply. A car that uses 0.06 litres of fuel per km, driving at 80 km per hour, uses 0.06×80=4.80.06 \times 80 = 4.8 litres per hour. The units cancel like fractions: litres per km times km per hour is litres per hour.

A composition works the same way. When xx moves, uu moves dudx\frac{du}{dx} times as much, and then yy moves dydu\frac{dy}{du} times as much as uu. So yy moves dydu⋅dudx\frac{dy}{du} \cdot \frac{du}{dx} times as much as xx.

Take y=(2x+1)2y = (2x + 1)^2 at x=1x = 1. The inner function u=2x+1u = 2x + 1 has slope 2, and at x=1x = 1 it gives u=3u = 3. The outer function y=u2y = u^2 has slope 2u2u, which is 6 at u=3u = 3.

A step of 0.05 in xx is stretched by 2 on the way to uu, then by about 6 on the way to yy. The stretches multiply, so yy moves about 2×6=122 \times 6 = 12 times as far as xx.

As the step shrinks, each stretch settles on a slope, and the total settles on their product.

step Δx\Delta x0.050.010.001
stretch Δu÷Δx\Delta u \div \Delta x222
stretch Δy÷Δu\Delta y \div \Delta u6.16.026.002
total Δy÷Δx\Delta y \div \Delta x12.212.0412.004

Algebra agrees. Expanding, (2x+1)2=4x2+4x+1(2x + 1)^2 = 4x^2 + 4x + 1, with derivative 8x+48x + 4, which is 12 at x=1x = 1.

Rule: Chain rule

ddx f(g(x))=f′(g(x))⋅g′(x)dydx=dydu⋅dudx\begin{gathered} \frac{d}{dx}\,f\big(g(x)\big) = f'\big(g(x)\big)\cdot g'(x) \\[4pt] \frac{dy}{dx} = \frac{dy}{du}\cdot\frac{du}{dx} \end{gathered}

Differentiate the outer function, leaving the inside as it is; then multiply by the derivative of the inside. f′(g(x))f'(g(x)) means the outer slope taken at u=g(x)u = g(x), not at xx.

That last point matters. For (2x+1)2(2x + 1)^2 at x=1x = 1, the outer slope 2u2u is taken at u=3u = 3, giving 6. Taking it at x=1x = 1 would give an outer slope of 2 and a total of 2×2=42 \times 2 = 4, which is wrong.

Try it

The loop measures both stretches for (2x+1)2(2x + 1)^2 from x=1x = 1, then the total, for three step sizes.

SymPy then differentiates (2x+1)2(2x + 1)^2 directly and evaluates the slope at 1.

⌘/Ctrl + Enter runs the cell

Worked examples

A linear inside. For (3x+1)5(3x + 1)^5, the outer u5u^5 gives 5u45u^4 and the inner 3x+13x + 1 gives 3. So the derivative is 5(3x+1)4⋅3=15(3x+1)45(3x + 1)^4 \cdot 3 = 15(3x + 1)^4, which is 15 at x=0x = 0. An inside ax+bax + b always adds the factor aa.

A falling inside. For (5−2x)3(5 - 2x)^3 the inner slope is −2-2. The derivative is 3(5−2x)2⋅(−2)=−6(5−2x)23(5 - 2x)^2 \cdot (-2) = -6(5 - 2x)^2, so the slope is −54-54 at x=1x = 1. The inner function falls, and its negative factor carries through.

A root. Write x2+1\sqrt{x^2 + 1} as (x2+1)1/2(x^2 + 1)^{1/2}. The outer power gives 12(x2+1)−1/2\frac{1}{2}(x^2 + 1)^{-1/2}, and the inside gives 2x2x:

ddxx2+1=2x2x2+1=xx2+1\begin{gathered} \frac{d}{dx}\sqrt{x^2 + 1} = \frac{2x}{2\sqrt{x^2 + 1}} \\[4pt] = \frac{x}{\sqrt{x^2 + 1}} \end{gathered}

At x=1x = 1 that is 12≈0.707107\frac{1}{\sqrt{2}} \approx 0.707107. Central differences agree.

gap hh0.10.010.001
central difference at 10.7062220.7070980.707107

A negative power. Write 1x2+1\frac{1}{x^2 + 1} as (x2+1)−1(x^2 + 1)^{-1}. The chain rule gives −(x2+1)−2⋅2x=−2x(x2+1)2-(x^2 + 1)^{-2} \cdot 2x = -\frac{2x}{(x^2 + 1)^2}, the same answer as the quotient rule in lesson 2.3.2.

A flat inside. (x2+1)3(x^2 + 1)^3 has derivative 3(x2+1)2⋅2x=6x(x2+1)23(x^2 + 1)^2 \cdot 2x = 6x(x^2 + 1)^2. At x=0x = 0 the inner slope is 0, so the whole slope is 0. At x=1x = 1 it is 24.

The chain rule also agrees with the power rule. (x2)3=x6(x^2)^3 = x^6, and the chain rule gives 3(x2)2⋅2x=6x53(x^2)^2 \cdot 2x = 6x^5, as the power rule does.

Try it

The loop prints the central differences of x2+1\sqrt{x^2 + 1} at 1. SymPy then differentiates each worked example.

SymPy writes −6(5−2x)2-6(5 - 2x)^2 as -6*(2*x - 5)**2. The two are equal, because squaring hides the sign inside.

⌘/Ctrl + Enter runs the cell

Chains of three

A function can have more than two layers. Take 16+(x2+2)2\sqrt{16 + (x^2 + 2)^2} and peel it from the outside in.

layerfunctionits slopeat x=1x = 1
insidev=x2+2v = x^2 + 22x2xv=3v = 3, slope 2
middleu=16+v2u = 16 + v^22v2vu=25u = 25, slope 6
outsidey=uy = \sqrt{u}12u\frac{1}{2\sqrt{u}}y=5y = 5, slope 0.1

Each layer adds one stretch factor: dydx=dydu⋅dudv⋅dvdx\frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dv} \cdot \frac{dv}{dx}. At x=1x = 1 that is 0.1×6×2=1.20.1 \times 6 \times 2 = 1.2. Central differences give 1.201943, 1.200019 and 1.2 for gaps 0.1, 0.01 and 0.001.

As a formula, the three factors are

1216+(x2+2)2⋅2(x2+2)⋅2x=2x(x2+2)16+(x2+2)2\begin{gathered} \frac{1}{2\sqrt{16 + (x^2 + 2)^2}} \cdot 2(x^2 + 2) \cdot 2x \\[4pt] = \frac{2x(x^2 + 2)}{\sqrt{16 + (x^2 + 2)^2}} \end{gathered}

Work from the outside in, one layer at a time, and write one factor for each. Skipping a layer is the usual slip.

Where the chain rule breaks

The chain rule needs both slopes: the inner one at xx, and the outer one at u=g(x)u = g(x). If either is missing, so is the slope of the composition.

x2\sqrt{x^2} is ∣x∣|x|, the corner of lesson 2.2.2. Its outer function u\sqrt{u} has no slope at u=0u = 0 (lesson 2.3.1), and its inside x2x^2 is 0 at x=0x = 0. The rule gives xx2=x∣x∣\frac{x}{\sqrt{x^2}} = \frac{x}{|x|}: 1 for every x>0x > 0, −1-1 for every x<0x < 0, and nothing at 0.

Compare x2+1\sqrt{x^2 + 1}. Its inside is never below 1, so the outer slope always exists, and the curve has a slope everywhere.

Try it

The first lines check the chain of three at 1. Then SymPy differentiates x2\sqrt{x^2} and evaluates the slope on both sides of 0.

SymPy writes the slope as sqrt(x**2)/x, which is ∣x∣x\frac{|x|}{x}. It is 1 on the right of 0 and −1-1 on the left.

⌘/Ctrl + Enter runs the cell

Which rule?

Evaluate the function at a number in your head, and look at the last step. That step decides the first rule; the pieces it leaves may need others.

functionlast stepfirst rulethen
x(2x+1)3x(2x + 1)^3a multiplicationproductchain, for (2x+1)3(2x + 1)^3
xx2+1\dfrac{x}{\sqrt{x^2 + 1}}a divisionquotientchain, for the bottom
(x+1x−1)2\left(\dfrac{x + 1}{x - 1}\right)^2a squarechainquotient, for the inside

Product, then chain. For x(2x+1)3x(2x + 1)^3, the product rule gives 1⋅(2x+1)3+x⋅ddx(2x+1)31 \cdot (2x + 1)^3 + x \cdot \frac{d}{dx}(2x + 1)^3. The chain rule gives ddx(2x+1)3=3(2x+1)2⋅2\frac{d}{dx}(2x + 1)^3 = 3(2x + 1)^2 \cdot 2. Together:

(2x+1)3+6x(2x+1)2=(2x+1)2(8x+1)\begin{gathered} (2x + 1)^3 + 6x(2x + 1)^2 \\[4pt] = (2x + 1)^2(8x + 1) \end{gathered}

At x=1x = 1 the slope is 32×9=813^2 \times 9 = 81.

Quotient, then chain. For xx2+1\frac{x}{\sqrt{x^2 + 1}}, the slope of the bottom comes from the chain rule: xx2+1\frac{x}{\sqrt{x^2 + 1}}, as worked out above. The quotient rule then gives the first line below. Multiplying its top and bottom by x2+1\sqrt{x^2 + 1} clears the small fraction:

1⋅x2+1−x⋅xx2+1x2+1=(x2+1)−x2(x2+1)3/2=1(x2+1)3/2\begin{gathered} \frac{1 \cdot \sqrt{x^2 + 1} - x \cdot \frac{x}{\sqrt{x^2 + 1}}}{x^2 + 1} \\[4pt] = \frac{(x^2 + 1) - x^2}{(x^2 + 1)^{3/2}} = \frac{1}{(x^2 + 1)^{3/2}} \end{gathered}

At x=0x = 0 the slope is 1.

Chain, then quotient. For (x+1x−1)2\left(\frac{x + 1}{x - 1}\right)^2, the outer square gives 2⋅x+1x−12 \cdot \frac{x + 1}{x - 1}. The inside, by the quotient rule, has slope −2(x−1)2-\frac{2}{(x - 1)^2}. Multiplied, the derivative is −4(x+1)(x−1)3-\frac{4(x + 1)}{(x - 1)^3}, which is −12-12 at x=2x = 2.

Try it

SymPy differentiates each function in the table and evaluates the slopes quoted above.

sp.factor gathers each answer into one fraction or product, which is how the text writes it.

⌘/Ctrl + Enter runs the cell

Lessons 2.3.4 and 2.3.5 use the chain rule with ee, ln⁡\ln, sine and cosine as outer functions. Module 2.4 relies on it to find peaks, and lesson 2.6.1 runs it backwards as integration by substitution. Case study 2.3.7 uses it on every discounted payment of a bond, (1+y)−t(1 + y)^{-t}.

Common mistakes

  1. Forgetting the inner derivative. (3x+1)5(3x + 1)^5 gives 15(3x+1)415(3x + 1)^4, not 5(3x+1)45(3x + 1)^4.
  2. Taking the outer slope at xx. It is taken at u=g(x)u = g(x): for (2x+1)2(2x + 1)^2 at 1, the outer slope is 6, not 2.
  3. Putting the inner derivative inside. (x2+1)3(x^2 + 1)^3 gives 3(x2+1)2⋅2x3(x^2 + 1)^2 \cdot 2x, not 3(2x)23(2x)^2.
  4. Mixing up inner and outer. The outer function is the last step of the evaluation. In x2+1\sqrt{x^2 + 1} it is the root.
  5. Skipping a layer. A chain of three functions gives three factors.
  6. No slope where a link has none. x2=∣x∣\sqrt{x^2} = |x| has no slope at 0, because u\sqrt{u} has none at u=0u = 0.

The chain rule

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • A composition f(g(x))f(g(x)) has an inner function gg and an outer function ff. The outer one is the last step of an evaluation.
  • Chain rule: ddxf(g(x))=f′(g(x)) g′(x)\frac{d}{dx}f(g(x)) = f'(g(x))\,g'(x), or dydx=dydududx\frac{dy}{dx} = \frac{dy}{du}\frac{du}{dx}. The outer slope is taken at u=g(x)u = g(x).
  • Slopes multiply like stretch factors, and units cancel like fractions. A chain of three gives three factors.
  • There is no slope where a link has none, as for x2=∣x∣\sqrt{x^2} = |x| at 0.
  • Which rule? The last step of the evaluation chooses the first rule; its pieces may need others. Check with sp.diff.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.

  • Derivatives

    Paul's Online Notes. Notes.

    The definition of the derivative and every differentiation rule, one page per rule.

  • Essence of Calculus

    3Blue1Brown. Video.

    An animated series that rebuilds calculus from pictures.