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

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

Practice set: the derivative

Rates of change and tangents

Rates of change and tangents (lesson 2.2.1)

0 of 6 correct

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

The derivative and finite differences

The derivative and finite differences (lesson 2.2.2)

0 of 9 correct

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

Forward, backward and central differences

Write forward_diff(f, x, h), backward_diff(f, x, h) and central_diff(f, x, h), the three estimates from lesson 2.2.2. Each should also work when x is a NumPy array.

The tests compare them with known derivatives. They also check how fast each error shrinks: halving hh should about halve the forward error and quarter the central one.

See the tests
import math

import numpy as np


def _cube(x):
    return x**3


def test_forward():
    """Forward difference of x³ at 1 with h = 0.1 is 3.31"""
    expect_close(forward_diff(_cube, 1.0, 0.1), 3.31, what="forward_diff(x³, 1, 0.1)")


def test_backward():
    """Backward difference of x³ at 1 with h = 0.1 is 2.71"""
    expect_close(backward_diff(_cube, 1.0, 0.1), 2.71, what="backward_diff(x³, 1, 0.1)")


def test_central():
    """Central difference of x³ at 1 with h = 0.1 is 3.01"""
    expect_close(central_diff(_cube, 1.0, 0.1), 3.01, what="central_diff(x³, 1, 0.1)")


def test_central_more_accurate():
    """For e^x at 0 (true slope 1), the central difference is closer than the forward and backward ones"""
    fwd = abs(forward_diff(math.exp, 0.0, 0.01) - 1)
    bwd = abs(backward_diff(math.exp, 0.0, 0.01) - 1)
    cen = abs(central_diff(math.exp, 0.0, 0.01) - 1)
    assert cen < fwd and cen < bwd, f"central error {cen:.2e} should be below forward {fwd:.2e} and backward {bwd:.2e}"


def test_error_orders():
    """For x³ at 1, halving h from 0.01 about halves the forward error and quarters the central one"""
    fwd = abs(forward_diff(_cube, 1.0, 0.01) - 3) / abs(forward_diff(_cube, 1.0, 0.005) - 3)
    cen = abs(central_diff(_cube, 1.0, 0.01) - 3) / abs(central_diff(_cube, 1.0, 0.005) - 3)
    assert 1.9 < fwd < 2.1, f"the forward error should shrink about 2 times, it shrank {fwd:.3f} times"
    assert 3.9 < cen < 4.1, f"the central error should shrink about 4 times, it shrank {cen:.3f} times"


def test_arrays():
    """All three work on an array of points: x² has slope 2x"""
    x = np.array([0.0, 1.0, 2.5])
    expect_close(central_diff(lambda t: t**2, x, 1e-3), 2 * x, what="central_diff(x², [0, 1, 2.5])")
    expect_close(forward_diff(lambda t: t**2, x, 1e-3), 2 * x + 1e-3, what="forward_diff(x², [0, 1, 2.5])")
    expect_close(backward_diff(lambda t: t**2, x, 1e-3), 2 * x - 1e-3, what="backward_diff(x², [0, 1, 2.5])")

Free further reading

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

  • Derivatives

    Paul's Online Notes. Notes.

    The definition of the derivative and every differentiation rule, one page per rule.