SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 2.7.1 Taylor polynomialsConcept · 30 min
      2. 2.7.2 Standard series and the size of the errorConcept · 35 min
      3. 2.7.3 Practice set: Taylor seriesPractice set · 20 min
      4. 2.7.4 Case study: duration–convexity and delta–gammaCase study · 35 min

Level 2, Single-variable calculus 2.7 Taylor series Lesson 1 of 4

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

Taylor polynomials

The tangent is the degree-1 copy

A Taylor polynomial copies a function near one point aa with a polynomial, which needs only adding and multiplying. The simplest copy is the tangent line of lesson 2.2.1.

Rule: Degree-1 copy: the tangent

p1(x)=f(a)+f′(a) (x−a)p_1(x) = f(a) + f'(a)\,(x - a)

It copies two facts about ff at aa: the value, since p1(a)=f(a)p_1(a) = f(a), and the slope, since the slope of p1p_1 is f′(a)f'(a) everywhere.

For f(x)=exf(x) = e^x at a=0a = 0, both the value and the slope are e0=1e^0 = 1 (lesson 2.3.4), so p1(x)=1+xp_1(x) = 1 + x. Lesson 2.4.2 measured the gaps: 0.3679 at x=−1x = -1, 0.1487 at 0.5 and 0.7183 at 1. The curve bends away from the line.

Case studies 2.2.4 and 2.3.7 used this line for a bond price: minus its slope, per unit of price, is the duration.

The quadratic copy, from f′′f''

To follow the bend, add a term c (x−a)2c\,(x - a)^2. At aa it is 0, and so is its slope 2c (x−a)2c\,(x - a). So it leaves the value and the slope of the copy alone.

Its second derivative is 2c2c everywhere. To match the bend of ff at aa, set 2c=f′′(a)2c = f''(a), so c=f′′(a)2c = \frac{f''(a)}{2}. That is where the 12\frac{1}{2} comes from.

Rule: Degree-2 copy

p2(x)=f(a)+f′(a) (x−a)+f′′(a)2 (x−a)2\begin{gathered} p_2(x) = f(a) + f'(a)\,(x - a) \\ + \frac{f''(a)}{2}\,(x - a)^2 \end{gathered}

The best quadratic copy of ff near aa: it matches the value, the slope and the second derivative. Without the 12\frac{1}{2} its second derivative would be 2f′′(a)2f''(a), twice the bend.

This is the copy lesson 2.4.2 promised, built from f′′f''. Case study 2.4.5's duration–convexity estimate has the same 12\frac{1}{2}, because it is this copy of a bond price (case study 2.7.4).

Example 1: exe^x at 0. Every derivative of exe^x is exe^x, so f′′(0)=1f''(0) = 1 and p2(x)=1+x+x22p_2(x) = 1 + x + \frac{x^2}{2}.

xx−1-1−0.5-0.50.51
exe^x0.36790.60651.64872.7183
gap to p1p_10.36790.10650.14870.7183
gap to p2p_2−0.1321-0.1321−0.0185-0.01850.02370.2183

The bend term cuts the gap at 0.5 from 0.1487 to 0.0237. Left of 0 the gap turns negative: there p2p_2 is slightly above the curve. A later section explains the sign.

Example 2: ln⁡x\ln x at 1. Here f(1)=0f(1) = 0, f′(1)=11=1f'(1) = \frac{1}{1} = 1 and f′′(1)=−112=−1f''(1) = -\frac{1}{1^2} = -1 (lessons 2.3.4 and 2.4.2). So

p2(x)=(x−1)−(x−1)22p_2(x) = (x - 1) - \frac{(x - 1)^2}{2}

The bend term is negative, because ln⁡x\ln x is concave down. At 1.1, ln⁡1.1=0.0953\ln 1.1 = 0.0953 against p2=0.095p_2 = 0.095: a gap of 0.0003, where the tangent's gap is −0.0047-0.0047. At 1.5 the gap is 0.0305, against the tangent's −0.0945-0.0945.

Try it

quadratic_copy takes the value, slope and second derivative at aa from SymPy and builds p2p_2. SymPy writes the terms in its own order.

