Code Programs: Finite Differences and Monte Carlo in Action
Chapters 82 and 83 are the “put your money where your mouth is” chapters. After hundreds of pages of theory, equations, and diagrams, Wilmott hands you actual working code. Visual Basic code for Excel, to be precise. These are not toy examples. They implement real pricing models for real financial products, from plain American calls to exotic Parisian options to stochastic volatility to credit risk. Let me walk you through what each program does and why it matters.
Chapter 82: Finite-Difference Programs
This chapter collects finite-difference implementations for a wide range of models discussed throughout the book. Each one turns a partial differential equation into a grid of numbers that a computer can crunch.
Kolmogorov Equation
The first program solves a probability question: given that volatility follows a stochastic process, what is the probability that it leaves a certain range before a given time? This is the Kolmogorov forward equation, which governs how probability distributions evolve over time.
Why you care: if you are modeling stochastic volatility (and many real-world models do), you need to understand the behavior of volatility itself. This program tells you how likely volatility is to stay within bounds, which directly affects option prices.
Convertible Bond Pricer
This one handles convertible bonds with intermittent conversion. The holder can convert the bond into stock during specified periods. The code uses an explicit finite-difference scheme and steps backward in time, checking at each step whether conversion is optimal.
The boundary conditions are thoughtful. At the top of the asset range, the code uses a zero-gamma condition (the second derivative of the option price with respect to the stock price is zero). At S equals zero, the bond decays at the risk-free rate. This is standard practice for finite-difference schemes.
Why you care: convertible bonds are everywhere in the market. They combine fixed-income features with equity optionality, and pricing them requires handling the conversion feature properly.
American Call with Implicit Scheme
This program values an American call using an implicit finite-difference method with successive over-relaxation (SOR). Unlike the explicit method (which just marches forward one step at a time), the implicit method solves a system of equations at each time step. It is unconditionally stable, meaning you do not need to worry about the time step being too large.
The early exercise constraint is enforced at each iteration: if the computed option value drops below the exercise value, it gets bumped up to the exercise value. The SOR method speeds up convergence of the iterative solver with a relaxation parameter (set to 1.5 in the code).
Why you care: American options are among the most commonly traded derivatives. Getting the early exercise boundary right is critical for accurate pricing, and the implicit method is the workhorse of the industry.
Parisian Option
This is where things get interesting. A Parisian option is a barrier option with a twist: the barrier is only triggered if the stock stays beyond it for a certain amount of time (BarTime). You can have the option knocked in or knocked out, and the stock can be above or below the barrier.
The code needs a two-dimensional array because the option value depends on both the asset price and the accumulated time beyond the barrier. This makes the numerical problem significantly harder than a standard barrier option. The time step is chosen automatically to satisfy the stability constraint.
Why you care: Parisian options show up in structured products. They are less sensitive to momentary price spikes than standard barrier options, which makes them more practical but harder to price.
Passport Options
A passport option is an option on a trading account. The holder paper-trades the underlying up to a set limit, and at expiry receives the positive (or negative, depending on call or put) balance. The value depends on the trading balance as a fraction of the asset price.
The code also includes a chooser variant, where the holder picks between the call and put payoff at a specified time before expiry. This adds another layer of optionality.
Why you care: passport options are fascinating because they are options on a trading strategy. The mathematics connects to stochastic control theory, and the pricing equation is nonlinear because the optimal trading strategy (go long or short) depends on the option value itself.
Stochastic Volatility
This two-factor program prices a European call when volatility is itself stochastic. The code operates on a two-dimensional grid (stock price and volatility level), with the finite-difference scheme stepping backward through time.
The program includes parameters for mean reversion of volatility, volatility of volatility, and correlation between stock and volatility moves. These are the standard parameters in any stochastic volatility model.
Why you care: the Black-Scholes assumption of constant volatility is wrong. Everyone knows it. Stochastic volatility models capture the volatility smile/skew that is observed in real option markets.
Uncertain Volatility
This is possibly the shortest “program” in the book. It is just two lines added to any existing finite-difference engine. If gamma is positive, use the minimum volatility. If gamma is negative, use the maximum volatility. That gives you the worst-case option price when volatility is uncertain but bounded.
Why you care: instead of guessing a single volatility number, this approach gives you price bounds. Very practical, very robust, and the implementation could not be simpler.
Crash Modeling
This code implements the crash model from Chapter 58. It prices options under the assumption that the stock could crash by a certain percentage at any time. The interesting part is the hedging decision: at each grid point, the code compares delta hedging (normal Black-Scholes hedge) with crash hedging (a hedge that protects against the crash), and picks whichever gives the worse outcome for the seller.
The asset grid is logarithmically spaced (not equally spaced) because crashes are percentage moves, not absolute moves. This makes the crash calculation cleaner.
Why you care: standard models assume continuous price paths. Real markets can jump. This model puts a price on that jump risk.
Epstein-Wilmott Solution
This handles uncertain interest rates with a first-order nonlinear model. The code prices coupon-bearing bonds when the interest rate can move up or down at a bounded rate. At each grid point, the algorithm picks the worst case among three possibilities: rate stays put, rate goes up, or rate goes down.
Why you care: interest rate uncertainty affects bond prices in ways that standard models underestimate. This approach gives you worst-case bounds rather than a single (possibly wrong) point estimate.
Risky Bond Calculator
The most complex program in Chapter 82. It prices coupon-bearing bonds with stochastic default risk using a CIR-style hazard rate model. The code handles guaranteed coupons, guaranteed principal, callable features, and mean-reverting default intensity.
The finite-difference scheme operates on a grid of hazard rate values and steps backward through time, adding coupons at the right dates and applying call constraints. The output includes the bond price, its sensitivity to the hazard rate, and the recovery value.
Why you care: credit risk is a massive market. Understanding how default probability, recovery, and guarantees interact is essential for anyone trading or investing in corporate bonds.
Chapter 83: Monte Carlo Programs
Chapter 83 provides Monte Carlo implementations that complement the finite-difference codes above.
Basket Option Pricing
The first program prices a European option on multiple lognormal assets. It generates uncorrelated normal variables using Box-Muller, correlates them using Cholesky factorization, calculates the lognormal asset prices at expiry, evaluates the payoff (in this case the maximum of all assets), and averages over all simulations.
The code is clean and modular. The Cholesky decomposition is separated into its own subroutine, making it reusable.
Why you care: basket options on multiple stocks, indices, or commodities are common in structured products. Monte Carlo is often the only practical pricing method when you have more than three or four underlyings.
Quasi Monte Carlo Basket Pricing
The same basket pricing problem, but using Halton sequences instead of random numbers. The code uses pairs of prime bases (2 and 13, 3 and 17, 5 and 19, and so on) for the Box-Muller transformation in each dimension.
The key difference from the Monte Carlo version: the points fill space more evenly, leading to faster convergence. As discussed in Chapter 81, quasi-random methods can be significantly more accurate than plain Monte Carlo for the same number of evaluations.
Why you care: if you are pricing baskets regularly (and many trading desks are), the speedup from quasi-random methods is free. Same code structure, just replace random numbers with Halton numbers.
American Options via Longstaff-Schwartz
The final program implements the Longstaff-Schwartz regression approach for pricing American options with Monte Carlo. This is the algorithm described in detail in Chapter 80.
The code simulates all stock paths forward, then works backwards from expiry. At each time step, it identifies in-the-money paths, performs a least-squares regression of discounted future cashflows against current stock prices (using Excel’s LinEst function), and decides whether to exercise or hold. The cashflow matrix gets updated as you step backwards.
The Payoff function is defined separately (as a put in this example), making it easy to change. The Norm function generates normally distributed random numbers using the Box-Muller rejection method.
Why you care: this is the standard method for pricing American options when finite differences are impractical (high dimensions, complex path dependence). If you work in derivatives, you will encounter this algorithm.
The Bigger Picture
Looking at these two chapters together, a pattern emerges. The finite-difference programs (Chapter 82) work best in one or two dimensions and give you the option value at every point in the grid. This is great for calculating greeks, finding exercise boundaries, and understanding how the option behaves across different scenarios.
The Monte Carlo programs (Chapter 83) work best in high dimensions and give you the option value at a single point. They are flexible, easy to modify, and scale gracefully with the number of underlying assets.
Most real-world pricing systems use both approaches. Vanilla options and low-dimensional exotics go through finite-difference solvers. Baskets, multi-asset structures, and path-dependent products with many underlying variables go through Monte Carlo engines.
Wilmott’s code is in Visual Basic for Excel, which might seem dated. But the algorithms themselves are timeless. The same logic works in Python, C++, Julia, or whatever language you prefer. The point is not the syntax. The point is understanding what each line does and why.
Key Takeaways
- Finite-difference methods handle one and two-factor problems well: convertible bonds, American options, stochastic volatility, Parisian options, crash models, and credit risk all get working implementations.
- Monte Carlo methods handle multi-asset problems: basket pricing with correlated assets, quasi-random acceleration via Halton sequences, and American option pricing via Longstaff-Schwartz regression.
- Uncertain volatility pricing requires just two extra lines of code. If gamma is positive, use minimum vol. If gamma is negative, use maximum vol.
- The choice between finite differences and Monte Carlo depends on dimensionality. Below three or four factors, finite differences usually win. Above that, Monte Carlo takes over.
- These code examples are building blocks. Real production systems combine, extend, and optimize them, but the core logic stays the same.
Previous post: Numerical Integration: From Simple Sums to Quasi-Random Sequences
Next post: Wrapping Up Paul Wilmott on Quantitative Finance: Final Thoughts