Geostatistics with GeoLattice tutorial banner

Geostatistics predicts a continuous surface from a limited number of samples and says how uncertain each prediction is. Soil carbon from a few thousand cores, arsenic in wells, air pollution from a handful of monitors: the variogram measures how quickly similarity fades with distance, kriging turns that into the best linear prediction at unsampled places, and simulation produces many equally likely maps when a single smooth estimate is not enough to judge risk.

These 55 notebooks are chapter 05 of the GeoLattice tutorials. They cover variograms, the kriging family, regression and space-time kriging, Poisson kriging of rates, geostatistical simulation and Bayesian kriging, and finish with eight capstone projects, using geolattice.geostatistics with real data from Switzerland (Jura), the United States and Bangladesh.

GeoLattice

GeoLattice is a Python library for geospatial analysis that brings vector and raster processing, spatial analysis, spatial statistics, geostatistics, spatial databases, web and cloud GIS and remote sensing together under one import. It is built on GDAL, NumPy, pandas, GeoPandas, Shapely and xarray, so its functions take and return objects these libraries already use: GeoDataFrames, NumPy arrays and GDAL-readable files.

Package geolattice
Repository github.com/zia207/GeoLattice
Python 3.10 or newer
License MIT
Author Zia Ahmed, Upatta Analytics

GDAL and PROJ must be installed first, with Python GDAL bindings that match the GDAL library (conda or your system package manager). Then install the geostatistics option. The deep-learning regression kriging notebooks use PyTorch, which geolattice[all] includes; the H2O and TensorFlow notebooks need the geostatistics-h2o and geostatistics-tensorflow extras.

python3 -m pip install "geolattice[geostats]"

Every name in the chapter is available from one import:

import geolattice.geostatistics as gstat

Variograms and kriging

A variogram plots half the average squared difference between pairs of samples against the distance between them. It rises from the nugget (measurement error and very short-range variation) to the sill (the total variance) at the range, beyond which samples are unrelated. Kriging uses the fitted model to weight the samples around each target so that the prediction is unbiased and its error variance is as small as possible, and the kriging variance maps that error.

Task Functions (gstat)
Variograms Variogram, VariogramCloud, fit_nested_variogram, transform_aniso, JaxVariogram
Variogram models spherical, exponential, gaussian, matern, stable, cubic
Kriging SimpleKriging, OrdinaryKriging, UniversalKriging, kt3d, JaxOrdinaryKriging
Several variables Cokriging, CrossVariogram, fit_lmc, MultivariateCokriging, ColocatedCokriging
Validation loo_cv, kfold_cv
Data and transforms from_geodataframe, make_prediction_grid, nscore_forward, nscore_back, transform_boxcox, declus_cell

Ordinary kriging of log cadmium on the Jura data, run from the repository root:

import numpy as np
import pandas as pd
import geolattice.geostatistics as gstat

DATA = "Data/00_shared/data_geostatistics"
jura = pd.read_csv(f"{DATA}/jura_data.csv")                         # 359 soil samples
grid = pd.read_csv(f"{DATA}/jura_grid.csv")                         # prediction nodes
coords, cd = jura[["Xloc", "Yloc"]].to_numpy(), np.log(jura["Cd"].to_numpy())

vg = gstat.Variogram(coords, cd, model="spherical")                 # empirical + fitted model
vg.fit()
ok = gstat.OrdinaryKriging(vg).fit(coords, cd)
pred, var = ok.predict(grid[["Xloc", "Yloc"]].to_numpy(), return_variance=True)

cv = gstat.loo_cv(gstat.OrdinaryKriging(vg), coords, cd)            # leave-one-out check
print(f"LOO RMSE {cv['rmse']:.3f}, R2 {cv['r2']:.3f}")

Regression and specialized kriging

When covariates explain part of the variation, regression kriging fits a trend model (from linear regression to random forests and neural networks) and krigs what the trend leaves behind. Specialized kriging handles questions ordinary kriging cannot: the probability of exceeding a threshold (indicator kriging), three-dimensional data, soft information, strongly skewed variables (disjunctive kriging) and separating the scales of a nested variogram (factorial kriging).

Task Functions (gstat)
Regression kriging RegressionKriging, GAMRegressor, RegressionKrigingH2O, DeepRegressionKriging, DeepRegressionKrigingTF
Graph neural networks GNNRegressionKriging, KCN, IGNNK
Indicator kriging IndicatorKriging, fit_indicator_variogram, compute_etype_from_probabilities
Other estimators DisjunctiveKriging, SoftKriging, markov_bayes_calibrate, FactorialKriging
Cross-validation LOOCV_RK, KFoldCV_RK, IndicatorKrigingCV

Space-time and Poisson kriging

Monitoring networks measure the same places repeatedly, so a space-time variogram describes how similarity fades with both distance and time lag. Poisson kriging is built for rates, such as disease incidence in counties, where small populations make raw rates unreliable: it weights each area by its population and can predict for areas or for points inside them.

Task Functions (gstat)
Space-time variograms empirical_st_variogram, fit_st_variogram, vgm_st
Space-time kriging STKriging, krige_st
Space-time deep learning CNNLSTMRegressionKriging, STGNNRegressionKriging, TGCN, DCRNN, STGTNRegressionKriging, STTN, GMAN
Poisson kriging PoissonKriging, AreaPoissonKriging, PointSupport, poisson_cokrige, fit_poisson_lmc, STPoissonKriging

Simulation and Bayesian kriging

Kriging maps are smoother than reality. Simulation draws many maps that honour the data, the histogram and the variogram, so that uncertainty about areas, volumes or connected paths can be counted directly. Bayesian kriging carries the uncertainty about the variogram itself into the predictions.

