SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 2.2.1 Rate of change and the slope of a tangentConcept · 25 min
      2. 2.2.2 The derivative as a limit, and finite differencesConcept · 35 min
      3. 2.2.3 Practice set: the derivativePractice set · 20 min
      4. 2.2.4 Case study: bond price sensitivity and durationCase study · 30 min

Level 2, Single-variable calculus 2.2 The derivative Lesson 2 of 4

ConceptLesson 2.2.2 in Level 2, Single-variable calculus. About 35 minutes.

The derivative as a limit, and finite differences

A slope at every point

Lesson 2.2.1 found the slope of x2x^2 at 3 by letting chord slopes settle on 6. Nothing about 3 was special. Do the same at other points, with a gap of h=0.001h = 0.001 on each side:

point xxfrom the rightfrom the leftslope
−2-2−3.999-3.999−4.001-4.001−4-4
−1-1−1.999-1.999−2.001-2.001−2-2
00.001−0.001-0.0010
12.0011.9992
24.0013.9994
36.0015.9996

Each slope is twice the point. Algebra shows why. At a general point xx, expand the top and cancel hh:

(x+h)2−x2h=2xh+h2h=2x+h\frac{(x + h)^2 - x^2}{h} = \frac{2xh + h^2}{h} = 2x + h

As h→0h \to 0 this tends to 2x2x. So the slope of x2x^2 at any input xx is 2x2x. That rule is a new function: the derivative.

Rule: Derivative

f′(x)=lim⁡h→0f(x+h)−f(x)hf'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}

The slope of ff at each input xx where this limit exists; there ff is differentiable. Read f′(x)f'(x) as "f prime of x". Finding f′f' is called differentiating.

The derivative takes a point and returns the slope there. For f(x)=x2f(x) = x^2, f′(3)=6f'(3) = 6, as in lesson 2.2.1, and f′(−1)=−2f'(-1) = -2.

Try it

The cell repeats the table: chord slopes from both sides at six points, then 2x2x.

round(…, 6) hides rounding noise in the last digits.

⌘/Ctrl + Enter runs the cell

Reading the sign of the slope

The sign of f′(x)f'(x) says which way the graph is heading at xx.

  • f′(x)>0f'(x) > 0: the graph rises there. x2x^2 rises for every x>0x > 0.
  • f′(x)<0f'(x) < 0: the graph falls there. x2x^2 falls for every x<0x < 0.
  • f′(x)=0f'(x) = 0: the tangent is flat, as at the bottom of a valley. For x2x^2 that happens only at 0.

The sign says nothing about the height. At x=−2x = -2, x2x^2 is 4, above zero, yet its slope is −4-4: it is falling.

Slide the point along x2x^2: the lower graph traces its slopes, the line 2x2x. Then switch to x3−3xx^3 - 3x. Its slope is zero at the hill (x=−1x = -1) and the valley (x=1x = 1), and negative between them. The next section works out that slope, 3x2−33x^2 - 3, from the limit.

Explore

Slide the point along the curve. Its tangent shows the slope there, and the lower graph plots that slope for every x: the derivative.

−20202468

The slope of y = x², which is 2x

−202−505
Point
(−2, 4)
Slope there
−4
The curve is
falling: the slope is negative, so the slope graph is below zero

Notation and units

If y=f(x)y = f(x), the derivative is also written dydx\frac{dy}{dx}, read "dee y by dee x". It recalls the chord slope ΔyΔx\frac{\Delta y}{\Delta x} of lesson 2.2.1, after the limit. It is one symbol, not a fraction of two numbers.

Rule: Two notations

f′(x)=dydxf'(x) = \frac{dy}{dx}

Both mean the derivative of y=f(x)y = f(x). The slope at one point is written f′(3)f'(3), or dydx\frac{dy}{dx} at x=3x = 3. The units are output units per input unit, as for any rate.

A square with side ss cm has area A=s2A = s^2 cm². By the first section, dAds=2s\frac{dA}{ds} = 2s, in cm² per cm. At s=3s = 3 the area grows at 6 cm² per cm of side.

