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

Practice setLesson 2.7.3 in Level 2, Single-variable calculus. About 20 minutes.

Practice set: Taylor series

Taylor polynomials

Taylor polynomials (lesson 2.7.1)

0 of 7 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
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.

Standard series and the error

Standard series and the size of the error (lesson 2.7.2)

0 of 8 correct

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

Coding exercise

Exercise

Taylor polynomials and the series for ln(1 + x)

Write two functions.

  1. taylor_poly(derivs, a, x) returns the Taylor polynomial about a at x (lesson 2.7.1). The list derivs holds f(a),f′(a),f′′(a),…f(a), f'(a), f''(a), \ldots, so its length minus one is the degree. math.factorial(k) gives k!k!.
  2. ln1p_series(x, n) returns the sum of the first n terms of x−x22+x33−⋯x - \frac{x^2}{2} + \frac{x^3}{3} - \cdots (lesson 2.7.2). If xx is not in −1<x≤1-1 < x \le 1, raise ValueError: the series does not converge there.

The tests use exe^x about 0, x3x^3 about 2 and x\sqrt{x} about 4, on both sides of the centre. They also check ln⁡1.05\ln 1.05, the slow series at x=1x = 1, and the two values of xx where the series fails.

See the tests
import math


def test_exp_about_0():
    """eˣ about 0 to degree 3, at 0.5, gives 1.645833…"""
    expect_close(taylor_poly([1, 1, 1, 1], 0, 0.5), 1 + 0.5 + 0.5**2 / 2 + 0.5**3 / 6, what="taylor_poly([1, 1, 1, 1], 0, 0.5)")


def test_factorials():
    """The k! is there: [1, 1, 1, 1] about 0 at x = 1 gives 8/3, not 4"""
    expect_close(taylor_poly([1, 1, 1, 1], 0, 1), 8 / 3, what="taylor_poly([1, 1, 1, 1], 0, 1)")


def test_cubic_about_2():
    """x³ about 2, with derivatives [8, 12, 12, 6], is exact: 15.625 at 2.5"""
    expect_close(taylor_poly([8, 12, 12, 6], 2, 2.5), 2.5**3, what="taylor_poly([8, 12, 12, 6], 2, 2.5)")


def test_left_of_centre():
    """√x about 4 to degree 2, at 3.8 (left of the centre), gives 1.949375"""
    expect_close(taylor_poly([2, 1 / 4, -1 / 32], 4, 3.8), 1.949375, what="taylor_poly([2, 1/4, -1/32], 4, 3.8)")


def test_degree_0():
    """With one value, the copy is the constant f(a)"""
    expect_close(taylor_poly([5], 3, 10), 5, what="taylor_poly([5], 3, 10)")


def test_ln_two_terms():
    """Two terms at x = 0.05 give 0.04875"""
    expect_close(ln1p_series(0.05, 2), 0.04875, what="ln1p_series(0.05, 2)")


def test_ln_inside():
    """40 terms at x = −0.5 give ln 0.5 to about 13 digits"""
    expect_close(ln1p_series(-0.5, 40), math.log(0.5), what="ln1p_series(-0.5, 40)", rel=1e-12)


def test_ln_slow_end():
    """x = 1 is allowed but slow: 1,000 terms are within 0.001 of ln 2"""
    value = ln1p_series(1, 1000)
    assert value is not None and abs(value - math.log(2)) < 0.001, f"expected within 0.001 of ln 2 = {math.log(2):.6f}, got {value}"


def test_ln_beyond_1():
    """x = 1.5 raises ValueError: the series diverges there"""
    try:
        ln1p_series(1.5, 10)
    except ValueError:
        return
    raise AssertionError("expected ValueError for x = 1.5")


def test_ln_at_minus_1():
    """x = −1 raises ValueError: there the series is minus the harmonic series"""
    try:
        ln1p_series(-1, 10)
    except ValueError:
        return
    raise AssertionError("expected ValueError for x = -1")

Free further reading

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