11.2 Integrals and Numerical Integration: From Local Rates to Cumulative Totals
A monitoring system records request rates every minute, yet business stakeholders ask how many requests were processed in a full day. A probability model provides a density function, but product teams care about the probability of events occurring within a specific interval. Both scenarios involve aggregating local rates into total quantities.
Learning Objectives
- Distinguish between indefinite and definite integrals;
- Use the Fundamental Theorem of Calculus to connect derivatives and integrals;
- Understand the relationship between probability density, cumulative quantities, and area;
- Compare the rectangle method, trapezoidal method, and sampling error.
1. Definite Integral as the Limit of a Sum
For
As the largest subinterval length tends to zero, if all choices of sample points produce a common limit, the definite integral is defined as:
If
Unit checking is highly useful: if
2. An Indefinite Integral Is a Family of Antiderivatives
If
The constant
A common sufficient set of conditions for the fundamental theorem is that
It does not mean that every function has an elementary antiderivative that can be written in closed form. Many practical integrals must be evaluated using special functions or numerical methods.
3. Probability Density Is Not Point Probability
Assume
The probability over an interval is given by:
For a distribution with a density, every single-point probability
The cumulative distribution function is:
At a point where the density
For a uniform distribution on
4. Sampled Data Can Only Approximate Integration
If you have only discrete monitoring points, you can use the trapezoidal rule:
from collections.abc import Sequence
from math import fsum, isfinite
def trapezoidal_integral(
times: Sequence[float], rates: Sequence[float], max_gap=None
) -> float:
if len(times) != len(rates) or len(times) < 2:
raise ValueError("need equal lengths and at least two points")
if not all(isfinite(x) for x in times) or not all(isfinite(r) for r in rates):
raise ValueError("observations must be finite")
gaps = [right - left for left, right in zip(times, times[1:])]
if any(gap <= 0 for gap in gaps):
raise ValueError("timestamps must be strictly increasing")
if max_gap is not None:
if not isfinite(max_gap) or max_gap <= 0:
raise ValueError("max_gap must be positive and finite")
if any(gap > max_gap for gap in gaps):
raise ValueError("gap exceeds the interpolation limit")
return fsum((left / 2 + right / 2) * gap
for left, right, gap in zip(rates, rates[1:], gaps))
seconds = [0, 60, 120, 180]
requests_per_second = [10, 14, 13, 9]
print(trapezoidal_integral(seconds, requests_per_second, max_gap=90)) # 2190.0This result relies on the sampling assumption that consecutive observations are connected by straight lines. If a sharp spike occurs between points, low-frequency sampling may entirely miss it. Increasing algorithmic precision cannot compensate for changes in the data that were not actually observed.
This Python 3.9+ example estimates 2,190 requests over 180 seconds. Uneven spacing is allowed, but NaN, infinity, and nonincreasing timestamps are rejected. max_gap=90 rejects interpolation across gaps longer than 90 seconds instead of silently treating missing data as zero. Choose the threshold from collection frequency and application tolerance; the exercise can explore other policies. Finite inputs alone do not prevent intermediate overflow at arbitrary magnitudes.
Common numerical integration methods include:
Order comparisons assume uniform refinement on a fixed interval and sufficient smoothness. Midpoint and trapezoidal rules are normally second order; composite Simpson is fourth order with an even panel count and a bounded fourth derivative. Adaptive error estimates can still miss an unsampled narrow peak.
| Method | Assumption within interval | Characteristics |
|---|---|---|
| Left/Right Rectangle Method | Function value remains constant | Simple; monotonicity determines bias direction |
| Midpoint Method | Uses midpoint to represent interval | Generally more accurate than endpoint rectangles |
| Trapezoidal Method | Assumes linear change between adjacent points | Well-suited for existing discrete samples |
| Simpson's Method | Uses local quadratic approximation | Higher accuracy for smooth functions |
| Adaptive Integration | Subdivides intervals at challenging points | Controls estimation error, but computational cost varies |
5. Errors Come From Multiple Sources
Numerical integration must distinguish at least among the following:
- Modeling error: the real process does not exactly match the chosen function;
- Sampling error: insufficient observation frequency or ignoring actual unequal time gaps;
- Discretization error: approximating a continuous integral with a finite number of segments;
- Rounding error: accumulated deviation from floating-point arithmetic.
Reducing step size typically reduces discretization error but increases computational load and the number of floating-point operations. When selecting a method, consider the function's smoothness, error tolerance, and computational budget, rather than the formula's order of accuracy alone.
The integral of x² over [0,1] is 1/3. This Python 3 example calculates signed errors with exact fractions and converts to floating point only for display:
from fractions import Fraction as F
for n in [4, 8, 16]:
h = F(1, n)
left = h * sum((i * h) ** 2 for i in range(n))
midpoint = h * sum(((i + F(1, 2)) * h) ** 2 for i in range(n))
trapezoid = left + h / 2 # f(1) - f(0) = 1
print(n, f"{float(left-F(1,3)):.6f}",
f"{float(midpoint-F(1,3)):.6f}", f"{float(trapezoid-F(1,3)):.6f}")
# 4 -0.114583 -0.005208 0.010417
# 8 -0.059896 -0.001302 0.002604
# 16 -0.030599 -0.000326 0.000651Doubling the panel count quarters midpoint and trapezoidal errors. Here their exact errors are -1/(12n²) and 1/(6n²) respectively; the left-rectangle error shrinks mainly as 1/n. An error order describes refinement behavior, not these same numbers for every function and grid.
6. Seeing Accumulation in Software Systems
- Request rate integrated over time yields a total request count;
- Power integrated over time yields energy;
- Probability density integrated over an interval yields probability;
- Velocity integrated over time yields displacement;
- A continuous cash flow, discounted and then integrated, yields present value.
Some systems use counters to directly track accumulated quantities, which can be more reliable than integrating a rate curve afterward. However, counters can reset, overflow, or be lost, meaning data quality issues still need to be addressed. Mathematical tools cannot replace an understanding of the underlying data collection mechanisms.
Common Misconceptions
- "The integral is always a positive area": Definite integrals carry sign; they can be negative.
- "Density values are probabilities": In continuous distributions, probability over an interval is obtained only by integrating the density function.
- "More sampling means greater accuracy": Sensor bias and model errors do not vanish simply because sampling is denser.
- "A closed-form formula eliminates the need for numerical methods": Even with a mathematical expression, floating-point implementation can still suffer from overflow, cancellation, or insufficient precision.
Exercise
- Find the displacement of an object moving with velocity
from to . - Approximate
using the rectangular method and the trapezoidal method, and compare the errors across different step sizes. - Explain why a uniform distribution on
[0,1]assigns probability0.5to[0,0.5], yet the probability of any single point is zero. - Add a missing interval strategy to monitor integral computation code, and explain the underlying business assumptions it implies.
Summary
Derivatives break down a whole process into local components, while integrals reassemble local rates into a total quantity. Computers work with finite samples and limited precision, so any numerical integration result must always be interpreted in conjunction with the sampling method, approximation technique, and assumed error bounds.
The optimization material that follows uses local slope to choose an update direction, then addresses step size, noise, and stopping conditions.