roll_sumw | R Documentation |
Calculate the rolling weighted sums over a time series or a matrix using Rcpp.
roll_sumw(tseries, endd = NULL, lookb = 1L, stub = NULL, weightv = NULL)
tseries |
A time series or a matrix. |
endd |
An integer vector of end points (the default is
|
lookb |
The length of the look-back interval, equal to the
number of data points included in calculating the rolling sum (the default
is |
stub |
An integer value equal to the first end point for
calculating the end points (the default is |
weightv |
A single-column matrix of weights (the default
is |
The function roll_sumw()
calculates the rolling weighted sums over
the columns of the data tseries
.
The function roll_sumw()
calculates the rolling weighted sums as
convolutions of the columns of tseries
with the column
vector of weights using the Armadillo
function
arma::conv2()
. It performs a similar calculation to the standard
R
function stats::filter(x=retp, filter=weightv,
method="convolution", sides=1)
, but it can be many times faster, and it
doesn't produce any leading NA
values.
The function roll_sumw()
returns a matrix with the same
dimensions as the input argument tseries
.
The arguments weightv
, endd
, and stub
are
optional.
If the argument weightv
is not supplied, then simple sums are
calculated, not weighted sums.
If either the stub
or endd
arguments are supplied,
then the rolling sums are calculated at the end points.
If only the argument stub
is supplied, then the end points are
calculated from the stub
and lookb
arguments. The first
end point is equal to stub
and the end points are spaced
lookb
periods apart.
If the arguments weightv
, endd
, and stub
are
not supplied, then the sums are calculated over a number of data points
equal to lookb
.
The function roll_sumw()
is also several times faster than
rutils::roll_sum()
which uses vectorized R
code.
Technical note:
The function roll_sumw()
has arguments with default values equal to
NULL
, which are implemented in Rcpp
code.
A matrix with the same dimensions as the input argument
tseries
.
## Not run:
# First example
# Calculate historical returns
retp <- na.omit(rutils::etfenv$returns[, c("VTI", "IEF")])
# Define parameters
lookb <- 11
# Calculate rolling sums and compare with rutils::roll_sum()
sumc <- HighFreq::roll_sum(retp, lookb)
sumr <- rutils::roll_sum(retp, lookb)
all.equal(sumc, coredata(sumr), check.attributes=FALSE)
# Calculate rolling sums using R code
sumr <- apply(zoo::coredata(retp), 2, cumsum)
sumlag <- rbind(matrix(numeric(2*lookb), nc=2), sumr[1:(NROW(sumr) - lookb), ])
sumr <- (sumr - sumlag)
all.equal(sumc, sumr, check.attributes=FALSE)
# Calculate rolling sums at end points
stubv <- 21
sumc <- HighFreq::roll_sumw(retp, lookb, stub=stubv)
endd <- (stubv + lookb*(0:(NROW(retp) %/% lookb)))
endd <- endd[endd < NROW(retp)]
sumr <- apply(zoo::coredata(retp), 2, cumsum)
sumr <- sumr[endd+1, ]
sumlag <- rbind(numeric(2), sumr[1:(NROW(sumr) - 1), ])
sumr <- (sumr - sumlag)
all.equal(sumc, sumr, check.attributes=FALSE)
# Calculate rolling sums at end points - pass in endd
sumc <- HighFreq::roll_sumw(retp, endd=endd)
all.equal(sumc, sumr, check.attributes=FALSE)
# Create exponentially decaying weights
weightv <- exp(-0.2*(1:11))
weightv <- matrix(weightv/sum(weightv), nc=1)
# Calculate rolling weighted sum
sumc <- HighFreq::roll_sumw(retp, weightv=weightv)
# Calculate rolling weighted sum using filter()
retc <- filter(x=retp, filter=weightv, method="convolution", sides=1)
all.equal(sumc[-(1:11), ], retc[-(1:11), ], check.attributes=FALSE)
# Calculate rolling weighted sums at end points
sumc <- HighFreq::roll_sumw(retp, endd=endd, weightv=weightv)
all.equal(sumc, retc[endd+1, ], check.attributes=FALSE)
# Create simple weights equal to a 1 value plus zeros
weightv <- matrix(c(1, rep(0, 10)), nc=1)
# Calculate rolling weighted sum
weighted <- HighFreq::roll_sumw(retp, weightv=weightv)
# Compare with original
all.equal(coredata(retp), weighted, check.attributes=FALSE)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.