SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 2.8.1 Finite differences and their errorsConcept · 35 min
      2. 2.8.2 Trapezoid and Simpson rulesConcept · 35 min
      3. 2.8.3 Practice set: numerical calculusPractice set · 20 min
      4. 2.8.4 Case study: bump-and-reprice GreeksCase study · 35 min

Level 2, Single-variable calculus 2.8 Numerical calculus Lesson 1 of 4

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

Finite differences and their errors

Two kinds of error

A finite difference with step hh goes wrong in two separate ways. The truncation error comes from stopping at a finite hh instead of taking the limit; it shrinks as hh shrinks. The rounding error comes from the computer keeping only about 16 digits; it grows as hh shrinks.

Both are in the units of the derivative: output units per input unit. This lesson measures each one, then picks the hh that keeps their sum smallest.

Truncation error, from Taylor polynomials

Lesson 2.7.1 wrote the Taylor polynomial about a point aa in step form: f(a+h)≈f(a)+f′(a)h+f′′(a)2h2+⋯f(a + h) \approx f(a) + f'(a)h + \frac{f''(a)}{2}h^2 + \cdots. Take the point aa to be xx itself, and go one term further:

f(x+h)=f(x)+f′(x) h+12f′′(x) h2+16f′′′(x) h3+⋯\begin{aligned} f(x + h) = f(x) &+ f'(x)\,h \\ &+ \tfrac{1}{2}f''(x)\,h^2 \\ &+ \tfrac{1}{6}f'''(x)\,h^3 + \cdots \end{aligned}

A step to the left replaces hh by −h-h, which flips the sign of every odd power:

f(x−h)=f(x)−f′(x) h+12f′′(x) h2−16f′′′(x) h3+⋯\begin{aligned} f(x - h) = f(x) &- f'(x)\,h \\ &+ \tfrac{1}{2}f''(x)\,h^2 \\ &- \tfrac{1}{6}f'''(x)\,h^3 + \cdots \end{aligned}

Each finite difference of lesson 2.2.2 combines these two lines.

  • Forward. Subtract f(x)f(x) from the first line and divide by hh. That leaves f′(x)+12f′′(x) h+16f′′′(x) h2+⋯f'(x) + \frac{1}{2}f''(x)\,h + \frac{1}{6}f'''(x)\,h^2 + \cdots.
  • Backward. The same with the second line leaves f′(x)−12f′′(x) h+16f′′′(x) h2−⋯f'(x) - \frac{1}{2}f''(x)\,h + \frac{1}{6}f'''(x)\,h^2 - \cdots.
  • Central. Subtract the second line from the first. The f(x)f(x) and h2h^2 terms cancel, and dividing by 2h2h leaves f′(x)+16f′′′(x) h2+⋯f'(x) + \frac{1}{6}f'''(x)\,h^2 + \cdots.

Rule: Truncation errors