The first loop prints the gaps to p1p_1 and p2p_2 for exe^x, as in the table. The second does the same for ln⁡x\ln x at 1.1 and 1.5.

⌘/Ctrl + Enter runs the cell

Where Newton's constant comes from

Lesson 2.4.3 found that near a root rr, each Newton error is about f′′(r)2f′(r)\frac{f''(r)}{2f'(r)} times the square of the last. Newton's step follows the tangent at the guess xnx_n. The quadratic copy about xnx_n shows what that leaves out.

At the root, f(r)=0f(r) = 0. Write the quadratic copy about xnx_n and evaluate it at rr:

0≈f(xn)+f′(xn) (r−xn)+f′′(xn)2 (r−xn)2\begin{gathered} 0 \approx f(x_n) + f'(x_n)\,(r - x_n) \\ + \frac{f''(x_n)}{2}\,(r - x_n)^2 \end{gathered}

Divide by f′(xn)f'(x_n). Newton's step is xn+1=xn−f(xn)f′(xn)x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}, so the first two terms become r−xn+1r - x_{n+1}. With errors en=xn−re_n = x_n - r, this leaves

en+1≈f′′(xn)2f′(xn) en2e_{n+1} \approx \frac{f''(x_n)}{2f'(x_n)}\, e_n^2

As xnx_n closes in on rr, the constant becomes f′′(r)2f′(r)\frac{f''(r)}{2f'(r)}: 0.819 for xex−1xe^x - 1 and 0.354 for x2−2x^2 - 2, as in lesson 2.4.3. For x2−2x^2 - 2 the quadratic copy is exact, since ff is a quadratic. So en+1=en22xne_{n+1} = \frac{e_n^2}{2x_n} holds exactly: from x0=1x_0 = 1, e0=−0.4142e_0 = -0.4142 and e1=0.0858e_1 = 0.0858.

Try it

errors runs 12 Newton steps, takes the last guess as the root, and returns the first five errors. Each line prints en+1en2\frac{e_{n+1}}{e_n^2} for four steps, then f′′(r)2f′(r)\frac{f''(r)}{2f'(r)}.

For xex−1xe^x - 1, f′′=ex(2+x)f'' = e^x(2 + x) and f′=ex(1+x)f' = e^x(1 + x) (lesson 2.4.3), so ere^r cancels. Both lines settle on the constant: 0.819 and 0.354.

⌘/Ctrl + Enter runs the cell

Degree nn about any point

Keep going: add a cube term c3(x−a)3c_3(x - a)^3, then a fourth power, and so on. Each new term is chosen to match one more derivative of ff at aa. Write f(k)f^{(k)} for the kk-th derivative, so f(1)=f′f^{(1)} = f', f(2)=f′′f^{(2)} = f'', and f(0)=ff^{(0)} = f itself.

Why each term has a k!k!

Differentiate (x−a)3(x - a)^3 three times with the power rule (lesson 2.3.1):

(x−a)3  →  3(x−a)2  →  6(x−a)  →  6(x - a)^3 \;\to\; 3(x - a)^2 \;\to\; 6(x - a) \;\to\; 6

The power rule brings down 3, then 2, then 1, so the third derivative is 3×2×1=63 \times 2 \times 1 = 6. At aa, the lower derivatives are 0, because a factor x−ax - a is left. Higher derivatives are 0, because the 6 is a constant.

So the kk-th derivative of ck(x−a)kc_k(x - a)^k at aa is ck×k!c_k \times k!, read "kk factorial": k!=1×2×⋯×kk! = 1 \times 2 \times \cdots \times k. To match f(k)(a)f^{(k)}(a), divide by it: ck=f(k)(a)k!c_k = \frac{f^{(k)}(a)}{k!}.

kk012345
k!k!112624120

By convention 0!=10! = 1, so the same formula gives the constant term f(a)f(a). The factorials grow fast, which is why later terms are often small.

Rule: Taylor polynomial of degree n about a

pn(x)=∑k=0nf(k)(a)k! (x−a)k=f(a)+f′(a)(x−a)+f′′(a)2!(x−a)2+⋯\begin{gathered} p_n(x) = \sum_{k=0}^{n} \frac{f^{(k)}(a)}{k!}\,(x - a)^k \\[4pt] = f(a) + f'(a)(x - a) \\ + \frac{f''(a)}{2!}(x - a)^2 + \cdots \end{gathered}

It matches ff and its first nn derivatives at aa. Each term has the units of ff. With a step h=x−ah = x - a, the same copy reads 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.

The coefficients are numbers: the derivatives are taken at aa, not at xx. The step form is the one case study 2.7.4 and lesson 2.8.1 use.

Example 1: exe^x about 0. Every derivative is exe^x, which is 1 at 0, so

pn(x)=1+x+x22!+x33!+⋯+xnn!p_n(x) = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots + \frac{x^n}{n!}

At x=0.5x = 0.5, p3=1.6458p_3 = 1.6458 against e0.5=1.6487e^{0.5} = 1.6487: a gap of 0.0029. The figure shows each degree hugging the curve over a wider stretch.

exe^x and its Taylor polynomials of degree 1, 2 and 3 about 0. Each extra term matches one more derivative at 0; at x=1x = 1 the gap falls from 0.72 to 0.22 to 0.05.

Example 2: x3x^3 about 2. The derivatives are 3x23x^2, 6x6x and 6, then 0. At 2 they are 12, 12 and 6.

kk01234
f(k)(2)f^{(k)}(2)8121260
k!k!112624
coefficient812610

So p3(x)=8+12(x−2)+6(x−2)2+(x−2)3p_3(x) = 8 + 12(x - 2) + 6(x - 2)^2 + (x - 2)^3. With h=x−2h = x - 2 this is 8+12h+6h2+h38 + 12h + 6h^2 + h^3, the expansion of (2+h)3(2 + h)^3 in lesson 2.2.1. A polynomial of degree 3 is its own Taylor polynomial of degree 3, with no error at all.

Try it below. Raise the degree nn from 1 to 5 and watch the copy of exe^x hug the curve further from the centre. Then move the centre aa to 1 and slide xx towards it: the error shrinks fast as xx nears aa.

Explore

Raise the degree and watch the polynomial hug the curve further from its centre. Move the centre, then the point, and compare the error with the first term left out.

−2020510

y = eˣits Taylor polynomial p1The large dot is the centre; the dashed line at x is the error.

Polynomial
p1(x)=1+xp_{1}(x) = 1 + x
At x = 1
exe^x = 2.71828, p1(x)p_{1}(x) = 2
Error
0.71828
First term left out
0.5
Reach
The series reaches every x: with enough terms the error shrinks to zero.

Try it

taylor builds pnp_n about aa from SymPy's derivatives and sp.factorial. The first line prints 0!0! to 5!5!.

SymPy multiplies out 8+12(x−2)8 + 12(x - 2) as 12x−1612x - 16. For x3x^3 about 2, sp.expand of p3−x3p_3 - x^3 prints 0: the copy is exact.

⌘/Ctrl + Enter runs the cell

How fast the error falls

Near aa, the error f(x)−pn(x)f(x) - p_n(x) behaves like a constant times (x−a)n+1(x - a)^{n+1}: the first power the copy leaves out. Halve the distance to aa and the error shrinks about 2n+12^{n+1} times.

xx0.40.20.1shrinks by
error of p1p_10.09180.02140.00517about 4
error of p2p_20.01180.00140.000171about 8
error of p3p_30.001160.00006940.00000425about 16

These are the errors for exe^x about 0. The tangent's error falls like x2x^2, as in lesson 2.2.1; one more term makes it fall like x3x^3, then x4x^4.

Rule: Error near a

f(x)−pn(x)≈C (x−a)n+1f(x) - p_n(x) \approx C\,(x - a)^{n+1}

For xx near aa, with a constant CC that depends on ff and aa. Lesson 2.7.2 shows how to find CC, and how big the error can be further out.

On log scales a power of xx is a straight line whose slope is the power. So each error is a line of slope n+1n + 1.

The error of p1p_1, p2p_2 and p3p_3 for exe^x against the distance from 0, on log scales. The slopes are 2, 3 and 4: one more term adds one to the power.

Both sides of aa

The sign of the error follows (x−a)n+1(x - a)^{n+1}. An even power is positive on both sides of aa; an odd power changes sign at aa.

For exe^x the tangent's error, like x2x^2, is positive both ways: exe^x lies above its tangent (lesson 2.4.2). The quadratic copy's error, like x3x^3, is negative left of 0.

That is why p2p_2 overshoots at −0.5-0.5: 0.625 against 0.6065. Its size still shrinks about 8 times per halving: 0.00968, 0.00127 and 0.000163 at −0.4-0.4, −0.2-0.2 and −0.1-0.1.

When a term is missing

If a coefficient is 0, the copy gains nothing from that degree. At an inflection point f′′(a)=0f''(a) = 0 (lesson 2.4.2), so the quadratic copy is just the tangent.

Take x3−3xx^3 - 3x at 0. Its derivatives there are 0, −3-3, 0 and 6, so p1=p2=−3xp_1 = p_2 = -3x. The error is x3x^3 exactly: it falls like the next power that is actually present.

Far from aa, and where there is no copy

Far from aa the rule says nothing. At x=3x = 3, p3p_3 for exe^x gives 1+3+4.5+4.5=131 + 3 + 4.5 + 4.5 = 13, against e3=20.0855e^3 = 20.0855. Lesson 2.7.2 asks how many terms fix that, and finds functions where no number of terms will.

A Taylor polynomial needs the derivatives at aa. A corner, such as ∣x∣|x| at 0, has no slope (lesson 2.2.2), and neither has a vertical tangent, such as x3\sqrt[3]{x} at 0. At such a point there is no copy of degree 1 or more.

Try it

The first loop prints the errors of p1p_1, p2p_2 and p3p_3 at 0.4, 0.2 and 0.1, then each error divided by the next. 6.94e-05 is Python's way of writing 0.0000694.

The last lines check the negative side for p2p_2, and p3p_3 at 3.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. Keep the k!k!. The cube term of exe^x about 0 is x36\frac{x^3}{6}, not x3x^3; the quadratic term is x22\frac{x^2}{2}, not x2x^2.
  2. Take the derivatives at aa. The coefficients are numbers such as f′′(a)f''(a), not functions of xx.
  3. Use powers of x−ax - a. About a=1a = 1, the copy of ln⁡x\ln x is (x−1)−(x−1)22(x - 1) - \frac{(x - 1)^2}{2}, not x−x22x - \frac{x^2}{2}.
  4. The error rule holds near aa. Far away, more degree may still leave a large error.
  5. Mind the sign on the left. An odd power (x−a)n+1(x - a)^{n+1} makes the error change sign at aa.
  6. A zero coefficient adds nothing. At an inflection point p2=p1p_2 = p_1.

Taylor polynomials

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

  • The tangent p1(x)=f(a)+f′(a)(x−a)p_1(x) = f(a) + f'(a)(x - a) is the degree-1 copy of ff near aa: same value and slope.
  • The quadratic copy adds f′′(a)2(x−a)2\frac{f''(a)}{2}(x - a)^2; the 12\frac{1}{2} makes its second derivative equal f′′(a)f''(a).
  • Degree nn: pn(x)=∑k=0nf(k)(a)k!(x−a)kp_n(x) = \sum_{k=0}^{n} \frac{f^{(k)}(a)}{k!}(x - a)^k. The k!k! undoes the factors the power rule brings down; 0!=10! = 1.
  • Near aa the error behaves like C(x−a)n+1C(x - a)^{n+1}: halve the distance and it shrinks 2n+12^{n+1} times. Odd powers change sign at aa; a zero coefficient skips a power.
  • The quadratic copy about a Newton guess gives en+1≈f′′(r)2f′(r)en2e_{n+1} \approx \frac{f''(r)}{2f'(r)}e_n^2.

Free further reading

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

  • Taylor series

    3Blue1Brown. Video.

    Approximating a function with polynomials, seen step by step.

  • Applications of Series

    Paul's Online Notes. Notes.

    Using Taylor polynomials to approximate functions.