Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Weighted summation

The column is already a weighted sum

A methane retrieval does not hand out a continuous profile c(p)c(p). It reports the mole fraction on a few model levels p1,…,pNp_1, \ldots, p_{N} and, next to it, a pressure weighting function h1,…,hNh_1, \ldots, h_{N}, so that the column average is

XCH4≈∑n=1Nhnc(pn)\mathrm{XCH_4} \approx \sum_{n=1}^{N} h_n c(p_n)

where hn≥0h_n \ge 0 and ∑nhn=1\sum_n h_n = 1. So the integral of chapter 1, 1ps∫0psc(p)dp\frac{1}{p_\text{s}} \int_0^{p_\text{s}} c(p) \mathrm{d}p, has become a dot product, and the weighting function is the part of the product that holds the numerics. Anyone who compares two retrievals, or a retrieval against a model, has to apply the same hnh_n to both, or the comparison picks up a difference in the weights instead of a difference in methane.

This is the whole idea of the section. Every integral integrix computes ends up as samples of the integrand times weights, summed, and the different modules are different ways of choosing those weights.

Rules: nodes and weights

A rule with NN nodes is a set of points sn∈Ω\vect{s}_n \in \Omega and weights wn∈Rw_n \in \mathbb{R}, n=1,…,Nn = 1, \ldots, N. It approximates (1.1) by

I^N[f]=∑n=1Nwnf(sn)=w⊤f\hat{I}_N[f] = \sum_{n=1}^{N} w_n f(\vect{s}_n) = \vect{w}^\top \vect{f}

where w∈RN\vect{w} \in \mathbb{R}^N holds the weights and f∈RN\vect{f} \in \mathbb{R}^N the samples fn=f(sn)f_n = f(\vect{s}_n). The error of the rule is EN[f]=I[f]−I^N[f]E_N[f] = I[f] - \hat{I}_N[f]. The measure of chapter 1 goes into the weights: a rule for ∫0psc(p)dp/ps\int_0^{p_\text{s}} c(p) \mathrm{d}p / p_\text{s} is a rule for dp\mathrm{d}p divided by psp_\text{s}, which is how the hnh_n of (2.1) come about.

There are only a few ways to choose the weights, and the six modules of integrix use all of them (Table 2.1).

Table 2.1:How each integrix module chooses the weights wnw_n of (2.2), with a geoscience example of each.

ModuleHow the weights are chosenGeoscience example
rulein advance, from the nodes: interpolation, Gauss, or cell areasthe cos⁡ϕ\cos \phi of a latitude band, a pressure weighting function
scanthe same weights, summed up to every nodeoptical depth from the top of the atmosphere to each level
marchthe weights of a Runge–Kutta or Adams method, on samples the stepper computes itselfan ocean model stepping its tracers
adaptafter looking at ff: more nodes where the local error is largea narrow absorption line
expectrandom nodes, with weights 1/N1/N or importance ratiosan ensemble of emission rates
adjointthe same weights, read backwards, since ∂I^N/∂fn=wn\partial \hat{I}_N / \partial f_n = w_nthe sensitivity of a column to each level

The rest of this chapter is about the first row of Table 2.1, because the others are built from it.

Interpolatory weights

The oldest way to get weights is to interpolate the samples and integrate the interpolant. Take NN distinct nodes on an interval and the polynomial πN−1\pi_{N-1} of degree N−1N - 1 through the samples. Integrating it gives a weighted sum of the samples, with weights that depend only on the nodes, and those weights are the ones that integrate every monomial sms^m of degree m≤N−1m \le N - 1 exactly:

∑n=1Nwnsnm=∫Ωsmdμ(s),m=0,…,N−1\sum_{n=1}^{N} w_n s_n^m = \int_\Omega s^m \mathrm{d}\mu(s), \quad m = 0, \ldots, N - 1

These are the moment equations. In matrix form they are Vw=b\mat{V} \vect{w} = \vect{b}, where V∈RN×N\mat{V} \in \mathbb{R}^{N \times N} is the transposed Vandermonde matrix with [V]mn=snm[\mat{V}]_{mn} = s_n^m, and b∈RN\vect{b} \in \mathbb{R}^N holds the moments bm=∫Ωsmdμ(s)b_m = \int_\Omega s^m \mathrm{d}\mu(s). On [0,1][0, 1] with the ordinary length, bm=1/(m+1)b_m = 1/(m + 1).

We solve them below for equally spaced nodes on [0,1][0, 1] (the Newton–Cotes rules). With three nodes we get Simpson’s rule, and with eleven we get something we should not use.

import jax

jax.config.update("jax_enable_x64", True)

import einx
import jax.numpy as jnp
import lineax as lx
from jaxtyping import Array, Float

# Shapes: N nodes on [0, 1]