forward: 12f′′(x) hbackward: −12f′′(x) hcentral: 16f′′′(x) h2\begin{gathered} \text{forward: } \tfrac{1}{2}f''(x)\,h \\[4pt] \text{backward: } -\tfrac{1}{2}f''(x)\,h \\[4pt] \text{central: } \tfrac{1}{6}f'''(x)\,h^2 \end{gathered}

The estimate minus the true f′(x)f'(x), to leading order: the first term of the series that the formula leaves over. Forward and backward errors shrink like hh, the central error like h2h^2.

For x3x^3 at 1, f′′(1)=6f''(1) = 6 and f′′′(1)=6f'''(1) = 6. The forward error is 12⋅6h=3h\frac{1}{2} \cdot 6h = 3h and the central error 16⋅6h2=h2\frac{1}{6} \cdot 6h^2 = h^2: lesson 2.2.2's 3h3h and h2h^2. A cubic's series stops at h3h^3, so the forward difference is exactly 3+3h+h23 + 3h + h^2; the extra h2h^2 is the 16f′′′(x) h2\frac{1}{6}f'''(x)\,h^2 term.

A second example: exe^x at 1

Every derivative of exe^x is exe^x, so at 1 they all equal e≈2.718282e \approx 2.718282. The rule predicts a forward error of e2h≈1.359h\frac{e}{2}h \approx 1.359h and a central error of e6h2≈0.453h2\frac{e}{6}h^2 \approx 0.453h^2.

step hh0.10.010.001
forward error0.140560.0136370.00136
predicted, e2h\frac{e}{2}h0.1359140.0135910.001359
central error4.533×10−34.533 \times 10^{-3}4.530×10−54.530 \times 10^{-5}4.530×10−74.530 \times 10^{-7}
predicted, e6h2\frac{e}{6}h^24.530×10−34.530 \times 10^{-3}4.530×10−54.530 \times 10^{-5}4.530×10−74.530 \times 10^{-7}

The predictions get better as hh shrinks, because the terms they leave out shrink faster.

The sign of the error

The error has the sign of the derivative in its term. Where f′′>0f'' > 0, the curve bends up (lesson 2.4.2): the forward difference is too large and the backward one too small.

ln⁡x\ln x bends down, with f′′(2)=−14f''(2) = -\frac{1}{4}. So at 2 its forward differences fall short, by about 18h\frac{1}{8}h: −0.012098-0.012098, −0.001246-0.001246 and −0.000125-0.000125 for hh = 0.1, 0.01 and 0.001. Its third derivative 2x3\frac{2}{x^3} is 14\frac{1}{4} at 2, so the central difference is too large, by 4.17×10−44.17 \times 10^{-4} at h=0.1h = 0.1, as 124h2\frac{1}{24}h^2 predicts.

A zero term makes a difference better than its rule says. At 0, sin⁡x\sin x has f′′=0f'' = 0, so its forward difference sin⁡hh\frac{\sin h}{h} is off by only about −16h2-\frac{1}{6}h^2: 0.998334 at h=0.1h = 0.1, as in lesson 2.3.5. A quadratic has f′′′=0f''' = 0 everywhere, so its central difference is exact.

Where it breaks: a corner inside the step

The series needs ff to be smooth all the way from x−hx - h to x+hx + h. Take ∣x∣|x| at x=0.001x = 0.001, where the slope is 1, with h=0.01h = 0.01. The points −0.009-0.009 and 0.0110.011 straddle the corner at 0, and the central difference gives 0.1.