So adding 0.1 cm to the side adds about 6×0.1=0.66 \times 0.1 = 0.6 cm². The true gain is 3.12−9=0.613.1^2 - 9 = 0.61 cm²: the tangent estimate of lesson 2.2.1.

Derivatives from the limit

Every derivative in this lesson comes from the same three steps.

  1. Write the chord slope f(x+h)−f(x)h\frac{f(x + h) - f(x)}{h}.
  2. Simplify the top until hh cancels from the bottom.
  3. Let h→0h \to 0.

x3x^3

Expanding gives (x+h)3−x3=3x2h+3xh2+h3(x + h)^3 - x^3 = 3x^2h + 3xh^2 + h^3. Divide by hh to get 3x2+3xh+h23x^2 + 3xh + h^2, which tends to 3x23x^2. At x=2x = 2 that is 12, the slope lesson 2.2.1 found from chords.

For x3−3xx^3 - 3x, the extra −3x-3x adds −3(x+h)+3x=−3h-3(x + h) + 3x = -3h to the top. The chord slope becomes 3x2+3xh+h2−33x^2 + 3xh + h^2 - 3, so the derivative is 3x2−33x^2 - 3. It is zero at x=−1x = -1 and x=1x = 1, the hill and the valley in the slope tracer.

1x\frac{1}{x}

Put the two fractions over the common bottom x(x+h)x(x + h):

1x+h−1x=x−(x+h)x(x+h)=−hx(x+h)\begin{aligned} \frac{1}{x + h} - \frac{1}{x} &= \frac{x - (x + h)}{x(x + h)} \\ &= \frac{-h}{x(x + h)} \end{aligned}

Dividing by hh leaves −1x(x+h)-\frac{1}{x(x + h)}, which tends to −1x2-\frac{1}{x^2}. This holds for every x≠0x \neq 0, and it is negative on both sides of 0: 1x\frac{1}{x} falls everywhere. At x=2x = 2 the slope is −0.25-0.25.

x\sqrt{x}

Here no hh factors out of the top. Multiply the top and bottom by x+h+x\sqrt{x + h} + \sqrt{x}. The top becomes (x+h)−x=h(x + h) - x = h, by the trick x−9=(x−3)(x+3)x - 9 = (\sqrt{x} - 3)(\sqrt{x} + 3) from lesson 2.1.1.

x+h−xh=(x+h)−xh(x+h+x)=1x+h+x\begin{aligned} \frac{\sqrt{x + h} - \sqrt{x}}{h} &= \frac{(x + h) - x}{h\left(\sqrt{x + h} + \sqrt{x}\right)} \\ &= \frac{1}{\sqrt{x + h} + \sqrt{x}} \end{aligned}

As h→0h \to 0 the bottom tends to 2x2\sqrt{x}, so the derivative is 12x\frac{1}{2\sqrt{x}}, for x>0x > 0. At x=4x = 4 it is 0.25.

A pattern

Written as powers (lesson 1.4.1), 1x=x−1\frac{1}{x} = x^{-1} and x=x1/2\sqrt{x} = x^{1/2}. In all four results the power comes down in front and drops by one: 2x2x, 3x23x^2, −x−2-x^{-2}, 12x−1/2\frac{1}{2}x^{-1/2}. Lesson 2.3.1 turns that pattern into the power rule.

Try it

SymPy follows the three steps. Each line prints the function, its simplified chord slope, and the limit as h→0h \to 0. It cannot simplify the x\sqrt{x} one, but finds its limit.

The last two lines check 1x\frac{1}{x} at 2 and x\sqrt{x} at 4 with h=±0.001h = \pm 0.001.

⌘/Ctrl + Enter runs the cell

Where a derivative does not exist

A derivative at aa needs a value f(a)f(a), and chord slopes that settle on one number from both sides. Three shapes break the second condition.

