View source: R/block_bootstrap.R
| block_bootstrap | R Documentation |
This function performs block bootstrap (moving or circular) to obtain a (1-\alpha)\% confidence-interval for the autocovariance function. It will also compute average autocovariance function across all bootstrapped estimates.
block_bootstrap(
X,
maxLag,
x = 0:length(X),
n_bootstrap = 100,
l = ceiling(length(X)^(1/3)),
estimator = standard_est,
type = c("autocovariance", "autocorrelation"),
alpha = 0.05,
boot_type = c("moving", "circular"),
parallel = FALSE,
ncores = parallel::detectCores() - 1,
cl_export = NULL,
boot_seed = NULL,
cl = NULL,
...
)
X |
A vector representing observed values of the time series. |
maxLag |
The maximum lag to compute the estimated autocovariance function at. |
x |
A vector of lag indices. Defaults to the sequence |
n_bootstrap |
The number of times to run moving block bootstrap. Defaults to 100. |
l |
The block length considered for bootstrap. Defaults to |
estimator |
The function name of the estimator to use. Defaults to |
type |
Compute either the 'autocovariance' or 'autocorrelation'. Defaults to 'autocovariance'. |
alpha |
The quantile used to compute the |
boot_type |
What type of block bootstrap should be used, either 'moving' for moving block bootstrap or 'circular' for circular block bootstrap. |
parallel |
Whether or not the bootstrap computations should be done in parallel or not. Defaults to |
ncores |
The number of cores to be used in the parallel bootstrap computations. Defaults to the number cores - 1 (threads if hyperthreading is available), calculated from |
cl_export |
A vector of any additional functions or variables to export for parallel computations. This may be required if |
boot_seed |
An integer seed for reproducibility. This is used for |
cl |
An optional cluster object created by |
... |
Optional named arguments to the chosen estimator. See the examples. |
This function performs block bootstrap to obtain a (1-\alpha)\% confidence-interval for the autocovariance function. It will also compute average autocovariance function across all bootstrapped estimates.
Moving block bootstrap can be described as follows.
For some times series X(1), X(2), \dots, X(n), construct k \in N overlapping blocks of the form
B_{i} = (X(i), \dots, X(i + \ell - 1)), where \ell \in \{1, \dots , n\} is the block length.
Randomly sample, with replacement, from the discrete uniform distribution with on \{1, \dots, n - \ell + 1\} to obtain a set of random starting locations I_{1}, \dots, I_{k}.
Construct a bootstrapped time series B_{1}^{\ast}, B_{2}^{\ast}, \dots, B_{k}^{\ast}, where B_{i}^{\ast} = B_{I_{i}}.
The bootstrapped time series is truncated to have length n, and will be of the form X^{\ast}(1), \dots , X^{\ast}(n).
The autocovariance function is then computed for the bootstrapped time series.
An alternative to moving block bootstrap is circular block bootstrap.
Circular block bootstrap uses the time series like a circle, that is, the observation at n + i is the same as the observation at location i.
For example, for the block B_{n - \ell + 2}, we obtain (X(n - \ell + 2) , \dots , X(n), X(n + 1)) is the same as (X(n - \ell + 2) , \dots , X(n), X(1)).
When performing random sampling to obtain starting locations, the set \{1, \dots, n\} is now considered.
The procedure for constructing the bootstrap time series after constructing the blocks and selecting the starting indices is the same as moving block bootstrap.
This process is repeated n_bootstrap times to obtain n_boostrap estimates of the autocovariance function using the bootstrapped time series, for which the average autocovariance function
and the (1 - \alpha)\% confidence intervals are constructed pointwise for each lag.
The choice of the block length, \ell , depends on the degree of dependence present in the time series. If the time series has a high degree of dependence, a larger block size should be chosen to ensure the dependency structure is maintained within the block.
Any estimator of the autocovariance function can be used in this function, including a custom estimator not in the package, see the examples.
When parallel = TRUE, L'Ecuyer-CMRG is used to ensure independent random numbers across workers. The serial version uses the Mersenne-Twister algorithm.
A BootEsts S3 object (list) with the following values
acf_avgA numeric vector containing the average autocovariance/autocorrelation bootstrap estimate.
lagsA numeric vector containing the lag indices used to compute the estimates on.
acf_origA numeric vector containing the nonbootstrapped autocovariance/autocorrelation estimate.
acf_matA numeric matrix of the a matrix of all of the bootstrap estimates.
conf_lowerA numeric vector containing the lower bounds for the estimated pointwise confidence interval.
conf_upperA numeric vector containing the upper bounds for the estimated pointwise confidence interval.
est_typeThe type of estimate, namely 'autocorrelation' or 'autocovariance', this depends on the argument type.
est_usedThe estimator function used, this depends on the argument estimator.
boot_typeThe type of estimate, namely 'moving' or 'circular', this depends on the argument boot_type.
alphaA numeric value, which is the \alpha value used to compute the confidence intervals, this depends on the argument alpha.
Chapters 2.5 and 2.7 in Lahiri, S. N. (2003). Resampling Methods for Dependent Data. Springer. https://doi.org/10.1007/978-1-4757-3803-2
Künsch, H. R. (1989). The Jackknife and the Bootstrap for General Stationary Observations. The Annals of Statistics 17(3), 1217-1241. https://doi.org/10.1214/aos/1176347265
Politis, D. N. & Romano, J. P. (1991). A Circular Block-Resampling Procedure for Stationary Data. In R. LePage & L. Billard, eds, Exploring the Limits of Bootstrap, Wiley, 263-270.
X <- c(1, 2, 3, 3, 2, 1)
block_bootstrap(X, 4, n_bootstrap = 3, l = 2, type = 'autocorrelation')
block_bootstrap(X, 4, n_bootstrap = 3, l = 2, type = 'autocovariance')
block_bootstrap(X, 4, n_bootstrap = 3, l = 2, estimator=tapered_est,
rho = 0.5, window_name = 'blackman', window_params = c(0.16),
type='autocorrelation')
my_cov_est <- function(X, maxLag) {
n <- length(X)
covVals <- rep(0, maxLag + 1)
for(h in 0:maxLag) {
covVals_t <- (X[1:(n-h)] - mean(X)) * (X[(1+h):n] - mean(X))
covVals[h] <- sum(covVals_t) / (n - h)
}
return(covVals)
}
block_bootstrap(X, 4, n_bootstrap = 3, l = 2, estimator=my_cov_est)
plot(LakeHuron, main="Lake Huron Levels", ylab="Feet")
X <- as.vector(LakeHuron)
block_bootstrap(X, 20, n_bootstrap = 100, l = 40, type = 'autocorrelation')
block_bootstrap(X, 20, n_bootstrap = 100, l = 40, type = 'autocorrelation')
block_bootstrap(X, 20, n_bootstrap = 100, l = 40, estimator=tapered_est,
rho = 0.5, window_name = 'blackman', window_params = c(0.16),
type='autocorrelation')
my_cov_est <- function(X, maxLag) {
n <- length(X)
covVals <- rep(0, maxLag + 1)
for(h in 0:maxLag) {
covVals_t <- (X[1:(n-h)] - mean(X)) * (X[(1+h):n] - mean(X))
covVals[h] <- sum(covVals_t) / (n - h)
}
return(covVals)
}
block_bootstrap(X, 20, n_bootstrap = 100, l = 40, estimator = my_cov_est,
type = 'autocorrelation')
## Not run:
library(parallel)
X <- c(1, 2, 3, 3, 2, 1)
my_cl <- makePSOCKcluster(2)
block_bootstrap(X, 4, n_bootstrap = 1000, l = 3, parallel = TRUE, cl = my_cl)
stopCluster(my_cl)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.