geossm.covmodel.SPDEApproxCov

class geossm.covmodel.SPDEApproxCov(latlon=True, geo_scale=57.29577951308232, nu=1, var=1.0, rescale=1.0, verbose=False)[source]

Bases: _Verbose, Matern

The SPDE approximation of the Matérn covariance model.

See [Rasmussen2003] for background on the Matérn family of covariance functions in the Gaussian-process setting that this class approximates. Solves the SPDE \((\kappa^2 - \Delta)^{\alpha/2} x = W\) by a finite element (FEM) discretisation with Neumann boundary conditions on a mesh covering the domain (see setup), giving the sparse precision matrix precision() – optionally marginalised onto the “inner” vertices of domain via precision(marginal=True), which is what other models (e.g. LRStateSpaceModel) should generally use.

Notes

SPDEApproxCov extends gstools.covmodel.Matern, but only precision() (and emp_range, sigma2k, distance()) are re-defined in terms of the FEM discretisation. The methods inherited unchanged from Matern – variogram, covariance, correlation, spectrum, spectral_density, and the *_yadrenko variants – describe the nominal, stationary, infinite-domain Matérn process that this SPDE approximates, using rescale as \(\kappa\). precision() is the \(\nu = 1\) (\(\alpha = 2\) in two dimensions), unit-variance SPDE, so only nu=1 and var=1 are accepted: with them the inherited methods describe the same field (changing nu or var afterwards, e.g. with the inherited fit_variogram, is not supported). They are exact only in the far interior of the mesh, away from the boundary; they do not reflect the finite mesh’s boundary effects, nor the Schur-complement marginalisation used by precision(marginal=True). Use them for diagnostics/plotting against the nominal target kernel, not as the exact model – for that, use precision()/precision(marginal=True).

References

[Rasmussen2003]

Rasmussen, C. E., “Gaussian processes in machine learning.” Summer school on machine learning. Springer, Berlin, Heidelberg, (2003)

Methods

covers_domain

Check that the mesh (set via setup()) covers domain_hull.

distance

Pairwise distance between mesh vertices (or points and mesh vertices).

fem_arrays

Finite-element inputs of the SPDE precision.

generate_summary

Build the (label, value) rows shown by summary, as two lists of (str, [str]) pairs (left and right columns); includes the fem_solver's own rows once setup() has been called.

latent_basis

FEM basis functions at points, restricted to the latent (inner) nodes and normalised so that each row sums to 1.

precision

Compute the precision matrix (Q) for the spatial process z with marginal variance sigma2_process.

setup

Initialize the covariance model with a mesh.

summary

Return a statsmodels.iolib.summary.Summary table of the model (kappa/range/variance/nu) and, once setup() has been called, the underlying fem_solver's mesh diagnostics; also backs __str__.

Attributes

box

Mesh bounding box [minx, miny, maxx, maxy] (see FEMSolver.box).

domain

Region(s) of scientific interest (see _validate_domain).

domain_hull

Convex hull of the union of domain -- a reasonable default choice of FEM computational domain Ω, e.g. to pass as domain to buildMesh2d before calling setup().

emp_range

Practical spatial correlation range of the Matérn field, \(\sqrt{8 \nu} / \kappa\), i.e. the same rescale-based range formula used by the parent Matern model, expressed in the distance units of rescale/geo_scale.

fem_solver

The FEMSolver built by setup from mesh_io/domain.

mesh_io

The meshio.Mesh passed to setup.

n_latent

Number of latent nodes (inner mesh vertices), i.e. the size of this covariance function's block of the latent state.

sigma2k

Marginal variance of the standardized SPDE approximation, i.e. the variance of the approximate field x(u) obtained from rescale (\(\kappa\)) alone.

property mesh_io

The meshio.Mesh passed to setup.

Raises:

RuntimeError – If setup hasn’t been called yet.

property fem_solver

The FEMSolver built by setup from mesh_io/domain.

Raises:

RuntimeError – If setup hasn’t been called yet.

property n_latent

Number of latent nodes (inner mesh vertices), i.e. the size of this covariance function’s block of the latent state.

Type:

int

property box

Mesh bounding box [minx, miny, maxx, maxy] (see FEMSolver.box).

latent_basis(points)[source]

FEM basis functions at points, restricted to the latent (inner) nodes and normalised so that each row sums to 1.

Parameters:

points ((n, 2) array_like) – Sites in the mesh coordinates.

Returns:

scipy.sparse.csr_matrix, shape (n, n_latent) – Row i maps the latent field to site i. A row is all zeros if the site lies in no mesh element, or only in elements whose nodes are all outer (see FEMSolver.eval_basis).

fem_arrays()[source]

Finite-element inputs of the SPDE precision.

Returns:

  • mass, stiff (scipy.sparse matrix, shape (n_nodes, n_nodes)) – Lumped mass matrix C and stiffness matrix G over all mesh nodes.

  • inner (ndarray of bool, shape (n_nodes,)) – True for the latent (inner) nodes.

