Learn R Programming

FatTailsR (version 2.0.0)

FatTailsR-package: Package FatTailsR

Description

This package includes Kiener distributions K1, K2, K3, K4 and K7 and two estimation algorithms to characterize with a high precision symmetric or asymmetric distributions with left and right fat tails that appear in market finance, neuroscience and many other disciplines. The estimation of the distribution parameters, quantiles, value-at-risk and expected shortfall is usually very accurate. Two datasets are provided, as well as power hyperbolas and power hyperbolic functions which are simplified versions of symmetric distribution K1.

Download the pdf cited in the references to get an overview of the theoretical part and several examples on stocks and indices.

A commercial package, FatTailsRplot, with advanced plotting functions and calculation of matrix of stocks over rolling windows is also developped by the author.

IMPORTANT: A breaking change has been introduced in version 2.0.0 in order to get asymptotic values consistent with the standardized logistic distribution, i.e. sd = 1 when kappa = Inf. The scaling parameter g is now combined with the fixed value g*sqrt(3)/pi and replace g/2. Other parameters stay unchanged. Do not update if you need consistent comparisons with versions <= 1.8.6 for the g parameter. Do not mix results with versions <= 1.8.6 and versions >= 2.0.0.

Arguments

Author

Maintainer: Patrice Kiener fattailsr@inmodelia.com (ORCID)

Details

With so many functions, this package could look fat. But it's not! It's rather agile and easy to use! The various functions included in this package can be assigned to the following groups:

  1. Two datasets presented in different formats: list, data.frame, matrix, timeSeries, xts, zoo:

    • getDSdata.

    • extractData, dfData, mData, tData, xData, zData.

  2. Functions to calculate (positive, negative) prices to returns on vector, matrix, array, list, data.frame, timeSeries, xts, zoo:

    • elevate.

    • fatreturns, logreturns.

  3. Several predefined vectors of probability. One function to check them. A conversion function from probabilities to characters

    • pprobs0, pprobs1, pprobs2, ..., pprobs9.

    • checkquantiles.

    • getnamesk.

  4. Miscellaneous functions related to the logistic function:

    • logit, invlogit, ltmlogisst, rtmlogisst, eslogis.

  5. Conversion functions between parameters related to Kiener distributions K1, K2, K3, K4:

    • aw2k, aw2d, aw2e, ad2e, ad2k, ad2w, ae2d, ae2k, ae2w, ak2e, ak2w, de2a, de2k, de2w, dk2a, dk2e, dw2a, dw2e, dw2k, ek2a, ak2d, ek2w, aw2a, aw2d, ew2a, aw2d, ew2k, kd2a, kd2e, kd2w, ke2a, ke2d, ke2w, kw2a, kw2d, kw2e.

    • pk2pk.

  6. Kiener distributions K1, K2, K3, K4 and the new K7 (introduced in v1.7-0):

    • d, p, q, r, dp, dq, l, dl, ql, var, ltm, rtm, dtmq, es kiener1,

    • d, p, q, r, dp, dq, l, dl, ql, var, ltm, rtm, dtmq, es kiener2,

    • d, p, q, r, dp, dq, l, dl, ql, var, ltm, rtm, dtmq, es kiener3,

    • d, p, q, r, dp, dq, l, dl, ql, var, ltm, rtm, dtmq, es kiener4,

    • d, p, q, r, dp, dq, l, dl, ql, var, ltm, rtm, dtmq, es kiener7.

  7. Quantile (VaR) corrective function (as a multiplier of the logistic function). Expected shortfall corrective function (as a multiplier of the expected shortfall of the logistic distribution):

    • ckiener1, ckiener2, ckiener3, ckiener4, ckiener7.

    • hkiener1, hkiener2, hkiener3, hkiener4, hkiener7.

  8. Moments of the distribution estimated from the dataset and from the regression parameters:

    • xmoments.

    • kmoments, kmoment, kcmoment, kmean, kstandev, kvariance, kskewness, kkurtosis, kekurtosis.

  9. Regression and estimation functions to estimate Kiener distribution parameters on a given dataset. *fit* and *param* are wrappers of algorithms reg and estim. reg uses an unweighted nonlinear regression function. estim uses a fast estimation based on quantiles:

    • regkienerLX, laplacegaussnorm.

    • fitkienerX.

    • paramkienerX, paramkienerX5, paramkienerX7.

  10. Functions related to paramkienerX:

    • elevenprobs, sevenprobs, fiveprobs.

    • estimkiener11, estimkiener7, estimkiener5.

    • roundcoefk.

    • checkcoefk.

  11. Predefined subsets of parameters to extract them from the long vector fitk obtained after regression/estimation regkienerLX, fitkienerX :

    • exfit0, ..., exfit7.

