Metamorphic tests#

giskard.testing.test_metamorphic_invariance(model: SuiteInput | BaseModel | None = None, dataset: SuiteInput | Dataset | None = None, transformation_function: SuiteInput | TransformationFunction | None = None, slicing_function: SuiteInput | SlicingFunction | None = None, threshold: SuiteInput | float | None = 0.5, output_sensitivity: SuiteInput | float | None = None) GiskardTestMethod[source]#

Tests if model prediction is invariant to perturbations

For classification: Test if the predicted classification label remains the same after feature values perturbation. For regression: Check whether the predicted output remains the same at the output_sensibility level after feature values perturbation.

The test is passed when the ratio of invariant rows is higher than the threshold.

Example: The test is passed when, by switching gender from male to female, more than 50%(threshold 0.5) of males have unchanged outputs.

Parameters:
  • model (BaseModel) โ€“ Model used to compute the test

  • dataset (Dataset) โ€“ Dataset used to compute the test

  • transformation_function (TransformationFunction) โ€“ Function performing the perturbations to be applied on dataset.

  • slicing_function (Optional[SlicingFunction]) โ€“ Slicing function to be applied on dataset.

  • threshold (float) โ€“ The threshold value for the ratio of invariant rows.

  • output_sensitivity (float) โ€“ For regression models. The threshold for ratio between the difference between perturbed prediction and actual prediction over the actual prediction for a regression model. We consider there is a prediction difference for regression if the ratio is above the output_sensitivity of 0.1.

  • debug (bool) โ€“ If True and the test fails, a dataset will be provided containing the non-invariant rows.

Returns:

A TestResult object containing the test result.

Return type:

TestResult

giskard.testing.test_metamorphic_increasing(model: SuiteInput | BaseModel | None = None, dataset: SuiteInput | Dataset | None = None, transformation_function: SuiteInput | TransformationFunction | None = None, slicing_function: SuiteInput | SlicingFunction | None = None, threshold: SuiteInput | float | None = 0.5, classification_label: SuiteInput | str | None = None) GiskardTestMethod[source]#

Tests if the model prediction increases when the features are perturbed.

For classification models, it tests if the model probability of a given classification_label is increasing after feature values perturbation.

For regression models, it tests if the model prediction is increasing after feature values perturbation.

The test is passed when the percentage of rows that are increasing is higher than the threshold.

Example: For a credit scoring model, the test is passed when a decrease of wage by 10%, default probability is increasing for more than 50% of people in the dataset.

Parameters:
  • model (BaseModel) โ€“ Model used to compute the test

  • dataset (Dataset) โ€“ Dataset used to compute the test

  • transformation_function (TransformationFunction) โ€“ Function performing the perturbations to be applied on dataset.

  • slicing_function (Optional[SlicingFunction]) โ€“ Slicing function to be applied on dataset (Default value = None)

  • threshold (float) โ€“ The threshold value for the ratio of increasing rows. Default is 0.5.

  • classification_label (str) โ€“ One specific label value from the target column (only for classification models).

  • debug (bool) โ€“ If True and the test fails, a dataset will be provided containing the non-increasing rows.

Returns:

A TestResult object containing the test result.

Return type:

TestResult

giskard.testing.test_metamorphic_decreasing(model: SuiteInput | BaseModel | None = None, dataset: SuiteInput | Dataset | None = None, transformation_function: SuiteInput | TransformationFunction | None = None, slicing_function: SuiteInput | SlicingFunction | None = None, threshold: SuiteInput | float | None = 0.5, classification_label: SuiteInput | str | None = None) GiskardTestMethod[source]#

Tests if the model prediction decreases when features are perturbed

For classification models, it tests if the model probability of a given classification_label is decreasing after feature values perturbation.

For regression models, it tests if the model prediction is decreasing after feature values perturbation.

The test is passed when the percentage of rows that are decreasing is higher than the threshold.

