roll_sumep: Calculate the rolling sums at the end points of a _time...

View source: R/RcppExports.R

roll_sumepR Documentation

Calculate the rolling sums at the end points of a time series or a matrix.

Description

Calculate the rolling sums at the end points of a time series or a matrix.

Usage

roll_sumep(tseries, startp = 0L, endd = 0L, step = 1L, lookb = 1L, stub = 0L)

Arguments

tseries

A time series or a matrix.

startp

An integer vector of start points (the default is startp = 0).

endd

An integer vector of end points (the default is endd = 0).

step

The number of time periods between the end points (the default is step = 1).

lookb

The number of end points in the look-back interval (the default is lookb = 1).

stub

An integer value equal to the first end point for calculating the end points.

Details

The function roll_sumep() calculates the rolling sums at the end points of the time series tseries.

The function roll_sumep() is implemented in RcppArmadillo C++ code, which makes it several times faster than R code.

Value

A matrix with the same number of columns as the input time series tseries, and the number of rows equal to the number of end points.

Examples

## Not run: 
# Calculate historical returns
retp <- na.omit(rutils::etfenv$returns[, c("VTI", "IEF")])
# Define end points at 25 day intervals
endd <- HighFreq::calc_endpoints(NROW(retp), step=25)
# Define start points as 75 day lag of end points
startp <- HighFreq::calc_startpoints(endd, lookb=3)
# Calculate rolling sums using Rcpp
sumc <- HighFreq::roll_sumep(retp, startp=startp, endd=endd)
# Calculate rolling sums using R code
sumr <- sapply(1:NROW(endd), function(ep) {
colSums(retp[(startp[ep]+1):(endd[ep]+1), ])
  })  # end sapply
sumr <- t(sumr)
all.equal(sumc, sumr, check.attributes=FALSE)

## End(Not run)


algoquant/HighFreq documentation built on Feb. 9, 2024, 8:15 p.m.