calc_kurtosis | R Documentation |
RcppArmadillo
.Calculate the kurtosis of the columns of a time series or a
matrix using RcppArmadillo
.
calc_kurtosis(tseries, method = "moment", confl = 0.75)
tseries |
A time series or a matrix of data. |
method |
A character string specifying the type of the
kurtosis model (the default is |
confl |
The confidence level for calculating the quantiles of
returns (the default is |
The function calc_kurtosis()
calculates the kurtosis of the columns
of the matrix tseries
using RcppArmadillo
C++
code.
If method = "moment"
(the default) then calc_kurtosis()
calculates the fourth moment of the data.
But it doesn't center the columns of tseries
because that requires
copying the matrix tseries
in memory, so it's time-consuming.
If method = "quantile"
then it calculates the skewness
\kappa
from the differences between the quantiles of the data as
follows:
\kappa = \frac{q_{\alpha} - q_{1-\alpha}}{q_{0.75} - q_{0.25}}
Where \alpha
is the confidence level for calculating the quantiles.
If method = "nonparametric"
then it calculates the kurtosis 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_kurtosis()
with the
kurtosis calculated using R
code.
A single-row matrix with the kurtosis of the columns of
tseries
.
## Not run:
# Define a single-column time series of returns
retp <- na.omit(rutils::etfenv$returns$VTI)
# Calculate the moment kurtosis
HighFreq::calc_kurtosis(retp)
# Calculate the moment kurtosis in R
calc_kurtr <- function(x) {
x <- (x-mean(x))
sum(x^4)/var(x)^2/NROW(x)
} # end calc_kurtr
all.equal(HighFreq::calc_kurtosis(retp),
calc_kurtr(retp), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::calc_kurtosis(retp),
Rcode=calc_kurtr(retp),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
# Calculate the quantile kurtosis
HighFreq::calc_kurtosis(retp, method="quantile", confl=0.9)
# Calculate the quantile kurtosis in R
calc_kurtq <- function(x, a=0.9) {
quantiles <- quantile(x, c(1-a, 0.25, 0.75, a), type=5)
(quantiles[4] - quantiles[1])/(quantiles[3] - quantiles[2])
} # end calc_kurtq
all.equal(drop(HighFreq::calc_kurtosis(retp, method="quantile", confl=0.9)),
calc_kurtq(retp, a=0.9), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
summary(microbenchmark(
Rcpp=HighFreq::calc_kurtosis(retp, method="quantile"),
Rcode=calc_kurtq(retp),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
# Calculate the nonparametric kurtosis
HighFreq::calc_kurtosis(retp, method="nonparametric")
# Compare HighFreq::calc_kurtosis() with R nonparametric kurtosis
all.equal(drop(HighFreq::calc_kurtosis(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_kurtosis(retp, method="nonparametric"),
Rcode=(mean(retp)-median(retp))/sd(retp),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.