SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 2.4.1 Maxima and minimaConcept · 25 min
      2. 2.4.2 Concavity and the second derivativeConcept · 25 min
      3. 2.4.3 Newton's method with derivativesConcept · 30 min
      4. 2.4.4 Practice set: using derivativesPractice set · 25 min
      5. 2.4.5 Case study: convexity and risk aversionCase study · 30 min
      6. 2.4.6 Case study: implied volatility by Newton's methodCase study · 30 min

Level 2, Single-variable calculus 2.4 Using derivatives Lesson 4 of 6

Practice setLesson 2.4.4 in Level 2, Single-variable calculus. About 25 minutes.

Practice set: using derivatives

Maxima and minima

Maxima and minima (lesson 2.4.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.

Concavity

Concavity and the second derivative (lesson 2.4.2)

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.

Newton's method

Newton's method with derivatives (lesson 2.4.3)

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.

Coding exercise

Exercise

Newton's method with a bracket

Write safe_newton(f, fprime, a, b, tol=1e-12, max_steps=100) that returns a root of f in the bracket [a,b][a, b], as in lesson 2.4.3.

  • If f(a) or f(b) is 0, return that end. If they have the same sign, raise ValueError: there is no bracket.
  • Start at the midpoint. After each guess xx, shrink the bracket to the half that still has the sign change.
  • Take the Newton step x−f(x)f′(x)x - \frac{f(x)}{f'(x)} when fprime(x) is not 0 and the step lands strictly inside the bracket. Otherwise take the midpoint of the bracket.
  • Return when a step moves xx by less than tol, or when f(x) is exactly 0. Raise RuntimeError after max_steps steps.

The tests include the cycle of lesson 1.3.3, a flat tangent at the start and the runaway xe−xxe^{-x}. One test allows only 12 steps, so bisection alone is too slow.

See the tests
import math


def test_square_root():
    """x² − 2 on [1, 2] gives √2"""
    expect_close(safe_newton(lambda x: x**2 - 2, lambda x: 2 * x, 1, 2), math.sqrt(2), what="root of x² − 2", rel=1e-12)


def test_x_exp_x():
    """xeˣ − 1 on [0, 1] gives about 0.567143"""
    r = safe_newton(lambda x: x * math.exp(x) - 1, lambda x: math.exp(x) * (1 + x), 0, 1)
    expect_close(r, 0.567143290409784, what="root of xeˣ − 1", rel=1e-12)


def test_fast():
    """It takes Newton steps: xeˣ − 1 on [0, 1] converges within 12 steps"""
    try:
        r = safe_newton(lambda x: x * math.exp(x) - 1, lambda x: math.exp(x) * (1 + x), 0, 1, max_steps=12)
    except RuntimeError:
        raise AssertionError("no answer within 12 steps: bisection alone needs about 40, Newton about 6")
    expect_close(r, 0.567143290409784, what="root of xeˣ − 1 within 12 steps", rel=1e-12)


def test_cycle():
    """x³ − 2x + 2 on [−3, 3], where plain Newton from 0 cycles, gives about −1.769292"""
    r = safe_newton(lambda x: x**3 - 2 * x + 2, lambda x: 3 * x**2 - 2, -3, 3)
    expect_close(r, -1.769292354238631, what="root of x³ − 2x + 2", rel=1e-12)


def test_flat_start():
    """x³ − 3x − 1 on [0, 2] starts on a flat tangent at 1 and gives about 1.879385"""
    r = safe_newton(lambda x: x**3 - 3 * x - 1, lambda x: 3 * x**2 - 3, 0, 2)
    expect_close(r, 1.8793852415718169, what="root of x³ − 3x − 1 in [0, 2]", rel=1e-12)


def test_runaway():
    """xe⁻ˣ on [−1, 4], where plain Newton from 1.5 runs away, gives 0"""
    r = safe_newton(lambda x: x * math.exp(-x), lambda x: (1 - x) * math.exp(-x), -1, 4)
    expect_close(r, 0.0, what="root of xe⁻ˣ", abs_tol=1e-12)


def test_root_at_end():
    """A root at an end of the bracket is returned: x² − 4 on [2, 5] gives 2"""
    expect_close(safe_newton(lambda x: x**2 - 4, lambda x: 2 * x, 2, 5), 2.0, what="root of x² − 4 on [2, 5]")


def test_no_bracket():
    """x² + 1 on [−1, 1] has no sign change: ValueError"""
    try:
        safe_newton(lambda x: x**2 + 1, lambda x: 2 * x, -1, 1)
    except ValueError:
        return
    raise AssertionError("expected ValueError when f(a) and f(b) have the same sign")


def test_max_steps():
    """Stopping after max_steps without converging raises RuntimeError"""
    try:
        safe_newton(lambda x: x**2 - 2, lambda x: 2 * x, 1, 2, max_steps=2)
    except RuntimeError:
        return
    raise AssertionError("expected RuntimeError after 2 steps without converging")

Free further reading

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