calc_mean: Calculate the mean (location) of the columns of a _time...

View source: R/RcppExports.R

calc_meanR Documentation

Calculate the mean (location) of the columns of a time series or a matrix using RcppArmadillo.

Description

Calculate the mean (location) of the columns of a time series or a matrix using RcppArmadillo.

Usage

calc_mean(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 mean (location) 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_mean() calculates the mean (location) values of the columns of the time series tseries using C++ RcppArmadillo code.

If method = "moment" (the default) then calc_mean() calculates the location as the mean - the first moment of the data.

If method = "quantile" then it calculates the location \bar{r} as the average of the quantiles as follows:

\bar{r} = \frac{q_{\alpha} + q_{1-\alpha}}{2}

Where \alpha is the confidence level for calculating the quantiles (argument confl).

If method = "nonparametric" then it calculates the location as the median.

The code examples below compare the function calc_mean() with the mean (location) calculated using R code.

Value

A single-row matrix with the mean (location) of the columns of tseries.

Examples

## Not run: 
# Calculate historical returns
retp <- na.omit(rutils::etfenv$returns[, c("XLP", "VTI")])
# Calculate the column means in RcppArmadillo
HighFreq::calc_mean(retp)
# Calculate the column means in R
sapply(retp, mean)
# Compare the values
all.equal(drop(HighFreq::calc_mean(retp)), 
  sapply(retp, mean), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
  Rcpp=HighFreq::calc_mean(retp),
  Rcode=sapply(retp, mean),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary
# Calculate the quantile mean (location)
HighFreq::calc_mean(retp, method="quantile", confl=0.9)
# Calculate the quantile mean (location) in R
colSums(sapply(retp, quantile, c(0.9, 0.1), type=5))
# Compare the values
all.equal(drop(HighFreq::calc_mean(retp, method="quantile", confl=0.9)), 
  colSums(sapply(retp, quantile, c(0.9, 0.1), type=5)), 
  check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
summary(microbenchmark(
  Rcpp=HighFreq::calc_mean(retp, method="quantile", confl=0.9),
  Rcode=colSums(sapply(retp, quantile, c(0.9, 0.1), type=5)),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary
# Calculate the column medians in RcppArmadillo
HighFreq::calc_mean(retp, method="nonparametric")
# Calculate the column medians in R
sapply(retp, median)
# Compare the values
all.equal(drop(HighFreq::calc_mean(retp, method="nonparametric")), 
  sapply(retp, median), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
summary(microbenchmark(
  Rcpp=HighFreq::calc_mean(retp, method="nonparametric"),
  Rcode=sapply(retp, median),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)


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