Quant techniques · Monte Carlo

Simulating rare events: importance sampling

Tail risk, deep out-of-the-money options and default probabilities all ask about events that rarely happen. Plain Monte Carlo spends almost all of its samples where nothing interesting happens. Importance sampling turns that around: sample from a distribution that hits the rare event often, and correct for the cheating with a weight on every sample.

1p=Ef[1A(X)]=Eg ⁣[1A(X) f(X)g(X)]p = E_f[\mathbf 1_A(X)] = E_g\!\left[\mathbf 1_A(X)\,\dfrac{f(X)}{g(X)}\right] Change the sampling density from f to g; the likelihood ratio f/g keeps the estimate unbiased.
2φ(x)φ(x−θ)=e−θx+θ2/2\dfrac{\varphi(x)}{\varphi(x - \theta)} = e^{-\theta x + \theta^2/2} For a normal shifted to mean θ, the weight is an exponential: this is exponential tilting, the same change of measure as Girsanov.
3REplain=1−pNp≈1Np\mathrm{RE}_{\text{plain}} = \sqrt{\dfrac{1-p}{Np}} \approx \dfrac{1}{\sqrt{Np}} Plain Monte Carlo needs about 1/(p·RE²) draws: 100/p for 10% error.
4θ≈c\theta \approx c For P(Z > c), centring the sampling distribution at the boundary of the event keeps the relative error nearly flat as c grows.
The problem

Plain Monte Carlo wastes its draws

Estimate p=P(Z>4)p = P(Z > 4) for a standard normal Z: the true value is 3.17×10−53.17 \times 10^{-5}. With 10,000 draws you expect 0.3 hits, so most runs return exactly zero. The relative error of plain Monte Carlo is about 1/Np1/\sqrt{Np} (step 3). To estimate p to 10%, you need N≈100/pN \approx 100/p, about 3.2 million draws. At 5σ, about 350 million.

The draws aren’t wasted because the computer is slow; they’re wasted because almost all of them land where the indicator is zero and teach you nothing about the tail.

The fix

Sample where the action is, then reweight

Instead of drawing X from N(0, 1), draw it from N(θ, 1) with θ near the threshold. Now about half the draws land past c. Each one is wrong by a known amount: it was drawn too often, by a factor φ(x−θ)/φ(x)\varphi(x - \theta)/\varphi(x). Multiply by the inverse, the likelihood ratio in step 2, and the average is unbiased again:

p^IS=1N∑i=1N1{Xi>c} e−θXi+θ2/2,Xi∼N(θ,1)\hat p_{\text{IS}} = \frac1N\sum_{i=1}^N \mathbf 1\{X_i > c\}\,e^{-\theta X_i + \theta^2/2}, \qquad X_i \sim N(\theta, 1)

Its variance has a closed form. The second moment is Eθ[1{X>c}e−2θX+θ2]=eθ2 Φˉ(c+θ)E_\theta\big[\mathbf 1\{X > c\}e^{-2\theta X + \theta^2}\big] = e^{\theta^2}\,\bar\Phi(c + \theta), so

REIS2(θ)=1N(eθ2 Φˉ(c+θ)p2−1)\mathrm{RE}^2_{\text{IS}}(\theta) = \frac{1}{N}\left(\frac{e^{\theta^2}\,\bar\Phi(c + \theta)}{p^2} - 1\right)
threshold cshift θ

Sample from N(θ, 1) instead of N(0, 1)

Relative error with 10,000 draws vs θ

N(0, 1): what we wantN(θ, 1): what we samplethe event Z > c
–true P(Z > c)
–plain Monte Carlo (hits)
–importance sampling estimate
–IS relative error (theory)

At θ = 0 (plain Monte Carlo) the error curve starts high: for c = 4, a 178% relative error with 10,000 draws. It drops steeply as θ moves towards c, bottoms out near θ = c at about 2%, and climbs again if you overshoot: then most samples land far beyond c, where the weights are tiny and very uneven. With θ = c, the relative error stays near 2% from c = 2 to c = 5, while plain Monte Carlo goes from 6.5% to hopeless.

import numpy as np
from scipy.stats import norm

