API Reference¶
Top-level API¶
Run bootstrap estimation.
Parameters¶
data : array-like or pandas Series/DataFrame
Observed sample.
statistic : callable
(array) -> float. If vectorized=True, must accept
(array_2d, axis=1) -> array_1d.
method : str
One of: bca, percentile, basic, studentized, poisson, bernoulli,
bayesian, subsampling, mbb, cbb, stationary, tapered, sieve,
wild, cluster, strata, cluster_strata.
ci_method : str or None
CI construction for generator-based methods that do not define a
specialized interval: "percentile" or "basic". Defaults to
"percentile". Bayesian, Bernoulli, and subsampling intervals are
not configurable through this parameter.
vectorized : bool
For percentile, basic, and BCa methods, call statistic as
statistic(batch, axis=1). Other methods reject this option.
n_jobs : int
Parallelism for jackknife in BCa (effective only for n >= 2000).
Other Parameters¶
weighted_statistic : callable
Required for custom Bayesian-bootstrap statistics. Called as
weighted_statistic(data, weights) for each Dirichlet draw.
subsample_size : int
Number of observations in each subsample.
rate : float
Convergence-rate exponent for subsampling. 0.5 means root-n.
prob : float
Inclusion probability for Bernoulli subsampling; strictly between 0 and 1.
n_inner : int
Number of inner resamples per outer sample for the studentized method.
Defaults to 100.
cluster_ids, strata_ids : array-like
Required together for method="cluster_strata". Each cluster must
belong to exactly one stratum; complete clusters are drawn within
their observed strata. The strata must represent a fixed design, not
categories selected after inspecting outcomes.
Source code in src/bootstrapx/api.py
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 | |
Container for bootstrap estimation results.
Source code in src/bootstrapx/api.py
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
interval(*, confidence_level=0.95, method='percentile')
¶
Reconstruct a percentile/basic interval without new statistic calls.
Specialized Bayesian, subsampling, Bernoulli and studentized results cannot be reinterpreted by this method. The result is not mutated.
Source code in src/bootstrapx/api.py
129 130 131 132 133 134 135 136 137 138 139 140 141 | |
to_dict(*, include_distribution=False)
¶
Return a compact summary mapping of the bootstrap result.
The potentially large bootstrap distribution is excluded by default.
Set include_distribution=True when it is needed for serialization
or downstream analysis. Arrays and extra metadata are copied so
callers cannot mutate the result through the returned dictionary.
Source code in src/bootstrapx/api.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
to_frame()
¶
Return a one-row pandas DataFrame with the result summary.
The bootstrap distribution is intentionally omitted to keep the frame
compact. Use :meth:to_dict with include_distribution=True when
the complete distribution is required.
Source code in src/bootstrapx/api.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
Cluster-design diagnostics¶
Describe independent units and one-class risk before bootstrapping.
cluster_ids identifies complete resampling units. strata_ids is
optional and must be constant within each cluster. If binary_labels
is provided, it must contain 0/1 labels constant within each cluster;
the report then computes the exact conditional probability that a whole-
cluster resample has only one class. It never chooses a method or decides
whether a reported interval has adequate coverage.
Source code in src/bootstrapx/design.py
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 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 159 160 161 162 163 164 165 166 167 | |
Observed design facts, not a guarantee of confidence-interval coverage.
clusters_per_stratum and binary_cluster_counts_by_stratum follow
NumPy's sorted order of unique stratum IDs. In the latter, each pair is
(number of label-0 clusters, number of label-1 clusters).
The optional one-class probabilities assume independent draws of whole clusters with replacement inside each observed stratum. They apply to a statistic that requires both binary classes (for example, ROC AUC), not to every statistic or to the original-sample collection process.
Source code in src/bootstrapx/design.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
to_dict()
¶
Return a detached mapping suitable for result metadata.
Source code in src/bootstrapx/design.py
42 43 44 | |
Experiment comparisons¶
Bootstrap an effect between control and treatment samples.
The observed effect and every resampled effect are calculated as
effect(statistic(control), statistic(treatment)). Independent samples
are resampled separately. With paired=True, both samples use the same
resampled indices. Supplying cluster IDs resamples complete clusters within
each arm and is mutually exclusive with paired analysis. For paired
clustered observations use paired=True, paired_cluster_ids=...:
both sides then receive the same complete-cluster row indices.
Parameters¶
control, treatment : array-like
Finite one-dimensional samples, or numeric matrices with
allow_2d=True. Matrix rows are observations and columns are jointly
observed features, always resampled together. Their order defines the
direction of every built-in effect. DataFrames are converted to NumPy;
matrix DataFrames must have unique column labels; two DataFrames must
have identical labels and order.
statistic : callable
Scalar function applied separately to each arm, array -> float.
For matrix input, receives a 2-D array and must still return one scalar.
effect : {"difference", "ratio", "relative_lift"} or callable
Transformation of the two arm statistics. A callable receives
(control_statistic, treatment_statistic) and returns one scalar.
Difference is treatment - control; ratio is
treatment / control; relative lift is
(treatment - control) / control.
method : {"percentile", "basic", "bca"}
Confidence-interval construction. BCa is not automatically more
accurate in finite samples. Ratio metrics with skewed data, correlated
numerator/denominator components, or few clusters can materially
undercover; compare methods against domain-relevant simulations and
treat results with few independent units cautiously.
paired : bool
Resample corresponding rows together. The samples must have equal
length. Arm-specific cluster IDs cannot be supplied; use
paired_cluster_ids for common clusters instead.
Pairing is positional: pandas indices are not used to align samples.
allow_2d : bool
Explicitly enable multicolumn input for composite scalar metrics.
Defaults to False, preserving the one-dimensional input contract.
Both arms must have the same dimensionality and feature count.
control_cluster_ids, treatment_cluster_ids : array-like or None
One cluster identifier per row. Both arrays are required for clustered
analysis; complete clusters are resampled independently within each
experiment arm. Labels must be scalar, non-missing, finite when numeric,
and mutually comparable within each arm. Mixed string/numeric labels
are rejected rather than coerced into one type.
control_unit_ids, treatment_unit_ids : array-like or None
Optional globally consistent identifiers for one-row-per-unit input.
Both are required together and must be unique within each arm.
Independent arms must be disjoint; paired arms must match in row order.
Cannot be combined with cluster IDs. No IDs are stored in the result.
Omitting IDs leaves correspondence/assignment validation to the caller.
paired_cluster_ids : array-like or None
Shared cluster identifier per aligned row, requires paired=True.
Resamples whole clusters jointly, not each side independently.
Currently supports only method="percentile" or "basic".
At least two clusters are required; this is not a safe-count threshold.
control_observation_ids, treatment_observation_ids : array-like or None
Optional unique row identifiers for paired comparisons, including
paired clusters. Both arrays must match in row order. No alignment,
sorting, joining or dropping is performed. Cannot be combined with
control_unit_ids/treatment_unit_ids. IDs are never stored.
metric_name, effect_unit : str or None
Optional non-empty reporting labels, for example "revenue/order"
and "USD/order" for a difference. Labels do not transform values or
verify the metric definition. Ratios/lifts are dimensionless.
n_resamples : int
Number of bootstrap effects.
batch_size : int or None
Number of resamples processed per technical batch. Changing it does
not change a seeded bootstrap distribution.
confidence_level : float
Requested interval level strictly between zero and one.
random_state : int, numpy.random.Generator, or None
Reproducible random-state source.
Returns¶
TwoSampleBootstrapResult Arm estimates, observed effect, interval, standard error, bootstrap distribution, and experiment-design metadata.
Notes¶
Ratio and relative-lift effects are rejected if the observed or any resampled control statistic is zero. Near-zero denominators are allowed because any fixed tolerance would depend on measurement units, but they can produce unstable intervals. No invalid replicates are silently discarded.
Source code in src/bootstrapx/comparison.py
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 | |
Result of a control/treatment bootstrap comparison.
Source code in src/bootstrapx/comparison.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 116 117 118 119 | |
theta_hat
property
¶
Alias for the observed effect estimate.
interval(*, confidence_level=0.95, method='percentile')
¶
Return a percentile/basic interval without evaluating the metric again.
This does not change the original result, design, or resample count. BCa cannot be reconstructed without its jackknife information.
Source code in src/bootstrapx/comparison.py
111 112 113 114 115 116 117 118 119 | |
to_dict(*, include_distribution=False)
¶
Return a compact, mutation-safe comparison summary.
Source code in src/bootstrapx/comparison.py
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 | |
to_frame()
¶
Return a one-row pandas DataFrame without the full distribution.
Source code in src/bootstrapx/comparison.py
100 101 102 103 104 105 106 107 108 109 | |
Composite metric helper¶
Added in 0.6.0. See Composite metrics.
Compute sum(sample[:, numerator]) / sum(sample[:, denominator]).
Column positions are non-negative integers. This is not the mean of
row-wise ratios, and it does not compare experiment arms: use it as the
statistic in bootstrap_two_sample(..., allow_2d=True). The callable
receives a numeric NumPy matrix, including when the original input is a
DataFrame. A zero total denominator or non-finite value raises an error;
no observations or replicates are discarded or stabilized.
Source code in src/bootstrapx/stats/metrics.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
Source code in src/bootstrapx/stats/confidence.py
28 29 30 31 32 33 34 35 36 37 38 39 | |
BootstrapResult.to_dict() excludes the potentially large bootstrap
distribution by default. Pass include_distribution=True when the full array
is required. BootstrapResult.to_frame() returns a compact one-row pandas
DataFrame.
TwoSampleBootstrapResult follows the same compact-export policy and adds arm
estimates, effect/design metadata, sample sizes, and optional cluster counts.
Added in 0.7.0¶
paired=True, paired_cluster_ids=...selects shared whole-cluster draws; specifymethod="percentile"or"basic". The default BCa is unsupported for this design and raises an error.control_observation_idsandtreatment_observation_idsoptionally validate unique, positionally matching row keys for paired comparisons. There is no automatic join/alignment or retention of IDs.extra["design"]reports counts/sizes for clustered results. Comparisons use"paired", or separate"control"/"treatment"reports.extra["distribution_diagnostics"]containsn_unique_valuesandis_degenerate. These are exact facts, not a coverage assessment.result.interval(confidence_level=0.95, method="percentile")returns a new percentile/basic interval from saved draws. It does not mutate the result or call the metric. BCa reconstruction and specialized one-sample interval reinterpretation are unsupported.
See Paired model comparison for a complete workflow.
Integrations¶
Bases: BaseCrossValidator
Bootstrap cross-validator compatible with scikit-learn's CV API.
Generates n_splits bootstrap train/test splits. Each training set
is a bootstrap resample of size n (with replacement); the test set
contains the out-of-bag (OOB) observations not selected for training.
Parameters¶
n_splits : int, default=200 Number of bootstrap iterations. random_state : int or np.random.Generator or None Seed for reproducibility.
Notes¶
- Usable with
cross_val_score,cross_validate,GridSearchCV. - OOB test set size ≈ 0.368 × n per split (Poisson approximation).
- Independent rows only: non-None
groupsare rejected. This splitter does not provide group-aware or time-series-safe validation. - For the 0.632 bootstrap estimator, average
0.368 * train_score + 0.632 * oob_scoreacross splits.
Source code in src/bootstrapx/compat/sklearn_cv.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 116 117 118 119 120 121 122 123 124 125 | |
Accessor registered as pd.Series.bootstrap.
Source code in src/bootstrapx/compat/pandas_accessor.py
43 44 45 46 47 48 49 50 51 52 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 | |
bca(statistic, n_resamples=9999, confidence_level=0.95, random_state=None)
¶
Shortcut for method='bca'.
Source code in src/bootstrapx/compat/pandas_accessor.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
ci(statistic, *, method='bca', n_resamples=9999, confidence_level=0.95, random_state=None, **kwargs)
¶
Run bootstrap and return a :class:~bootstrapx.BootstrapResult.
Source code in src/bootstrapx/compat/pandas_accessor.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
percentile(statistic, n_resamples=9999, confidence_level=0.95, random_state=None)
¶
Shortcut for method='percentile'.
Source code in src/bootstrapx/compat/pandas_accessor.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
Accessor registered as pd.DataFrame.bootstrap.
Applies bootstrap column-wise and returns a summary DataFrame.
Source code in src/bootstrapx/compat/pandas_accessor.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 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
ci(statistic, *, method='bca', n_resamples=9999, confidence_level=0.95, random_state=None, **kwargs)
¶
Alias for :meth:summary.
Source code in src/bootstrapx/compat/pandas_accessor.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
summary(statistic, *, method='bca', n_resamples=9999, confidence_level=0.95, random_state=None, **kwargs)
¶
Return a DataFrame with bootstrap CI summary for each column.
Returns¶
pd.DataFrame
Index: column names of the original DataFrame.
Columns: theta_hat, ci_low, ci_high, se, method.
Source code in src/bootstrapx/compat/pandas_accessor.py
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 | |