roll_var | R Documentation |
Calculate a matrix of dispersion (variance) estimates over a rolling look-back interval attached at the end points of a time series or a matrix.
roll_var(
tseries,
lookb = 1L,
startp = 0L,
endd = 0L,
step = 1L,
stub = 0L,
method = "moment",
confl = 0.75
)
tseries |
A time series or a matrix of data. |
lookb |
The number of end points in the look-back interval
(the default is |
startp |
An integer vector of start points (the default
is |
endd |
An integer vector of end points (the default is
|
step |
The number of time periods between the end points (the
default is |
stub |
An integer value equal to the first end point for
calculating the end points (the default is |
method |
A character string representing the type of the
measure of dispersion (the default is |
The function roll_var()
calculates a matrix of dispersion
(variance) estimates over rolling look-back intervals attached at the end
points of the time series tseries
.
The function roll_var()
performs a loop over the end points, and at
each end point it subsets the time series tseries
over a look-back
interval equal to lookb
number of end points.
It passes the subset time series to the function calc_var()
, which
calculates the dispersion.
See the function calc_var()
for a description of the dispersion
methods.
If the arguments endd
and startp
are not given then it
first calculates a vector of end points separated by step
time
periods. It calculates the end points along the rows of tseries
using the function calc_endpoints()
, with the number of time
periods between the end points equal to step
time periods.
For example, the rolling variance at 25
day end points, with a
75
day look-back, can be calculated using the parameters
step = 25
and lookb = 3
.
The function roll_var()
with the parameter step = 1
performs the same calculation as the function roll_var()
from
package
RcppRoll,
but it's several times faster because it uses RcppArmadillo
C++
code.
The function roll_var()
is implemented in RcppArmadillo
RcppArmadillo
C++
code, which makes it several times faster
than R
code.
A matrix dispersion (variance) estimates with the same number
of columns as the input time series tseries
, and the number of rows
equal to the number of end points.
## Not run:
# Define time series of returns using package rutils
retp <- na.omit(rutils::etfenv$returns$VTI)
# Calculate the rolling variance at 25 day end points, with a 75 day look-back
varv <- HighFreq::roll_var(retp, lookb=3, step=25)
# Compare the variance estimates over 11-period look-back intervals
all.equal(HighFreq::roll_var(retp, lookb=11)[-(1:10), ],
drop(RcppRoll::roll_var(retp, n=11)), check.attributes=FALSE)
# Compare the speed of HighFreq::roll_var() with RcppRoll::roll_var()
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::roll_var(retp, lookb=11),
RcppRoll=RcppRoll::roll_var(retp, n=11),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
# Compare the speed of HighFreq::roll_var() with TTR::runMAD()
summary(microbenchmark(
Rcpp=HighFreq::roll_var(retp, lookb=11, method="quantile"),
TTR=TTR::runMAD(retp, n = 11),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.