The rule predicts no error at all, since f′′′=0f''' = 0 on each side of the corner. The estimate has measured the corner, not the slope. With h=0.0001h = 0.0001 both points stay right of the corner, and it gives 1.

Case study 2.8.4 meets the same trap at an option's strike.

Try it

The first loop prints the forward and central errors of exe^x at 1 beside the predictions. The second prints the errors of ln⁡x\ln x at 2, signs included.

The last line straddles the corner of ∣x∣|x|, then keeps to its smooth side.

⌘/Ctrl + Enter runs the cell

Rounding error

A float keeps 53 binary digits, about 16 decimal digits (lesson 2.1.1). The gap between 1 and the next float up is the machine epsilon, ε≈2.2×10−16\varepsilon \approx 2.2 \times 10^{-16}. Storing a number moves it by at most about 12ε\frac{1}{2}\varepsilon of its size.

Even the step is rounded. 1+10−151 + 10^{-15} is stored as 1+5ε1 + 5\varepsilon, which is 1+1.11×10−151 + 1.11 \times 10^{-15}. That is why lesson 2.1.1's table gave 2.22044605 instead of 2 at h=10−15h = 10^{-15}: the step really taken was 11% too long.

Subtracting two nearly equal numbers keeps their rounding errors but cancels their leading digits. That was the trap of lesson 2.1.5: for huge nn, 1+r/n1 + r/n kept only a few digits of r/nr/n, and math.log1p avoided the subtraction. A finite difference cannot avoid it, since its top, f(x+h)−f(x)f(x + h) - f(x), is exactly such a subtraction.

Each value of ff is off by up to about 12ε∣f(x)∣\frac{1}{2}\varepsilon|f(x)|, so the top is off by up to about ε∣f(x)∣\varepsilon|f(x)|. Dividing by hh gives the rule.

Rule: Rounding error

forward: about ε ∣f(x)∣hcentral: about ε ∣f(x)∣2h\begin{gathered} \text{forward: about } \frac{\varepsilon\,|f(x)|}{h} \\[4pt] \text{central: about } \frac{\varepsilon\,|f(x)|}{2h} \end{gathered}

A ceiling, not an exact size: the actual rounding error jumps around below it. It grows as hh shrinks: a step 10 times smaller allows a rounding error 10 times larger. A large ∣f(x)∣|f(x)| raises it too.

For exe^x at 1 the forward ceiling εeh\frac{\varepsilon e}{h} is 6.0×10−66.0 \times 10^{-6} at h=10−10h = 10^{-10} and 6.0×10−46.0 \times 10^{-4} at h=10−12h = 10^{-12}. The measured errors are 1.5×10−61.5 \times 10^{-6} and 4.3×10−44.3 \times 10^{-4}: under the ceiling, and far above the truncation errors 1.4×10−101.4 \times 10^{-10} and 1.4×10−121.4 \times 10^{-12}.

Try it

sys.float_info.epsilon is ε\varepsilon. The second line shows the stored step: (1+10−15)−1(1 + 10^{-15}) - 1 is exactly 5ε5\varepsilon.

The loop prints the forward error of exe^x at 1 for tiny steps, beside the ceiling εeh\frac{\varepsilon e}{h} and the truncation error e2h\frac{e}{2}h.

⌘/Ctrl + Enter runs the cell

The total error and the best step

The total error is at most about the truncation error plus the rounding ceiling. For the forward difference that is 12∣f′′(x)∣ h+ε∣f(x)∣h\frac{1}{2}|f''(x)|\,h + \frac{\varepsilon|f(x)|}{h}. The first part falls as hh shrinks and the second rises, so the total is smallest close to where they are equal.

Rule: The best step

forward: h≈2ε ∣f(x)∣∣f′′(x)∣central: h≈(3ε ∣f(x)∣∣f′′′(x)∣)1/3\begin{gathered} \text{forward: } h \approx \sqrt{\frac{2\varepsilon\,|f(x)|}{|f''(x)|}} \\[4pt] \text{central: } h \approx \left(\frac{3\varepsilon\,|f(x)|}{|f'''(x)|}\right)^{1/3} \end{gathered}

Set the truncation error equal to the rounding ceiling and solve for hh. When ff and its derivatives have about the same size, these are about ε≈1.5×10−8\sqrt{\varepsilon} \approx 1.5 \times 10^{-8} and ε1/3≈6×10−6\varepsilon^{1/3} \approx 6 \times 10^{-6}.

For exe^x at 1 both ratios are 1, so the best steps are 2ε≈2.1×10−8\sqrt{2\varepsilon} \approx 2.1 \times 10^{-8} and (3ε)1/3≈8.7×10−6(3\varepsilon)^{1/3} \approx 8.7 \times 10^{-6}. These match lesson 2.2.2's 10−810^{-8} and 10−510^{-5} for x3x^3 at 1. At those steps the errors are 5.0×10−85.0 \times 10^{-8} (forward) and 3.7×10−113.7 \times 10^{-11} (central).

So the central difference wins twice: its error falls faster, and at its best it is about a thousand times more accurate.

step hhforward errorcentral error
10−210^{-2}1.4×10−21.4 \times 10^{-2}4.5×10−54.5 \times 10^{-5}
10−410^{-4}1.4×10−41.4 \times 10^{-4}4.5×10−94.5 \times 10^{-9}
10−610^{-6}1.4×10−61.4 \times 10^{-6}1.6×10−101.6 \times 10^{-10}
10−810^{-8}6.6×10−96.6 \times 10^{-9}6.6×10−96.6 \times 10^{-9}
10−1010^{-10}1.5×10−61.5 \times 10^{-6}6.7×10−76.7 \times 10^{-7}

Try it

The loop builds the table. The last two lines compute the best steps for exe^x at 1 and the errors there.

⌘/Ctrl + Enter runs the cell

Scale the step to the function

