The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
Author: Sadikul Islam
Package: xaiHydro v0.1.0
Reference: Islam, S. (2026). Explainable AI for Hydro-Climate Models. In: Hydro-Climate Analytics: Remote Sensing, AI and Geospatial Modelling (Springer).
This vignette documents the mathematical foundations of each XAI
method implemented in xaiHydro, their computational
implementation, and practical guidance for hydro-climate applications.
It serves as the Methods section reference for the
accompanying book chapter.
For a prediction \(\hat{f}(\mathbf{x})\) and background dataset \(\mathcal{D}\), the SHAP value for feature \(j\) and observation \(\mathbf{x}\) is defined as the weighted average of marginal contributions across all feature coalitions (Lundberg and Lee 2017):
\[ \phi_j(\hat{f}, \mathbf{x}) = \sum_{S \subseteq \mathcal{F} \setminus \{j\}} \frac{|S|!\,(|\mathcal{F}| - |S| - 1)!}{|\mathcal{F}|!} \left[\hat{f}_{S \cup \{j\}}(\mathbf{x}_{S \cup \{j\}}) - \hat{f}_{S}(\mathbf{x}_{S})\right] \]
where \(\mathcal{F}\) is the full set of features and \(S\) ranges over all subsets excluding feature \(j\).
| Property | Definition | Hydrology relevance |
|---|---|---|
| Efficiency | \(\sum_j \phi_j = \hat{f}(\mathbf{x}) - \mathbb{E}[\hat{f}]\) | Contributions explain full gap from mean discharge |
| Symmetry | Symmetric features receive equal attribution | Ensures fair credit between correlated rain indices |
| Dummy | Non-contributing features receive \(\phi_j = 0\) | Zero-importance variables identified automatically |
| Linearity | Additive under model combination | Supports ensemble model analysis |
xaiHydro uses a built-in permutation-based Monte Carlo
SHAP estimator (Strumbelj and Kononenko, 2014) requiring no additional
packages:
\[ \hat{\phi}_j = \frac{1}{B} \sum_{b=1}^{B} \left[\hat{f}(\mathbf{x}^{(b)}_{+j}) - \hat{f}(\mathbf{x}^{(b)}_{-j})\right] \]
where \(\mathbf{x}^{(b)}_{+j}\) and \(\mathbf{x}^{(b)}_{-j}\) are randomly constructed versions of \(\mathbf{x}\) with and without feature \(j\) replaced by background values.
Practical guidance for hydrology:
nsim ≥ 50 for stable importance rankings in final
figures.nsim = 100 runs in approximately 2–3 minutes on a standard
laptop.LIME fits a locally weighted linear surrogate \(g \in G\) that approximates the complex model \(\hat{f}\) in the neighbourhood of observation \(\mathbf{x}'\) (Ribeiro et al. 2016):
\[ \xi(\mathbf{x}') = \arg\min_{g \in G} \mathcal{L}(\hat{f},\, g,\, \pi_{\mathbf{x}'}) + \Omega(g) \]
where \(\pi_{\mathbf{x}'}(\mathbf{z}) = \exp(-d(\mathbf{x}', \mathbf{z})^2 / \sigma^2)\) is an exponential kernel weighting neighbourhood samples by proximity, and \(\Omega(g)\) is a complexity penalty (LASSO for linear \(g\)).
kernel_widthkernel_width |
Neighbourhood | Best for |
|---|---|---|
| 0.25–0.5 | Very local | Highly non-linear models; single event explanation |
| 0.75 (default) | Moderate | General use |
| 1.0–2.0 | Broad | Smooth models; more stable but less local |
Note: Check local R² in the plot subtitle. An R²
< 0.5 indicates the surrogate does not capture local model behaviour
adequately; try increasing kernel_width or reducing
n_features.
The partial dependence function for feature set \(S\) is (Friedman 2001):
\[ \hat{f}_S(\mathbf{x}_S) = \mathbb{E}_{\mathbf{x}_C}\left[\hat{f}(\mathbf{x}_S,\, \mathbf{x}_C)\right] = \int \hat{f}(\mathbf{x}_S,\, \mathbf{x}_C)\, d\mathbb{P}(\mathbf{x}_C) \]
estimated by averaging over the observed marginal distribution:
\[ \hat{f}_S(\mathbf{x}_S) \approx \frac{1}{n} \sum_{i=1}^{n} \hat{f}(\mathbf{x}_S,\, \mathbf{x}^{(i)}_C) \]
ALE resolves the PDP extrapolation problem by integrating local conditional effects over observed data slices (Apley and Zhu 2020):
\[ \hat{f}^{\text{ALE}}(x_j) = \int_{x_{j,\min}}^{x_j} \mathbb{E}\left[ \frac{\partial \hat{f}(X)}{\partial X_j} \Big| X_j = z_j \right] dz_j - c \]
where \(c\) is a centering constant ensuring \(\mathbb{E}[\hat{f}^{\text{ALE}}] = 0\).
# ALE — recommended when predictors are correlated
ale <- hydro_pdp(exp, variable = c("precipitation", "soil_moisture"),
type = "accumulated")
# PDP — acceptable when predictors are approximately independent
pdp <- hydro_pdp(exp, variable = "ndvi",
type = "partial")Decision rule: Compute the Pearson correlation matrix of your predictors. If any |r| > 0.5 between the target predictor and others, prefer ALE.
Permutation importance for feature \(j\) is (Breiman 2001):
\[ \text{VI}_j = \frac{1}{B} \sum_{b=1}^{B} \left[L\!\left(\hat{f}, \tilde{\mathbf{X}}^{(b)}_j\right) - L(\hat{f}, \mathbf{X})\right] \]
where \(\tilde{\mathbf{X}}^{(b)}_j\) is the dataset with feature \(j\) randomly permuted in the \(b\)-th repetition, and \(L\) is the loss function (RMSE by default).
| Question | Recommended method | Function |
|---|---|---|
| Which features matter globally? | Permutation importance + SHAP summary | hydro_importance(), hydro_shap() |
| How does feature X affect predictions on average? | PDP or ALE | hydro_pdp() |
| Why was this specific day predicted as a flood? | SHAP waterfall or breakdown | plot_shap_waterfall(),
hydro_breakdown() |
| What simple rule explains this event locally? | LIME | hydro_lime() |
| Are there unexpected residual patterns? | Residual diagnostics | hydro_residuals() |
When using xaiHydro results in a publication:
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.