recall_guard.core.bootstrap
recall_guard.core.bootstrap
Bootstrap confidence-interval helper for the honest-model-ranking harness.
Implements the design's core.bootstrap interface (Req 6.1, 6.3, 6.5):
bootstrap_ci(samples, statistic, n_resamples=1000, confidence=0.95, seed=0)returns(point, lo, hi)wherepoint = statistic(samples)is computed once on the original sample andlo/hicome from the percentile bootstrap overn_resamplesresamples drawn with replacement.- Determinism is guaranteed by
numpy.random.default_rng(seed); resamples are drawn via index sampling so thatsamplesmay contain arbitrary Python objects (the design allowsSequence[T]for any T). - Degenerate cases:
len(samples) == 0->ValueError.len(samples) == 1->(point, point, point).- Resamples on which
statisticraisesValueErroror returns a non-finite value (NaN/inf) are dropped; a singlelogging.WARNINGperbootstrap_cicall summarises the count. - If every resample is dropped ->
(point, point, point)plus a WARNING describing the degenerate result.
The implementation is hand-rolled with numpy; scipy is intentionally
avoided per the "Build vs Adopt" design entry on bootstrap.
bootstrap_ci
bootstrap_ci(
samples,
statistic,
n_resamples=1000,
confidence=0.95,
seed=0,
)
Compute a percentile bootstrap CI for statistic over samples.
Args:
samples: Sequence of arbitrary objects (may be ints, floats, tuples,
dicts, dataclasses, etc.). Resampling uses index sampling, so the
element type does not need to be numpy-friendly.
statistic: Callable taking a resampled sequence and returning a float.
n_resamples: Number of bootstrap resamples (>=1). Default 1000 matches
the harness's Req 6.1 minimum.
confidence: Two-sided confidence level in (0, 1). Default 0.95.
seed: Seed for numpy.random.default_rng; same seed -> same output.
Returns:
Tuple (point, lo, hi):
- ``point = statistic(samples)`` (computed once on the original).
- ``lo``, ``hi`` are the lower / upper percentile bounds.
- Postcondition: ``lo <= point <= hi`` (clamped on tiny float drift).
Raises:
ValueError: if samples is empty, n_resamples < 1, or
confidence is outside (0, 1).
Source code in recall_guard/core/bootstrap.py
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 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 | |