SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 4.1.1 Functions of two variables: surfaces and level curvesConcept · 30 min
      2. 4.1.2 Partial derivativesConcept · 35 min
      3. 4.1.3 Practice set: partial derivativesPractice set · 20 min
      4. 4.1.4 Case study: the Greeks as partial derivativesCase study · 35 min

Level 4, Multivariable calculus and optimisation 4.1 Functions of several variables Lesson 3 of 4

Practice setLesson 4.1.3 in Level 4, Multivariable calculus and optimisation. About 20 minutes.

Practice set: partial derivatives

Functions of two variables

Functions of two variables: surfaces and level curves (lesson 4.1.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.

Partial derivatives

Partial derivatives (lesson 4.1.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

Partial derivatives by central differences

Write two functions. In both, f takes one argument per variable, so f(*point) gives its value. point is a list such as [1, 2] or [2, 3, 4].

  1. partials(f, point, h=1e-5) returns a list with one central difference for each variable, as in lesson 4.1.2. Move that coordinate by ±h\pm h and keep the others fixed.
  2. second_partial(f, point, i, j, h=1e-4) returns the second partial in variable i, then variable j (counting from 0). For i == j use the second central difference; otherwise use the four corner points.

Raise ValueError if h is not positive. Do not change the list point: build a new list for each moved point.

The tests use the bowl x2+2y2x^2 + 2y^2, the box volume xyzxyz, exye^{xy} and x2y+3y2x^2y + 3y^2 from lessons 4.1.1 and 4.1.2. One test needs 8 correct digits, which a one-sided difference cannot reach.

See the tests
import math


def test_bowl():
    """x² + 2y² at (1, 1) has partials 2 and 4"""
    expect_close(partials(lambda x, y: x**2 + 2 * y**2, [1, 1]), [2, 4], what="partials of x² + 2y² at (1, 1)", rel=1e-8)


def test_three_variables():
    """The box volume xyz at (2, 3, 4) has partials 12, 8 and 6"""
    expect_close(partials(lambda x, y, z: x * y * z, [2, 3, 4]), [12, 8, 6], what="partials of xyz at (2, 3, 4)", rel=1e-8)


def test_central_accuracy():
    """e^(xy) at (1, 2) is accurate to 8 digits, which a one-sided difference is not"""
    e2 = math.exp(2)
    expect_close(partials(lambda x, y: math.exp(x * y), [1, 2]), [2 * e2, e2], what="partials of e^(xy) at (1, 2)", rel=1e-8)


def test_point_unchanged():
    """The point passed in is left as it was"""
    point = [1.0, 2.0]
    partials(lambda x, y: x * y, point)
    second_partial(lambda x, y: x * y, point, 0, 1)
    expect_equal(point, [1.0, 2.0], what="the point after the calls")


def test_second_partials():
    """x²y + 3y² at (2, 1): fxx = 2, fyy = 6 and fxy = 4"""
    f = lambda x, y: x**2 * y + 3 * y**2
    expect_close(second_partial(f, [2, 1], 0, 0), 2, what="fxx", rel=1e-5)
    expect_close(second_partial(f, [2, 1], 1, 1), 6, what="fyy", rel=1e-5)
    expect_close(second_partial(f, [2, 1], 0, 1), 4, what="fxy", rel=1e-5)


def test_mixed_symmetric():
    """For e^(xy) at (1, 2) the mixed partial is 3e², in either order"""
    f = lambda x, y: math.exp(x * y)
    expect_close(second_partial(f, [1, 2], 0, 1), 3 * math.exp(2), what="fxy", rel=1e-5)
    expect_close(second_partial(f, [1, 2], 1, 0), 3 * math.exp(2), what="fyx", rel=1e-5)


def test_three_variable_mixed():
    """For xyz at (2, 3, 4) the mixed partial in x and z is y = 3"""
    expect_close(second_partial(lambda x, y, z: x * y * z, [2, 3, 4], 0, 2), 3, what="the xz partial of xyz", rel=1e-6)


def test_bad_step():
    """A step of 0 raises ValueError"""
    try:
        partials(lambda x, y: x + y, [1, 1], h=0)
    except ValueError:
        return
    raise AssertionError("expected ValueError for h = 0")

Free further reading

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