Skip to content

VOL 10 / CH 11 / LESSON 02

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 a<b, divide the interval [a,b] into many small subintervals, and select one sample point xi∗ from each, forming a Riemann sum:

∑i=1nf(xi∗)Δxi.

As the largest subinterval length tends to zero, if all choices of sample points produce a common limit, the definite integral is defined as:

∫abf(x)dx.

If f(x)≥0, this integral represents the 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:

∫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.

A common sufficient set of conditions for the fundamental theorem is that f is continuous on [a,b] and F′=f. Then:

∫abf(x)dx=F(b)−F(a),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 ​

Assume X has a density. Its density function p(x) satisfies:

p(x)≥0,∫−∞∞p(x)dx=1.

The probability over an interval is given by:

P(a≤X≤b)=∫abp(x)dx.

For a distribution with a density, every single-point probability P(X=x) is 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≤x)=∫−∞xp(t)dt.

At a point where the density p is continuous, F′(x)=p(x). For a general density this holds almost everywhere, not necessarily pointwise. Changing a density at a single point changes no interval probability. An atomless continuous distribution need not have a density; that case is outside this section’s model.

For a uniform distribution on [0,0.5], the density inside the interval is 2, yet P(0≤X≤0.25)=2×0.25=0.5. A density above 1 is valid because the probability, obtained by integration, is what must stay within 1.

4. Sampled Data Can Only Approximate Integration ​

If you have only discrete monitoring points, you can use the trapezoidal rule:

∫abf(x)dx≈∑if(ti)+f(ti+1)2(ti+1−ti).
python
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.0

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.

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.

MethodAssumption within intervalCharacteristics
Left/Right Rectangle MethodFunction value remains constantSimple; monotonicity determines bias direction
Midpoint MethodUses midpoint to represent intervalGenerally more accurate than endpoint rectangles
Trapezoidal MethodAssumes linear change between adjacent pointsWell-suited for existing discrete samples
Simpson's MethodUses local quadratic approximationHigher accuracy for smooth functions
Adaptive IntegrationSubdivides intervals at challenging pointsControls 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:

python
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.000651

Doubling 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 ​

  1. Find the displacement of an object moving with velocity v(t)=3t2 from t=0 to t=2.
  2. Approximate ∫01x2dx using the rectangular method and the trapezoidal method, and compare the errors across different step sizes.
  3. Explain why a uniform distribution on [0,1] assigns probability 0.5 to [0,0.5], yet the probability of any single point is zero.
  4. 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.

Built with VitePress | Software Systems Atlas