Skip to contents

Two stochastic resamplers for generating synthetic records of arbitrary length.

Usage

ts_resample_knn(ts, time_steps, k, seed = 12345)

ts_resample_block_bootstrap(ts, time_steps, block_size, seed = 12345)

Arguments

ts

a corehydro_ts.

time_steps

the length of the synthetic record.

k

the number of nearest neighbours. Lall and Sharma suggest floor(sqrt(n)).

seed

integer PRNG seed. Default 12345.

block_size

the resampling block length.

Value

a corehydro_ts of length time_steps.

Details

ts_resample_knn() is the conditional k-nearest-neighbour bootstrap of Lall and Sharma (1996): at each step it finds the k historical observations closest to the current value, picks one at random, and advances to whatever historically came NEXT. That conditioning is what preserves the lag-1 structure – returning the neighbour's own value instead would collapse the trajectory toward its starting point.

ts_resample_block_bootstrap() draws contiguous blocks of block_size observations uniformly with replacement and concatenates them. It preserves the marginal distribution and the within-block dependence, at the cost of a discontinuity at each block boundary.

Both draw from the generator inside the shared C++ core, so a seeded call gives bit-identical results in R and Python.

References

Lall, U. and Sharma, A. (1996). A nearest neighbor bootstrap for resampling hydrologic time series. Water Resources Research 32(3), 679-693.

Kuensch, H.R. (1989). The jackknife and the bootstrap for general stationary observations. Annals of Statistics 17(3), 1217-1241.

Examples

ts <- time_series(as.Date("2000-01-01") + 0:29, sin(seq(0, 6, length.out = 30)) * 10 + 20)
ts_resample_knn(ts, time_steps = 10, k = 5, seed = 42)
#> <corehydro_ts> 10 ordinates, interval "one_day"
#>   2000-01-01 to 2000-01-10
#>   2000-01-01  24.02085
#>   2000-01-02  20.38135
#>   2000-01-03  22.05424
#>   2000-01-04  22.05424
#>   2000-01-05  20.38135
#>   ... 5 more