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 2 of 4

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

Trapezoid and Simpson rules

The trapezoid rule

Lesson 2.5.1 gave each strip a flat top, at the height of one point. The trapezoid rule joins the heights at the two edges of the strip with a straight line instead. Each strip becomes a trapezoid, whose area is the average of its two heights times its width.

As in lesson 2.5.1, cut [a,b][a, b] into nn strips of width Δx=b−an\Delta x = \frac{b - a}{n}, with edges xi=a+i Δxx_i = a + i\,\Delta x. Write LnL_n, RnR_n and MnM_n for the left, right and midpoint sums, and TnT_n for the trapezoid rule.

Rule: Trapezoid rule

Tn=Δx[12f(x0)+f(x1)+⋯+f(xn−1)+12f(xn)]Tn=12 (Ln+Rn)\begin{gathered} T_n = \Delta x \big[\tfrac{1}{2}f(x_0) + f(x_1) + \cdots \\ + f(x_{n-1}) + \tfrac{1}{2}f(x_n)\big] \\[6pt] T_n = \tfrac{1}{2}\,(L_n + R_n) \end{gathered}

Every inner edge belongs to two strips, so its height counts in full; the two outer edges count half. The left sum uses the heights at x0x_0 to xn−1x_{n-1}, and the right sum those at x1x_1 to xnx_n. Their average weights the heights the same way.

Like any area, TnT_n is in height units times width units. Take x2x^2 on [0,1][0, 1] with 4 strips. The heights at the edges are 0,116,14,9160, \frac{1}{16}, \frac{1}{4}, \frac{9}{16} and 1, so

T4=14(02+116+416+916+12)=14⋅2216=0.34375\begin{aligned} T_4 &= \tfrac{1}{4}\left(\tfrac{0}{2} + \tfrac{1}{16} + \tfrac{4}{16} + \tfrac{9}{16} + \tfrac{1}{2}\right) \\ &= \tfrac{1}{4} \cdot \tfrac{22}{16} = 0.34375 \end{aligned}

That is the average of lesson 2.5.1's left sum 0.21875 and right sum 0.46875. The exact area is 13\frac{1}{3}, so T4T_4 is too large by 0.0104. For 1x\frac{1}{x} on [1,2][1, 2], the left and right sums 0.759524 and 0.634524 average to 0.697024, against the exact ln⁡2≈0.693147\ln 2 \approx 0.693147.

The trapezoid rule with 4 strips on y=sin⁡xy = \sin x over [0,π][0, \pi]. The curve bends down, so every straight top runs under it and the total, 1.8961, is short of 2.

Try it

trapezoid_rule is the rule above: half weights at the two ends, full weights inside. riemann is lesson 2.5.1's sum.

The cell prints T4T_4 for x2x^2 and for 1x\frac{1}{x}, each beside the left and right sums and their average. The last line is the error for x2x^2.

⌘/Ctrl + Enter runs the cell

The trapezoid error, and the midpoint's

The trapezoid error shrinks like Δx2\Delta x^2, as the midpoint error does (lesson 2.5.1). With 10 strips, against the exact integrals of lesson 2.5.1:

integralmidpoint errortrapezoid error
x2x^2 on [0,1][0, 1]−8.33×10−4-8.33 \times 10^{-4}1.67×10−31.67 \times 10^{-3}
1x\frac{1}{x} on [1,2][1, 2]−3.12×10−4-3.12 \times 10^{-4}6.24×10−46.24 \times 10^{-4}
sin⁡x\sin x on [0,π][0, \pi]8.25×10−38.25 \times 10^{-3}−1.65×10−2-1.65 \times 10^{-2}
exe^x on [0,1][0, 1]−7.16×10−4-7.16 \times 10^{-4}1.43×10−31.43 \times 10^{-3}

In every row the midpoint error is about half the trapezoid error, with the opposite sign. With 100 strips each error is a hundred times smaller.

The sign comes from the bend (lesson 2.4.2). Where the curve bends up, each straight top lies above it, so the trapezoid rule is too large.

The midpoint rectangle has the same area as the strip under the tangent at the midpoint. That tangent lies below the curve, so the midpoint sum is too small.

Rule: Trapezoid and midpoint errors

Tn−I≈112 Δx2 BMn−I≈−124 Δx2 BB=f′(b)−f′(a)\begin{gathered} T_n - I \approx \tfrac{1}{12}\,\Delta x^2\,B \\[4pt] M_n - I \approx -\tfrac{1}{24}\,\Delta x^2\,B \\[4pt] B = f'(b) - f'(a) \end{gathered}

II is the exact integral. BB is the integral of f′′f'' (lesson 2.5.3): the total bend over [a,b][a, b]. Bending up makes it positive, so the trapezoid rule is too large and the midpoint sum too small, by half as much.

