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.

xaiHydro Methods Reference

SHAP, LIME, PDP and ALE — Mathematical Background and Implementation

Sadikul Islam

Department of Hydrology, [Your Institution], India
sadikul.islam@institution.ac.in

2026-09-13


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).


1 Overview

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.


2 Method 1: SHAP (SHapley Additive exPlanations)

2.1 Mathematical definition

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\).

2.2 Key properties

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

2.3 Computational implementation

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.

# B = nsim Monte Carlo replicates
shap_vals <- hydro_shap(exp, nsim = 50, seed = 42)

Practical guidance for hydrology:


3 Method 2: LIME

3.1 Mathematical definition

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\)).

3.2 Implementation

lime_result <- hydro_lime(
  explainer    = exp,
  new_obs      = X[storm_day, , drop = FALSE],
  n_features   = 6,       # number of features in local model
  kernel_width = 0.75     # σ — controls neighbourhood size
)

3.3 Selecting kernel_width

kernel_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.


4 Method 3: Partial Dependence Plots (PDP)

4.1 Mathematical definition

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) \]

4.2 Limitation: correlated predictors

When features in \(S\) and \(C\) are correlated, the marginal average integrates over covariate combinations that never occur in practice (e.g., very high precipitation with very low soil moisture in monsoon hydrology). ALE avoids this.


5 Method 4: Accumulated Local Effects (ALE)

5.1 Mathematical definition

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\).

5.2 When to use ALE vs PDP

# 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.


6 Method 5: Permutation variable importance

6.1 Definition

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).

imp <- hydro_importance(exp, loss_function = "rmse", B = 20)

7 Choosing the right XAI method

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()

8 Reporting checklist for book chapters

When using xaiHydro results in a publication:


References

Apley, Daniel W, and Jingyu Zhu. 2020. “Visualizing the Effects of Predictor Variables in Black Box Supervised Learning Models.” Journal of the Royal Statistical Society: Series B 82 (4): 1059–86. https://doi.org/10.1111/rssb.12377.
Breiman, Leo. 2001. “Random Forests.” Machine Learning 45 (1): 5–32. https://doi.org/10.1023/A:1010933404324.
Friedman, Jerome H. 2001. “Greedy Function Approximation: A Gradient Boosting Machine.” Annals of Statistics, 1189–232. https://doi.org/10.1214/aos/1013203451.
Lundberg, Scott M, and Su-In Lee. 2017. “A Unified Approach to Interpreting Model Predictions.” Advances in Neural Information Processing Systems 30.
Ribeiro, Marco Tulio, Sameer Singh, and Carlos Guestrin. 2016. “"Why Should I Trust You?": Explaining the Predictions of Any Classifier.” Proceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 1135–44. https://doi.org/10.1145/2939672.2939778.

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.