Validation Bootstrap#
bootstrap
#
| FUNCTION | DESCRIPTION |
|---|---|
bootstrap |
Estimate score statistics via bootstrap resampling with replacement. |
Classes#
Functions#
bootstrap(model: Any, X: torch.Tensor, fit_score_fn: Callable, weights: torch.Tensor | None = None, target: torch.Tensor | None = None, n_bootstrap: int = 1000, sample_size: float | int | None = None, confidence_level: float = 0.95, random_state: int | None = None, device: str | torch.device | None = None, n_jobs: int = 1, backend: str = 'threading', verbose: int = 0, pre_dispatch: str = '2*n_jobs', return_samples: bool = False, return_estimator: bool = False, return_times: bool = True, error_score: float | str = 'raise', dtype: torch.dtype = torch.float32) -> dict[str, float | torch.Tensor]
#
Estimate score statistics via bootstrap resampling with replacement.
Performs repeated random sampling with replacement to estimate confidence intervals and standard errors of a scoring metric.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Fitted model to evaluate.
TYPE:
|
X
|
Input data tensor of shape (n_samples, n_features).
TYPE:
|
fit_score_fn
|
Scoring function with signature:
TYPE:
|
weights
|
Sample weights of shape
TYPE:
|
target
|
Target values of shape
TYPE:
|
n_bootstrap
|
Number of bootstrap samples to generate.
TYPE:
|
sample_size
|
Size of each bootstrap sample.
TYPE:
|
confidence_level
|
Confidence level for CI bounds (must be in (0, 1)).
TYPE:
|
random_state
|
Random seed for reproducibility.
TYPE:
|
device
|
Device for computation ('cpu', 'cuda', 'cuda:0', etc.).
Default uses device of
TYPE:
|
n_jobs
|
Number of parallel jobs. -1 uses all processors. Automatically set to 1 when using CUDA device.
TYPE:
|
backend
|
Joblib backend for parallelization.
TYPE:
|
verbose
|
Verbosity level for joblib.
TYPE:
|
pre_dispatch
|
Number of jobs to pre-dispatch for parallel execution.
TYPE:
|
return_samples
|
Whether to return individual bootstrap scores.
TYPE:
|
return_estimator
|
Whether to return fitted estimators.
TYPE:
|
return_times
|
Whether to include execution times per sample.
TYPE:
|
error_score
|
Behavior on scoring errors:
TYPE:
|
dtype
|
Data type for error score tensors.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, float | Tensor]
|
Dictionary with keys:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
ValueError
|
If |
Notes
Bootstrap distribution approximates the sampling distribution of the
statistic, enabling non-parametric confidence intervals. Standard bootstrap
uses full-size resamples; undersampling available via sample_size.
Examples:
Basic usage with fitted model:
>>> from spectre.decomposition import PrincipialCompotentAnalysis as PCA
>>> from spectre.validation import bootstrap
>>> import torch
>>>
>>> # Fit model
>>> pca = PCA(n_components=3)
>>> X = torch.randn(200, 20)
>>> pca.fit(X)
>>>
>>> # Define scoring function
>>> def score_fn(model, data):
... batch = data["X"]
... # Custom metric: negative reconstruction error
... pred = model.transform(batch.data)
... loss = torch.norm(batch.data - pred)
... return {"score": -loss}
>>>
>>> # Bootstrap evaluation
>>> stats = bootstrap(pca, X, fit_score_fn=score_fn, n_bootstrap=500)
>>> print(f"Mean: {stats['score_mean']:.3f}")
>>> print(f"95% CI: [{stats['score_ci_lower']:.3f}, {stats['score_ci_upper']:.3f}]")
With weighted samples and reduced sample size:
>>> weights = torch.ones(200)
>>> stats = bootstrap(
... pca,
... X,
... fit_score_fn=score_fn,
... weights=weights,
... sample_size=0.7, # 70% of original
... n_bootstrap=1000,
... )
Parallel execution on multi-core CPU:
>>> stats = bootstrap(
... pca,
... X,
... fit_score_fn=score_fn,
... n_bootstrap=1000,
... n_jobs=-1, # Use all available cores
... )
GPU-accelerated execution (no parallelization):
>>> X_gpu = X.cuda()
>>> pca_gpu = pca.to("cuda")
>>> stats = bootstrap(
... pca_gpu,
... X_gpu,
... fit_score_fn=score_fn,
... device="cuda",
... n_bootstrap=500,
... )
Source code in spectre/validation/bootstrap.py
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 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 153 154 155 156 157 158 159 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 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 | |