ARGridBootstrap.jl
This package implements the grid bootstrap of Hansen (1999).
For teaching purposes, multiple versions of the “same” function are included. The differences between these versions illustrate various techniques to improve performance. See Coding for Performance.
Functions
ARGridBootstrap.ar1_originalARGridBootstrap.argridbootstrap_gpuARGridBootstrap.argridkernel!ARGridBootstrap.b_est_mldivideARGridBootstrap.b_est_noxARGridBootstrap.b_est_originalARGridBootstrap.b_est_strideARGridBootstrap.gridbootstrapARGridBootstrap.gridbootstrap_threadedARGridBootstrap.invsymARGridBootstrap.onetoARGridBootstrap.rngarrayARGridBootstrap.simulate_estimate_arpARGridBootstrap.simulate_estimate_arp_lv
AR
# ARGridBootstrap.ar1_original — Function.
ar1_original(y0, a, e, rindex=T->rand(1:length(e), T))Simulate AR1 model by sampling errors from e with replacement.
y[t] = a*y[t-1] + ϵ[t]
Arguments
y0: initial value forya: AR parametere: values of for error term.ϵ = e[rindex(T)]]rindexfunction that returns random index in 1:length(e)
Returns
y: vector of lengthT = length(e)
# ARGridBootstrap.b_est_mldivide — Method.
b_est_mldivide(y)Estimate AR(1) model with intercept and time trend.
y[t] = θ[0] + θ[1]t + θ[2]y[t-1] + e[t]
Arguments
y: vector
Returns
θ: estimated coefficientsse: standard errorse: residuals
# ARGridBootstrap.b_est_nox — Method.
b_est_nox(y)Estimate AR(1) model with intercept and time trend.
y[t] = θ[0] + θ[1]t + θ[2]y[t-1] + e[t]
Arguments
y: vector
Returns
θ: estimated coefficientsse: standard errorse: residualas
# ARGridBootstrap.b_est_original — Method.
b_est_original(y)Estimate AR(1) model with intercept and time trend
y[t] = θ[0] + θ[1]t + θ[2]y[t-1] + e[t]
Arguments
y: vector
Returns
θ: estimated coefficientsse: standard errorse: residuals
# ARGridBootstrap.b_est_stride — Method.
b_est_stride(y)Estimate AR(1) model with intercept and time trend.
y[t] = θ[0] + θ[1]t + θ[2]y[t-1] + e[t]
Arguments
y: vector
Returns
θ: estimated coefficientsse: standard errorse: residualas
# ARGridBootstrap.invsym — Method.
fast inverse function for statically sized 3x3 StrideArray
# ARGridBootstrap.oneto — Method.
oneto(Val(N)) Creates at a compile time the tuple (1, 2, …, N)
# ARGridBootstrap.simulate_estimate_arp — Method.
simulate_estimate_arp(y0, a, e, ar::Val{P}, rindex=T->rand(1:length(e),T))Simulates and estimates an AR(P) model. y is simulated as
y[t] = a*y[t-1] + ϵ[t]
and the estimate of θ from
y[t] = θ[1] + θ[2]t + θ[3] y[t-1] + … + θ[P] y[t-P] + u[t]
is computed.
Arguments
y0initial value of yaAR(1) parametereerror terms to sample fromϵ[t] = e[rindex(1)]ar::Val{P}order of autoregressive model to estimaterindexfunction that returns random index in 1:length(e)
Returns
θestimated coefficientssestandard errors
# ARGridBootstrap.simulate_estimate_arp_lv — Method.
simulate_estimate_arp_lv(y0, a, e, ar::Val{P}, rindex=T->rand(1:length(e),T))Simulates and estimates an AR(P) model. Uses LoopVectorization.jl to produce fast code.
y is simulated as
y[t] = a*y[t-1] + ϵ[t]
and the estimate of θ from
y[t] = θ[1] + θ[2]t + θ[3] y[t-1] + … + θ[P] y[t-P] + u[t]
is computed.
Arguments
y0initial value of yaAR(1) parametereerror terms to sample fromϵ[t] = e[rindex(1)]ar::Val{P}order of autoregressive model to estimaterindexfunction that returns random index in 1:length(e)
Returns
θestimated coefficientssestandard errors
Grid Bootstrap
# ARGridBootstrap.argridbootstrap_gpu — Method.
argridbootstrap_gpu(e; αgrid = 0.84:(0.22/20):1.06,
nboot=199, RealType = Float32)Computes grid bootstrap estimates for an AR(1) model.
For each α ∈ grid, repeatedly simulate data with parameter α and then compute an estimate.
Arguments
evector error terms that will be resampled with replacement to generate bootstrap samplegridgrid of parameter values. For each value,nbootdatasets will be simulated and estimates computed.nbootRealTypetype of numbers for GPU computation. On many GPUs, Float32 will have better performance than Float64.
Returns
bahatα - α for each grid value and simulated datasettt-stat for each grid value and simulated dataset
# ARGridBootstrap.argridkernel! — Method.
argridkernel!(ba,bootq, bootse, ar::Val{P}, e, ei, αgrid)GPU kernel for simulation and estimation of AR(P) model.
Arguments (modified on return)
ba:nboot × ngridarray. Will be filled with bootstrap estimates of α grid values of true αbootq:nboot × ngridarray. Will be filled with bootstrap estimates of αbootse:nboot × ngridarray. Will be filled with standard errors of α for each bootstrap repetition
Arguments (not modified)
ar::Val{P}: autoregressive order for estimation. Simulated model will always be AR(1) with 0 intercept and time trend, but estimation will use an AR(P) model with intercept and time trend. Only the AR(1) parameter estimate is included inba,bootq, andbootse.e: error terms to draw with replacementei:nboot × ngrid × length(e)array of indices ofeto use to generate bootstrap sample1αgrid: lengthngridvalues of AR(1) parameter to perform bootstrap on.
Returns nothing, but modifies in place ba, bootq, and bootse
# ARGridBootstrap.gridbootstrap — Function.
gridbootstrap(estimator, simulator,
grid::AbstractVector,
nboot=199)Computes grid bootstrap estimates a single parameter model.
For each α ∈ grid, repeatedly simulate data with parameter α and then compute an estimate.
Arguments
estimatorfunction of output ofsimulatorthat returns a 2-tuple containing an estimate of α and its standard error.simulatorfunction that givenαsimulates data that can be used to estimate αgridgrid of parameter values. For each value,nbootdatasets will be simulated and estimates computed.nboot
Returns
bahatα - α for each grid value and simulated datasettt-stat for each grid value and simulated dataset
# ARGridBootstrap.gridbootstrap_threaded — Function.
gridbootstrap_threaded(estimator, simulator,
grid::AbstractVector,
nboot=199, rng=rngarray(nthreads())Computes grid bootstrap estimates a single parameter model.
Multithreaded version.
For each α ∈ grid, repeatedly simulate data with parameter α and then compute an estimate.
Arguments
estimatorfunction of output ofsimulatorthat returns a 2-tuple containing an estimate of α and its standard error.simulatorfunction that givenαandrng, simulates data that can be used to estimate αgridgrid of parameter values. For each value,nbootdatasets will be simulated and estimates computed.nbootnumber of bootstrap simulations per grid point
Returns
bahatα - α for each grid value and simulated datasettt-stat for each grid value and simulated dataset
# ARGridBootstrap.rngarray — Method.
rngarray(n)Create n rng states that will not overlap for 10^20 steps.
Note: this will be unneeded in Julia 1.3 when thread-safe RNG is included.