For a quick start, jump to the functions regkienerLX, fitkienerX and run the examples. Then, download and read the documents in pdf format cited in the references to get an overview on the major functions. Finally, explore the other examples.

References

P. Kiener, Explicit models for bilateral fat-tailed distributions and applications in finance with the package FatTailsR, 8th R/Rmetrics Workshop and Summer School, Paris, 27 June 2014. Download it from: https://www.inmodelia.com/exemples/2014-0627-Rmetrics-Kiener-en.pdf

P. Kiener, Fat tail analysis and package FatTailsR, 9th R/Rmetrics Workshop and Summer School, Zurich, 27 June 2015. Download it from: https://www.inmodelia.com/exemples/2015-0627-Rmetrics-Kiener-en.pdf

See Also

Examples

Run this code
    

require(graphics)
require(minpack.lm)
require(timeSeries)

### Load the datasets and select one number (1-16)
DS     <- getDSdata()
j      <- 5

### and run this block
X      <- DS[[j]]
nameX  <- names(DS)[j]
reg    <- regkienerLX(X)
lgn    <- laplacegaussnorm(X)
lleg   <- c("logit(0.999) = 6.9", "logit(0.99)   = 4.6", 
           "logit(0.95)   = 2.9", "logit(0.50)   = 0", 
           "logit(0.05)   = -2.9", "logit(0.01)   = -4.6", 
           "logit(0.001) = -6.9  ")
pleg   <- c( paste("m =",  reg$coefr4[1]), paste("g  =", reg$coefr4[2]), 
             paste("k  =", reg$coefr4[3]), paste("e  =", reg$coefr4[4]) )

## Main plot
op     <- par(mfrow = c(1,1), mgp = c(1.5,0.8,0), mar = c(3,3,2,1))
plot(reg$dfrXP, main = nameX)
legend(x = min(X), y = 0.5, legend = pleg, cex = 0.9, inset = 0.02 )
lines(reg$dfrEP, col = 2, lwd = 2)
points(reg$dfrQkPk, pch = 3, col = 2, lwd = 2, cex = 1.5)
lines(lgn$dfrXPn, col = 7, lwd = 2)

## Plot F(X) > 0,97
front = c(0.06, 0.39, 0.50, 0.95)
par(fig = front, new = TRUE, mgp = c(1.5, 0.6, 0), las = 0)
plot( reg$dfrXP[which(reg$dfrXP$P > 0.97),] , pch = 1, xlab = "", ylab = "", main = "F(X) > 0,97" )
lines(reg$dfrEP[which(reg$dfrEP$P > 0.97),], type="l", col = 2, lwd = 3 )
lines(lgn$dfrXPn[which(lgn$dfrXPn$Pn > 0.97),], type = "l", col = 7, lwd= 2 )
points(reg$dfrQkPk, pch = 3, col = 2, lwd = 2, cex = 1.5)
points(lgn$dfrQnPn, pch = 3, col = 7, lwd = 2, cex = 1)

## Plot F(X) < 0,03
front = c(0.58, 0.98, 0.06, 0.61)
par(fig = front, new = TRUE, mgp = c(0.5, 0.6, 0), las = 0 )
plot( reg$dfrXP[which(reg$dfrXP$P < 0.03),] , pch = 1, xlab = "", ylab = "", main = "F(X) < 0,03")
lines(reg$dfrEP[which(reg$dfrEP$P < 0.03),], type = "l", col = 2, lwd = 3 )
lines(lgn$dfrXPn[which(lgn$dfrXPn$Pn < 0.03),], type = "l", col= 7, lwd= 2 )
points(reg$dfrQkPk, pch = 3, col = 2, lwd = 2, cex = 1.5)
points(lgn$dfrQnPn, pch = 3, col = 7, lwd = 2, cex = 1)

## Moments from the parameters (k) and from the Dataset (X)
round(cbind("k" = kmoments(reg$coefk, lengthx = nrow(reg$dfrXL)), "X" = xmoments(X)), 2)
attributes(reg)
### End block


Run the code above in your browser using DataLab