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

View source: R/RcppExports.R

calc_ranksR 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(tseries)

Arguments

tseries

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

Details

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

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() start at zero, following the C++ convention.

The Armadillo function arma::sort_index() calculates the permutation index which sorts a given vector into an ascending order. Applying the function arma::sort_index() twice:
arma::sort_index(arma::sort_index()),
calculates the reverse permutation index to sort the vector from ascending order back into its original unsorted order.

The function calc_ranks() calls the Armadillo function arma::sort_index() twice to calculate the reverse permutation index, to sort the vector from ascending order back into its original unsorted order.

Value

An integer vector with the ranks of the elements of the 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(datav))+1)
# Compare the speed of R code with RcppArmadillo
library(microbenchmark)
summary(microbenchmark(
  Rcode=rank(datav),
  Rcpp=calc_ranks(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.