Example: For a credit scoring model, the test is passed when an increase of wage by 10%, default probability is decreasing for more than 50% of people in the dataset.

Parameters:
  • model (BaseModel) โ€“ Model used to compute the test

  • dataset (Dataset) โ€“ Dataset used to compute the test

  • transformation_function (TransformationFunction) โ€“ Function performing the perturbations to be applied on dataset.

  • slicing_function (Optional[SlicingFunction]) โ€“ Slicing function to be applied on dataset

  • threshold (float) โ€“ Threshold of the ratio of decreasing rows

  • classification_label (str) โ€“ Optional. One specific label value from the target column

  • debug (bool) โ€“ If True and the test fails, a dataset will be provided containing the non-decreasing rows.

Returns:

A TestResult object containing the test result.

Return type:

TestResult

giskard.testing.test_metamorphic_decreasing_t_test(model: SuiteInput | BaseModel | None = None, dataset: SuiteInput | Dataset | None = None, transformation_function: SuiteInput | TransformationFunction | None = None, slicing_function: SuiteInput | SlicingFunction | None = None, critical_quantile: SuiteInput | float | None = 0.05, classification_label: SuiteInput | str | None = None) GiskardTestMethod[source]#

Tests if the model prediction decreases when the feature are perturbed

Performs a t-test on two related samples. Sample A is constituted by the original predictions (probability of classification_label for classification models, or predicted value for regression models). Sample B is constituted by the predictions after perturbation of one or more of the features (by tranformation_function).

It performs a t-test to study if mean(B) < mean(A).

The test is passed when the p-value of the t-test between (A) and (B) is below the critical quantile.

Example: For a credit scoring model, the test is passed when an increase of wage by 10%, causes a statistically significant decrease of the default probability.

Parameters:
  • model (BaseModel) โ€“ Model used to compute the test

  • dataset (Dataset) โ€“ Dataset used to compute the test

  • transformation_function (TransformationFunction) โ€“ Function performing the perturbations to be applied on dataset.

  • slicing_function (Optional[SlicingFunction]) โ€“ Slicing function to be applied on dataset

  • critical_quantile (float) โ€“ Critical quantile above which the null hypothesis cannot be rejected

  • classification_label (str) โ€“ (Default value = None)

  • debug (bool) โ€“ If True and the test fails, a dataset will be provided containing the non-decreasing rows.

Returns:

A TestResult object containing the test result.

Return type:

TestResult

giskard.testing.test_metamorphic_increasing_t_test(model: SuiteInput | BaseModel | None = None, dataset: SuiteInput | Dataset | None = None, transformation_function: SuiteInput | TransformationFunction | None = None, slicing_function: SuiteInput | SlicingFunction | None = None, critical_quantile: SuiteInput | float | None = 0.05, classification_label: SuiteInput | str | None = None) GiskardTestMethod[source]#

Tests if the model prediction increases when feature values are perturbed

Performs a t-test on two related samples. Sample A is constituted by the original predictions (probability of classification_label for classification models, or predicted value for regression models). Sample B is constituted by the predictions after perturbation of one or more of the features (by tranformation_function).

It performs a t-test to study if mean(B) > mean(A).

The test is passed when the p-value of the t-test between (A) and (B) is below the critical quantile.

Example: For a credit scoring model, the test is passed when a decrease of wage by 10%, causes a statistically significant increase of the default probability.

Parameters:
  • model (BaseModel) โ€“ Model used to compute the test

  • dataset (Dataset) โ€“ Dataset used to compute the test

  • transformation_function (TransformationFunction) โ€“ Function performing the perturbations to be applied on dataset.

  • slicing_function (Optional[SlicingFunction]) โ€“ Slicing function to be applied on dataset (Default value = None)

  • critical_quantile (float) โ€“ Critical quantile above which the null hypothesis cannot be rejected

  • classification_label (str) โ€“ Only required for classification models.

  • debug (bool) โ€“ If True and the test fails, a dataset will be provided containing the non-increasing rows.

