SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 6.4.1 Diffusion and the heat equationConcept · 30 min
      2. 6.4.2 The fundamental solutionConcept · 30 min
      3. 6.4.3 Practice set: the heat equationPractice set · 20 min
      4. 6.4.4 Case study: from Black–Scholes to the heat equationCase study · 35 min

Level 6, Differential equations 6.4 The heat equation Lesson 3 of 4

Practice setLesson 6.4.3 in Level 6, Differential equations. About 20 minutes.

Practice set: the heat equation

Diffusion and the heat equation

Diffusion and the heat equation (lesson 6.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.

The fundamental solution

The fundamental solution (lesson 6.4.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

The heat equation from any start

Write heat_solution(u0, x, t). It returns u(x,t)u(x, t) for ut=uxxu_t = u_{xx} on the whole line, started from the profile u0 (a function of one number), by lesson 6.4.2's integral ∫u0(y) Φ(x−y,t) dy\int u_0(y)\,\Phi(x - y, t)\,dy.

  • Integrate with quad over a window of 12 widths 2t\sqrt{2t} on each side of xx, not over a fixed range. For a tiny tt the bell is too narrow for quad to find in a wide range.
  • At t=0t = 0 return u0(x). For t<0t < 0 raise ValueError: the equation cannot be run backwards from a start.

The tests try the step, the kink, exe^x, x2x^2 (which becomes x2+2tx^2 + 2t), a box near its edge, t=10−6t = 10^{-6}, t=0t = 0 and a negative tt.

See the tests
import math

from scipy.stats import norm


def _step(y):
    return 1.0 if y > 0 else 0.0


def _kink(y):
    return max(y, 0.0)


def _box(y):
    return 1.0 if -1 < y < 1 else 0.0


def test_step():
    """The step from 0 to 1 gives N(1) = 0.841345 at x = 1, t = 0.5"""
    expect_close(heat_solution(_step, 1, 0.5), norm.cdf(1), what="heat_solution(step, 1, 0.5)", rel=1e-8)


def test_kink():
    """The kink max(x, 0) is lifted to sqrt(t/pi) = 0.398942 at x = 0, t = 0.5"""
    expect_close(heat_solution(_kink, 0, 0.5), math.sqrt(0.5 / math.pi), what="heat_solution(kink, 0, 0.5)", rel=1e-8)


def test_exponential():
    """e^x becomes e^(x + t): e^2 at x = 1, t = 1"""
    expect_close(heat_solution(math.exp, 1, 1), math.exp(2), what="heat_solution(exp, 1, 1)", rel=1e-8)


def test_square():
    """x^2 becomes x^2 + 2t: 2 at x = 1, t = 0.5"""
    expect_close(heat_solution(lambda y: y * y, 1, 0.5), 2.0, what="heat_solution(x^2, 1, 0.5)", rel=1e-8)


def test_box_near_its_edge():
    """A box of height 1 on (-1, 1) gives 0.760250 at x = 0.9, t = 0.01"""
    exact = norm.cdf(1.9 / math.sqrt(0.02)) - norm.cdf(-0.1 / math.sqrt(0.02))
    expect_close(heat_solution(_box, 0.9, 0.01), exact, what="heat_solution(box, 0.9, 0.01)", rel=1e-7)


def test_tiny_time():
    """At t = 0.000001 the kink has barely moved: 0.5 at x = 0.5"""
    expect_close(heat_solution(_kink, 0.5, 1e-6), 0.5, what="heat_solution(kink, 0.5, 1e-6)", rel=1e-6)


def test_time_zero():
    """At t = 0 the answer is the starting profile itself"""
    expect_close(heat_solution(_step, 0.3, 0), 1.0, what="heat_solution(step, 0.3, 0)")


def test_negative_time():
    """A negative time raises ValueError"""
    try:
        heat_solution(_kink, 0, -0.1)
    except ValueError:
        return
    raise AssertionError("expected ValueError: the heat equation cannot be run backwards from u0")

Free further reading

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