At a corner the two sides settle on different slopes. At a vertical tangent and at a jump the chord slopes grow without bound. In all three, ff has no derivative at 0.

  • A corner. For ∣x∣|x| at 0, the chord slope is ∣h∣h\frac{|h|}{h}: 1 for every h>0h > 0 and −1-1 for every h<0h < 0. The one-sided limits differ (lesson 2.1.2), so there is no derivative at 0.
  • A vertical tangent. For the cube root x3\sqrt[3]{x} at 0, the chord slope is h3h\frac{\sqrt[3]{h}}{h}. It is 100 at h=0.001h = 0.001 and 10,000 at h=0.000001h = 0.000001, from either side. The tangent is vertical, and a vertical line has no slope (lesson 1.2.1).
  • A jump. Take the step u(x)u(x), which is 0 for x<0x < 0 and 1 for x≥0x \ge 0. From the right every chord slope is 0. From the left it is 0−1h\frac{0 - 1}{h}: 10 at h=−0.1h = -0.1 and 1,000 at h=−0.001h = -0.001.

Away from 0, ∣x∣|x| has slope −1-1 on the left and 1 on the right. That derivative is the function ∣x∣x\frac{|x|}{x} of lesson 2.1.2, with its jump at 0. Switch the slope tracer above to ∣x∣|x| to watch it.

The ramp max⁡(x,0)\max(x, 0) of lesson 1.1.2 has a corner at 0 too: slope 0 on the left, 1 on the right. At 0, x\sqrt{x} has no left side, and from the right its chord slopes blow up like the cube root's.

Differentiable means continuous, not the reverse

Rule: Differentiable implies continuous

differentiable at a⇓continuous at a\begin{gathered} \text{differentiable at } a \\ \Downarrow \\ \text{continuous at } a \end{gathered}

If ff has a derivative at aa, its graph has no break at aa (lesson 2.1.2). The reverse fails: ∣x∣|x| and x3\sqrt[3]{x} are continuous at 0 but have no derivative there. A Proof Gym stage proves the first part.

So a jump, a hole or a blow-up always rules out a derivative. A graph without a break can still fail, at a corner or a vertical tangent.

Try it

The cell computes chord slopes at 0 from the right and from the left, for three gaps. math.cbrt is the cube root.

The corner gives 1 and −1-1 every time. The cube root and the left side of the step grow without bound.

⌘/Ctrl + Enter runs the cell

Finite differences

Sometimes you can compute f(x)f(x) but cannot do the algebra: ff is a long program, or a price from a model. Then stop at a small hh instead of taking the limit. The result is a finite difference, an estimate of f′(x)f'(x).

Rule: Finite differences

forward: f(x+h)−f(x)hbackward: f(x)−f(x−h)hcentral: f(x+h)−f(x−h)2h\begin{gathered} \text{forward: } \frac{f(x + h) - f(x)}{h} \\[4pt] \text{backward: } \frac{f(x) - f(x - h)}{h} \\[4pt] \text{central: } \frac{f(x + h) - f(x - h)}{2h} \end{gathered}

Forward uses a point to the right of xx, backward a point to the left. Central uses one on each side, 2h2h apart; it is the average of the other two.

Take x3x^3 at 1, where the true slope is 3×12=33 \times 1^2 = 3.

gap hh0.10.010.001
forward3.313.03013.003001
backward2.712.97012.997001
central3.013.00013.000001

Algebra explains the pattern. Expanded, the forward difference is exactly 3+3h+h23 + 3h + h^2 and the backward one 3−3h+h23 - 3h + h^2. The central one is 3+h23 + h^2.

So the forward and backward errors are about 3h3h, with opposite signs: a gap 10 times smaller gives an error about 10 times smaller. The central error is h2h^2: 10 times smaller hh, 100 times smaller error. For the same hh, central is far more accurate.

Case study 2.2.4 uses the central difference to measure how a bond price responds to its yield.

Try it

The three estimates for x3x^3 at 1. Compare each line with 3+3h+h23 + 3h + h^2, 3−3h+h23 - 3h + h^2 and 3+h23 + h^2.

⌘/Ctrl + Enter runs the cell

How small should hh be?

