Derivation of the Black-Scholes-Merton PDE › Code Implementation

Michael BrenndoerferNovember 26, 20253 min read
Quantitative FinanceAll chapters
Part 5 of 15Stochastic Calculus and Pricing Theory
Chapter 29Derivation of the Black-Scholes-Merton PDE › Code Implementation
Reading section 9 of 16 · Code Implementation · 3 minSection 9 of 16 · 3 min

Book contents

Section 9 of 16 · 3 min read

Derivation of the Black-Scholes-Merton PDE › Code ImplementationOne section · Sections in this chapter9 / 16 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's implement a numerical verification that a proposed solution satisfies the Black-Scholes PDE. We'll compute the partial derivatives numerically and check that the PDE residual is close to zero.

In[9]:
Code
def bs_pde_residual(V_func, S, t, r, sigma, T, h_S=0.01, h_t=0.0001):
    """
    Compute the residual of the Black-Scholes PDE for a given pricing function.
    A valid solution should have residual close to zero.

    Parameters:
    - V_func: Function V(S, t) giving the derivative price
    - S: Stock price
    - t: Current time
    - r: Risk-free rate
    - sigma: Volatility
    - T: Maturity time
    - h_S, h_t: Step sizes for numerical differentiation
    """
    # Compute V at current point
    V = V_func(S, t, r, sigma, T)

    # Numerical partial derivatives using central differences
    dV_dt = (
        V_func(S, t + h_t, r, sigma, T) - V_func(S, t - h_t, r, sigma, T)
    ) / (2 * h_t)
    dV_dS = (
        V_func(S + h_S, t, r, sigma, T) - V_func(S - h_S, t, r, sigma, T)
    ) / (2 * h_S)
    d2V_dS2 = (
        V_func(S + h_S, t, r, sigma, T)
        - 2 * V
        + V_func(S - h_S, t, r, sigma, T)
    ) / (h_S**2)

    # Compute PDE residual: LHS - RHS should equal zero
    lhs = dV_dt + r * S * dV_dS + 0.5 * sigma**2 * S**2 * d2V_dS2
    rhs = r * V

    return lhs - rhs

Now let's define a test pricing function and verify it satisfies the PDE. We'll use the power payoff derivative from our worked example.

In[10]:
Code
def power_derivative_price(S, t, r, sigma, T):
    """
    Price of a derivative that pays S^2 at maturity T.
    """
    return S**2 * np.exp((r + sigma**2) * (T - t))
In[11]:
Code
# Test parameters
S_test = 100.0
t_test = 0.0
r_test = 0.05
sigma_test = 0.20
T_test = 1.0

# Compute PDE residual
residual = bs_pde_residual(
    power_derivative_price, S_test, t_test, r_test, sigma_test, T_test
)
Out[12]:
Console
PDE residual for power derivative: -2.82e-06
This should be close to zero (numerical error only)

The residual is essentially zero, confirming our analytical solution is correct up to numerical precision.

Let's also test with a function that should not satisfy the PDE, showing that our verification catches invalid solutions.

In[13]:
Code
def invalid_pricing_function(S, t, r, sigma, T):
    """
    An arbitrary function that does NOT satisfy the BS PDE.
    """
    return S * np.exp(-0.1 * (T - t))  # Wrong formula
In[14]:
Code
residual_invalid = bs_pde_residual(
    invalid_pricing_function, S_test, t_test, r_test, sigma_test, T_test
)
Out[15]:
Console
PDE residual for invalid function: 9.0484
Non-zero residual indicates this is NOT a valid BS solution

The non-zero residual confirms that arbitrary functions generally do not satisfy the Black-Scholes PDE.