setup(mesh_obj, domain=None, stats=False)[source]

Initialize the covariance model with a mesh.

This method must be called after instantiation to set up the FEM discretization.

Parameters:
  • mesh_obj (meshio.Mesh) – A meshio.Mesh object (already loaded in memory)

  • domain (shapely.geometry.Polygon or MultiPolygon, or a list/tuple) – of them, optional Region(s) of scientific interest, e.g. one polygon (or a single MultiPolygon) per landmass for a multi-polygon country (Sicily, Sardinia, mainland Italy). Any MultiPolygon is expanded into its constituent polygons. Forwarded to FEMSolver, where it is used only to classify mesh vertices as inner/outer – see _validate_domain for the full contract, including how it differs from the FEM computational domain \(\Omega\) (implicitly given by mesh_obj; see domain_hull for a reasonable default choice used to build it). If omitted, FEMSolver falls back to the convex hull of the mesh’s own vertices, i.e. every vertex is treated as inner.

  • stats (bool, default False) – Compute and log the mesh quality statistics at setup (see FEMSolver); otherwise they are computed on first access.

Returns:

self – Returns self for method chaining

Raises:
  • ValueError – If domain is invalid (see _validate_domain)

  • IOError – If meshpath file cannot be read

  • RuntimeError – If mesh is invalid or FEM setup fails

Examples

>>> mesh = meshio.read("my_mesh.msh")
>>> cov.setup(mesh_obj=mesh)
precision(rescale=None, marginal=False)[source]

Compute the precision matrix (Q) for the spatial process z with marginal variance sigma2_process.

Parameters:
  • rescale (float, optional) – SPDE kappa to compute the precision matrix with (default: self.rescale). self.rescale is not changed.

  • marginal (bool, optional) – If True, return the precision matrix marginalised (via the Schur complement, see _schur_marginal_precision) onto the domain inner vertices only, integrating out the outer (boundary) vertices – i.e. the actual precision of the latent field once the mesh’s boundary effects have been removed, as used e.g. by LRStateSpaceModel. Default: False, i.e. the full FEM precision matrix over every mesh vertex (inner and outer).

Returns:

scipy.sparse matrix if `marginal=False`, else a dense numpy array – (the Schur complement is generally dense even though Q is sparse).

distance(points=None)[source]

Pairwise distance between mesh vertices (or points and mesh vertices).

Honors latlon/geo_scale the same way the parent Matern model does: great-circle distance (in geo_scale units) when latlon=True, flat Euclidean distance on the raw mesh/point coordinates otherwise.

Note this only affects distance-based diagnostics (e.g. comparing against variogram/covariance, see the class Notes) – the FEM stiffness/mass matrices behind precision() are always assembled directly on the raw mesh coordinates with a flat metric, regardless of latlon. A raw lon/lat mesh under latlon=True therefore still carries anisotropic distortion (up to ~cos(latitude)) in the FEM operator that this distance correction does not fix; for a geodesically accurate SPDE solve, build the mesh in a projected (e.g. UTM/km) CRS instead.

Parameters:

points ((p, 2) array_like, optional) – Query points (lon, lat when latlon=True). Defaults to the mesh vertices.

Returns:

ndarray, shape (nvertex, nvertex) or (p, nvertex) – Great-circle distance in geo_scale units if latlon, else Euclidean distance in mesh units.

property emp_range

Practical spatial correlation range of the Matérn field, \(\sqrt{8 \nu} / \kappa\), i.e. the same rescale-based range formula used by the parent Matern model, expressed in the distance units of rescale/geo_scale.

property sigma2k

Marginal variance of the standardized SPDE approximation, i.e. the variance of the approximate field x(u) obtained from rescale (\(\kappa\)) alone. Used by _compute_precision_spde as the scalar multiplier of the precision matrix.

property domain

Region(s) of scientific interest (see _validate_domain). Delegates to fem_solver.domain, so it is only available once setup() has been called – domain is now a setup()-only argument, since nothing reads it before the mesh exists.

property domain_hull

Convex hull of the union of domain – a reasonable default choice of FEM computational domain Ω, e.g. to pass as domain to buildMesh2d before calling setup(). See _domain_hull.

Raises:

RuntimeError – If setup() hasn’t been called yet – domain is unknown until then.

covers_domain()[source]

Check that the mesh (set via setup()) covers domain_hull. See FEMSolver.covers_domain.

Returns:

bool

generate_summary()[source]

Build the (label, value) rows shown by summary, as two lists of (str, [str]) pairs (left and right columns); includes the fem_solver’s own rows once setup() has been called.

Returns:

gen_top_left, gen_top_right (list of (str, list of str))

summary()[source]

Return a statsmodels.iolib.summary.Summary table of the model (kappa/range/variance/nu) and, once setup() has been called, the underlying fem_solver’s mesh diagnostics; also backs __str__.

Returns:

statsmodels.iolib.summary.Summary