Trading Dev AcademyFree quant education

Free lesson · Integral calculus

Numerical integration and a continuous cash-flow building block

Open interactive lessonPractice calculationsExplore labs

Start with the idea

  • Many integrals do not have a convenient antiderivative.
  • Numerical quadrature approximates the integrand on small intervals and adds the resulting areas.
  • The step size controls one source of error, while inaccurate input data create another.
Symbols, units & horizon
  • I: exact integral being approximated
  • h=(b−a)/n: uniform interval width
  • xᵢ=a+ih: sample locations
  • n: number of intervals
  • PV: present value in currency
  • c: constant currency flow per year
  • r: continuous discount rate per year
  • T: horizon in years
  • t: integration time
  • At r=0: PV=cT by continuity

When and why to use this

Use a known exact integral to validate an integration routine, then examine step convergence on the target integrand. Separate mathematical approximation error from uncertainty in the inputs.

  • The trapezoidal rule joins neighbouring samples with straight lines and integrates those lines.
  • For a sufficiently smooth integrand its error decreases quadratically with uniform step size over a fixed interval.
  • Halving the step is a useful convergence check, not a cure for discontinuities or biased data.
  • As a separate application, a continuous currency flow c(t) discounted at a constant continuously compounded rate r has present value ∫ c(t)exp(−rt)dt.
  • This is a model of continuous payments; a discrete coupon schedule should normally be summed at its actual payment dates.
I≈h2[f(x0)+2∑i=1n−1f(xi)+f(xn)],PV=∫0Tce−rtdt=c(1−e−rT)r (r≠0)
Calculus: derivation and arithmetic

Numerical integration and a continuous cash-flow building block

  1. One strip has trapezoidal area h[f(xi)+f(xi+1)]2. Adding strips counts every interior sample twice and each endpoint once, giving the displayed weighted sum.
  2. For f(x)=x2 on [0,1] with n=2, use h=0.5: I2=0.25[0+2(0.25)+1]=0.375. This approximates the exact integral 13; it is not an identity for the curved integrand.
  3. For constant flow and r≠0, an antiderivative is −ce−rtr. Subtract endpoints: PV=[−ce−rtr]0T=c(1−e−rT)r.
  4. As r→0, the ratio (1−e−rT)r tends to T, giving PV=cT. With c=100 dollars/year, r=0.05 per year and T=2 years, PV=100(1−e−0.1)0.05≈190.325 dollars.
Work it by hand

A continuous $100/year flow over 2 years at r=.05 has PV=100(1−exp(−.1))/.05≈$190.325. At zero discount rate it is $200.

Architecture pseudocode

from math import expm1

def trapezoid(function,a,b,n):
    if not isinstance(n,int) or n<1: raise ValueError("Positive integer n required")
    h=(b-a)/n
    return h*(.5*function(a)+sum(function(a+i*h) for i in range(1,n))+.5*function(b))

def continuous_flow_pv(flow,rate,years):
    if years<0: raise ValueError("Nonnegative horizon required")
    return flow*years if rate==0 else -flow*expm1(-rate*years)/rate

print(trapezoid(lambda x:x*x,0,1,2),continuous_flow_pv(100,.05,2))

An analogy to remember

Surveyors can approximate uneven terrain with many flat-sided strips. Finer strips help if the terrain is adequately sampled; they cannot reveal a feature absent from the data.

How this becomes a building block

Quadrature is a reusable numerical component for expectations, discounting and some option-pricing integrals. The continuous-flow formula is a complete valuation under stated assumptions and also a term in a broader valuation model.

Python implementation

Self-contained teaching example. Python 3.10+; dependencies and input conventions are shown in the code and notation. Run in your own Python environment.

from math import expm1

def trapezoid(function,a,b,n):
    if not isinstance(n,int) or n<1: raise ValueError("Positive integer n required")
    h=(b-a)/n
    return h*(.5*function(a)+sum(function(a+i*h) for i in range(1,n))+.5*function(b))

def continuous_flow_pv(flow,rate,years):
    if years<0: raise ValueError("Nonnegative horizon required")
    return flow*years if rate==0 else -flow*expm1(-rate*years)/rate

print(trapezoid(lambda x:x*x,0,1,2),continuous_flow_pv(100,.05,2))

Continue learning

Integral Calculus: Accumulation & Area — all lessons
  1. Start with rectangles: rate × time = amount
  2. Definite integrals: adding many small contributions
  3. Core integral rules: undo a derivative, then check
  4. Antiderivatives and the fundamental theorem
  5. Integration techniques: substitution and integration by parts
  6. Improper integrals, densities and continuous expectations
  7. Numerical integration and a continuous cash-flow building block
  8. Substitution: reverse the chain rule without losing the bounds
  9. Integration by parts: transfer a derivative

Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations