drift_detection¶
skforecast.drift_detection._range_drift.RangeDriftDetector ¶
RangeDriftDetector()
Detector of out-of-range values based on training feature ranges.
The detector is intentionally lightweight: it does not compute advanced drift statistics since it is used to check single observations during inference. Suitable for real-time applications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self
|
|
required |
Attributes:
| Name | Type | Description |
|---|---|---|
series_names_in_ |
list
|
Names of the series used during training. |
series_values_range_ |
dict
|
Range of values of the target series used during training. |
exog_names_in_ |
list
|
Names of the exogenous variables used during training. |
exog_values_range_ |
dict
|
Range of values of the exogenous variables used during training. |
series_specific_exog_ |
bool
|
Indicates whether exogenous variables have different values across target series during training (i.e., exogenous is series-specific rather than global). |
is_fitted |
bool
|
Whether the detector has been fitted to the training data. |
Methods:
| Name | Description |
|---|---|
fit |
Fit detector, storing training ranges. |
predict |
Check if there is any value outside the training range for last_window and exog. |
Source code in skforecast/drift_detection/_range_drift.py
59 60 61 62 63 64 65 66 | |
_repr_html_ ¶
_repr_html_()
HTML representation of the object. The "General Information" section is expanded by default.
Source code in skforecast/drift_detection/_range_drift.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
_get_features_range
classmethod
¶
_get_features_range(X)
Get a summary of the features in the DataFrame or Series. For numeric features, it returns the min and max values. For categorical features, it returns the unique values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas Series, pandas DataFrame
|
Input data to summarize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
features_ranges |
(tuple, set, dict)
|
Feature ranges. If X is a Series, returns a tuple (min, max) for numeric data or a set of unique values for categorical data. If X is a DataFrame, returns a dictionary with column names as keys and their respective ranges (tuple or set) as values. |
Source code in skforecast/drift_detection/_range_drift.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
_check_feature_range
classmethod
¶
_check_feature_range(feature_range, X)
Check if there is any value outside the training range. For numeric features, it checks if the values are within the min and max range. For categorical features, it checks if the values are among the seen categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_range
|
(tuple, set)
|
Output from _get_features_range() for a single feature. |
required |
X
|
Series
|
New data to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if there is any value outside the training range, False otherwise. |
Source code in skforecast/drift_detection/_range_drift.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
_display_warnings
classmethod
¶
_display_warnings(
not_compliant_feature, feature_range, series_name=None
)
Display warnings for features with values outside the training range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
not_compliant_feature
|
str
|
Name of the feature with values outside the training range. |
required |
feature_range
|
tuple | set
|
Training range of the feature. |
required |
series_name
|
str
|
Name of the series being checked, if applicable. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast/drift_detection/_range_drift.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
_summary
classmethod
¶
_summary(
out_of_range_series,
out_of_range_series_ranges,
out_of_range_exog,
out_of_range_exog_ranges,
)
Summarize the results of the range check.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_of_range_series
|
list
|
List of series names that are out of range. |
required |
out_of_range_series_ranges
|
list
|
List of ranges for the out-of-range series. |
required |
out_of_range_exog
|
list
|
List of exogenous variable names that are out of range. |
required |
out_of_range_exog_ranges
|
list
|
List of ranges for the out-of-range exogenous variables. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast/drift_detection/_range_drift.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
_normalize_input ¶
_normalize_input(X, name, series_ids=None)
Convert pd.Series, pd.DataFrame or dict into a standardized dict of pd.Series or pd.DataFrames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas Series, pandas DataFrame, dict
|
Input data to normalize. |
required |
name
|
str
|
Name of the input being normalized. Used for error messages. Expected values are 'series', 'last_window' or 'exog'. |
required |
series_ids
|
list
|
Series IDs to include in the normalization of exogenous variables. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
X |
dict
|
Normalized input as a dictionary of pandas Series or DataFrames. |
Source code in skforecast/drift_detection/_range_drift.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | |
fit ¶
fit(series=None, exog=None, **kwargs)
Fit detector, storing training ranges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
pandas Series, pandas DataFrame, dict, aliases: `y`
|
Input time series data to fit the detector, ideally the same ones used to fit the forecaster. |
None
|
exog
|
pandas Series, pandas DataFrame, dict
|
Exogenous variables to include in the forecaster. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast/drift_detection/_range_drift.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
predict ¶
predict(
last_window=None,
exog=None,
verbose=True,
suppress_warnings=False,
)
Check if there is any value outside the training range for last_window and exog.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
last_window
|
pandas Series, pandas DataFrame, dict
|
Series values used to create the predictors (lags) needed in the first iteration of the prediction (t + 1). |
None
|
exog
|
pandas Series, pandas DataFrame, dict
|
Exogenous variable/s included as predictor/s. |
None
|
verbose
|
bool
|
Whether to print a summary of the check. |
False
|
suppress_warnings
|
bool
|
Whether to suppress warnings. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
flag_out_of_range |
bool
|
True if there is any value outside the training range, False otherwise. |
out_of_range_series |
list
|
List of series names that are out of range. |
out_of_range_exog |
(list, dict)
|
Exogenous variables that are out of range.
|
Source code in skforecast/drift_detection/_range_drift.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
skforecast.drift_detection._population_drift.PopulationDriftDetector ¶
PopulationDriftDetector(
chunk_size=None, threshold=3, threshold_method="std"
)
A class to detect population drift between reference and new datasets. This implementation computes Kolmogorov-Smirnov (KS) test for numeric features, Chi-Square test for categorical features, and Jensen-Shannon (JS) distance for all features. It calculates empirical distributions of these statistics from the reference data and uses quantile thresholds to determine drift in new data.
This implementation is inspired by NannyML's DriftDetector. See Notes for details.
For an in-depth explanation of the underlying calculations, see https://skforecast.org/latest/user_guides/drift-detection.html#deep-dive-into-temporal-drift-detection-in-time-series
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_size
|
(int, str)
|
Size of chunks for sequential drift analysis. If int, number of rows per chunk. If str (e.g., 'D' for daily, 'W' for weekly), time-based chunks assuming a datetime index. If None, analyzes the full dataset as a single chunk. |
None
|
threshold
|
(int, float)
|
Threshold value for determining drift. Interpretation depends on
|
3
|
threshold_method
|
str
|
Method for calculating thresholds from empirical distributions:
|
'std'
|
Attributes:
| Name | Type | Description |
|---|---|---|
chunk_size |
(int, str)
|
Size of chunks for sequential drift analysis. If int, number of rows per chunk. If str (e.g., 'D' for daily, 'W' for weekly), time-based chunks assuming a datetime index. If None, analyzes the full dataset as a single chunk. |
threshold |
float
|
Threshold value for determining drift. Interpretation depends on
|
threshold_method |
str
|
Method for calculating thresholds ('quantile' or 'std'). |
is_fitted |
bool
|
Indicates if the detector has been fitted with reference data. |
ref_features_ |
list
|
List of features in the reference data. |
empirical_dist_ks_ |
dict
|
Empirical distributions of KS test statistics for each numeric feature in reference data. |
empirical_dist_chi2_ |
dict
|
Empirical distributions of Chi-Square test statistics for each categorical feature in reference data. |
empirical_dist_js_ |
dict
|
Empirical distributions of Jensen-Shannon distance for each feature in reference data (numeric and categorical). |
empirical_threshold_ks_ |
dict
|
Thresholds for KS statistics based on empirical distributions for each numeric feature in reference data. |
empirical_threshold_chi2_ |
dict
|
Thresholds for Chi-Square statistics based on empirical distributions for each categorical feature in reference data. |
empirical_threshold_js_ |
dict
|
Thresholds for Jensen-Shannon distance based on empirical distributions for each feature in reference data (numeric and categorical). |
n_chunks_reference_data_ |
int
|
Number of chunks in the reference data used during fitting to compute empirical distributions. |
ref_ecdf_ |
dict
|
Precomputed ECDFs for numeric features in the reference data. |
ref_bins_edges_ |
dict
|
Precomputed bin edges for numeric features in the reference data. |
ref_hist_ |
dict
|
Precomputed histograms for numeric features in the reference data. |
ref_probs_ |
dict
|
Precomputed normalized value counts (probabilities) for each category of categorical features in the reference data. |
ref_ranges_ |
dict
|
Min and max values for numeric features in the reference data. |
ref_categories_ |
dict
|
Unique categories for categorical features in the reference data. |
detectors_ |
dict
|
Dictionary of PopulationDriftDetector instances for each group when fitting/predicting on MultiIndex DataFrames. |
series_names_in_ |
list
|
List of series IDs present during fitting when using MultiIndex DataFrames. |
Notes
This implementation is inspired by NannyML's DriftDetector [1]_.
It is a lightweight version adapted for skforecast's needs: - It does not store the raw reference data, only the necessary precomputed information to calculate the statistics efficiently during prediction. - All empirical thresholds are calculated using the specified quantile from the empirical distributions obtained from the reference data chunks. - It includes checks for out of range values in numeric features and new categories in categorical features. - It supports multiple time series by fitting separate detectors for each series ID when provided with a MultiIndex DataFrame.
If user requires more advanced features, such as multivariate drift detection or data quality checks, consider using https://nannyml.readthedocs.io/en/stable/ directly.
References
.. [1] NannyML API Reference. https://nannyml.readthedocs.io/en/stable/tutorials/detecting_data_drift/univariate_drift_detection.html
Methods:
| Name | Description |
|---|---|
fit |
Fit the drift detector by calculating empirical distributions and thresholds |
predict |
Predict drift in new data by comparing the estimated statistics to |
get_thresholds |
Return a DataFrame with all computed thresholds per feature. |
Source code in skforecast/drift_detection/_population_drift.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
_repr_html_ ¶
_repr_html_()
HTML representation of the object. The "General Information" section is expanded by default.
Source code in skforecast/drift_detection/_population_drift.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
_reset_attributes ¶
_reset_attributes()
Reset all fitted attributes to their initial state.
Source code in skforecast/drift_detection/_population_drift.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
_create_chunks ¶
_create_chunks(X)
Split X into chunks based on chunk_size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas DataFrame
|
Data to be chunked. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
chunks |
list
|
List of DataFrames, each representing a chunk of the data. |
Source code in skforecast/drift_detection/_population_drift.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
_compute_js_with_leftover ¶
_compute_js_with_leftover(ref_probs, new_probs)
Compute Jensen-Shannon distance handling out-of-range/unseen values.
If new data contains values outside the reference range (numeric) or unseen categories (categorical), they won't be counted in the probability distribution, leading to a sum < 1. To ensure distributions are comparable, we add an extra bin for "leftover" probability mass in the new distribution and a corresponding zero bin in the reference distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref_probs
|
numpy ndarray
|
Probability distribution from reference data (histogram for numeric, normalized counts for categorical). |
required |
new_probs
|
numpy ndarray
|
Probability distribution from new data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
js_distance |
float
|
Jensen-Shannon distance between the two distributions. |
Source code in skforecast/drift_detection/_population_drift.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
_fit ¶
_fit(X)
Fit the drift detector by calculating empirical distributions and thresholds
from reference data. The empirical distributions are computed by chunking
the reference data according to the specified chunk_size and calculating
the statistics for each chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas DataFrame
|
Reference data used as the baseline for drift detection. |
required |
Source code in skforecast/drift_detection/_population_drift.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
fit ¶
fit(X)
Fit the drift detector by calculating empirical distributions and thresholds
from reference data. The empirical distributions are computed by chunking
the reference data according to the specified chunk_size and calculating
the statistics for each chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas DataFrame
|
Reference data used as the baseline for drift detection.
|
required |
Source code in skforecast/drift_detection/_population_drift.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | |
_predict ¶
_predict(X)
Predict drift in new data by comparing the estimated statistics to reference thresholds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas DataFrame
|
New data to compare against the reference. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
results |
pandas DataFrame
|
DataFrame with the drift detection results for each chunk. |
Source code in skforecast/drift_detection/_population_drift.py
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 | |
predict ¶
predict(X)
Predict drift in new data by comparing the estimated statistics to reference thresholds. Two dataframes are returned, the first one with detailed information of each chunk, the second only the total number of chunks where drift have been detected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas DataFrame
|
New data to compare against the reference. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
results |
pandas DataFrame
|
DataFrame with the drift detection results for each chunk. |
summary |
pandas DataFrame
|
Summary DataFrame with the total number and percentage of chunks with detected drift per feature (or per series_id and feature if MultiIndex), and the list of chunk IDs where drift was detected. |
Source code in skforecast/drift_detection/_population_drift.py
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 | |
_collect_attributes ¶
_collect_attributes()
Collect attributes for representation and inspection and update the instance dictionary with the collected values. For multi-series (when detectors_ is populated), attributes are aggregated into nested dictionaries keyed by detector names. For single-series, attributes remain unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self
|
|
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast/drift_detection/_population_drift.py
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 | |
get_thresholds ¶
get_thresholds()
Return a DataFrame with all computed thresholds per feature. For multi-series, returns thresholds per series_id and feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
thresholds |
pandas DataFrame
|
DataFrame with the computed thresholds per feature (and per series_id if MultiIndex was used during fitting). |
Source code in skforecast/drift_detection/_population_drift.py
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 | |