Smaller is better only down to a point. The computer stores about 16 significant digits (lesson 2.1.1). With a tiny hh, f(x+h)f(x + h) and f(x)f(x) agree in nearly all of them, and subtracting leaves mostly rounding error.

Dividing by hh then magnifies that error. A rounding error of about 10−1610^{-16} divided by h=10−12h = 10^{-12} is about 10−410^{-4}. Here are the errors for x3x^3 at 1 as hh shrinks:

gap hhforward errorcentral error
10−210^{-2}3.0×10−23.0 \times 10^{-2}1.0×10−41.0 \times 10^{-4}
10−510^{-5}3.0×10−53.0 \times 10^{-5}9.7×10−119.7 \times 10^{-11}
10−810^{-8}4.0×10−94.0 \times 10^{-9}7.1×10−97.1 \times 10^{-9}
10−1110^{-11}2.5×10−72.5 \times 10^{-7}2.5×10−72.5 \times 10^{-7}
10−1410^{-14}2.4×10−32.4 \times 10^{-3}2.4×10−32.4 \times 10^{-3}

The errors of the forward and central differences of x3x^3 at 1, on log scales. Each falls as hh shrinks until rounding takes over; the central error falls faster and bottoms out at a larger hh.

For inputs of about size 1, the forward difference is best near h=10−8h = 10^{-8} and the central one near h=10−5h = 10^{-5}. Below that, every step smaller makes the answer worse. At h=10−20h = 10^{-20}, 1+h1 + h is stored as exactly 1, and the forward difference of x2x^2 at 1 comes out as 0.

Lesson 2.8.1 explains the 3h3h and h2h^2 with Taylor series (lesson 2.7.1), and picks hh for any function.

Try it

The errors for five gaps. f"{e:.1e}" prints a number in Python's scientific notation: 3.0e-02 means 3.0×10−23.0 \times 10^{-2}.

The last line pushes hh to 10−2010^{-20}.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. f′(x)f'(x) is a function; f′(3)f'(3) is a number. The derivative of x2x^2 is 2x2x, and its slope at 3 is 6.
  2. The sign of f′f' is about direction, not height. x2x^2 is positive at −2-2 but falling there.
  3. Units are per input unit. dAds\frac{dA}{ds} for an area is in cm² per cm, not cm².
  4. Mind the domain. −1x2-\frac{1}{x^2} holds only for x≠0x \neq 0, and 12x\frac{1}{2\sqrt{x}} only for x>0x > 0.
  5. Check both sides. At a corner the slopes from the two sides differ. The slope of ∣x∣|x| at 0 is not 0, nor the average of −1-1 and 1: it does not exist.
  6. Continuous is not enough. ∣x∣|x| and x3\sqrt[3]{x} have no break at 0 and still have no derivative there.
  7. A finite difference is an estimate. The forward one is off by about 3h3h for x3x^3 at 1; prefer the central one.
  8. A smaller hh is not always better. Below about 10−810^{-8}, rounding swamps the answer.

The derivative

0 of 13 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.
  13. 13.
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

  • The derivative f′(x)=lim⁡h→0f(x+h)−f(x)hf'(x) = \lim_{h \to 0}\frac{f(x + h) - f(x)}{h} is the slope at every xx. Also written dydx\frac{dy}{dx}; units are output per input.
  • f′>0f' > 0: rising. f′<0f' < 0: falling. f′=0f' = 0: a flat tangent.
  • From the limit: (x2)′=2x(x^2)' = 2x, (x3)′=3x2(x^3)' = 3x^2, (1x)′=−1x2(\frac{1}{x})' = -\frac{1}{x^2} for x≠0x \neq 0, (x)′=12x(\sqrt{x})' = \frac{1}{2\sqrt{x}} for x>0x > 0.
  • No derivative at a corner (∣x∣|x| at 0), a vertical tangent (x3\sqrt[3]{x} at 0) or a jump. Differentiable implies continuous, not the reverse.
  • Finite differences: forward and backward errors shrink like hh, central like h2h^2. Below about h=10−8h = 10^{-8} rounding makes them worse.

Free further reading

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