calc_skew: Calculate the skewness of the columns of a _time series_ or a...

View source: R/RcppExports.R

calc_skewR Documentation

Calculate the skewness of the columns of a time series or a matrix using RcppArmadillo.

Description

Calculate the skewness of the columns of a time series or a matrix using RcppArmadillo.

Usage

calc_skew(tseries, method = "moment", confl = 0.75)

Arguments

tseries

A time series or a matrix of data.

method

A character string specifying the type of the skewness model (the default is method = "moment" - see Details).

confl

The confidence level for calculating the quantiles of returns (the default is confl = 0.75).

Details

The function calc_skew() calculates the skewness of the columns of a time series or a matrix of data using C++ RcppArmadillo code.

If method = "moment" (the default) then calc_skew() calculates the skewness as the third moment of the data.

If method = "quantile" then it calculates the skewness \varsigma from the differences between the quantiles of the data as follows:

\varsigma = \frac{q_{\alpha} + q_{1-\alpha} - 2 q_{0.5}}{q_{\alpha} - q_{1-\alpha}}

Where \alpha is the confidence level for calculating the quantiles.

If method = "nonparametric" then it calculates the skewness as the difference between the mean of the data minus its median, divided by the standard deviation.

If the number of rows of tseries is less than 3 then it returns zeros.

The code examples below compare the function calc_skew() with the skewness calculated using R code.

Value

A single-row matrix with the skewness of the columns of tseries.

Examples

## Not run: 
# Define a single-column time series of returns
retp <- na.omit(rutils::etfenv$returns$VTI)
# Calculate the moment skewness
HighFreq::calc_skew(retp)
# Calculate the moment skewness in R
calc_skewr <- function(x) {
  x <- (x-mean(x))
  sum(x^3)/var(x)^1.5/NROW(x)
}  # end calc_skewr
all.equal(HighFreq::calc_skew(retp), 
  calc_skewr(retp), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
  Rcpp=HighFreq::calc_skew(retp),
  Rcode=calc_skewr(retp),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary
# Calculate the quantile skewness
HighFreq::calc_skew(retp, method="quantile", confl=0.9)
# Calculate the quantile skewness in R
calc_skewq <- function(x, a = 0.75) {
  	quantiles <- quantile(x, c(1-a, 0.5, a), type=5)
  	(quantiles[3] + quantiles[1] - 2*quantiles[2])/(quantiles[3] - quantiles[1])
}  # end calc_skewq
all.equal(drop(HighFreq::calc_skew(retp, method="quantile", confl=0.9)), 
  calc_skewq(retp, a=0.9), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
summary(microbenchmark(
  Rcpp=HighFreq::calc_skew(retp, method="quantile"),
  Rcode=calc_skewq(retp),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary
# Calculate the nonparametric skewness
HighFreq::calc_skew(retp, method="nonparametric")
# Compare HighFreq::calc_skew() with R nonparametric skewness
all.equal(drop(HighFreq::calc_skew(retp, method="nonparametric")), 
  (mean(retp)-median(retp))/sd(retp), 
  check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
summary(microbenchmark(
  Rcpp=HighFreq::calc_skew(retp, method="nonparametric"),
  Rcode=(mean(retp)-median(retp))/sd(retp),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)


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