roll_sumw: Calculate the rolling weighted sums over a _time series_ or a...

View source: R/RcppExports.R

roll_sumwR Documentation

Calculate the rolling weighted sums over a time series or a matrix using Rcpp.

Description

Calculate the rolling weighted sums over a time series or a matrix using Rcpp.

Usage

roll_sumw(tseries, endd = NULL, lookb = 1L, stub = NULL, weightv = NULL)

Arguments

tseries

A time series or a matrix.

endd

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

lookb

The length of the look-back interval, equal to the number of data points included in calculating the rolling sum (the default is lookb = 1).

stub

An integer value equal to the first end point for calculating the end points (the default is stub = NULL).

weightv

A single-column matrix of weights (the default is weightv = NULL).

Details

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.

Value

A matrix with the same dimensions as the input argument tseries.

Examples

## 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)


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