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
Divide the interval $[a,b]$ into many small subintervals, and select one sample point $x_i^*$ from each, forming a Riemann sum:
$$ \sum_{i=1}^{n}f(x_i^*)\Delta x_i. $$
As the partition becomes finer, if these sums approach a common limit, the definite integral is defined as:
$$ \int_a^b f(x),dx. $$
If $f(x) \ge 0$, this integral represents the signed area between the curve and the x-axis. When the function lies below the axis, its contribution is negative, so the definite integral is not simply the geometric area.
Unit checking is highly useful: if $f(t)$ has units of "requests per second" and $dt$ has units of seconds, the result of the integral is in "requests."
2. An Indefinite Integral Is a Family of Antiderivatives
If $F'(x) = f(x)$, then $F$ is an antiderivative of $f$:
$$ \int f(x),dx = F(x) + C. $$
The constant $C$ cannot be omitted, because derivatives eliminate arbitrary constants. In contrast, definite integrals are evaluated between specified limits and yield a numerical result, so no arbitrary constant is needed.
The Fundamental Theorem of Calculus connects local rates of change with accumulated quantities:
$$ \int_a^b f(x),dx = F(b) - F(a),\qquad F' = f. $$
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
For a continuous random variable $X$, its probability density function $p(x)$ satisfies:
$$ p(x) \ge 0, \quad \int_{-\infty}^{\infty} p(x),dx = 1. $$
The probability over an interval is given by:
$$ P(a \le X \le b) = \int_a^b p(x),dx. $$
In continuous distributions, the probability at a single point $P(X = x)$ is typically zero. However, the density $p(x)$ can be positive, even greater than 1. The density has units, while probability is unitless. Therefore, the value of the density at a point cannot be interpreted as the "probability that this value occurs."
The cumulative distribution function is:
$$ F(x) = P(X \le x) = \int_{-\infty}^{x} p(t),dt. $$
Wherever $F(x)$ is differentiable, $F'(x) = p(x)$, which reflects the relationship between accumulated probability and local rate of change.
4. Sampled Data Can Only Approximate Integration
If you have only discrete monitoring points, you can use the trapezoidal rule:
$$ \int_a^b f(x),dx \approx \sum_i \frac{f(t_i)+f(t_{i+1})}{2}(t_{i+1}-t_i). $$
from collections.abc import Sequence
def trapezoidal_integral(
times: Sequence[float], rates: Sequence[float]
) -> float:
if len(times) != len(rates) or len(times) < 2:
raise ValueError("times and rates Must be equal length and contain at least two points")
if any(right <= left for left, right in zip(times, times[1:])):
raise ValueError("Timestamp must be strictly increasing")
total = 0.0
for left, right, r_left, r_right in zip(
times, times[1:], rates, rates[1:]
):
total += (r_left + r_right) * (right - left) / 2
return total
seconds = [0, 60, 120, 180]
requests_per_second = [10, 14, 13, 9]
print(trapezoidal_integral(seconds, requests_per_second))This 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.
Common numerical integration methods include:
| Method | Assumption within interval | Characteristics |
|---|---|---|
| Left/Right Rectangle Method | Function value remains constant | Simple, but exhibits clear directional bias |
| 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 irregular timestamps;
- 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.
6. Seeing Accumulation in Software Systems
Request rates integrated over time yield total request counts. 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, and comparing rate curves against these counters can provide more reliable results. 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, underflow, or insufficient precision.
Exercise
- Find the displacement of an object moving with velocity $v(t) = 3t^2$ from $t = 0$ to $t = 2$.
- Approximate $\int_0^1 x^2 , dx$ using the rectangular method and the trapezoidal method, and compare the errors across different step sizes.
- Explain why the probability of a continuous uniform distribution over
[0, 0.5]is0.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 next lesson returns to the valley: given the local slope at a current position, how should we choose step size, handle noise, and determine whether we've truly reached an optimal solution.