calc_ranks_stl: Calculate the ranks of the elements of a single-column _time...

View source: R/RcppExports.R

calc_ranks_stlR Documentation

Calculate the ranks of the elements of a single-column time series, matrix, or a vector using RcppArmadillo.

Description

Calculate the ranks of the elements of a single-column time series, matrix, or a vector using RcppArmadillo.

Usage

calc_ranks_stl(tseries)

Arguments

tseries

A single-column time series, matrix, or a vector.

Details

The function calc_ranks_stl() calculates the ranks of the elements of a single-column time series, matrix, or a vector. The function calc_ranks_stl() is slightly faster than the function calc_ranks().

The permutation index is an integer vector which sorts a given vector into ascending order. The permutation index of the permutation index is the reverse permutation index, because it sorts the vector from ascending order back into its original unsorted order. The ranks of the elements are equal to the reverse permutation index. The function calc_ranks() calculates the reverse permutation index.

The ranks produced by calc_ranks_stl() start at zero, following the C++ convention.

The STL C++ function std::sort() sorts a vector into ascending order. It can also be used to calculate the permutation index which sorts the vector into an ascending order.

The function calc_ranks_stl() calls the function std::sort() twice: First, it calculates the permutation index which sorts the vector tseries into ascending order. Second, it calculates the permutation index of the permutation index, which are the ranks (the reverse permutation index) of the vector tseries.

Value

An integer vector with the ranks of the elements of tseries.

Examples

## Not run: 
# Create a vector of data
datav <- rnorm(1e3)
# Calculate the ranks of the elements using R code and RcppArmadillo
all.equal(rank(datav), drop(HighFreq::calc_ranks_stl(datav))+1)
# Compare the speed of R code with RcppArmadillo
library(microbenchmark)
summary(microbenchmark(
  Rcode=rank(datav),
  Rcpp=HighFreq::calc_ranks_stl(datav),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)


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