The best-step rule needs f′′f'' or f′′′f''', the very things you are avoiding. What matters is a ratio such as ∣f(x)∣∣f′′′(x)∣\frac{|f(x)|}{|f'''(x)|}, which has the units of the input cubed. Its cube root is ss, the scale of ff: roughly the distance over which ff changes by its own size.

Rule: A default step

forward: h=ε scentral: h=ε1/3 s\begin{gathered} \text{forward: } h = \sqrt{\varepsilon}\,s \\[4pt] \text{central: } h = \varepsilon^{1/3}\,s \end{gathered}

For powers and logarithms of xx, the scale is s=∣x∣s = |x| (use 1 at x=0x = 0). For exe^x and sin⁡x\sin x it is s=1s = 1, wherever xx is.

Two examples show both cases. Below, the relative error is the error as a fraction of the true slope.

  • x\sqrt{x} at 10610^6. The slope is 0.0005 and s=106s = 10^6, so the central step is ε1/3×106≈6.06\varepsilon^{1/3} \times 10^6 \approx 6.06. Its relative error is 8.5×10−148.5 \times 10^{-14}. The unscaled h=10−5h = 10^{-5} gives 1.1×10−51.1 \times 10^{-5}.
  • exe^x at 30. Here ff changes by its own size over a distance of about 1, whatever xx is. The unscaled h=10−5h = 10^{-5} has relative error 1.7×10−111.7 \times 10^{-11}. Scaling by xx gives h=1.8×10−4h = 1.8 \times 10^{-4}, and 5.5×10−95.5 \times 10^{-9}.

The step must also keep x−hx - h where ff is defined. For x\sqrt{x} at 10−610^{-6}, h=10−5h = 10^{-5} puts x−hx - h below 0, and Python raises ValueError. The scaled step, 6×10−126 \times 10^{-12}, gives a relative error of 2.4×10−112.4 \times 10^{-11}.

At the other extreme, a step below ε∣x∣\varepsilon|x| vanishes. 108+10−910^8 + 10^{-9} is stored as 10810^8, so the forward difference of x2x^2 at 10810^8 with h=10−9h = 10^{-9} is 0.

Try it

relative is the relative error. Each line compares the unscaled step 10−510^{-5} with a scaled one, for x\sqrt{x} at 10610^6 and exe^x at 30.

Then x\sqrt{x} near the edge of its domain, and a step too small for x=108x = 10^8.

⌘/Ctrl + Enter runs the cell

The second derivative

Lesson 2.4.2 estimated f′′f'' by the second central difference. Add the two Taylor lines, each taken one term further, to 124f(4)(x) h4\frac{1}{24}f^{(4)}(x)\,h^4, where f(4)f^{(4)} is the fourth derivative. The odd powers cancel:

f(x+h)−2f(x)+f(x−h)=f′′(x) h2+112f(4)(x) h4+⋯\begin{aligned} &f(x + h) - 2f(x) + f(x - h) \\ &= f''(x)\,h^2 + \tfrac{1}{12}f^{(4)}(x)\,h^4 + \cdots \end{aligned}

So after dividing by h2h^2, the truncation error is 112f(4)(x) h2\frac{1}{12}f^{(4)}(x)\,h^2. The top combines three values of ff, one of them doubled, so its rounding can reach about 2ε∣f(x)∣2\varepsilon|f(x)|, and that is divided by h2h^2.

Rule: Second difference: errors and best step

truncation: 112f(4)(x) h2rounding: about 2ε ∣f(x)∣h2best: h≈(24ε ∣f(x)∣∣f(4)(x)∣)1/4\begin{gathered} \text{truncation: } \tfrac{1}{12}f^{(4)}(x)\,h^2 \\[4pt] \text{rounding: about } \frac{2\varepsilon\,|f(x)|}{h^2} \\[4pt] \text{best: } h \approx \left(\frac{24\varepsilon\,|f(x)|}{|f^{(4)}(x)|}\right)^{1/4} \end{gathered}

When ff and f(4)f^{(4)} have about the same size, the best step is about ε1/4≈10−4\varepsilon^{1/4} \approx 10^{-4}, times the scale ss as before.

