robustness_score: Robustness Score Under Input Perturbation

View source: R/robustness_score.R

robustness_scoreR Documentation

Robustness Score Under Input Perturbation

Description

Evaluates the robustness of a machine learning model by measuring how much its predictions change when small amounts of noise are added to the input data. A robustness score of 1 indicates that predictions are completely unaffected by perturbations, while values near 0 indicate high sensitivity to input noise.

Usage

robustness_score(predict_fn, X, noise_level = 0.05, n_rep = 10L)

Arguments

predict_fn

A function that accepts a numeric matrix (observations in rows, features in columns) and returns a numeric vector of predictions with length equal to nrow(X).

X

A numeric matrix or data.frame of input features. Rows are observations and columns are features. Must contain at least two rows and no missing values.

noise_level

A positive numeric scalar controlling the magnitude of Gaussian noise added to each feature, expressed as a fraction of the feature's standard deviation. Default is 0.05 (5 percent).

n_rep

A positive integer specifying the number of perturbation repetitions. Default is 10L.

Details

Gaussian noise proportional to each feature's standard deviation is added to the input data. The magnitude of the noise is controlled by noise_level. Predictions on the perturbed data are compared to baseline predictions using normalised mean squared error. The process is repeated n_rep times and the average score is returned.

Value

A numeric scalar between 0 and 1, where 1 indicates perfect robustness and values near 0 indicate high sensitivity to noise.

Examples

# A simple linear prediction function
pred_fn <- function(X) X %*% c(1, 2, 3)
set.seed(42)
X <- matrix(rnorm(300), ncol = 3)
robustness_score(pred_fn, X, noise_level = 0.05, n_rep = 10)

# A constant prediction function is perfectly robust
const_fn <- function(X) rep(5, nrow(X))
robustness_score(const_fn, X)


TrustworthyMLR documentation built on Feb. 20, 2026, 5:09 p.m.