require(graphics)
### EXAMPLE 1
x <- seq(-5, 5, by = 0.1) ; x
pkiener2(x, m=0, g=1, a=2, w=5)
dkiener2(x, m=0, g=1, a=2, w=5)
lkiener2(x, m=0, g=1, a=2, w=5)
plot( x, pkiener2(x, m=0, g=1, a=2, w=5), las=1)
lines(x, pkiener1(x, m=0, g=1, k=9999))
plot( x, dkiener2(x, m=0, g=1, a=2, w=5), las=1, type="l", lwd=2)
lines(x, dkiener1(x, m=0, g=1, k=9999))
plot( x, lkiener2(x, m=0, g=1, a=2, w=5), las=1)
lines(x, lkiener1(x, m=0, g=1, k=9999))
p <- c(ppoints(11, a = 1), NA, NaN) ; p
qkiener2(p, a=2, w=5)
dpkiener2(p, a=2, w=5)
dqkiener2(p, a=2, w=5)
varkiener2(p=0.01, a=2, w=5)
ltmkiener2(p=0.01, a=2, w=5)
eskiener2(p=0.01, a=2, w=5) # 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, a) legend(xy, title = expression(alpha), legend = names(a),
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) ; x ; length(x)
a <- c(9999, 9, 5, 3, 2, 1) ; names(a) <- a
fun1 <- function(a, x) pkiener2(x, a=a, w=5)
fun2 <- function(a, x) dkiener2(x, a=a, w=5)
fun3 <- function(a, x) lkiener2(x, a=a, w=5)
fun4 <- function(a, x) dkiener2(x, a=a, w=5, log=TRUE)
mat11 <- sapply(a, fun1, x) ; head(mat11, 10)
mat12 <- sapply(a, fun2, x) ; head(mat12, 10)
mat13 <- sapply(a, fun3, x) ; head(mat13, 10)
mat14 <- sapply(a, fun4, x) ; head(mat14, 10)
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="pkiener2(x, m=0, g=1, a=a, w=5)", xlab="", ylab="")
funleg("topleft", a)
matplot(x, mat12, type="l", lwd=lwd, lty=lty, col=col,
main="dkiener2", xlab="", ylab="")
funleg("topleft", a)
matplot(x, mat13, type="l", lwd=lwd, lty=lty, col=col, yaxt="n", ylim=c(-10,10),
main="lkiener2", xlab="", ylab="")
axis(2, at=lat, las=1)
funleg("bottomright", a)
funlgt("topleft")
matplot(x, mat14, type="l", lwd=lwd, lty=lty, col=col, ylim=c(-8,0),
main="log(dkiener2)", xlab="", ylab="")
funleg("bottom", a)
par(op)
### END EXAMPLE 2
### EXAMPLE 3
### QUANTILE, DIFF-QUANTILE, DENSITY, LOG-DENSITY FROM p
p <- ppoints(1999, a=0) ; head(p, n=10)
a <- c(9999, 9, 5, 3, 2, 1) ; names(a) <- a
mat15 <- outer(p, a, \(p,a) qkiener2(p, a=a, w=5)) ; head(mat15, 10)
mat16 <- outer(p, a, \(p,a) dqkiener2(p, a=a, w=5)) ; head(mat16, 10)
mat17 <- outer(p, a, \(p,a) dpkiener2(p, a=a, w=5)) ; 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="qkiener2(p, m=0, g=1, a=a, w=5)", xlab="", ylab="")
funleg("topleft", a)
matplot(p, mat16, type="l", xlim=c(0,1), ylim=c(0,40),
lwd=lwd, lty=lty, col=col, las=1,
main="dqkiener2", xlab="", ylab="")
funleg("top", a)
plot(NA, NA, xlim=c(-5, 5), ylim=c(0, 0.5), las=1,
main="qkiener2, dpkiener2", xlab="", ylab="")
invisible(mapply(matlines, x=as.data.frame(mat15), y=as.data.frame(mat17),
lwd=lwd, lty=1, col=col))
funleg("topright", a)
plot(NA, NA, xlim=c(-5, 5), ylim=c(-7, -0.5), las=1,
main="qkiener2, log(dpkiener2)", xlab="", ylab="")
invisible(mapply(matlines, x=as.data.frame(mat15), y=as.data.frame(log(mat17)),
lwd=lwd, lty=lty, col=col))
funleg("bottom", a)
par(op)
### END EXAMPLE 3
### EXAMPLE 4: PROCESSUS: which processus look credible?
### PARAMETER a VARIES, w=4 IS CONSTANT
### RUN SEED ii <- 1 THEN THE cairo_pdf CODE WITH THE 6 SEEDS
# cairo_pdf("K2-6x6-stocks-a.pdf")
# for (ii in c(1,2016,2018,2022,2023,2024)) {
ii <- 1
set.seed(ii)
p <- sample(ppoints(299, a=0), 299)
a <- c(9999, 9, 4, 3, 2, 1) ; names(a) <- a
mat18 <- outer(p, a, \(p,a) qkiener2(p=p, g=0.85, a=a, w=4))
mat19 <- apply(mat18, 2, cumsum)
title <- paste0(
"stock_", ii,
": a_left = c(", paste(a[1:3], collapse = ", "), ")",
", a_right = c(", paste(a[4:6], collapse = ", "), ")",
", w = 4")
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