Bootstrap¶
Block bootstrap confidence intervals for trend analysis: resampling, bias correction, p-values, and likelihood descriptors.
wrtds.bootstrap
¶
Block bootstrap confidence intervals for WRTDS trend analysis.
Implements the bootstrap uncertainty estimation from the R EGRETci package. The key idea is to block-resample the water-quality sample DataFrame (preserving temporal autocorrelation), re-fit WRTDS on each replicate, and build an empirical distribution of the trend estimates.
Bias correction follows the classical bootstrap formula::
corrected = 2 * original_estimate - bootstrap_replicate
P-values use two-sided linear interpolation at zero. Confidence intervals use the Weibull plotting-position quantile (type 6 in R).
block_resample(sample, block_length=200, rng=None)
¶
Block bootstrap resample of the sample DataFrame.
Blocks are defined by Julian days. A block of block_length days captures all samples within that contiguous window. Blocks are drawn with replacement until the resampled dataset has at least as many rows as the original, then trimmed to match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Populated sample DataFrame (must have |
required | |
block_length
|
Block length in days (default 200). |
200
|
|
rng
|
Optional :class: |
None
|
Returns:
| Type | Description |
|---|---|
|
Resampled DataFrame with the same number of rows as input, |
|
|
sorted by Julian date. May contain duplicate observations. |
Source code in wrtds/bootstrap.py
pval(s)
¶
Compute two-sided p-value from a bootstrap distribution.
Uses linear interpolation between the largest negative and smallest
positive bootstrap replicates to estimate where zero falls in the
empirical distribution. Matches the pVal function in R EGRETci.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
1-D array of bootstrap replicate values. |
required |
Returns:
| Type | Description |
|---|---|
|
Two-sided p-value (float). |
Source code in wrtds/bootstrap.py
likelihood_descriptor(likelihood)
¶
Map a likelihood value to a descriptive word.
Uses the EGRETci convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
likelihood
|
Probability in [0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
|
String descriptor. |
Source code in wrtds/bootstrap.py
bootstrap_pairs(sample, daily, year1, year2, n_boot=100, block_length=200, window_side=7, pa_start=10, pa_long=12, fit_params=None, seed=None)
¶
Block bootstrap CI for pairwise trend comparison.
For each bootstrap replicate:
- Block-resample the sample DataFrame.
- Estimate two 1-year surfaces and run trend decomposition.
- Apply bias correction:
corrected = 2 * original - bootstrap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Populated sample DataFrame. |
required | |
daily
|
Populated daily DataFrame. |
required | |
year1
|
First comparison year. |
required | |
year2
|
Second comparison year. |
required | |
n_boot
|
Number of bootstrap replicates. |
100
|
|
block_length
|
Block length in days for resampling (default 200). |
200
|
|
window_side
|
Half-window for generalized flow normalisation. |
7
|
|
pa_start
|
Period of analysis start month. |
10
|
|
pa_long
|
Period of analysis length in months. |
12
|
|
fit_params
|
Dict of regression parameters
( |
None
|
|
seed
|
Optional integer seed for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
|
Dict with keys: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Source code in wrtds/bootstrap.py
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 | |
bootstrap_groups(daily, sample, surfaces, surface_index, group1_years, group2_years, n_boot=100, block_length=200, window_side=7, pa_start=10, pa_long=12, fit_params=None, seed=None)
¶
Block bootstrap CI for group trend comparison.
For each bootstrap replicate:
- Block-resample the sample DataFrame.
- Re-estimate the full surface from the resampled data.
- Run group trend decomposition.
- Apply bias correction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
daily
|
Populated daily DataFrame. |
required | |
sample
|
Populated sample DataFrame. |
required | |
surfaces
|
3-D surfaces array (original). |
required | |
surface_index
|
Grid parameters dict (original). |
required | |
group1_years
|
|
required | |
group2_years
|
|
required | |
n_boot
|
Number of bootstrap replicates. |
100
|
|
block_length
|
Block length in days (default 200). |
200
|
|
window_side
|
Half-window for generalized flow normalisation. |
7
|
|
pa_start
|
Period of analysis start month. |
10
|
|
pa_long
|
Period of analysis length in months. |
12
|
|
fit_params
|
Dict of regression parameters. |
None
|
|
seed
|
Optional integer seed for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
|
Dict with same keys as :func: |
Source code in wrtds/bootstrap.py
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 | |