Read in Data
rm(list = ls())
source("~/OneDrive - INSEAD/SkewEllipt/Code_Update/Rfns.R")
library("data.table")
library("ggplot2")
library("psych")
library("lintools")
library("xtable")
source("~/OneDrive - INSEAD/SkewEllipt/Code_Update/Rfns.R")
path = "/Users/jinyuanzhang/OneDrive - INSEAD/SkewEllipt/Code_Update/CoVaR_new/"
folders= 2:99
load(paste(path,"s",as.character(1),"/COVAR.RDATA",sep=""))
DF = out$df
fit = c(out$df$firm[1],out$fit$EVT)
for(folder in folders){
load(paste(path,"s",as.character(folder),"/COVAR.RDATA",sep=""))
DF = rbind(DF, out$df)
fit = rbind(fit, c(out$df$firm[1],out$fit$EVT))
}
DF = data.table(DF)
fit = data.table(fit)
fit[, 2:7 := lapply(.SD, as.numeric), .SDcols = 2:7]
colnames(fit) = c("firm", "alpha1", "alpha2", "rho", "nu", "sd1", "sd2")
DF$CoVaR_fp99 = abs(DF$CoVaR_fp99)
DF[, N := .N, by = .(firm)]
key <- fread("~/OneDrive - INSEAD/SkewEllipt/Code_Update/Risk Contagion/key.csv")
colnames(key)[1] <- "firm"
key$firm = as.character(key$firm)
DF = merge(DF, key, by = "firm")
fit = merge(fit, key, by = "firm")
report.1 <- function(dat){
c(length(dat),min(as.matrix(dat),na.rm=T),
quantile(as.matrix(dat), na.rm=T, c(0.05,0.25,0.50,0.75,0.95)), max(as.matrix(dat),na.rm=T))
}
#### Summary statistics for estiamted parameters
report = t(apply(fit[,2:7], 2, report.1))
colnames(report) = c("Count","Min", "5%", "25%", "50%", "75%", "95%", "Max")
xtable(report)
## % latex table generated in R 3.4.0 by xtable 1.8-2 package
## % Sat Dec 9 20:19:22 2017
## \begin{table}[ht]
## \centering
## \begin{tabular}{rrrrrrrrr}
## \hline
## & Count & Min & 5\% & 25\% & 50\% & 75\% & 95\% & Max \\
## \hline
## alpha1 & 99.00 & -0.13 & -0.07 & -0.01 & 0.03 & 0.05 & 0.09 & 0.15 \\
## alpha2 & 99.00 & -0.02 & 0.01 & 0.05 & 0.08 & 0.10 & 0.13 & 0.21 \\
## rho & 99.00 & -0.00 & 0.00 & 0.03 & 0.06 & 0.10 & 0.18 & 0.43 \\
## nu & 99.00 & 2.89 & 3.31 & 3.89 & 4.16 & 4.45 & 5.06 & 5.44 \\
## sd1 & 99.00 & 0.55 & 0.65 & 0.73 & 0.77 & 0.82 & 0.95 & 1.08 \\
## sd2 & 99.00 & 0.49 & 0.64 & 0.75 & 0.79 & 0.82 & 0.89 & 1.00 \\
## \hline
## \end{tabular}
## \end{table}
#### Comparison between EVT and FP mthods
##### Empirical estimator for violatoin: Loss_Index > CoVaR_Index given Loss_stock > VaR_stock
DF_CoVaR = DF[Loss_stock > VaR99]
DF_CoVaR[, CoVaR_N := .N, by = .(firm)]
# DF_CoVaR = DF_CoVaR[!(firm %in% c(10562,66157,86868))] #10562 66157 86868
DF_CoVaR = DF_CoVaR[CoVaR_N > 5]
print(length(unique(DF_CoVaR$firm)))
## [1] 97
### EVT method
mean(DF_CoVaR[, list(sum((Loss_Index > CoVaR_evt99)/CoVaR_N)), by = .(firm)]$V1)
## [1] 0.00968942
### FP method
mean(DF_CoVaR[, list(sum((Loss_Index > CoVaR_fp99)/CoVaR_N)), by = .(firm)]$V1)
## [1] 0.010177
#### LRuc #########
LRuc_pvalue = DF_CoVaR[, list(evt_LRuc = LRuc(0.01, Loss_Index > CoVaR_evt99)$pvalue, fp_LRuc = LRuc(0.01, Loss_Index > CoVaR_fp99)$pvalue), by = .(Group)]
print(LRuc_pvalue)
## Group evt_LRuc fp_LRuc
## 1: broker 0.53772252 0.006648722
## 2: depository 0.08767449 0.044749522
## 3: insurance 0.76270773 0.713126012
## 4: other 0.12636645 0.126366453
#### cct.twosided ####
#### pv.avg
fp_cct = DF_CoVaR[, (cct.2s.VaR(r = CoVaR_fp99, x = Loss_Index , lev=0.99, hac = FALSE)$pv.avg), by = .(Group)]
colnames(fp_cct)[2] = "fp_cct"
evt_cct = DF_CoVaR[, (cct.2s.VaR(r = CoVaR_evt99, x = Loss_Index , lev=0.99, hac = FALSE)$pv.avg), by = .( Group)]
colnames(evt_cct)[2] = "evt_cct"
merge(evt_cct, fp_cct, by = "Group")
## Group evt_cct fp_cct
## 1: broker 0.5848539 0.05966308
## 2: depository 0.1463839 0.09509660
## 3: insurance 0.7698805 0.70134914
## 4: other 0.0459228 0.04592280
#### pv.cond
fp_cct = DF_CoVaR[, (cct.2s.VaR(r = CoVaR_fp99, x = Loss_Index , lev=0.99, hac = FALSE)$pv.cond), by = .(Group)]
colnames(fp_cct)[2] = "fp_cct"
evt_cct = DF_CoVaR[, (cct.2s.VaR(r = CoVaR_evt99, x = Loss_Index , lev=0.99, hac = FALSE)$pv.cond), by = .(Group)]
colnames(evt_cct)[2] = "evt_cct"
merge(evt_cct, fp_cct, by = "Group")
## Group evt_cct fp_cct
## 1: broker 0.3138250217 0.0080332854
## 2: depository 0.0004942605 0.0002114904
## 3: insurance 0.6617320631 0.0172180375
## 4: other 0.0318513773 0.0318513773
## Comparative backtesting
com1 = DF_CoVaR[, as.list(TLMfn.biv(r = CoVaR_evt99, rS = CoVaR_fp99, y= Loss_Index , lev=0.99, h = 1)), by = .(Group)]
com0 = DF_CoVaR[, as.list(TLMfn.biv(r = CoVaR_evt99, rS = CoVaR_fp99, y= Loss_Index , lev=0.99, h = 0)), by = .(Group)]
colnames(com0)[2:5] = c("TLM01_0", "TLM05_0", "TLM10_0", "Tn_0")
colnames(com1)[2:5] = c("TLM01_1", "TLM05_1", "TLM10_1", "Tn_1")
com = merge(com0, com1, by = c("Group"))
print(com)
## Group TLM01_0 TLM05_0 TLM10_0 Tn_0 TLM01_1 TLM05_1 TLM10_1
## 1: broker 1 1 1 0.9799576 1 1 0
## 2: depository 0 0 0 3.5697653 0 0 0
## 3: insurance 0 0 0 4.2134759 0 0 0
## 4: other 0 0 0 6.0127761 0 0 0
## Tn_1
## 1: 1.544670
## 2: 4.021795
## 3: 6.510779
## 4: 5.175045