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_original
ARGridBootstrap.argridbootstrap_gpu
ARGridBootstrap.argridkernel!
ARGridBootstrap.b_est_mldivide
ARGridBootstrap.b_est_nox
ARGridBootstrap.b_est_original
ARGridBootstrap.b_est_stride
ARGridBootstrap.gridbootstrap
ARGridBootstrap.gridbootstrap_threaded
ARGridBootstrap.invsym
ARGridBootstrap.oneto
ARGridBootstrap.rngarray
ARGridBootstrap.simulate_estimate_arp
ARGridBootstrap.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 fory
a
: AR parametere
: values of for error term.ϵ = e[rindex(T)]]
rindex
function 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
y0
initial value of ya
AR(1) parametere
error terms to sample fromϵ[t] = e[rindex(1)]
ar::Val{P}
order of autoregressive model to estimaterindex
function that returns random index in 1:length(e)
Returns
θ
estimated coefficientsse
standard 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
y0
initial value of ya
AR(1) parametere
error terms to sample fromϵ[t] = e[rindex(1)]
ar::Val{P}
order of autoregressive model to estimaterindex
function that returns random index in 1:length(e)
Returns
θ
estimated coefficientsse
standard errors
Grid Bootstrap
# ARGridBootstrap.argridbootstrap_gpu
— Method.
argridbootstrap_gpu(e; αgrid = 0.84:(0.22/20):1.06,
=199, RealType = Float32) nboot
Computes grid bootstrap estimates for an AR(1) model.
For each α ∈ grid, repeatedly simulate data with parameter α and then compute an estimate.
Arguments
e
vector error terms that will be resampled with replacement to generate bootstrap samplegrid
grid of parameter values. For each value,nboot
datasets will be simulated and estimates computed.nboot
RealType
type of numbers for GPU computation. On many GPUs, Float32 will have better performance than Float64.
Returns
ba
hatα - α for each grid value and simulated datasett
t-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 × ngrid
array. Will be filled with bootstrap estimates of α grid values of true αbootq
:nboot × ngrid
array. Will be filled with bootstrap estimates of αbootse
:nboot × ngrid
array. 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 ofe
to use to generate bootstrap sample1αgrid
: lengthngrid
values of AR(1) parameter to perform bootstrap on.
Returns nothing, but modifies in place ba
, bootq
, and bootse
# ARGridBootstrap.gridbootstrap
— Function.
gridbootstrap(estimator, simulator,
::AbstractVector,
grid=199) nboot
Computes grid bootstrap estimates a single parameter model.
For each α ∈ grid, repeatedly simulate data with parameter α and then compute an estimate.
Arguments
estimator
function of output ofsimulator
that returns a 2-tuple containing an estimate of α and its standard error.simulator
function that givenα
simulates data that can be used to estimate αgrid
grid of parameter values. For each value,nboot
datasets will be simulated and estimates computed.nboot
Returns
ba
hatα - α for each grid value and simulated datasett
t-stat for each grid value and simulated dataset
# ARGridBootstrap.gridbootstrap_threaded
— Function.
gridbootstrap_threaded(estimator, simulator,
::AbstractVector,
grid=199, rng=rngarray(nthreads()) nboot
Computes grid bootstrap estimates a single parameter model.
Multithreaded version.
For each α ∈ grid, repeatedly simulate data with parameter α and then compute an estimate.
Arguments
estimator
function of output ofsimulator
that returns a 2-tuple containing an estimate of α and its standard error.simulator
function that givenα
andrng
, simulates data that can be used to estimate αgrid
grid of parameter values. For each value,nboot
datasets will be simulated and estimates computed.nboot
number of bootstrap simulations per grid point
Returns
ba
hatα - α for each grid value and simulated datasett
t-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.