Skip to contents

Shapley feature attributions for one instance (sampling estimator)

Usage

morie_fairness_xai_shap_values(
  predict_fn,
  x,
  background,
  feature_names = NULL,
  n_samples = 200L,
  seed = 0L
)

Arguments

predict_fn

Function mapping (n, d) matrix to n predictions.

x

Numeric vector of length d (the instance).

background

Reference matrix (n_bg, d) for marginal averaging.

feature_names

Optional character vector.

n_samples

Number of random permutations averaged.

seed

Reproducibility seed.

Value

morie_fairness_result; $value is the largest-magnitude SHAP value.

Examples

set.seed(11)
X <- matrix(rnorm(120), 30, 4); colnames(X) <- paste0("f", 1:4)
predict_fn <- function(M) as.numeric(M %*% c(1.5, -0.7, 0, 0.3))
morie_fairness_xai_shap_values(predict_fn, X[1L, ], background = X,
  feature_names = colnames(X), n_samples = 10L, seed = 1L)
#> SHAP Feature Attributions (sampling estimator)
#> ==============================================
#>   Most influential feature  f2
#>   Its SHAP value            0.73643
#>   Prediction                0.24715
#>   Background mean           -0.43264
#> 
#> For this instance the prediction 0.2472 departs from the background mean -0.4326; 'f2' contributes the most (+0.7364). The SHAP values sum to that departure.