Learn R Programming

FatTailsR (version 2.0.0)

kiener1: Symmetric Kiener Distribution K1

Description

Density, distribution function, quantile function, random generation, value-at-risk, expected shortfall (+ signed left/right tail mean) and additional formulae for symmetric Kiener distribution K1. This distribution is similar to the power hyperbola logistic distribution but with additional parameters for location (m) and scale (g).

Usage

dkiener1(x, m = 0, g = 1, k = 3.2, log = FALSE)

pkiener1(q, m = 0, g = 1, k = 3.2, lower.tail = TRUE, log.p = FALSE)

qkiener1(p, m = 0, g = 1, k = 3.2, lower.tail = TRUE, log.p = FALSE)

rkiener1(n, m = 0, g = 1, k = 3.2)

dpkiener1(p, m = 0, g = 1, k = 3.2, log = FALSE)

dqkiener1(p, m = 0, g = 1, k = 3.2, log = FALSE)

lkiener1(x, m = 0, g = 1, k = 3.2)

dlkiener1(lp, m = 0, g = 1, k = 3.2, log = FALSE)

qlkiener1(lp, m = 0, g = 1, k = 3.2, lower.tail = TRUE)

varkiener1(p, m = 0, g = 1, k = 3.2, lower.tail = TRUE, log.p = FALSE)

ltmkiener1(p, m = 0, g = 1, k = 3.2, lower.tail = TRUE, log.p = FALSE)

rtmkiener1(p, m = 0, g = 1, k = 3.2, lower.tail = TRUE, log.p = FALSE)

dtmqkiener1(p, m = 0, g = 1, k = 3.2, lower.tail = TRUE, log.p = FALSE)

eskiener1(p, m = 0, g = 1, k = 3.2, lower.tail = TRUE, log.p = FALSE, signedES = FALSE)

Arguments

x

vector of quantiles.

m

numeric. The median.

g

numeric. The scale parameter, preferably strictly positive.

k

numeric. The tail parameter, preferably strictly positive.

log

logical. If TRUE, densities are given in log scale.

q

vector of quantiles.

lower.tail

logical. If TRUE, use p. If FALSE, use 1-p.

log.p

logical. If TRUE, probabilities p are given as log(p).

p

vector of probabilities.

n

number of observations. If length(n) > 1, the length is taken to be the number required.

lp

vector of logit of probabilities.

signedES

logical. FALSE (default) returns positive numbers for left and right tails. TRUE returns negative number (= ltmkiener1) for left tail and positive number (= rtmkiener1) for right tail.

Details

Kiener distributions use the following parameters, some of them being redundant. See aw2k and pk2pk for the formulas and the conversion between parameters:

  • m (mu) is the median of the distribution,.

  • g (gamma) is the scale parameter.

  • a (alpha) is the left tail parameter.

  • k (kappa) is the harmonic mean of a and w and describes a global tail parameter.

  • w (omega) is the right tail parameter.

  • d (delta) is the distortion parameter.

  • e (epsilon) is the eccentricity parameter.

Kiener distributions K1(m, g, k, ...) describe distributions with symmetric left and right fat tails and with a tail parameter k. This parameter is the power exponent mentionned in the Pareto formula and Karamata theorems.

m is the median of the distribution. g is the scale parameter and is linked for any value of k to the density at the median through the relation $$ g * f(x=m, g=g) = \frac{\pi}{4\sqrt{3}} \approx 0.453 $$

When k = Inf, g is very close to sd(x). NOTE: In order to match this standard deviation, the value of g has been updated from versions < 1.9.0 by a factor \( \frac{2\pi}{\sqrt{3}}\).

The functions dkiener1, pkiener1 and lkiener1 have an explicit form (whereas dkiener2347, pkiener2347 and lkiener2347 have no explicit forms).

dkiener1 function is defined for x in (-Inf, +Inf) by: $$ \begin{array}{l} y = \frac{1}{k}\frac{\pi}{\sqrt{3}}\frac{(x-m)}{g} \\[4pt] dkiener1(x,m,g,k) = \pi*\left[2\sqrt{3}\,g\,\sqrt{y^2 +1} \left(1+\cosh(k*asinh(y))\right)\right]^{-1} \end{array} $$

pkiener1 function is defined for q in (-Inf, +Inf) by: $$ \begin{array}{l} y = \frac{1}{k}\frac{\pi}{\sqrt{3}}\frac{(x-m)}{g} \\[4pt] pkiener1(q,m,g,k) = 1/(1+exp(-k*asinh(y))) \end{array} $$

qkiener1 function is defined for p in (0, 1) by: $$ qkiener1(p,m,g,k) = m + \frac{\sqrt{3}}{\pi}*g*k* \sinh\left(\frac{logit(p)}{k}\right) $$

rkiener1 generates n random quantiles.

In addition to the classical d, p, q, r functions, the prefixes dp, dq, l, dl, ql are also provided.