Returns:

  • actual_slices_size โ€“ Length of dataset tested

  • message โ€“ Test result message

  • metric โ€“ The t-test in terms of p-value between unchanged rows over the perturbed rows

  • passed โ€“ TRUE if the p-value of the t-test between (A) and (B) is below the critical value

giskard.testing.test_metamorphic_invariance_t_test(model: SuiteInput | BaseModel | None = None, dataset: SuiteInput | Dataset | None = None, transformation_function: SuiteInput | TransformationFunction | None = None, slicing_function: SuiteInput | SlicingFunction | None = None, window_size: SuiteInput | float | None = 0.2, critical_quantile: SuiteInput | float | None = 0.05) GiskardTestMethod[source]#

Tests if the model predictions are statistically invariant when the feature values are perturbed.

Performs a t-test on two related samples. Sample A is constituted by the original predictions (probability of classification_label for classification models, or predicted value for regression models). Sample B is constituted by the predictions after perturbation of one or more of the features (by tranformation_function).

It performs a t-test to study if mean(A) is between mean(B) - window_size/2 and mean(B) + window_size/2.

The test is passed when the following tests pass:
  • the p-value of the t-test between (A) and (B) + window_size/2 is below the critical quantile

  • the p-value of the t-test between (B) - window_size/2 and (A) is below the critical quantile

The test is passed when the p-value of the t-test between (A) and (B) is below the critical quantile.

Example: The test is passed when, by switching gender from male to female, the probability distributions remains statistically invariant. In other words, the test is passed if the mean of the perturbed sample is statistically within a window determined by the user.

Parameters:
  • model (BaseModel) โ€“ Model used to compute the test

  • dataset (Dataset) โ€“ Dataset used to compute the test

  • transformation_function (TransformationFunction) โ€“ Function performing the perturbations to be applied on dataset.

  • slicing_function (Optional[SlicingFunction]) โ€“ Slicing function to be applied on dataset (Default value = None)

  • window_size (float) โ€“ Probability window in which the mean of the perturbed sample can be in order to pass the test

  • critical_quantile (float) โ€“ Critical quantile above which the null hypothesis cannot be rejected

  • debug (bool) โ€“ If True and the test fails, a dataset will be provided containing the non-invariant rows.

Returns:

A TestResult object containing the test result.

Return type:

TestResult

giskard.testing.test_metamorphic_decreasing_wilcoxon(model: SuiteInput | BaseModel | None = None, dataset: SuiteInput | Dataset | None = None, transformation_function: SuiteInput | TransformationFunction | None = None, slicing_function: SuiteInput | SlicingFunction | None = None, critical_quantile: SuiteInput | float | None = 0.05, classification_label: SuiteInput | str | None = None) GiskardTestMethod[source]#

Tests if the model prediction decreases when feature values are perturbed

Performs the Wilcoxon signed-rank test on two related samples. Sample (A) is constituted by the original predictions (probability of classification_label for classification models, or predicted value for regression models). Sample B is constituted by the predictions after perturbation of one or more features by tranformation_function.

This test computes the decreasing test to study if mean(B) < mean(A) The test is passed when the p-value of the Wilcoxon signed-rank test between (A) and (B) is below the critical quantile.

Example: For a credit scoring model, the test is passed when a decrease of wage by 10% causes a statistically significant probability decrease of the default probability.

Parameters:
  • model (BaseModel) โ€“ Model used to compute the test

  • dataset (Dataset) โ€“ Dataset used to compute the test

  • transformation_function (TransformationFunction) โ€“ Function performing the perturbations to be applied on dataset.

  • slicing_function (Optional[SlicingFunction]) โ€“ Slicing function to be applied on dataset

  • critical_quantile (float) โ€“ Critical quantile above which the null hypothesis cannot be rejected

  • classification_label (str) โ€“ Only required for classification models.

  • debug (bool) โ€“ If True and the test fails, a dataset will be provided containing the non-decreasing rows. (Default value = False)

