alibi_detect.cd.mmd_online module

class alibi_detect.cd.mmd_online.MMDDriftOnline(x_ref, ert, window_size, backend='tensorflow', preprocess_fn=None, x_ref_preprocessed=False, kernel=None, sigma=None, n_bootstraps=1000, device=None, verbose=True, input_shape=None, data_type=None)[source]

Bases: DriftConfigMixin

__init__(x_ref, ert, window_size, backend='tensorflow', preprocess_fn=None, x_ref_preprocessed=False, kernel=None, sigma=None, n_bootstraps=1000, device=None, verbose=True, input_shape=None, data_type=None)[source]

Online maximum Mean Discrepancy (MMD) data drift detector using preconfigured thresholds.

Parameters:
  • x_ref (Union[ndarray, list]) – Data used as reference distribution.

  • ert (float) – The expected run-time (ERT) in the absence of drift. For the multivariate detectors, the ERT is defined as the expected run-time from t=0.

  • window_size (int) – The size of the sliding test-window used to compute the test-statistic. Smaller windows focus on responding quickly to severe drift, larger windows focus on ability to detect slight drift.

  • backend (str) – Backend used for the MMD implementation and configuration.

  • preprocess_fn (Optional[Callable]) – Function to preprocess the data before computing the data drift metrics.

  • x_ref_preprocessed (bool) – Whether the given reference data x_ref has been preprocessed yet. If x_ref_preprocessed=True, only the test data x will be preprocessed at prediction time. If x_ref_preprocessed=False, the reference data will also be preprocessed.

  • kernel (Optional[Callable]) – Kernel used for the MMD computation, defaults to Gaussian RBF kernel.

  • sigma (Optional[ndarray]) – Optionally set the GaussianRBF kernel bandwidth. Can also pass multiple bandwidth values as an array. The kernel evaluation is then averaged over those bandwidths. If sigma is not specified, the ‘median heuristic’ is adopted whereby sigma is set as the median pairwise distance between reference samples.

  • n_bootstraps (int) – The number of bootstrap simulations used to configure the thresholds. The larger this is the more accurately the desired ERT will be targeted. Should ideally be at least an order of magnitude larger than the ERT.

  • device (Union[Literal[‘cuda’, ‘gpu’, ‘cpu’], device, None]) – Device type used. The default tries to use the GPU and falls back on CPU if needed. Can be specified by passing either 'cuda', 'gpu', 'cpu' or an instance of torch.device. Only relevant for ‘pytorch’ backend.

  • verbose (bool) – Whether or not to print progress during configuration.

  • input_shape (Optional[tuple]) – Shape of input data.

  • data_type (Optional[str]) – Optionally specify the data type (tabular, image or time-series). Added to metadata.

get_config()[source]

Get the detector’s configuration dictionary.

Return type:

dict

Returns:

The detector’s configuration dictionary.

load_state(filepath)[source]

Load the detector’s state from disk, in order to restart from a checkpoint previously generated with save_state.

Parameters:

filepath (Union[str, PathLike]) – The directory to load state from.

predict(x_t, return_test_stat=True)[source]

Predict whether the most recent window of data has drifted from the reference data.

Parameters:
  • x_t (Union[ndarray, Any]) – A single instance to be added to the test-window.

  • return_test_stat (bool) – Whether to return the test statistic (squared MMD) and threshold.

Return type:

Dict[Dict[str, str], Dict[str, Union[int, float]]]

Returns:

Dictionary containing 'meta' and 'data' dictionaries. –

  • 'meta' has the model’s metadata.

  • 'data' contains the drift prediction and optionally the test-statistic and threshold.

reset_state()[source]

Resets the detector to its initial state (t=0). This does not include reconfiguring thresholds.

save_state(filepath)[source]

Save a detector’s state to disk in order to generate a checkpoint.

Parameters:

filepath (Union[str, PathLike]) – The directory to save state to.

score(x_t)[source]

Compute the test-statistic (squared MMD) between the reference window and test window.

Parameters:

x_t (Union[ndarray, Any]) – A single instance to be added to the test-window.

Return type:

float

Returns:

Squared MMD estimate between reference window and test window.

property t
property test_stats
property thresholds