For x2x^2 on [0,1][0, 1], B=f′(1)−f′(0)=2B = f'(1) - f'(0) = 2, so the rule gives 212Δx2=196≈0.0104\frac{2}{12}\Delta x^2 = \frac{1}{96} \approx 0.0104 for 4 strips: exactly the error found above. For sin⁡x\sin x on [0,π][0, \pi] the total bend is cos⁡π−cos⁡0=−2\cos\pi - \cos 0 = -2. So the trapezoid rule is too small there, as the figure above shows, and the midpoint sum too large.

Whether ff rises or falls does not matter, unlike for the left and right sums: only the bend does. A straight line has no bend, so both rules are exact for it.

Try it

bend is B=f′(b)−f′(a)B = f'(b) - f'(a) for each example. Each line prints the midpoint error, the trapezoid error and the rule's prediction for the trapezoid, with 10 strips and then 100.

⌘/Ctrl + Enter runs the cell

Simpson's rule

Straight tops cannot follow a bend. Simpson's rule replaces them by curves: over each pair of strips, the parabola through the three heights.

Over one pair, from x0x_0 to x2x_2, that parabola has area Δx3(f(x0)+4f(x1)+f(x2))\frac{\Delta x}{3}\big(f(x_0) + 4f(x_1) + f(x_2)\big), where Δx\Delta x is the width of one strip. Adding the pairs gives the weights. The middle of each pair counts 4, an edge shared by two pairs counts 1+1=21 + 1 = 2, and the two ends count 1.

Simpson's rule with one pair of strips on y=sin⁡xy = \sin x: π/23(0+4+0)=2.0944\frac{\pi/2}{3}(0 + 4 + 0) = 2.0944. Three points already give the area to within 5%.

Rule: Simpson's rule

Sn=Δx3[f(x0)+4f(x1)+2f(x2)+4f(x3)+⋯+4f(xn−1)+f(xn)]\begin{gathered} S_n = \frac{\Delta x}{3}\big[f(x_0) + 4f(x_1) \\ + 2f(x_2) + 4f(x_3) + \cdots \\ + 4f(x_{n-1}) + f(x_n)\big] \end{gathered}

Weights 1, 4, 2, 4, …, 2, 4, 1, times Δx3\frac{\Delta x}{3}. The number of strips nn must be even, so that the strips pair up.

For 1x\frac{1}{x} on [1,2][1, 2] with 4 strips, Δx=0.25\Delta x = 0.25 and the heights at 1, 1.25, 1.5, 1.75 and 2 are 1, 0.8, 0.666667, 0.571429 and 0.5. So

S4=0.253(1+3.2+1.333333+2.285714+0.5)=0.693254\begin{aligned} S_4 &= \tfrac{0.25}{3}\big(1 + 3.2 + 1.333333 \\ &\quad + 2.285714 + 0.5\big) \\ &= 0.693254 \end{aligned}

It is off by 1.07×10−41.07 \times 10^{-4}. Lesson 2.5.1's midpoint sum with the same 4 strips, 0.69122, is off by −1.93×10−3-1.93 \times 10^{-3}: eighteen times as much, the other way.

Try the rules side by side below, starting from these four strips of 1x\frac{1}{x}. Switch between midpoints, trapezoids and Simpson's parabolas, then raise nn to 10 and 100 and compare how fast each error shrinks.

Explore

Add strips and change how each strip's top is drawn: flat, a straight line (trapezoids) or a parabola through each pair (Simpson). Compare how fast each error shrinks.

11.5200.51
Width of each
1 ÷ 4 = 0.25
Total of 4 strips
0.69325397
Exact area
ln 2 = 0.693147
Error
0.000107 (an overestimate)

Try it

SymPy fits the parabola through three heights f0f_0, f1f_1, f2f_2 at 0, hh and 2h2h, and integrates it: the area is h3(f0+4f1+f2)\frac{h}{3}(f_0 + 4f_1 + f_2).

simpson_rule builds the weights 1, 4, 2, …, 4, 1 and refuses an odd number of strips. The last line compares S4S_4 for 1x\frac{1}{x} with lesson 2.5.1's M4=0.69122M_4 = 0.69122.

⌘/Ctrl + Enter runs the cell

Simpson's rule from the other two

The midpoint error is about −12-\frac{1}{2} times the trapezoid error. So in 2Mm+Tm2M_m + T_m the two errors cancel, to leading order.

Rule: Simpson from midpoint and trapezoid

S2m=2Mm+Tm3S_{2m} = \frac{2M_m + T_m}{3}

With mm strips, the trapezoid rule uses the edges and the midpoint sum the middles. Together they are the 2m+12m + 1 points of Simpson's rule with 2m2m strips, and the weights work out to 1, 4, 2, …, 4, 1.

