| roll_moment | R Documentation |
Calculate a matrix of moment values over a rolling look-back interval attached at the end points of a time series or a matrix.
roll_moment(
timeser,
funame = "calc_mean",
method = "moment",
confl = 0.75,
startp = 0L,
endd = 0L,
step = 1L,
lookb = 1L,
stub = 0L
)
timeser |
A time series or a matrix of data. |
funame |
A character string specifying the moment
function (the default is |
method |
A character string specifying the type of the
model for the moment (the default is |
confl |
The confidence level for calculating the quantiles of
returns (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 |
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 |
The function roll_moment() calculates a matrix of moment
values, over rolling look-back intervals attached at the end points of the
time series timeser.
The function roll_moment() performs a loop over the end points, and
at each end point it subsets the time series timeser over a
look-back interval equal to lookb number of end points.
It passes the subset time series to the function specified by the argument
funame, which calculates the statistic.
See the functions calc_*() for a description of the different
moments.
The function name must be one of the following:
"calc_mean" for the estimator of the mean (location),
"calc_var" for the estimator of the dispersion (variance),
"calc_skew" for the estimator of the skewness,
"calc_kurtosis" for the estimator of the kurtosis.
(The default is the funame = "calc_mean").
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 timeser
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_moment() calls the function calc_momptr()
to calculate a pointer to a moment function from the function name
funame (string). The function pointer is used internally in the
C++ code, but the function calc_momptr() is not exported to
R.
The function roll_moment() is implemented in RcppArmadillo
C++ code, which makes it several times faster than R code.
A matrix with the same number of columns as the input time
series timeser, 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
var_rollfun <- HighFreq::roll_moment(retp, fun="calc_var", step=25, lookb=3)
# Calculate the rolling variance using roll_var()
var_roll <- HighFreq::roll_var(retp, step=25, lookb=3)
# Compare the two methods
all.equal(var_rollfun, var_roll, check.attributes=FALSE)
# Define end points and start points
endd <- HighFreq::calc_endpoints(NROW(retp), step=25)
startp <- HighFreq::calc_startpoints(endd, lookb=3)
# Calculate the rolling variance using RcppArmadillo
var_rollfun <- HighFreq::roll_moment(retp, fun="calc_var", startp=startp, endd=endd)
# Calculate the rolling variance using R code
var_roll <- sapply(1:NROW(endd), function(it) {
var(retp[startp[it]:endd[it]+1, ])
}) # end sapply
var_roll[1] <- 0
# Compare the two methods
all.equal(drop(var_rollfun), var_roll, check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::roll_moment(retp, fun="calc_var", startp=startp, endd=endd),
Rcode=sapply(1:NROW(endd), function(it) {
var(retp[startp[it]:endd[it]+1, ])
}),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
## End(Not run) # end dontrun
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.