The best step is larger because the rounding ceiling grows 100 times for each step 10 times smaller, so it overtakes truncation sooner. For exe^x at 1 the best step is (24ε)1/4≈2.7×10−4(24\varepsilon)^{1/4} \approx 2.7 \times 10^{-4}, thirty times the central difference's.

step hherrortruncation e12h2\frac{e}{12}h^2ceiling 2εeh2\frac{2\varepsilon e}{h^2}
10−210^{-2}2.3×10−52.3 \times 10^{-5}2.3×10−52.3 \times 10^{-5}1.2×10−111.2 \times 10^{-11}
10−310^{-3}2.3×10−72.3 \times 10^{-7}2.3×10−72.3 \times 10^{-7}1.2×10−91.2 \times 10^{-9}
10−410^{-4}3.8×10−83.8 \times 10^{-8}2.3×10−92.3 \times 10^{-9}1.2×10−71.2 \times 10^{-7}
10−510^{-5}6.0×10−66.0 \times 10^{-6}2.3×10−112.3 \times 10^{-11}1.2×10−51.2 \times 10^{-5}
10−610^{-6}1.2×10−51.2 \times 10^{-5}2.3×10−132.3 \times 10^{-13}1.2×10−31.2 \times 10^{-3}

At its best step the error is 2.4×10−82.4 \times 10^{-8}, against 3.7×10−113.7 \times 10^{-11} for the first derivative: a second derivative from three values is always rougher. Lesson 2.4.2 saw the right-hand end of this table. Case study 2.4.5 measured a bond's convexity with a bump of 10−410^{-4}, close to the best step.

Case study 2.8.4 uses these steps to measure an option's sensitivities by repricing it.

The measured error of each estimate with its two parts: the truncation error from the Taylor series and the rounding ceiling. The best step sits where they cross. For the second difference the ceiling is steeper, so the crossing moves to a larger step and a larger error.

Try it

second is lesson 2.4.2's estimate. Each line prints the step, the error, the truncation error e12h2\frac{e}{12}h^2 and the rounding ceiling 2εeh2\frac{2\varepsilon e}{h^2}.

The last line uses the best step, (24ε)1/4(24\varepsilon)^{1/4}.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. Smaller is not always better. Below the best step, rounding grows like 1h\frac{1}{h}, or like 1h2\frac{1}{h^2} for the second difference.
  2. One step does not fit every function. Scale it: about 6 for x\sqrt{x} at 10610^6, but about 10−510^{-5} for exe^x at 30.
  3. Use a larger step for f′′f''. About 10−410^{-4} times the scale, not 10−810^{-8}.
  4. Keep all the points on smooth ground, inside the domain. A corner between x−hx - h and x+hx + h ruins the estimate, and x\sqrt{x} has no value below 0.
  5. The error has a sign. A forward difference is too large where f′′>0f'' > 0 and too small where f′′<0f'' < 0; a backward one does the opposite.
  6. A step below ε∣x∣\varepsilon|x| vanishes. Then x+hx + h is stored as xx, and the difference is 0.

Finite differences and their errors

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

  • Truncation errors, from the Taylor series: forward 12f′′(x) h\frac{1}{2}f''(x)\,h, backward −12f′′(x) h-\frac{1}{2}f''(x)\,h, central 16f′′′(x) h2\frac{1}{6}f'''(x)\,h^2. They need ff smooth between x−hx - h and x+hx + h.
  • Rounding error: about ε∣f(x)∣h\frac{\varepsilon|f(x)|}{h} at most, with ε≈2.2×10−16\varepsilon \approx 2.2 \times 10^{-16}. It grows as hh shrinks.
  • The best step makes the two about equal: ε s\sqrt{\varepsilon}\,s forward and ε1/3s\varepsilon^{1/3}s central, where the scale ss is ∣x∣|x| for powers and logs and 1 for exe^x and sin⁡x\sin x.
  • Second difference: truncation 112f(4)(x) h2\frac{1}{12}f^{(4)}(x)\,h^2, rounding about 2ε∣f(x)∣h2\frac{2\varepsilon|f(x)|}{h^2}, best step about ε1/4s≈10−4s\varepsilon^{1/4}s \approx 10^{-4}s.

Free further reading

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