Bootstrap
Bootstrap
Computes a two-sided bootstrap confidence interval of a statistic. Note that \( \alpha \) is then defined as \( \frac{1 - \text{confidence}}{2} \). Regardless of method, the result will be a three-tuple of (lower, mean, upper). The process is as follows:
- Resample 100% of the data with replacement for
iterations
- Compute the statistic on each resample
If the method is standard
,
- Compute the mean \( \hat{\theta} \) of the bootstrap statistics
- Compute the standard error of the bootstrap statistics. Note that the standard error of any statistic is defined as the standard deviation of its sampling distribution.
-
Compute the Z-score
\[ z_{\alpha} = \phi^{-1}(\alpha) \]where \( \phi^{-1} \) is the quantile, inverse CDF, or percent-point function
Then the "Standard" or "First-Order Normal Approximation" interval is
If the method is percentile
, we stop here and compute the interval of the
bootstrap distribution that is symmetric about the median and contains
confidence
of the bootstrap statistics. Then the "Percentile" interval is
where \( \hat{\theta}^{*} \) is the vector of bootstrap statistics.
If the method is basic
,
- Compute the statistic on the original data
- Compute the "Percentile" interval
Then the "Basic" or "Reverse Percentile" interval is
where \( \hat{\theta} \) is the statistic on the original data, \( PCI_u \) is the upper bound of the "Percentile" interval, and \( PCI_l \) is the lower bound of the "Percentile" interval.
If the method is BCa
,
- Compute the statistic on the original data \( \hat{\theta} \)
- Compute the statistic on the data with the \( i^{th} \) row deleted (jacknife)
-
Compute the bias correction factor as
\[ \hat{z_0} = \phi^{-1}( \frac{\sum_{i=1}^B \hat{\theta_i}^{*} \le \hat{\theta} + \sum_{i=1}^B \hat{\theta_i}^{*} \leq \hat{\theta}}{2 * B} ) \]where \( \hat{\theta}^{*} \) is the vector of bootstrap statistics and \( B \) is the length of that vector.
-
Compute the acceleration factor as
\[ \hat{a} = \frac{1}{6} \frac{ \sum_{i=1}^{N} (\hat{\theta_{(.)}} - \hat{\theta_i})^3 }{ \sum_{i=1}^{N} [(\hat{\theta_{(.)}} - \hat{\theta_i})^2]^{1.5} } \]where \( \hat{\theta_{(.)}} \) is the mean of the jacknife statistics and \( \hat{\theta_i} \) is the \( i^{th} \) element of the jacknife vector.
-
Compute the lower and upper percentiles as
\[ \alpha_l = \phi( \hat{z_0} + \frac{\hat{z_0} + z_{\alpha}}{1 - \hat{a}(\hat{z} + z_{\alpha})} ) \]and
\[ \alpha_u = \phi( \hat{z_0} + \frac{ \hat{z_0} + z_{1 - \alpha} }{ 1 - \hat{a}(\hat{z} + z_{1-\alpha}) } ) \]
Then the "BCa" or "Bias-Corrected and Accelerated" interval is
where \( \hat{\theta}^{*} \) is the vector of bootstrap statistics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterations |
int
|
How many times to resample the data, by default 1_000 |
1000
|
confidence |
float
|
The confidence level, by default 0.95 |
0.95
|
method |
Literal['standard', 'percentile', 'basic', 'BCa']
|
Whether to return the Percentile, Basic / Reverse Percentile, or Bias Corrected and Accelerated Interval, by default "percentile" |
'percentile'
|
seed |
Optional[int]
|
Seed that controls resampling. Set this to any integer to make results reproducible, by default None |
None
|
n_jobs |
Optional[int]
|
How many threads to run with. None means let the executor decide, and 1 means run sequentially, by default None |
None
|
chunksize |
Optional[int]
|
The chunksize for each thread. None means let the executor decide, by default None |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the method is not one of |
Examples:
import rapidstats
ci = rapidstats.Bootstrap(seed=208).mean([1, 2, 3])
Source code in python/rapidstats/_bootstrap.py
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 284 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 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 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 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 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 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 777 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 853 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 884 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 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 |
|
adverse_impact_ratio(y_pred, protected, control)
Bootstrap AIR. See rapidstats.metrics.adverse_impact_ratio for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_pred |
ArrayLike
|
Predicted target |
required |
protected |
ArrayLike
|
An array of booleans identifying the protected class |
required |
control |
ArrayLike
|
An array of booleans identifying the control class |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, upper) |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 |
|
adverse_impact_ratio_at_thresholds(y_score, protected, control, thresholds=None, strategy='auto')
Bootstrap AIR at thresholds. See rapidstats.metrics.adverse_impact_ratio_at_thresholds for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_score |
ArrayLike
|
Predicted scores |
required |
protected |
ArrayLike
|
An array of booleans identifying the protected class |
required |
control |
ArrayLike
|
An array of booleans identifying the control class |
required |
thresholds |
Optional[list[float]]
|
The thresholds to compute |
None
|
strategy |
LoopStrategy
|
Computation method, by default "auto" |
'auto'
|
Returns:
Type | Description |
---|---|
DataFrame
|
A DataFrame of |
Raises:
Type | Description |
---|---|
NotImplementedError
|
When |
Source code in python/rapidstats/_bootstrap.py
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 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 |
|
average_precision(y_true, y_score)
Bootstrap average precision. See rapidstats.metrics.average_precision for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_score |
ArrayLike
|
Predicted scores |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, upper) |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
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 777 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 |
|
brier_loss(y_true, y_score)
Bootstrap Brier loss. See rapidstats.metrics.brier_loss for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_score |
ArrayLike
|
Predicted scores |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, upper) |
Source code in python/rapidstats/_bootstrap.py
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 |
|
confusion_matrix(y_true, y_pred, beta=1.0)
Bootstrap confusion matrix. See rapidstats.metrics.confusion_matrix for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_pred |
ArrayLike
|
Predicted target |
required |
beta |
float
|
\( \beta \) to use in \( F_\beta \), by default 1 |
1.0
|
Returns:
Type | Description |
---|---|
BootstrappedConfusionMatrix
|
A dataclass of confusion matrix metrics as (lower, mean, upper). See rapidstats._bootstrap.BootstrappedConfusionMatrix for more details. |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
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 |
|
confusion_matrix_at_thresholds(y_true, y_score, thresholds=None, metrics=DefaultConfusionMatrixMetrics, strategy='auto', beta=1.0)
Bootstrap confusion matrix at thresholds. See rapidstats.metrics.confusion_matrix_at_thresholds for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_score |
ArrayLike
|
Predicted scores |
required |
thresholds |
Optional[list[float]]
|
The thresholds to compute |
None
|
metrics |
Iterable[ConfusionMatrixMetric]
|
The metrics to compute, by default DefaultConfusionMatrixMetrics |
DefaultConfusionMatrixMetrics
|
strategy |
LoopStrategy
|
Computation method, by default "auto" |
'auto'
|
beta |
float
|
\( \beta \) to use in \( F_\beta \), by default 1 |
1.0
|
Returns:
Type | Description |
---|---|
DataFrame
|
A DataFrame of |
Raises:
Type | Description |
---|---|
NotImplementedError
|
When |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
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 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 |
|
max_ks(y_true, y_score)
Bootstrap Max-KS. See rapidstats.metrics.max_ks for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_score |
ArrayLike
|
Predicted scores |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, upper) |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 |
|
mean(y)
Bootstrap mean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y |
ArrayLike
|
A 1D-array |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, upper) |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 |
|
mean_squared_error(y_true, y_score)
Bootstrap MSE. See rapidstats.metrics.mean_squared_error for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_score |
ArrayLike
|
Predicted scores |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, upper) |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 |
|
r2(y_true, y_score)
Bootstrap R2. See rapidstats.metrics.r2 for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_score |
ArrayLike
|
Predicted scores |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, upper) |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 |
|
roc_auc(y_true, y_score)
Bootstrap ROC-AUC. See rapidstats.metrics.roc_auc for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_score |
ArrayLike
|
Predicted scores |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, upper) |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
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 |
|
root_mean_squared_error(y_true, y_score)
Bootstrap RMSE. See rapidstats.metrics.root_mean_squared_error for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_score |
ArrayLike
|
Predicted scores |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, upper) |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 |
|
run(df, stat_func, **kwargs)
Run bootstrap for an arbitrary function that accepts a Polars DataFrame and returns a scalar real number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The data to pass to |
required |
stat_func |
StatFunc
|
A callable that takes a Polars DataFrame as its first argument and returns a scalar real number. |
required |
Returns:
Type | Description |
---|---|
ConfidenceInterval
|
A tuple of (lower, mean, higher) |
Added in version 0.1.0
Source code in python/rapidstats/_bootstrap.py
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 |
|
BootstrappedConfusionMatrix
dataclass
Result object returned by rapidstats.Bootstrap().confusion_matrix
.
See rapidstats.metrics.ConfusionMatrix for a detailed breakdown of the attributes stored in this class. However, instead of storing the statistic, it stores the bootstrapped confidence interval as (lower, mean, upper).
Source code in python/rapidstats/_bootstrap.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
to_polars()
Transform the dataclass to a long Polars DataFrame with columns
metric
, lower
, mean
, and upper
.
Returns:
Type | Description |
---|---|
DataFrame
|
A DataFrame with columns |
Source code in python/rapidstats/_bootstrap.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|