Back to the main page

Tools

Three models I programmed in order to understand them. All of them run right in the browser, without a server.

I am adding more models over time.

Black–Scholes

I built an option pricer to understand what risk managers look at, not to rediscover the formula.

European option, no dividends, constant volatility σ.

Type
%
%
  • value today
  • payoff at expiry
Option price
–
Δ delta
–
Γ gamma
–
ν vega (per 1 pp of σ)
–
Θ theta (per day)
–
Intrinsic value
–
Time value
–

Theta accelerates as expiry approaches: an at-the-money option loses value fastest in its final weeks. Shorten T and watch the curve collapse onto the payoff.

How it is computed
  • C = S·N(d₁) − K·e^(−rT)·N(d₂); P = K·e^(−rT)·N(−d₂) − S·N(−d₁)
  • d₁ = [ln(S/K) + (r + σ²/2)·T] / (σ·√T); d₂ = d₁ − σ·√T
  • N(x) is computed through erf (Abramowitz–Stegun 7.1.26, error below 1.5·10⁻⁷).
  • Δ = N(d₁) for a call, N(d₁) − 1 for a put; Γ = φ(d₁)/(S·σ·√T); ν = S·φ(d₁)·√T / 100 (per 1 pp of σ); Θ per day = Θ per year / 365.
  • Assumptions: European exercise, no dividends, constant σ and r, continuous compounding.

Monte Carlo

I built it to get a feel for modelling uncertainty. What surprised me most was how badly the normal model underestimates crash risk.

Geometric Brownian motion with daily steps, jumps following Merton's crash model.

%
%
Horizon (days)
Number of paths
Model

  • median
  • 5th and 95th percentile
  • 5% VaR level
Outputsnormalwith jumps
Median Sₜ––
Mean Sₜ––
5% VaR––
5% ES––
P(loss)––

Same drift and volatility, plus only rare downward jumps (one every two years on average, typically around −14%). The median barely moves, but the 5% VaR and the expected shortfall grow substantially. The normal model would call such a drop practically impossible; the paths below its VaR level are drawn in oxide red.

How it is computed
  • Sₜ₊Δₜ = Sₜ · exp[(μ − σ²/2)·Δt + σ·√Δt · z], Δt = 1/252 (daily step), z ~ N(0, 1) via Box–Muller.
  • Jump model (Merton 1976): with probability λ·Δt per day a jump exp(J) arrives, J ~ N(−0.15, 0.10²), λ = 0.5 per year; the drift is compensated by λ·κ·Δt (κ = e^(μJ + σJ²/2) − 1) so the expected price stays the same.
  • 5% VaR = S₀ − 5th percentile of terminal values; 5% ES = S₀ − mean of the worst 5% of terminal values; P(loss) = share of paths with Sₜ < S₀.
  • Both models are always computed from the same seed so the columns can be compared; the chart shows the selected model.
  • Note: Student-t innovations on daily steps average out over longer horizons (CLT) and the terminal distribution approaches the normal one, which is why crashes are modelled as jumps.

Markowitz

I programmed the efficient frontier to understand why diversification works, and above all where the model breaks down.

Single-period model with three assets and no transaction costs.

Assets
Assetμ %σ %
A
B
C
Correlation
%
Short selling
  • random portfolios
  • efficient frontier
  • max Sharpe and CML

Max-Sharpe portfolio

E[r]
–
σ
–
Sharpe
–
Weightsestimate
A–
B–
C–

The model is sensitive to its inputs: shifting expected returns by 1–2 percentage points reshuffles the "optimal" portfolio substantially. That is why raw Markowitz is rarely used on its own in practice; constraints, shrinkage estimators or Black–Litterman views are layered on top.

How it is computed
  • E[rp] = Σ wi·μi; σp² = Σi Σj wi·wj·ρij·σi·σj; Sharpe = (E[rp] − rf) / σp; Σ wi = 1, without shorting wi ≥ 0.
  • Frontier = upper envelope of 3,000 random portfolios (no shorting: weights from normalised |N(0,1)|; with shorting: from N(0,1) limited to [−1, 2]) plus the two-asset mixes.
  • Estimation error: 20 scenarios with μi ± 1.5 pp and σi ± 10% (fixed seed); their frontiers are drawn and the half-range of the max-Sharpe weights is reported as ±.
  • The three correlations entered must form a positive definite matrix; I require its smallest eigenvalue to be at least 0.05. If it is not, all three are scaled towards zero by the same factor until it is, and a note appears under the chart.
  • Assumptions: one period, no transaction costs; the inputs are themselves estimates.