Skip to content
QFT.org

Search QFT.org

Enter a word or phrase.

    You can also browse the main navigation and chapter contents.

    Adaptive Quadrature and Numerical Precision

    A numerical loop integral can return an error estimate smaller than the requested tolerance and still be wrong. One reason is that the sampling nodes never encounter a narrow feature. Another is that the integral is accurate but the final subtraction destroys its useful digits. These failures require different repairs.

    We develop an adaptive rule whose decisions can be inspected, then evaluate a narrow Gaussian and a small, subtracted bubble remainder. The examples separate integration error, floating-point error, and the uncertainty in an error estimate. The setup needs one-variable integration and a first loop integral; the numerical argument is self-contained.

    What an adaptive error indicator measures

    On an interval I=[a,b]I=[a,b], map the nn-point Gauss–Legendre rule from [−1,1][-1,1]:

    Qn[I]=b−a2∑j=1nwjf ⁣(a+b2+b−a2xj).Q_n[I] =\frac{b-a}{2}\sum_{j=1}^{n}w_j f\!\left(\frac{a+b}{2}+\frac{b-a}{2}x_j\right).

    Its weights and nodes integrate every polynomial of degree at most 2n−12n-1 exactly. Our local approximation is Q5Q_5, with indicator

    eI=∣Q5[I]−Q3[I]∣.e_I=\left|Q_5[I]-Q_3[I]\right|.

    This pair uses eight evaluations: the implementation evaluates the two rules separately. It is a small teaching implementation, rather than an embedded Gauss–Kronrod rule or a reimplementation of QUADPACK. If both rules resolve a smooth function, their difference often detects where more resolution is useful. It is not a rigorous bound for an arbitrary continuous function.

    For a partition P\mathcal P, form

    Q=∑I∈PQ5[I],E=∑I∈PeI,τ=max⁡(τabs,τrel∣Q∣).Q=\sum_{I\in\mathcal P}Q_5[I], \qquad E=\sum_{I\in\mathcal P}e_I, \qquad \tau=\max(\tau_{\rm abs},\tau_{\rm rel}|Q|).

    The algorithm is concrete:

    1. Evaluate both rules on each initial interval.
    2. If E≤τE\leq\tau, report that the indicator meets the target.
    3. Otherwise remove the interval with the largest eIe_I, bisect it, and evaluate the two child intervals.
    4. Replace its contributions to QQ and EE, then repeat. Stop with a distinct failure status if the evaluation budget is exhausted.

    Each bisection costs sixteen new evaluations. A priority queue selects the next interval; accurate summation of the retained contributions reduces accumulation error. An absolute tolerance is indispensable when the integral is zero or close to zero.

    The production library scipy.integrate.quad also supplies an absolute error estimate, optional known breakpoints, and evaluation counts, but uses its own QUADPACK algorithms. Its tolerance request cannot guarantee discovery of an unseen sharp feature. See the tested SciPy 1.13.1 documentation, parameters epsabs, epsrel, points, and the final pulse example.

    A peak that no initial node sees

    Consider

    f(x)=1πσexp⁡ ⁣[−(x−μ)2σ2],0≤x≤1,μ=0.37,σ=0.002.f(x)=\frac{1}{\sqrt{\pi}\sigma} \exp\!\left[-\frac{(x-\mu)^2}{\sigma^2}\right], \qquad 0\leq x\leq1, \qquad \mu=0.37,\quad \sigma=0.002.

    Its exact integral is

    I=12[erf⁡ ⁣(1−μσ)+erf⁡ ⁣(μσ)],I=\frac12\left[ \operatorname{erf}\!\left(\frac{1-\mu}{\sigma}\right) +\operatorname{erf}\!\left(\frac{\mu}{\sigma}\right) \right],

    indistinguishable from 11 at binary64 precision. With the single initial interval [0,1][0,1], every node of our pair is far from the peak. All eight function evaluations underflow to zero. The result and indicator are both zero; the requested absolute tolerance 10−1010^{-10} is falsely reassuring.

    Known structure changes the problem. Introduce breakpoints at μ−8σ\mu-8\sigma, μ\mu, and μ+8σ\mu+8\sigma. Alternatively set

    t=x−μσ,f(x) dx=e−t2π dt.t=\frac{x-\mu}{\sigma}, \qquad f(x)\,dx=\frac{e^{-t^2}}{\sqrt{\pi}}\,dt.

    Integrate the central interval [−8,8][-8,8], split at zero. The discarded part of the original domain is bounded by the larger full Gaussian tail erfc⁡(8)<1.13×10−29\operatorname{erfc}(8)<1.13\times10^{-29}. This analytic tail bound is separate from the quadrature indicator.

    MethodIntegralIndicatorEvaluations
    G3/G5, no breakpoints000088
    G3/G5, known peak breakpoints1.00000000000000021.00000000000000029.67×10−119.67\times10^{-11}12801280
    G3/G5, rescaled central interval0.99999999999999930.99999999999999939.67×10−119.67\times10^{-11}12641264
    SciPy quad, no breakpoints1.73×10−141.73\times10^{-14}3.23×10−143.23\times10^{-14}6363

    The last row is a retained failure on this declared input, not a general comparison of integrator quality. The repaired calculations use information about the feature that the blind calls lack. Merely tightening a tolerance cannot force discovery when current observations suggest an identically zero function.

    A fixed-energy detector bin uses exactly this Gaussian to measure the vacuum line of a free scalar. Changing the lattice spacing then moves the center of the peak. An accurately integrated line can give a biased continuum bin weight even after the breakpoints have fixed the integration error.

    A precise integral followed by an imprecise subtraction

    Use the dimensionless Euclidean variable q=Q2/m2>0q=Q^2/m^2>0 and define

    J(q)=−F(−q)=∫01du log⁡[1+q u(1−u)].J(q)=-F(-q)=\int_0^1du\,\log[1+q\,u(1-u)].

    Here F(z)=−∫01du log⁡[1−z u(1−u)]F(z)=-\int_0^1du\,\log[1-z\,u(1-u)] is the chapter's subtracted massive bubble. There is no branch ambiguity on this Euclidean interval. Let h(u)=u(1−u)h(u)=u(1-u) and subtract its first Taylor term:

    R(q)=q6−J(q)=∫01du [qh(u)−log⁡(1+qh(u))].R(q)=\frac q6-J(q) =\int_0^1du\,\bigl[qh(u)-\log(1+qh(u))\bigr].

    The two subtracted terms are of order qq, whereas R(q)R(q) is of order q2q^2. Their subtraction condition number is

    κsub=∣q/6∣+∣J(q)∣∣R(q)∣∼20q.\kappa_{\rm sub} =\frac{|q/6|+|J(q)|}{|R(q)|} \sim\frac{20}{q}.

    In binary64, unit roundoff is 2−53≃1.11×10−162^{-53}\simeq1.11\times10^{-16}. Many relative digits in RR can therefore be lost even when both larger terms have small relative errors. The scale κsub2−53\kappa_{\rm sub}2^{-53} describes amplification of small input errors, not the exact rounding error in every run.

    Using log1p(x) instead of log(1+x) avoids rounding 1+x1+x to 11 too early. It does not itself stabilize the subsequent subtraction x−log⁡(1+x)x-\log(1+x). For 0≤x≤1/40\leq x\leq1/4, evaluate the remainder directly:

    x−log⁡(1+x)=∑n=2p(−1)nxnn+rp(x),∣rp(x)∣≤xp+1p+1.x-\log(1+x) =\sum_{n=2}^{p}\frac{(-1)^n x^n}{n} +r_p(x), \qquad |r_p(x)|\leq\frac{x^{p+1}}{p+1}.

    The code uses p=32p=32 and Horner evaluation. Factoring out x2x^2 preserves the small scale instead of creating it by subtracting two numbers of order xx. Series truncation has its own analytic bound.

    The following results compare separate integration and subtraction with the stable remainder integrand. References use exact decimal inputs and independent 80- and 120-digit integration; the binary64 computation uses the corresponding rounded inputs.

    qqReference R(q)R(q)Relative error, separate termsRelative error, stable integrand
    111.4621961847×10−21.4621961847\times10^{-2}5.6×10−165.6\times10^{-16}2.2×10−162.2\times10^{-16}
    10−410^{-4}1.6666428575×10−101.6666428575\times10^{-10}1.0×10−111.0\times10^{-11}00 at displayed precision
    10−810^{-8}1.6666666643×10−181.6666666643\times10^{-18}6.8×10−86.8\times10^{-8}1.1×10−161.1\times10^{-16}
    10−1210^{-12}1.6666666667×10−261.6666666667\times10^{-26}1.2×10−31.2\times10^{-3}2.2×10−162.2\times10^{-16}

    Directly integrating the unstable pointwise difference also has a cost: at q=10−8q=10^{-8} and 10−1210^{-12}, the adaptive rule exhausts its 99,992 usable evaluations while trying to resolve rounding fluctuations. The stable integrand meets the same requested tolerance with eight evaluations in these two cases. The nearly polynomial integrand becomes easy to integrate once it is represented accurately.

    The narrow Gaussian and its refined intervals, alongside relative errors from separate subtraction and stable remainder integration.

    The left panel resolves the peak region that all initial nodes missed. The right panel compares with the decimal-input reference for R(q)R(q); plotted errors have a display floor at 10−1710^{-17}. Subdivision and stable arithmetic address different failures.

    Reproduction and interpretation

    Run python3 examples/computational-qft/numerical-integration/adaptive_quadrature.py from the source distribution to regenerate quadrature-results.json. The companion checks polynomial exactness, interval splitting, budget termination, the Gaussian tail, and high-precision remainder references. Its largest polynomial-rule discrepancy is 4.5×10−164.5\times10^{-16}. All four stable remainders pass a relative-error check, including the 10−2610^{-26} result.

    The output preserves every final interval and indicator, revealing where evaluations were spent. It does not certify a bound between the nodes. For a loop calculation, first expose known singularities or narrow scales, then choose a quadrature method and a stable expression. Endpoint subtraction and contour deformation treat analytic structure that this smooth Euclidean example avoids.

    Exercises

    1. Derive the small remainder and its truncation bound

    Show that R(q)=q2/60−q3/420+O(q4)R(q)=q^2/60-q^3/420+O(q^4). For 0≤q≤10\leq q\leq1, bound the error from truncating the integrand series after degree p≥2p\geq2.

    Solution

    Since 0≤qh≤q/40\leq qh\leq q/4, the alternating logarithm series converges uniformly in this range and can be integrated term by term. The beta integral gives

    ∫01[u(1−u)]n du=B(n+1,n+1)=(n!)2(2n+1)!.\int_0^1[u(1-u)]^n\,du =B(n+1,n+1)=\frac{(n!)^2}{(2n+1)!}.

    Therefore

    R(q)=∑n=2∞(−1)nqn(n!)2n(2n+1)!.R(q)=\sum_{n=2}^{\infty} \frac{(-1)^n q^n(n!)^2}{n(2n+1)!}.

    The first coefficients are 4/(2⋅120)=1/604/(2\cdot120)=1/60 and −36/(3⋅5040)=−1/420-36/(3\cdot5040)=-1/420. Integrating the pointwise alternating remainder bound yields

    ∣ΔRp∣≤qp+1p+1[(p+1)!]2(2p+3)!≤(q/4)p+1p+1.|\Delta R_p| \leq\frac{q^{p+1}}{p+1} \frac{[(p+1)!]^2}{(2p+3)!} \leq\frac{(q/4)^{p+1}}{p+1}.

    The latter is the simple uniform bound retained with the computation. Neither bound controls floating-point rounding or quadrature.

    2. Can finite sampled values certify every smooth integrand?

    Suppose an algorithm samples finitely many points and observes zero at each. Construct a smooth nonnegative function with the same observations and integral 11. What additional information would permit a useful deterministic error bound?

    Solution

    The finite set leaves an open interval (a,b)(a,b) without sampled points. Let c=(a+b)/2c=(a+b)/2 and choose 0<r<(b−a)/20<r<(b-a)/2. Set

    br(x)={exp⁡ ⁣[−11−((x−c)/r)2],∣x−c∣<r,0,∣x−c∣≥r.b_r(x)= \begin{cases} \exp\!\left[-\dfrac{1}{1-((x-c)/r)^2}\right], & |x-c|<r,\\ 0,& |x-c|\geq r. \end{cases}

    This function and all its derivatives vanish at the support boundary. Divide it by its positive integral. The resulting smooth function has integral 11 and vanishes at every sampled node, exactly as the zero function does.

    The observations cannot distinguish these functions. A bound needs further assumptions: for example, a known derivative bound, analyticity in a specified complex neighborhood, or known locations and widths for sharp features. Such information can enter an analytic remainder estimate or a guaranteed mesh resolution. The G3/G5 difference alone supplies none of it.

    Summary

    Adaptive subdivision allocates evaluations according to observed structure. It cannot infer a feature that all its nodes miss, or restore digits already lost inside an integrand or final subtraction. A useful calculation combines analytic information, stable variable changes and arithmetic, an explicit stopping status, and independently controlled reference answers.

    References

    • SciPy developers, SciPy 1.13.1 Manual, scipy.integrate.quad, tolerance and breakpoint parameters, QUADPACK notes, and the final sharp-pulse example. The companion's G3/G5 implementation and retained tests are original teaching calculations.

    Scrollable formula. Use Left and Right arrow keys to reveal hidden content.

    Scrollable table. Use Left and Right arrow keys to reveal hidden columns.

    Scrollable figure. Use Left and Right arrow keys to reveal hidden content.