Chapter 7 Laboratory — Module 7: Stochastic Calculus in the Hands¶

Mathematical Foundations of Modern Finance · Part II · Week 7

Four panels: the integral builder, the Itô verifier (classical chain rule fails by exactly the quadratic-variation term), the Girsanov panel, and the $1/\sqrt{n}$ hedging-error law.

Seeds: 20260701–20260704.

In [1]:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams.update({"figure.figsize": (7, 4.2), "axes.grid": True,
                     "grid.alpha": 0.3, "font.size": 11})
BRASS, INK = "#B4884A", "#0F1E3D"
import mfmf_engine_ch07 as eng

E1 — $\int W\,dW$: evaluation point matters (supports LOS 7.1)¶

The Itô integral (left evaluation) gives $\tfrac12(W_T^2 - T)$; the Stratonovich (midpoint) gives $\tfrac12 W_T^2$. The gap is exactly $T/2$.

In [2]:
r1 = eng.E1_wdw()
print(f"Left  (Ito)        : {r1['left']:+.4f}   closed form {r1['ito_closed_form']:+.4f}")
print(f"Midpoint (Strat)   : {r1['midpoint']:+.4f}   closed form {r1['strat_closed_form']:+.4f}")
print(f"Midpoint - Left    : {r1['midpoint_minus_left']:.4f}   target T/2 = {r1['gap_target_T_over_2']}")
Left  (Ito)        : -0.1866   closed form -0.1861
Midpoint (Strat)   : +0.3139   closed form +0.3139
Midpoint - Left    : 0.5005   target T/2 = 0.5

E2 — Itô's formula, and the failure of ordinary calculus (supports LOS 7.2)¶

For $f(x)=x^2$, $df = 2x\,dW + dt$. Drop the $dt$ (the classical chain rule) and the residual is exactly $\int \tfrac12 f_{xx}\,dt = T$ — the quadratic variation made visible.

In [3]:
r2 = eng.E2_ito_verify()
print(f"Ito residual        : {r2['ito_residual']:+.4f}   (~ 0)")
print(f"Classical residual  : {r2['classical_residual']:+.4f}   target T = {r2['classical_residual_target_T']}")
print(f"Matches the QV term : {r2['matches_QV_term']}")
Ito residual        : -0.0023   (~ 0)
Classical residual  : +0.9977   target T = 1.0
Matches the QV term : True

E3 — Girsanov: the drift changes, the volatility does not (supports LOS 7.4)¶

Set $\theta = \lambda = 0.178$. The change of measure shifts the index's drift to $r$ under $Q$, while the realized volatility is unchanged at every $\theta$.

In [4]:
r3 = eng.E3_girsanov()
print(f"P-drift mu        : {r3['P_drift']:.4f}")
print(f"Q-drift           : {r3['Q_drift']:.4f}   equals r: {r3['Q_drift_equals_r']}")
print(f"Density Z_T       : {r3['Z_T']:.4f}")
print(f"Realized vol      : {r3['realized_vol']:.4f}   unchanged: {r3['vol_unchanged']}")
P-drift mu        : 0.0701
Q-drift           : 0.0398   equals r: True
Density Z_T       : 0.7215
Realized vol      : 0.1712   unchanged: True

E4 — The $1/\sqrt{n}$ hedging-error law (supports LOS 7.5–7.6)¶

Discrete rebalancing leaves a hedging error whose standard deviation scales as $1/\sqrt{n}$. Its mean stays near zero regardless of the real-world drift — the hedge is $P$-insensitive.

In [5]:
r4 = eng.E4_hedge_error()
for n, d in r4['by_n'].items():
    print(f"n = {n:3d}: error mean {d['error_mean']:+.4f}, std {d['error_std']:.4f}")
print(f"\nStd ratio (21 vs 252): {r4['std_ratio_21_to_252']:.2f}   target ~{r4['target_ratio']:.2f}")
print(f"Error mean near zero: {r4['error_mean_near_zero']}")
n =  21: error mean -0.0126, std 1.2656
n =  63: error mean -0.0244, std 0.7419
n = 252: error mean -0.0045, std 0.3773

Std ratio (21 vs 252): 3.35   target ~3.46
Error mean near zero: True

Validation checks¶

Every laboratory report must reproduce these; a report whose checks do not pass is returned ungraded.

In [6]:
checks = eng.validation_checks()
for k, v in checks.items():
    if k.startswith("_"): continue
    print(f"[{'PASS' if v else 'FAIL'}] {k}")
assert checks["ALL_PASS"], "Validation failed — do not submit."
print("\nAll Module validation checks pass.")
[PASS] V1_wdw_gap_is_T_over_2
[PASS] V2_ito_residual_zero
[PASS] V3_classical_fails_by_QV
[PASS] V4_girsanov_Q_drift_r
[PASS] V5_vol_unchanged
[PASS] ALL_PASS

All Module validation checks pass.