Returns:

A TestResult object containing the test result.

Return type:

TestResult

giskard.testing.test_metamorphic_increasing_wilcoxon(model: SuiteInput | BaseModel | None = None, dataset: SuiteInput | Dataset | None = None, transformation_function: SuiteInput | TransformationFunction | None = None, slicing_function: SuiteInput | SlicingFunction | None = None, critical_quantile: SuiteInput | float | None = 0.05, classification_label: SuiteInput | str | None = None) GiskardTestMethod[source]#

Tests if the model prediction increases when feature values are perturbed

Performs the Wilcoxon signed-rank test on two related samples. Sample (A) is constituted by the original predictions (probability of classification_label for classification models, or predicted value for regression models). Sample B is constituted by the predictions after perturbation of one or more features by tranformation_function.

This test computes the decreasing test to study if mean(B) > mean(A) The test is passed when the p-value of the Wilcoxon signed-rank test between (A) and (B) is below the critical quantile.

Example: For a credit scoring model, the test is passed when a decrease of wage by 10% causes a statistically significant probability increase of the default probability.

Parameters:
  • model (BaseModel) โ€“ Model used to compute the test

  • dataset (Dataset) โ€“ Dataset used to compute the test

  • transformation_function (TransformationFunction) โ€“ Function performing the perturbations to be applied on dataset.

  • slicing_function (Optional[SlicingFunction]) โ€“ Slicing function to be applied on dataset

  • critical_quantile (float) โ€“ Critical quantile above which the null hypothesis cannot be rejected

  • classification_label (str) โ€“ Only required for classification models.

  • debug (bool) โ€“ If True and the test fails, a dataset will be provided containing the non-increasing rows.

Returns:

A TestResult object containing the test result.

Return type:

TestResult

giskard.testing.test_metamorphic_invariance_wilcoxon(model: SuiteInput | BaseModel | None = None, dataset: SuiteInput | Dataset | None = None, transformation_function: SuiteInput | TransformationFunction | None = None, slicing_function: SuiteInput | SlicingFunction | None = None, window_size: SuiteInput | float | None = 0.2, critical_quantile: SuiteInput | float | None = 0.05) GiskardTestMethod[source]#

Tests if the model predictions are statistically invariant when the feature values are perturbed

Performs the Wilcoxon signed-rank test on two related samples. Sample (A) is constituted by the original predictions (probability of classification_label for classification models, or predicted value for regression models). Sample B is constituted by the predictions after perturbation of one or more features by tranformation_function.

This test computes the equivalence test to show that mean(B) - window_size/2 < mean(A) < mean(B) + window_size/2

The test is passed when the following tests pass: - the p-value of the t-test between (A) and (B)+window_size/2 is below the critical quantile - the p-value of the t-test between (B)-window_size/2 and (A) is below the critical quantile

Example: The test is passed when, by switching gender from male to female, the probability distributions remains statistically invariant. In other words, the test is passed if the mean of the perturbed sample is statistically within a window determined by the user.

Parameters:
  • model (BaseModel) โ€“ Model used to compute the test

  • dataset (Dataset) โ€“ Dataset used to compute the test

  • transformation_function (TransformationFunction) โ€“ Function performing the perturbations to be applied on dataset.

  • slicing_function (Optional[SlicingFunction]) โ€“ Slicing function to be applied on dataset (Default value = None)

  • window_size (float) โ€“ Probability window in which the mean of the perturbed sample can be in

  • critical_quantile (float) โ€“ Critical quantile above which the null hypothesis cannot be rejected

  • debug (bool) โ€“ If True and the test fails, a dataset will be provided containing the non-invariant rows. (Default value = False)

Returns:

A TestResult object containing the test result.

Return type:

TestResult