Expectations are integrals, dynamics are ODEs
Much of quantitative finance is one of two jobs. The first is an expectation, which is an integral: an option price is the discounted average of its payoff, e^(-rT)∫(S_T-K)⁺ φ(z) dz, and an expected shortfall is the average loss beyond a quantile. scipy.integrate.quad(f, a, b) returns the value and an error estimate, accepts -np.inf and np.inf as limits, and is accurate to many digits when the integrand is smooth. Where the integrand has a kink, as a call payoff does at the strike, start the integral at the kink instead of letting quad straddle it. Even when a closed form exists, computing the integral too is the best test that you understand the formula, and the integral still works for the payoffs that have no formula.
The second job is a dynamic: a quantity evolves according to a rule for its rate of change, an ordinary differential equation y'=f(t,y). scipy.integrate.solve_ivp(f, [t0, t1], y0, rtol=..., atol=...) follows it forward and returns the path; the last column of sol.y is the value at t₁. The Vasicek short-rate model gives a bond price P=e^(A(τ)-B(τ)r₀) whose two functions solve B'=1-κB and A'=-κθB+1/2σ²B² from zero, and the solver reproduces the textbook formula to 10⁻¹³ once you tighten the tolerances. The t distribution with 2.6 degrees of freedom has a 1% expected shortfall of 8.54 standard units, against 2.67 for the normal: the integral shows what the fat tail costs. From the fed funds rate of 3.88% the model's yield curve slopes gently from 3.96% at one year to 4.36% at thirty.
- An expectation is an integral:
quadhandles infinite limits; start at the kink of a payoff. - A dynamic is an ODE:
solve_ivpfollows it from its starting values; tightenrtolandatolto match a formula. - Check numerics against a closed form: when both exist, compute both and look at the gap.
Lab · what's graded
- A call price as an integral20 pts
- An expected shortfall by integration15 pts
- The closed form10 pts
- A bond price from two ODEs25 pts
- The Vasicek formula10 pts
- The model yield curve10 pts
- The study10 pts