Task Functions (gstat)
Gaussian and indicator simulation sgsim, sgsim_gpu, sisim, sgcosim, sicosim, dssim_direct
Other continuous methods lusim, tbsim, fftsim, sasim
Categorical and multiple-point pgsim, PluriGaussianSimulator, dssim, DirectSampling, snesim, SNESIM, tau_model
Bayesian kriging EmpiricalBayesianKriging, BayesianKriging, bayesian_kriging, ConjugateBayesianKriging
Bayesian transforms and counts BayesianTransformedKriging, BayesianGLMKriging, correlation_function
GSLIB files read_gslib, write_gslib, read_gslib_grid

Tutorials

Start with the foundation notebooks and ordinary kriging, since the variogram and the kriging system introduced there are used everywhere else. The other topics can be read in any order, and the capstones combine them.

Foundation

Tutorial Topic
Introduction to Geostatistics Kriging, variogram types and the GeoLattice geostatistics module
Variogram Modeling Robust estimators, isotropic and anisotropic models, CPU vs GPU

Kriging

Tutorial Topic
Kriging How kriging works and the four classic estimators
Simple Kriging Kriging with a known mean on the Jura data
Ordinary Kriging Kriging with an unknown mean, validation and maps
Universal Kriging Polynomial trends and the residual variogram
Cokriging Full and colocated cokriging with a linear model of coregionalization
Kriging with JAX: Great Plains SOC Four kriging methods with JAX on 5,000 soil carbon samples

Regression Kriging

Tutorial Topic
Regression Kriging Trend plus kriged residuals, external drift
Regression Kriging: scikit-learn Linear, ensemble and GAM trends for soil carbon
Regression Kriging: H2O H2O AutoML and boosted trends
Regression Kriging: PyTorch Deep neural network trends in PyTorch
Regression Kriging: TensorFlow Deep neural network trends in TensorFlow
Regression Kriging: Graph Neural Networks Graph neural network trends

Specialized Kriging

Tutorial Topic
Specialized Kriging Indicator, 3-D, soft, disjunctive and factorial kriging
Indicator Kriging Exceedance probabilities, conditional CDFs, E-type estimates
3-D Kriging (KT3D) GSLIB kt3d: 3-D search, anisotropy and block kriging
Soft Kriging Hard and soft data with Markov-Bayes calibration
Disjunctive Kriging Gaussian anamorphosis and Hermite polynomials
Factorial Kriging Separating the structures of a nested variogram

Space-Time Kriging

Tutorial Topic
Spatio-Temporal Kriging Neural trends with kriged space-time residuals
Space-Time Kriging Space-time variograms and kriging of California PM2.5
ConvLSTM Regression Kriging Rasterised sensor network with a ConvLSTM
Space-Time GNN Kriging TGCN and DCRNN on a sensor graph
Space-Time Graph Transformer Kriging STTN and GMAN graph transformers

Poisson Kriging

Tutorial Topic
Poisson Kriging Kriging of rates from small populations
Area-to-Area and Area-to-Point Centroid, area-to-area and area-to-point estimators
Poisson Cokriging Several diseases with a linear model of coregionalization
Space-Time Poisson Kriging Space-time Poisson kriging of cancer rates

Geostatistical Simulation

Tutorial Topic
Geostatistical Simulation What simulation adds to kriging, and which method to use
Sequential Gaussian Simulation Sequential Gaussian simulation: conditioning and reproduction
Sequential Gaussian Co-Simulation Co-simulation with an exhaustive secondary variable
Sequential Indicator Simulation Indicator variograms, order relations and tails
Sequential Indicator Co-Simulation Indicator co-simulation with soft data
Direct Sequential Simulation Simulation in original units
Simulated Annealing Variogram reproduction by simulated annealing
Plurigaussian Simulation Facies from truncated latent Gaussian fields
Multiple-Point Simulation Direct Sampling and SNESIM from a training image
SNESIM Templates, multiple grids and proportion control

Bayesian Kriging

Tutorial Topic
Bayesian Kriging Types of Bayesian kriging and how to choose
Empirical Bayesian Kriging An ensemble of variograms instead of one
Bayesian Kriging Models Priors on range and nugget, exceedance maps
Bayesian GLM Kriging Poisson spatial GLMM for count data
Bayesian Transformed Kriging Box-Cox uncertainty and back-transformation
Conjugate Bayesian Kriging Closed-form posterior with a known correlation
Bayesian Kriging in One Call Validation and maps with bayesian_kriging
Correlation Functions Correlation models, ranges and smoothness

Capstone Projects

Tutorial Topic
Groundwater Arsenic Risk Probability that well arsenic exceeds a limit, Bangladesh
Arsenic and Cancer Risk Arsenic exposure from water and rice, cancer risk
Paddy Soil Arsenic Risk Four kriging methods for paddy soil arsenic
Soil Cadmium in the USA Factorial and Bayesian kriging of soil cadmium
Diabetes and Obesity Multivariate Poisson cokriging across US counties
Micronutrient Deficiency Deficiency probability maps for soil nutrients
STIs and HIV in California Poisson kriging in space, time and across diseases
Groundwater Forecasting Space-time kriging of groundwater levels, Rajshahi

Quick reference

Geostatistics quick reference for geolattice.geostatistics

Sample data

The notebooks read the sample data in Data/00_shared/data_geostatistics/ of the repository: the Jura soil metals data and prediction grid, Great Plains soil organic carbon, California PM2.5 monitors, US county disease rates, Bangladesh groundwater and soil data, and GSLIB files.

License

GeoLattice is released under the MIT License. See LICENSE.


Developed by Zia Ahmed, Upatta Analytics

Back to top