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
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 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 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 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 |
|
adverse_impact_ratio(y_pred, protected, control)
Bootstrap AIR. See rapidstats.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) |
Source code in python/rapidstats/_bootstrap.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
|
adverse_impact_ratio_at_thresholds(y_score, protected, control, thresholds=None, strategy='auto')
Bootstrap AIR at thresholds. See rapidstats.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
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 |
|
brier_loss(y_true, y_score)
Bootstrap Brier loss. See rapidstats.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
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
|
confusion_matrix(y_true, y_pred)
Bootstrap confusion matrix. See rapidstats.confusion_matrix for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
ArrayLike
|
Ground truth target |
required |
y_pred |
ArrayLike
|
Predicted target |
required |
Returns:
Type | Description |
---|---|
BootstrappedConfusionMatrix
|
A dataclass of confusion matrix metrics as (lower, mean, upper). See rapidstats._bootstrap.BootstrappedConfusionMatrix for more details. |
Source code in python/rapidstats/_bootstrap.py
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 |
|
confusion_matrix_at_thresholds(y_true, y_score, thresholds=None, metrics=DefaultConfusionMatrixMetrics, strategy='auto')
Bootstrap confusion matrix at thresholds. See rapidstats.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'
|
Returns:
Type | Description |
---|---|
DataFrame
|
A DataFrame of |
Raises:
Type | Description |
---|---|
NotImplementedError
|
When |
Source code in python/rapidstats/_bootstrap.py
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 |
|
max_ks(y_true, y_score)
Bootstrap Max-KS. See rapidstats.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) |
Source code in python/rapidstats/_bootstrap.py
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
|
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) |
Source code in python/rapidstats/_bootstrap.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
|
mean_squared_error(y_true, y_score)
Bootstrap MSE. See rapidstats.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) |
Source code in python/rapidstats/_bootstrap.py
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
|
roc_auc(y_true, y_score)
Bootstrap ROC-AUC. See rapidstats.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) |
Source code in python/rapidstats/_bootstrap.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
|
root_mean_squared_error(y_true, y_score)
Bootstrap RMSE. See rapidstats.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) |
Source code in python/rapidstats/_bootstrap.py
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
|
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) |
Source code in python/rapidstats/_bootstrap.py
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 |
|
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
53 54 55 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 |
|
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
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 |
|