dpkiener1 is the density function calculated from the probability p. It is defined for p in (0, 1) by: $$ dpkiener1(p,m,g,k) = \frac{\pi}{\sqrt{3}}\frac{p(1-p)}{g} sech\left(\frac{logit(p)}{k}\right) $$

dqkiener1 is the derivate of the quantile function calculated from the probability p. It is defined for p in (0, 1) by: $$ dqkiener1(p,m,g,k) = \frac{\sqrt{3}}{\pi}\frac{g}{p(1-p)} \cosh\left(\frac{logit(p)}{k}\right) $$

lkiener1 function is equivalent to kashp function but with additional parameters m and g. Being computed from the x (or q) vector, it can be compared to the logit of the empirical probability logit(p) through a nonlinear regression with ordinary or weighted least squares to estimate the distribution parameters. It is defined for x in (-Inf, +Inf) by: $$ \begin{array}{l} y = \frac{1}{k}\frac{\pi}{\sqrt{3}}\frac{(x-m)}{g} \\[4pt] lkiener1(q,m,g,k) = k*asinh(y) \end{array} $$

dlkiener1 is the density function calculated from the logit of the probability lp = logit(p). It is defined for lp in (-Inf, +Inf) by: $$ dlkiener1(lp,m,g,k) = \frac{\pi}{\sqrt{3}}\frac{p(1-p)}{g} sech\left(\frac{lp}{k}\right) $$

qlkiener1 is the quantile function calculated from the logit of the probability lp = logit(p). It is defined for lp in (-Inf, +Inf) by: $$ qlkiener1(p,m,g,k) = m + \frac{\sqrt{3}}{\pi}*g*k*2* \sinh\left(\frac{lp}{k}\right) $$

varkiener1 designates the Value a-risk and turns negative numbers into positive numbers with the following rule: $$ varkiener1 <- if\;(p <= 0.5)\;\; (- qkiener1)\;\; else\;\; (qkiener1) $$ Usual values in finance are p = 0.01, p = 0.05, p = 0.95 and p = 0.99. lower.tail = FALSE uses 1-p rather than p.

ltmkiener1, rtmkiener1 and eskiener1 are respectively the left tail mean, the right tail mean and the expected shortfall of the distribution (sometimes called average VaR, conditional VaR or tail VaR). Left tail mean is the integrale from -Inf to p of the quantile function qkiener1 divided by p. Right tail mean is the integrale from p to +Inf of the quantile function qkiener1 divided by 1-p. Expected shortfall turns negative numbers into positive numbers with the following rule: $$ eskiener1 <- if\;(p <= 0.5)\;\; (- ltmkiener1)\;\; else\;\; (rtmkiener1) $$ Usual values in finance are p = 0.01, p = 0.025, p = 0.975 and p = 0.99. lower.tail = FALSE uses 1-p rather than p.

dtmqkiener1 is the difference between the left tail mean and the quantile when (p <= 0.5) and the difference between the right tail mean and the quantile when (p > 0.5). It is in quantile unit and is an indirect measure of the tail curvature.

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

C. Acerbi, D. Tasche, Expected shortfall: a natural coherent alternative to Value at Risk, 9 May 2001. Download it from: https://www.bis.org/bcbs/ca/acertasc.pdf

See Also

Standardized logistic distribution logisst, asymmetric Kiener distributions K2, K3, K4 and K7 kiener2, kiener3, kiener4, kiener7, regression function regkienerLX.

Examples

Run this code
require(graphics)

### EXAMPLE 1
x <- seq(-5, 5, by = 0.1) ; x
pkiener1(x, m=0, g=1, k=4)
dkiener1(x, m=0, g=1, k=4)
lkiener1(x, k=4)
plot( x, pkiener1(x, m=0, g=1, k=4), las=1)
lines(x, pkiener1(x, m=0, g=1, k=9999))

plot( x, lkiener1(x, m=0, g=1, k=4), las=1)
lines(x, lkiener1(x, m=0, g=1, k=9999))


p <- c(ppoints(11, a = 1), NA, NaN) ; p
qkiener1(p, k = 4)
dpkiener1(p, k = 4)
dqkiener1(p, k=4)

varkiener1(p=0.01, k=4)
ltmkiener1(p=0.01, k=4) 
 eskiener1(p=0.01, k=4) # VaR and ES should be positive
### END EXAMPLE 1


### PREPARE THE GRAPHICS FOR EXAMPLES 2 AND 3
xx  <- c(-4,-2, 0, 2, 4)
lty <- c( 1, 2, 3, 4, 5, 1)
lwd <- c( 2, 1, 1, 1, 1, 1)
col <- c("black","green3","cyan3","dodgerblue2","purple2","brown3")
lat <- c(-6.9, -4.6, -2.9, 0, 2.9, 4.6, 6.9)
lgt <- 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  ")
funleg <- function(xy, k) legend(xy, title = expression(kappa), legend = names(k),
                  lty = lty, col = col, lwd = lwd, inset = 0.02, cex = 0.8)
funlgt <- function(xy) legend(xy, title = "logit(p)", legend = lgt,
                              inset = 0.02, cex = 0.6)