rng = np.random.default_rng(0)
N, c = 10_000, 4.0
p = norm.sf(c)                                    # 3.17e-5

z = rng.standard_normal(N)
plain = (z > c).mean()                            # usually 0

x = rng.standard_normal(N) + c                    # sample from N(c, 1)
w = (x > c) * np.exp(-c * x + c**2 / 2)           # likelihood ratio
print(p, plain, w.mean(), w.std() / np.sqrt(N) / p)   # IS estimate within ~2%
In finance

The same trick in option pricing and risk

  • Deep out-of-the-money options. Shift the drift of the simulated Brownian motion so more paths finish in the money, and weight each path by the Girsanov density e−θWT−θ2T/2e^{-\theta W_T - \theta^2 T/2}. It’s the same calculation as above, applied to the terminal normal draw.
  • Portfolio VaR and credit losses. Tilt the factor distribution towards the stress scenarios that drive large losses (Glasserman, Heidelberger and Shahabuddin did this for portfolio credit risk).
  • Barrier hits and default times. Push paths towards the barrier, weight them back.

The choice of tilt is the craft. A good rule for tail probabilities is to make the rare event typical under the new measure: centre g on the most likely way the event happens.

Where people slip in interviews

  • Forgetting the weights. Sampling from g without the likelihood ratio estimates the probability under g, not under f.
  • Overshooting. Tilting too far puts samples in the far tail, where weights vary by orders of magnitude; the variance blows up.
  • Choosing g with thinner tails than f. If g is near zero where f·1_A isn’t, the weights explode, and the variance can be infinite.
  • Reporting the plain-MC error formula. The IS estimator has its own variance; compute it from the weighted samples.
Practice

Try these before you open the answer

Warm-upDerive the likelihood ratio φ(x)/φ(x − θ).
Show answer
e−x2/2e−(x−θ)2/2=e−x2/2+(x2−2θx+θ2)/2=e−θx+θ2/2\frac{e^{-x^2/2}}{e^{-(x-\theta)^2/2}} = e^{-x^2/2 + (x^2 - 2\theta x + \theta^2)/2} = e^{-\theta x + \theta^2/2}
Plain MCHow many plain Monte Carlo draws do you need to estimate P(Z > 3) ≈ 0.00135 to 5% relative error?
Show answer
N≈1−pp×0.052≈10.00135×0.0025≈296,000N \approx \frac{1 - p}{p \times 0.05^2} \approx \frac{1}{0.00135 \times 0.0025} \approx 296{,}000
WeightsWith θ = c = 4, what weight does a draw at exactly x = 4 get? And at x = 5?
Show answer

e−16+8=e−8≈3.4×10−4e^{-16 + 8} = e^{-8} \approx 3.4 \times 10^{-4} and e−20+8=e−12≈6.1×10−6e^{-20 + 8} = e^{-12} \approx 6.1 \times 10^{-6}. Draws further out count for much less.

UnbiasedShow that E_θ[1{X > c} e^{−θX + θ²/2}] = P(Z > c).
Show answer
∫c∞e−θx+θ2/2 φ(x−θ) dx=∫c∞φ(x) dx=Φˉ(c)\int_c^\infty e^{-\theta x + \theta^2/2}\,\varphi(x - \theta)\,dx = \int_c^\infty \varphi(x)\,dx = \bar\Phi(c)

using the identity from the warm-up.

OptionsYou want the price of a digital that pays 1 if a stock, now 100, finishes above 200 in a year (20% vol, zero rates). How would you shift the simulation?
Show answer

The digital pays when Z>(ln⁡2+12σ2)/σ≈3.57Z > (\ln 2 + \tfrac12\sigma^2)/\sigma \approx 3.57. Sample Z from N(3.57, 1) and weight each path by e−3.57Z+3.572/2e^{-3.57Z + 3.57^2/2}. The price is about Φˉ(3.57)≈1.8×10−4\bar\Phi(3.57) \approx 1.8 \times 10^{-4}.

The relative errors were checked with 10,000-draw simulations at c = 2, 3, 4 and 5. The figure’s error curve is the exact formula above.