For 1x\frac{1}{x} on [1,2][1, 2] with m=4m = 4: 2×0.69122+0.6970243=0.693155\frac{2 \times 0.69122 + 0.697024}{3} = 0.693155, which is S8S_8, off by only 7.4×10−67.4 \times 10^{-6}.

Combining two estimates so that their leading errors cancel is called Richardson extrapolation. Halving the strips cuts the trapezoid error about 4 times, so 4T8−T43\frac{4T_8 - T_4}{3} gives S8S_8 as well.

Its error shrinks like Δx4\Delta x^4

Simpson's rule beats the midpoint sum on lesson 2.5.1's examples, and by more as the strips get thinner:

integralM4M_4 errorS4S_4 errorM100M_{100} errorS100S_{100} error
x2x^2 on [0,1][0, 1]−5.2×10−3-5.2 \times 10^{-3}0−8.3×10−6-8.3 \times 10^{-6}0
1x\frac{1}{x} on [1,2][1, 2]−1.9×10−3-1.9 \times 10^{-3}1.1×10−41.1 \times 10^{-4}−3.1×10−6-3.1 \times 10^{-6}3.1×10−103.1 \times 10^{-10}
sin⁡x\sin x on [0,π][0, \pi]5.2×10−25.2 \times 10^{-2}4.6×10−34.6 \times 10^{-3}8.2×10−58.2 \times 10^{-5}1.1×10−81.1 \times 10^{-8}
exe^x on [0,1][0, 1]−4.5×10−3-4.5 \times 10^{-3}3.7×10−53.7 \times 10^{-5}−7.2×10−6-7.2 \times 10^{-6}9.5×10−119.5 \times 10^{-11}

From 10 to 100 strips, Simpson's error for 1x\frac{1}{x} falls from 3.05×10−63.05 \times 10^{-6} to 3.12×10−103.12 \times 10^{-10}: ten times as many strips, ten thousand times smaller. On a log-log chart that is a slope of −4-4, against −2-2 for the other two rules.

Rule: Simpson's error

Sn−I≈1180 Δx4×(f′′′(b)−f′′′(a))\begin{gathered} S_n - I \approx \tfrac{1}{180}\,\Delta x^4 \\ \times \big(f'''(b) - f'''(a)\big) \end{gathered}