### EXAMPLE 2
### PROBA, DENSITY, LOGIT-PROBA, LOG-DENSITY FROM x
x <- seq(-5, 5, by = 0.1) ; head(x, 10)
k <- c(9999, 9, 5, 3, 2, 1) ; names(k) <- k

mat11 <- outer(x, k, \(x,k) pkiener1(x, k=k)) ; head(mat11, 10)
mat12 <- outer(x, k, \(x,k) dkiener1(x, k=k)) ; mat12
mat13 <- outer(x, k, \(x,k) lkiener1(x, k=k)) ; mat13
mat14 <- outer(x, k, \(x,k) dkiener1(x, k=k, log=TRUE)) ; mat14

op <- par(mfcol = c(2,2), mar = c(2.5,3,1.5,1), las=1)
	matplot(x, mat11, type="l", lwd=lwd, lty=lty, col=col, 
			main="pkiener1(x, m=0, g=1, k=k)", xlab="", ylab="")
	funleg("topleft", k)
	matplot(x, mat12, type="l", lwd=lwd, lty=lty, col=col, 
			main="dkiener1", xlab="", ylab="")
	funleg("topleft", k)
	matplot(x, mat13, type="l", lwd=lwd, lty=lty, col=col, yaxt="n", 
			main="lkiener1", xlab="", ylab="")
	   axis(2, at=lat, las=1)
	funleg("bottomright", k)
	funlgt("topleft")
	matplot(x, mat14, type="l", lwd=lwd, lty=lty, col=col, 
			main="log(dkiener1)", xlab="", ylab="")
	funleg("bottom", k)
par(op)
### END EXAMPLE 2


### EXAMPLE 3
### QUANTILE, DIFF-QUANTILE, DENSITY, LOG-DENSITY FROM p
p <- ppoints(1999, a=0) ; head(p, n=10)
k <- c(9999, 9, 5, 3, 2, 1) ; names(k) <- k

mat15 <- outer(p, k, \(p,k)  qkiener1(p, k=k)) ; head(mat15, 10)
mat16 <- outer(p, k, \(p,k) dqkiener1(p, k=k)) ; head(mat16, 10)
mat17 <- outer(p, k, \(p,k) dpkiener1(p, k=k)) ; head(mat17, 10)

op <- par(mfcol = c(2,2), mar = c(2.5,3,1.5,1), las=1)
	matplot(p, mat15, type="l", xlim=c(0,1), ylim=c(-5,5), 
            lwd=lwd, lty=lty, col=col, las=1,
			main="qkiener1(p, m=0, g=1, k=k)", xlab="", ylab="")
	funleg("topleft", k)
	matplot(p, mat16, type="l", xlim=c(0,1), ylim=c(0,40), 
            lwd=lwd, lty=lty, col=col, las=1,
			main="dqkiener1", xlab="", ylab="")
	funleg("top", k)
	plot(NA, NA, xlim=c(-5, 5), ylim=c(0, 0.5), las=1,
		 main="qkiener1, dpkiener1", xlab="", ylab="")
	mapply(matlines, x=as.data.frame(mat15), y=as.data.frame(mat17), 
		   lwd=lwd, lty=1, col=col)
	funleg("topright", k)
	plot(NA, NA, xlim=c(-5, 5), ylim=c(-7, -0.5), las=1,
		 main="qkiener1, log(dpkiener1)", xlab="", ylab="")
	mapply(matlines, x=as.data.frame(mat15), y=as.data.frame(log(mat17)), 
		   lwd=lwd, lty=lty, col=col)
	funleg("bottom", k)
par(op)
### END EXAMPLE 3


### EXAMPLE 4: PROCESSUS: which processus look credible?
### PARAMETER k VARIES
### RUN SEED ii <- 1 THEN THE cairo_pdf CODE WITH THE 6 SEEDS
# cairo_pdf("K1-6x6-stocks-k.pdf")
# for (ii in c(1,2016,2018,2022,2023,2024)) {
	ii <- 1
	set.seed(ii)
	p <- sample(ppoints(299, a=0), 299)
	k <- c(9999, 6, 4, 3, 2, 1) ; names(k) <- k
	mat18 <- outer(p, k, \(p,k)  qkiener1(p=p, g=0.85, k=k)) 
	mat19 <- apply(mat18, 2, cumsum)
	title <- paste0(
		"stock_", ii,    
	     ":  k_left = c(", paste(k[1:3], collapse = ", "), ")",
	    ",  k_right = c(", paste(k[4:6], collapse = ", "), ")")
	plot.ts(mat19, ann=FALSE, las=1, 
			mar.multi=c(0,3,0,1), oma.multi=c(3,0,3,0.5))
	mtext(title, outer = TRUE, line=-1.5, font=2)
	plot.ts(mat18, ann=FALSE, las=1, 
			mar.multi=c(0,3,0,1), oma.multi=c(3,0,3,0.5))
	mtext(title, outer=TRUE, line=-1.5, font=2)
# }
# dev.off()
### END EXAMPLE 4



Run the code above in your browser using DataLab