Definitionadvanced
Gaspari-Cohn function
Also called Gaspari-Cohn correlation function.
A fifth order piecewise rational function with compact support that is a valid correlation function in up to three dimensions, the standard taper for localization.
The Gaspari-Cohn function is a correlation function of the distance that equals at , falls smoothly like a Gaussian and is exactly zero for , where is the half-width. Gaspari and Cohn (1999) gave it as eq. (4.10): a fifth order piecewise rational function of , positive definite in . Because it has compact support, a matrix of its values is sparse. Because it is positive definite, the Schur product of such a matrix with a sample covariance is again a covariance. This makes it the usual taper of covariance localization in ensemble filters, since Houtekamer and Mitchell (2001) and Hamill, Whitaker and Snyder (2001).
Definition
Let be the half-width and . The Gaspari-Cohn function is
For points and of with , is a correlation function: for any points the matrix is symmetric positive semidefinite with unit diagonal (Gaspari and Cohn 1999). Since and lie in , the same holds in one and two dimensions. The code evaluates it in four lines:
GC(r, c)- 1
z <- |r| / c - 2
if z <= 1 then return -z^5/4 + z^4/2 + 5 z^3/8 - 5 z^2/3 + 1 - 3
if z < 2 then return z^5/12 - z^4/2 + 5 z^3/8 + 5 z^2/3 - 5 z + 4 - 2/(3 z) - 4
return 0
Properties. These follow from the formula by direct computation, and the test file checks them.
- At both pieces have the value , and their first four derivatives agree. At the outer piece and its first three derivatives are zero. As a function of it is twice continuously differentiable; the term has no third derivative at the origin.
- It decreases strictly from to on .
- Near zero, . A Gaussian has the same second derivative at zero when . This is the usual way to match to a Gaussian length scale.
- A matrix of its values on a grid has no nonzero entry for points more than apart.
Examples and non-examples
Values. With : , , , and for . For example at : . The LETKF and covariance localization pages use this value in their worked examples.
A ring of points. On the ring of 40 points of the Lorenz-96 model, the taper matrix is circulant, and its eigenvalues are the discrete Fourier transform of its first row. With the chordal distance , which is the distance in the plane for a circle of circumference , the smallest eigenvalue was positive for every tested ( to ). With the distance along the ring, , it was positive for and negative for each tested (, , , , and ): about for , for and for . For the support is at most half the ring, so the matrix is the periodic sum of the matrix on the integers and is positive semidefinite. For a larger the distance along the ring is not a distance in , and the matrix is not a correlation matrix. The EnSRF measurement with used the distance along the ring, so its taper matrix had a negative eigenvalue; the serial filter does not form the full matrix, and the run still worked.
Non-examples.
- The Gaussian is a correlation function in every dimension, but it has no compact support, so the matrix is dense.
- The triangle is a correlation function on the line but not in two or three dimensions, so on a two-dimensional grid it can give a matrix with negative eigenvalues.
- A Gaussian set to zero beyond a cutoff, or a step that is up to a distance and after it, is not positive definite in general.
- The Gaspari-Cohn function of the great circle distance on a sphere is not guaranteed to be positive definite. With the chordal distance it is, because the sphere lies in .
Related definitions
- Positive definite matrix and covariance matrix: what "valid correlation" means for the matrix of weights.
- Covariance localization: the Schur product of the ensemble covariance with the taper, and the proof that it stays a covariance.
- Random field, Gaussian process and kriging: other uses of correlation functions of distance. A compactly supported one gives sparse matrices there as well.
- Local ensemble transform Kalman filter: the same function used to weight observations instead of covariances.
History
Gaspari and Cohn built compactly supported correlation functions in two and three dimensions in 1999, for the modelling of error covariances in data assimilation. Their function of eq. (4.10) has the shape of a Gaussian and support . Two years later Houtekamer and Mitchell and Hamill, Whitaker and Snyder used such functions to localize the covariances of the ensemble Kalman filter.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/gaspari-cohn-function/. The authors are listed in prov/4P6AFK.jsonl.
"""The Gaspari-Cohn function, eq. (4.10) of Gaspari and Cohn (1999).
gaspari_cohn(r, c) is a correlation function of the distance r with the
half-width c: it is 1 at r = 0, 5/24 at r = c and 0 for r >= 2c. With
fractions.Fraction arguments the result is exact.
taper_matrix(points, c, dist) builds the matrix of the weights for a list of
points and a distance function, for covariance localization. ring_distance
and chordal_ring_distance are two distances on a ring of n equally spaced
points (the grid of the Lorenz-96 model).
"""
import math
def _inner(z):
"""The piece for 0 <= z <= 1."""
return -z ** 5 / 4 + z ** 4 / 2 + 5 * z ** 3 / 8 - 5 * z ** 2 / 3 + 1
def _outer(z):
"""The piece for 1 <= z <= 2."""
return z ** 5 / 12 - z ** 4 / 2 + 5 * z ** 3 / 8 + 5 * z ** 2 / 3 - 5 * z + 4 - 2 / (3 * z)
def gaspari_cohn(r, c):
"""Return the Gaspari-Cohn weight of the distance r for the half-width c > 0."""
z = abs(r) / c # @line L1
if z <= 1: # @line L2
return _inner(z)
if z < 2: # @line L3
return _outer(z)
return 0 * z # @line L4
def taper_matrix(points, c, dist):
"""Return the matrix [gaspari_cohn(dist(p, q), c)] for the points p and q."""
return [[gaspari_cohn(dist(p, q), c) for q in points] for p in points]
def ring_distance(n):
"""Distance along a ring of n points with spacing 1: min(|i - j|, n - |i - j|)."""
def dist(i, j):
k = abs(i - j) % n
return min(k, n - k)
return dist
def chordal_ring_distance(n):
"""Chordal distance on a ring of n points with spacing 1 along the ring.
The ring is the circle of circumference n in the plane, so the chordal
distance is (n / pi) sin(pi |i - j| / n). It is close to ring_distance
for nearby points.
"""
def dist(i, j):
return n / math.pi * math.sin(math.pi * (abs(i - j) % n) / n)
return dist
test_gaspari_cohn_function.py 89 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/gaspari-cohn-function/. The authors are listed in prov/4P6AFK.jsonl.
import math
import random
from fractions import Fraction as F
from gaspari_cohn_function import (
_inner,
_outer,
chordal_ring_distance,
gaspari_cohn,
ring_distance,
taper_matrix,
)
def test_check_values_exact():
c = F(2)
assert gaspari_cohn(F(0), c) == 1
assert gaspari_cohn(F(1), c) == F(263, 384)
assert gaspari_cohn(F(2), c) == F(5, 24)
assert gaspari_cohn(F(3), c) == F(19, 1152)
assert gaspari_cohn(F(4), c) == 0
assert gaspari_cohn(F(9, 2), c) == 0
assert gaspari_cohn(F(-3), c) == F(19, 1152)
def test_pieces_join_smoothly():
one, two = F(1), F(2)
assert _inner(one) == _outer(one) == F(5, 24)
assert _outer(two) == 0
h = 1e-3
for z0, f, g in ((1.0, _inner, _outer), (2.0, _outer, lambda z: 0.0)):
d1f = (f(z0 + h) - f(z0 - h)) / (2 * h)
d1g = (g(z0 + h) - g(z0 - h)) / (2 * h)
d2f = (f(z0 + h) - 2 * f(z0) + f(z0 - h)) / h ** 2
d2g = (g(z0 + h) - 2 * g(z0) + g(z0 - h)) / h ** 2
assert abs(d1f - d1g) < 1e-5 and abs(d2f - d2g) < 1e-4
def test_decreasing_between_zero_and_2c():
vals = [gaspari_cohn(k / 100, 1.0) for k in range(201)]
assert all(b < a for a, b in zip(vals, vals[1:]))
assert all(0 <= v <= 1 for v in vals)
def circulant_eigenvalues(row):
n = len(row)
return [sum(row[k] * math.cos(2 * math.pi * j * k / n) for k in range(n)) for j in range(n)]
def test_ring_of_40_points():
n = 40
for c in (2, 4, 8, 10, 12, 15, 20, 40):
lam = circulant_eigenvalues([gaspari_cohn(chordal_ring_distance(n)(0, k), c) for k in range(n)])
assert min(lam) > 0
for c in (2, 4, 8, 10):
lam = circulant_eigenvalues([gaspari_cohn(ring_distance(n)(0, k), c) for k in range(n)])
assert min(lam) > 0
lam = circulant_eigenvalues([gaspari_cohn(ring_distance(n)(0, k), 15) for k in range(n)])
assert min(lam) < -0.06
def cholesky_ok(a):
n = len(a)
low = [[0.0] * n for _ in range(n)]
for j in range(n):
s = a[j][j] - sum(low[j][k] ** 2 for k in range(j))
if s <= 0:
return False
low[j][j] = math.sqrt(s)
for i in range(j + 1, n):
low[i][j] = (a[i][j] - sum(low[i][k] * low[j][k] for k in range(j))) / low[j][j]
return True
def test_positive_definite_for_points_in_three_dimensions():
rng = random.Random(7)
pts = [tuple(rng.uniform(0, 3) for _ in range(3)) for _ in range(40)]
A = taper_matrix(pts, 1.0, math.dist)
assert all(A[i][i] == 1 for i in range(40))
assert cholesky_ok(A)
def test_gaussian_with_the_same_curvature():
L = math.sqrt(0.3)
for z in (0.05, 0.1):
assert abs(gaspari_cohn(z, 1.0) - math.exp(-z * z / (2 * L * L))) < 2 * z ** 3
References
- G. Gaspari and S. E. Cohn, Construction of correlation functions in two and three dimensions, Quarterly Journal of the Royal Meteorological Society 125(554) (1999) 723-757, eq. (4.10), the compactly supported fifth order function and its positive definiteness in three dimensions
- P. L. Houtekamer and H. L. Mitchell, A sequential ensemble Kalman filter for atmospheric data assimilation, Monthly Weather Review 129(1) (2001) 123-137, the use of a compactly supported correlation function to localize the ensemble covariance
- T. M. Hamill, J. S. Whitaker and C. Snyder, Distance-dependent filtering of background error covariance estimates in an ensemble Kalman filter, Monthly Weather Review 129(11) (2001) 2776-2790, the Schur product of the ensemble covariance with a compactly supported correlation function
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 4P6AFK Edit this page History Report an error Markdown