The error shrinks like Δx4\Delta x^4. For a cubic, f′′′f''' is a constant, so f′′′(b)−f′′′(a)=0f'''(b) - f'''(a) = 0 and Simpson's rule is exact.

A parabola cannot follow a cubic, yet the errors on the two halves of each pair cancel. For ∫02x3 dx=4\int_0^2 x^3\,dx = 4, two strips give 13(0+4×1+8)=4\frac{1}{3}(0 + 4 \times 1 + 8) = 4 exactly. For x4x^4 they give 6.666667 against the exact 6.4, off by 1180×48=0.2667\frac{1}{180} \times 48 = 0.2667, just as the rule says.

The size of each rule's error for ∫121x dx\int_1^2 \frac{1}{x}\,dx. The trapezoid and midpoint lines are parallel, a factor of 2 apart; Simpson's line is twice as steep, until rounding stops it near 10−1610^{-16}.

Try it

The first loop checks 2M+T3\frac{2M + T}{3} and 4T8−T43\frac{4T_8 - T_4}{3} against S8S_8 for 1x\frac{1}{x}. The second prints the table: midpoint and Simpson errors with 4 and 100 strips.

With 100 strips Simpson's error for x2x^2 prints as 5.6×10−175.6 \times 10^{-17}: the rule is exact, and only rounding is left.

⌘/Ctrl + Enter runs the cell

Where the rules break

The error rules assume that ff is smooth on the whole of [a,b][a, b], and Simpson's rule needs pairs of strips. Three things break them.

A corner

e∣x−0.37∣e^{|x - 0.37|} has a corner at 0.37, and its exact integral over [0,1][0, 1] is (e0.37−1)+(e0.63−1)≈1.325345(e^{0.37} - 1) + (e^{0.63} - 1) \approx 1.325345. A parabola cannot bend sharply, so the pair of strips holding the corner gets an error of order Δx2\Delta x^2:

stripsmidpoint errortrapezoid errorSimpson error
10−1.5×10−3-1.5 \times 10^{-3}3.2×10−33.2 \times 10^{-3}1.1×10−31.1 \times 10^{-3}
20−5.4×10−4-5.4 \times 10^{-4}8.8×10−48.8 \times 10^{-4}1.0×10−41.0 \times 10^{-4}
100−5.5×10−6-5.5 \times 10^{-6}1.1×10−51.1 \times 10^{-5}−3.3×10−5-3.3 \times 10^{-5}

With 100 strips Simpson's rule is worse than both of the others. Its error jumps about with the position of the corner inside its pair.

The fix is to split the integral at the corner and apply the rule to each smooth piece. With 100 strips on each piece, the error is 8.1×10−128.1 \times 10^{-12}.

Case study 2.6.5 split an option-pricing integral at the corner of its payoff for this reason.

A curve that blows up at an end

∫011x dx=2\int_0^1 \frac{1}{\sqrt{x}}\,dx = 2 is an improper integral (lesson 2.6.3). The trapezoid and Simpson rules both need the height at x=0x = 0, and 10\frac{1}{\sqrt{0}} does not exist: Python raises ZeroDivisionError.

The midpoint sum never touches the ends, so it runs, but slowly. Its error is −6.05×10−2-6.05 \times 10^{-2} with 100 strips and −6.05×10−3-6.05 \times 10^{-3} with 10,000: a hundred times the strips for ten times the accuracy.

Remove the blow-up first, with a substitution such as x=u2x = u^2 (lesson 2.6.1), which turns this integral into ∫012 du\int_0^1 2\,du. Or use quad, which handles such ends.

An odd number of strips

Simpson's weights need pairs. Applied blindly to 3 strips of x2x^2 on [0,1][0, 1], the pattern gives weights 1, 4, 2, 1 and the answer 727≈0.259259\frac{7}{27} \approx 0.259259 against 13\frac{1}{3}. No error message warns you.

With data at an even number of points, which makes an odd number of strips, use one of the fixes in the next section.

Try it

The first loop prints the three errors for the corner at 0.37, then Simpson's error after splitting there.

Next the blow-up at 0: the first height the trapezoid rule needs, then the midpoint sum. Last, the 1, 4, 2, 1 weights on three strips of x2x^2.

⌘/Ctrl + Enter runs the cell

SciPy's trapezoid and simpson

scipy.integrate has both rules. They take the heights, not the function: trapezoid(y, x=x) and simpson(y, x=x), where x holds the points and y the values there. That suits data you already have, such as a table measured at fixed times.

The points need not be evenly spaced. simpson also accepts an odd number of strips: it treats the last strip with a special formula, so its answer stays accurate.

For a function you can evaluate anywhere, quad (lesson 2.5.3) is usually the better tool: it picks its own points and reports an error estimate.

Try it

For 1x\frac{1}{x} on 11 points (10 strips), SciPy gives T10=0.693771T_{10} = 0.693771 and S10=0.6931502307S_{10} = 0.6931502307. They are ln⁡2\ln 2 plus the errors found above, 6.24×10−46.24 \times 10^{-4} and 3.05×10−63.05 \times 10^{-6}.

On 10 points, 9 strips, simpson still gives ln⁡2\ln 2 to within 1.6×10−61.6 \times 10^{-6}.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. The trapezoid rule halves the two end heights. Counting them in full adds 12Δx (f(a)+f(b))\frac{1}{2}\Delta x\,\big(f(a) + f(b)\big) too much.
  2. Simpson's weights are 1, 4, 2, …, 4, 1, times Δx3\frac{\Delta x}{3}, where Δx\Delta x is the width of one strip, not of a pair.
  3. Simpson's rule needs an even number of strips. The pattern applied to an odd number gives a wrong answer with no warning.
  4. M+T2\frac{M + T}{2} is not Simpson's rule. The midpoint error is half the trapezoid's, so the weights are 2 and 1.
  5. A corner or a blow-up spoils the error rules. Split at a corner; remove a blow-up at an end, or use quad.
  6. trapezoid and simpson take values, not a function. Evaluate ff at the points first.

Trapezoid and Simpson rules

0 of 11 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
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

  • Trapezoid rule: Tn=Δx[12f(x0)+f(x1)+⋯+f(xn−1)+12f(xn)]=12(Ln+Rn)T_n = \Delta x\big[\frac{1}{2}f(x_0) + f(x_1) + \cdots + f(x_{n-1}) + \frac{1}{2}f(x_n)\big] = \frac{1}{2}(L_n + R_n).
  • Tn−I≈112Δx2(f′(b)−f′(a))T_n - I \approx \frac{1}{12}\Delta x^2\big(f'(b) - f'(a)\big) and Mn−I≈−124Δx2(f′(b)−f′(a))M_n - I \approx -\frac{1}{24}\Delta x^2\big(f'(b) - f'(a)\big): the midpoint error is about half the trapezoid's, with the opposite sign.
  • Simpson's rule: weights 1, 4, 2, …, 4, 1 times Δx3\frac{\Delta x}{3}, with nn even. S2m=2Mm+Tm3S_{2m} = \frac{2M_m + T_m}{3}.
  • Simpson's error shrinks like Δx4\Delta x^4, and is zero for cubics.
  • Split at a corner, remove a blow-up at an end, and never apply Simpson's weights to an odd number of strips.
  • scipy.integrate.trapezoid(y, x=x) and simpson(y, x=x) take values; quad takes a function.

Free further reading

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