# user
def newton_cotes_weights(
    n_nodes: int,
) -> tuple[Float[Array, "node"], Float[Array, "node"]]:
    """Solve the moment equations V w = b for N equally spaced nodes."""
    # sₙ = (n − 1)/(N − 1)
    nodes = jnp.linspace(0.0, 1.0, n_nodes)             # → (N,)
    # Aₙₘ = sₙᵐ
    vandermonde = jnp.vander(nodes, increasing=True)    # (N,) → (N, N)
    # Vₘₙ = Aₙₘ
    moment_matrix = einx.id("n m -> m n", vandermonde)  # (N, N) → (N, N)
    # m + 1 for m = 0, …, N − 1
    exponents = jnp.arange(1.0, n_nodes + 1.0)          # → (N,)
    # bₘ = ∫₀¹ sᵐ ds = 1/(m + 1)
    moments = 1.0 / exponents                           # (N,) → (N,)
    # today (lineax)
    # V as a linear operator
    operator = lx.MatrixLinearOperator(moment_matrix)   # (N, N) → (N, N) op
    # today (lineax)
    # w = V⁻¹ b, by a solve
    solution = lx.linear_solve(operator, moments)       # (N,) → (N,)
    return nodes, solution.value


# the Simpson and Newton–Cotes weights, from the solves
simpson_nodes, simpson = newton_cotes_weights(3)
print("N = 3: ", jnp.round(simpson * 6.0, 10).tolist(), "/ 6")
eleven_nodes, eleven = newton_cotes_weights(11)
print(f"N = 11: min w = {float(eleven.min()):.3f}")
print(f"N = 11: sum |w| = {float(jnp.abs(eleven).sum()):.3f}")
N = 3:  [1.0, 4.0, 1.0] / 6
N = 11: min w = -0.435
N = 11: sum |w| = 3.065

Exactness

A rule has degree of exactness m⋆m^\star when it integrates every polynomial of degree m≤m⋆m \le m^\star exactly and fails for some polynomial of degree m⋆+1m^\star + 1. The moment equations give an interpolatory rule with NN nodes a degree of at least N−1N - 1, and sometimes one more for free: Simpson’s three nodes are symmetric, so the odd moment s3s^3 about the midpoint also cancels and m⋆=3m^\star = 3.

from integrix import rule

# Shapes: N = 3 nodes of Simpson's rule

for degree in (2, 3, 4):
    # fₙ = sₙᵐ
    powers = simpson_nodes**degree                    # (N,) → (N,)
    # today (integrix)
    # Î = ∑ₙ wₙ sₙᵐ
    estimate = rule.integrate(simpson, powers).value  # → ()
    # I = ∫₀¹ sᵐ ds = 1/(m + 1)
    exact = 1.0 / (degree + 1)                        # → ()
    # E = Î − I
    error = float(estimate) - exact                   # → ()
    # round-off below 10⁻¹² counts as exact
    verdict = "exact" if abs(error) < 1e-12 else f"error = {error:.2e}"
    print(f"m = {degree}: {verdict}")
m = 2: exact
m = 3: exact
m = 4: error = 8.33e-03

Exactness is the language in which rules are compared. With NN nodes fixed in advance, N−1N - 1 is what the moment equations can promise. If we are also free to place the nodes, we have 2N2N unknowns and can match 2N2N moments, and that is the Gauss rule, with m⋆=2N−1m^\star = 2N - 1 (chapter 3). A high degree is only useful when the integrand looks like a polynomial on the whole domain, though, and this is where the global and local thread starts.

Positive weights and noise

The eleven-node weights above sum to one, as they must, since they integrate the constant exactly. However, several of them are negative and the sum of their absolute values is about 3. That number matters for real data, because samples are never exact. If each sample carries an error ∣εn∣≤εmax|\varepsilon_n| \le \varepsilon_\text{max} (instrument noise, a retrieval error, round-off), the rule passes it on as

∣∑n=1Nwn(fn+εn)−∑n=1Nwnfn∣≤εmax∑n=1N∣wn∣\Big\vert \sum_{n=1}^{N} w_n (f_n + \varepsilon_n) - \sum_{n=1}^{N} w_n f_n \Big\vert \le \varepsilon_\text{max} \sum_{n=1}^{N} \vert w_n \vert

so ∑n∣wn∣\sum_n |w_n| is the amplification factor of the rule. With positive weights it equals ∑nwn=μ(Ω)\sum_n w_n = \mu(\Omega), the size of the domain, which is the smallest it can be. With negative weights it grows, and for equally spaced nodes it grows without bound as NN increases (the Runge phenomenon, seen through the weights). Negative weights also break the positivity of chapter 1: a positive methane profile can come out with a negative column.

So we have three practical rules for weights, which the later chapters keep returning to.

  1. Prefer positive weights. Gauss, Clenshaw–Curtis, the composite trapezoid and Simpson rules, cell areas and Monte Carlo all have them.

  2. Never raise the degree by adding equally spaced nodes to one polynomial. Either move the nodes (Gauss, chapter 3) or cut the domain into pieces and use a low-degree rule on each (composite rules, chapter 3).

  3. Keep the weights next to the data they came from. A pressure weighting function, a cell-area grid or a spectral response is part of the dataset, and integrix takes it as a plain array for exactly that reason.