roll_skew | R Documentation |
Calculate a matrix of skewness estimates over a rolling look-back interval attached at the end points of a time series or a matrix.
roll_skew(
tseries,
startp = 0L,
endd = 0L,
step = 1L,
lookb = 1L,
stub = 0L,
method = "moment",
confl = 0.75
)
tseries |
A time series or a matrix of data. |
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 |
lookb |
The number of end points in the look-back interval
(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 specifying the type of the
skewness model (the default is |
confl |
The confidence level for calculating the quantiles of
returns (the default is |
The function roll_skew()
calculates a matrix of skewness
estimates over rolling look-back intervals attached at the end points of
the time series tseries
.
The function roll_skew()
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_skew()
, which
calculates the skewness.
See the function calc_skew()
for a description of the skewness
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 skewness 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_skew()
is implemented in RcppArmadillo
C++
code, which makes it several times faster than R
code.
A matrix of skewness 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)
# Define end points and start points
endd <- 1 + HighFreq::calc_endpoints(NROW(retp), step=25)
startp <- HighFreq::calc_startpoints(endd, lookb=3)
# Calculate the rolling skewness at 25 day end points, with a 75 day look-back
skewv <- HighFreq::roll_skew(retp, step=25, lookb=3)
# Calculate the rolling skewness using R code
skewr <- sapply(1:NROW(endd), function(it) {
HighFreq::calc_skew(retp[startp[it]:endd[it], ])
}) # end sapply
# Compare the skewness estimates
all.equal(drop(skewv), skewr, check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::roll_skew(retp, step=25, lookb=3),
Rcode=sapply(1:NROW(endd), function(it) {
HighFreq::calc_skew(retp[startp[it]:endd[it], ])
}),
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.