Adding up a curve: areas and moving systems
An integral is the area under a curve. integrate.quad(f, a, b) computes for a function you write, and returns the value together with an error estimate. It is adaptive: it samples more where the function changes fast. If you only have values at points, not a function, you add up thin slices instead. The trapezoid rule, np.trapezoid, joins neighbouring points by straight lines and adds the areas of the trapezoids. Simpson's rule, integrate.simpson, uses small parabolas and is much more accurate for smooth curves. A function with a kink, such as a call payoff , breaks the smoothness that quad expects, so you tell it where the kink is with points=[K].
The second tool is for systems that move. An ordinary differential equation (ODE) says how fast something changes, for example a short interest rate pulled toward a long-run level at speed : . integrate.solve_ivp(f, [0, T], [r0]) follows it forward in time. The simplest hand method is the Euler step, , which assumes the speed stays constant over a step of length . In finance, integrals turn a forward-rate curve into a discount factor: , and they turn a payoff and a probability density into an expected value.
solve_ivp agrees. A forward-rate curve has area from to years, so . For a call with , volatility and one year, quad of the payoff times the normal density, with points at the kink, gives , the Black–Scholes price. On real data, the Treasury 10-year yield of on 9 October 2026, read as a continuously compounded rate, gives .- Function or points: use
quadwhen you can write the function, and trapezoid or Simpson when you only have values. - Tell quad about kinks: a payoff such as needs
points=[K]for a reliable answer. - Integrals price things: area under a forward curve gives a discount factor, and payoff times density gives an expected value.
Lab · what's graded
- A trapezoid area10 pts
- An Euler step10 pts
- The exact solution15 pts
- A discount factor15 pts
- The right integrator15 pts
- Pricing a call by integration10 pts
- Why tell quad about the kink25 pts