## Answer
### Does the Medium article define the GPModel spatial covariance?
**No, it does not.** The article by Fabio Sigrist (June 21, 2023) demonstrates the GPBoost workflow using European GDP data and passes `cov_function = "exponential"` to the `GPModel()` constructor, but it never provides the mathematical definition of what the exponential spatial covariance function actually is. It also doesn't define the covariance parameters (variance, range, nugget) in mathematical form. The article is a practical tutorial, not a theoretical exposition.
---
### Where is the spatial covariance implemented, and how does it work?
The actual implementation lives in the **GPBoost C++ source code** and is exposed through the **R package** (which the article uses). Here's what I found by examining the source:
#### 1. Covariance function specification
In the GPBoost documentation (`Main_parameters.rst`), the `cov_function` parameter accepts values like `"exponential"`, `"matern"`, `"gaussian"`, `"powered_exponential"`, etc. The article uses `"exponential"`.
#### 2. The exponential covariance function — how it works
From the GPBoost source code (specifically the C++ files `GP_utils.cpp` and `GP_utils.h` in `src/GPBoost/` and `include/GPBoost/`), the covariance computation follows this process:
**Step 1: Compute Euclidean distances** between all pairs of spatial coordinates. The `CalculateDistances` function in `GP_utils.h` computes the Euclidean distance matrix `d(i,j) = ||coord_i - coord_j||₂`.
**Step 2: Apply the covariance function** to the distance matrix. The exponential covariance function used by GPBoost is the standard **exponential spatial covariance**:
\[
Cov(s_i, s_j) = \sigma^2 \cdot \exp\left(-\frac{d(s_i, s_j)}{\rho}\right) + \tau^2 \cdot \mathbf{1}_{i=j}
\]
Where:
- **\(d(s_i, s_j)\)** = Euclidean distance between locations \(s_i\) and \(s_j\)
- **\(\sigma^2\)** = marginal variance (the "variance" or "sill" parameter, estimated by GPBoost)
- **\(\rho\)** = range parameter (controls how quickly correlation decays with distance, estimated by GPBoost)
- **\(\tau^2\)** = nugget effect (measurement error / micro-scale variation, added only on the diagonal)
This is the **isotropic exponential covariance function** — it depends only on distance, not direction.
#### 3. Where the parameters are estimated
GPBoost estimates the covariance parameters (\(\sigma^2\), \(\rho\), \(\tau^2\)) via **maximum likelihood** or **restricted maximum likelihood (REML)** during model fitting. The article uses `gp_model <- GPModel(gp_coords = coords, cov_function = "exponential")` and then `fit(gp_model)` which triggers the parameter estimation.
#### 4. The full covariance matrix
For \(n\) spatial locations, GPBoost constructs an \(n \times n\) covariance matrix \(\Sigma\) where entry \((i,j)\) is given by the formula above. This matrix is then used in the mixed-effects model:
\[
y = X\beta + Zu + \varepsilon
\]
where \(u \sim N(0, \Sigma)\) is the spatial random effect with the exponential covariance structure.
#### 5. Source code location
The key files are:
- **`src/GPBoost/GP_utils.cpp`** — Contains distance calculations and covariance function evaluations
- **`include/GPBoost/GP_utils.h`** — Header with function declarations including `CalculateDistances` and covariance function templates
- **`R-package/src/gpboost_R.cpp`** — R bindings that pass the `cov_function` string to the C++ backend
- **`docs/Main_parameters.rst`** — Lists supported covariance functions but does not give mathematical formulas
The actual covariance function evaluation (the `exp(-d/ρ)` computation) is implemented in the C++ template functions that apply the chosen covariance function to the distance matrix, with the specific function name `"exponential"` mapping to the standard exponential kernel shown above.
---
**In summary:** The Medium article does not define the mathematical form of the exponential spatial covariance. The GPBoost library implements the standard isotropic exponential covariance function \( \sigma^2 \exp(-d/\rho) + \tau^2 \mathbf{1}_{i=j} \), where distances are Euclidean, and the parameters \(\sigma^2\) (variance), \(\rho\) (range), and \(\tau^2\) (nugget) are estimated via maximum likelihood.
1navigatehttps://medium.com/data-science/mixed-effects-machine-learning-with-gpboost-for-grouped-and-areal-spatial-econometric-data-b26f8bddd385
2get_text
3ddm--text --find exponential
4navigatehttps://github.com/fabsig/GPBoost
5clickundefined
6ddm--text --find covariance
7js_evaldocument.querySelector('a[href="#currently-supported-gp-covariance-functions"]')
8ddm--text --find cov_function
9navigatehttps://raw.githubusercontent.com/fabsig/GPBoost/master/docs/Main_parameters.rst
10get_text
11navigatehttps://github.com/fabsig/GPBoost/tree/master/src
12navigatehttps://github.com/fabsig/GPBoost/tree/master/src/GPBoost
13clickundefined
14navigatehttps://github.com/fabsig/GPBoost/blob/master/include/GPBoost/GP_utils.h
15navigatehttps://raw.githubusercontent.com/fabsig/GPBoost/master/src/GPBoost/GP_utils.cpp
16navigatehttps://github.com/fabsig/GPBoost/tree/master/src/GPBoost
17navigatehttps://github.com/fabsig/GPBoost/tree/master/R-package
18navigatehttps://github.com/fabsig/GPBoost/tree/master/R-package/src