This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

PDE solvers · Diffusion (parabolic)

The Black-Scholes equation

Method
Crank-Nicolson on a grid in the stock price, started with four implicit half-steps (Rannacher)
Checked
a European call against the closed form: the price, the delta, and the measured order with and without the start-up steps

The price of an option on a stock at price . It is a heat equation run backward from a known final condition — at expiry a call is worth its payoff, — with diffusion that grows with , a drift, and a decay at the interest rate. Counting time to expiry, , turns it into an ordinary forward march.

The coefficients vary along the grid, so the tridiagonal matrix is three arrays rather than three numbers, and that is the only change from the heat equation. At the call is worthless; at the far edge it is worth , the stock less the discounted strike.

The payoff has a kink at the strike, and Crank-Nicolson does not damp the high-frequency error a kink excites: every mode's amplification tends to as grows, so the error flips sign each step and never decays. It shows up in the derivatives — the delta — at the strike. Rannacher's remedy is to take the first steps with implicit Euler, which damps those modes hard, and only then switch. Both versions are run below, and their orders measured.

What the run shows

A European call, one year from expiry
payoff at expiryvalue today
05010015020050100150200250stock price Svalue
  • the at-the-money price matches the closed form to a tenth of a cent
  • halving h and the time step together cuts the error by four: second order
  • with long time steps, plain Crank-Nicolson leaves the kink ringing in the delta
  • four implicit half-steps at the start remove it
exact
10.4506
computed
10.4499
rough
0.0424931
smoothed
0.00013003
order
2.00203

Computed when this page was built, by the same interpreter that runs it in your browser: 4 of 4 tests, under 0.1 s. The numbers above are the program's show lines.

The program

126 lines, 7 functions. It is editable: change it and press Run (or ⌘/Ctrl + Enter). The tests at the bottom run too, so if your change breaks the solver, a test fails.

normal_cdf
The standard normal cumulative distribution.
call_exact
The Black-Scholes closed form for a European call at stock price s.
delta_exact
Its delta, dV/dS = N(d1).
theta_step
One theta-step of length dt from time-to-expiry tau: theta = 1 is implicit Euler, theta = 1/2 Crank-Nicolson. `s` holds the n interior stock prices, spacing h.
price_call
Price the call on n interior points in `steps` time steps. With `smooth`, the first two steps are each replaced by two implicit-Euler half-steps.
delta_error
The worst error in the delta (a centered difference of the prices) within 20 of the strike.
price_error
The error in the at-the-money price. The strike sits on a grid point: (n + 1) / 3 is whole.
Viv [1]

Open in the workbench The atlas Download black_scholes.viv