|
| 1 | +"""Anderson-Darling test module.""" |
| 2 | + |
| 3 | +from typing import Optional, List, Union |
| 4 | + |
| 5 | +import numpy as np # type: ignore |
| 6 | +from scipy.stats import anderson_ksamp # type: ignore |
| 7 | + |
| 8 | +from frouros.callbacks.batch.base import BaseCallbackBatch |
| 9 | +from frouros.detectors.data_drift.base import NumericalData, UnivariateData |
| 10 | +from frouros.detectors.data_drift.batch.statistical_test.base import ( |
| 11 | + BaseStatisticalTest, |
| 12 | + StatisticalResult, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class AndersonDarlingTest(BaseStatisticalTest): |
| 17 | + """Anderson-Darling test [scholz1987k]_ detector. |
| 18 | +
|
| 19 | + :Note: |
| 20 | + p-values are bounded between 0.001 and 0.25 according to scipy documentation [1]_. |
| 21 | +
|
| 22 | + :References: |
| 23 | +
|
| 24 | + .. [scholz1987k] Scholz, Fritz W., and Michael A. Stephens. |
| 25 | + "K-sample Anderson–Darling tests." |
| 26 | + Journal of the American Statistical Association 82.399 (1987): 918-924. |
| 27 | + [1] https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.anderson_ksamp.html # noqa: E501 # pylint: disable=line-too-long |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + callbacks: Optional[Union[BaseCallbackBatch, List[BaseCallbackBatch]]] = None, |
| 33 | + ) -> None: |
| 34 | + """Init method. |
| 35 | +
|
| 36 | + :param callbacks: callbacks |
| 37 | + :type callbacks: Optional[Union[BaseCallbackBatch, List[BaseCallbackBatch]]] |
| 38 | + """ |
| 39 | + super().__init__( |
| 40 | + data_type=NumericalData(), |
| 41 | + statistical_type=UnivariateData(), |
| 42 | + callbacks=callbacks, |
| 43 | + ) |
| 44 | + |
| 45 | + def _statistical_test( |
| 46 | + self, X_ref: np.ndarray, X: np.ndarray, **kwargs # noqa: N803 |
| 47 | + ) -> StatisticalResult: |
| 48 | + test = anderson_ksamp( |
| 49 | + samples=[ |
| 50 | + X_ref, |
| 51 | + X, |
| 52 | + ], |
| 53 | + **kwargs, |
| 54 | + ) |
| 55 | + test = StatisticalResult( |
| 56 | + statistic=test.statistic, |
| 57 | + p_value=test.pvalue, |
| 58 | + ) |
| 59 | + return test |
0 commit comments