diff_vec: Calculate the differences between the neighboring elements of...

View source: R/RcppExports.R

diff_vecR Documentation

Calculate the differences between the neighboring elements of a single-column time series or a vector.

Description

Calculate the differences between the neighboring elements of a single-column time series or a vector.

Usage

diff_vec(tseries, lagg = 1L, pad_zeros = TRUE)

Arguments

tseries

A single-column time series or a vector.

lagg

An integer equal to the number of time periods to lag when calculating the differences (the default is lagg = 1).

pad_zeros

Boolean argument: Should the output vector be padded (extended) with zeros, in order to return a vector of the same length as the input? (the default is pad_zeros = TRUE)

Details

The function diff_vec() calculates the differences between the input time series or vector and its lagged version.

The argument lagg specifies the number of lags. For example, if lagg=3 then the differences will be taken between each element minus the element three time periods before it (in the past). The default is lagg = 1.

The argument pad_zeros specifies whether the output vector should be padded (extended) with zeros at the front, in order to return a vector of the same length as the input. The default is pad_zeros = TRUE. The padding operation can be time-consuming, because it requires the copying the data in memory.

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

Value

A column vector containing the differences between the elements of the input vector.

Examples

## Not run: 
# Create a vector of random returns
retp <- rnorm(1e6)
# Compare diff_vec() with rutils::diffit()
all.equal(drop(HighFreq::diff_vec(retp, lagg=3, pad=TRUE)),
  rutils::diffit(retp, lagg=3))
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
  Rcpp=HighFreq::diff_vec(retp, lagg=3, pad=TRUE),
  Rcode=rutils::diffit(retp, lagg=3),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)


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