---
title: "Thesis"
author: "Ibraheem"
date: "5 April 2017"
output: html_document
---
```{r}
library(quantmod)
library(MASS)
library(tseries)
library(survival)
library(fitdistrplus)
library(car)
library(nortest)
library(fBasics)
library(moments)
library(rugarch)
library(QRM)

#Creating a working directory and reading of investment returns as data
setwd("C:/Users/Ibraheem/Documents/Thesis") #working directory
data <- read.csv("stock_returns.csv", header = T, sep = ",") # all investment returns
##class(data)
attach(data)

#library(PerfomanceAnalytics)
#library(dplyr)
#install.packages("tidyverse")
#install.packages("fitdistrplus")
#install.packages("moments")
#install.packages("rugarch")
#install.packages("QRM")

#set up estimation window (e.w) and testing window (t.w) for APPLE
S1 <- data$APPLE #returns for APPLE
T <- length(S1) #number of returns for 3 years
e.w <- 252 #number of trading days in a year
t.w <- T-e.w # number of days for testing window with a day shift
alpha <- c(0.05,0.01,0.02,0.1,0.001,0.005) #probability 
p <- 1-alpha # confidence probability



#FOR MU(TREND) VALUES
Calmu <- function(x){
  mu <- mean(x)
  return(mu)
}

mu_values_S1 <- rollapply(as.zoo(S1), width=e.w, FUN=Calmu, align="right")
mu_values_S1 <- lag(mu_values_S1,k=-1)
mu_values_S1 #expected value for each year of 503 returns of the testing window (t.w)

#FOR SIGMA(VOLATILITY) VALUES
Calsigma <- function(x){
  sigma <- sd(x)
  return(sigma)
}

Sigma_values_S1 <- rollapply(as.zoo(S1), width=e.w, FUN=Calsigma, align="right")
Sigma_values_S1 <- lag(Sigma_values_S1,k=-1)
Sigma_values_S1 #volatility value for each year of 503 returns of the testing window (t.w)

##CALCULATING VaR FOR STANDARD PARAMETRIC MODEL i.e. NORMAL DISTRIBUTION
#FOR VALUE AT RISK WITH NONCONSTANT TREND
Cal_VaR <- function(x){
  VaR <- mean(x)-(sd(x)*sqrt(e.w/(e.w-1)))*qnorm(p)
  return(VaR)
}

VaR_results_S1 <- rollapply(as.zoo(S1), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S1 <- lag(VaR_results_S1,k=-1)
VaR_results_S1 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S1_Alpha1 <- VaR_results_S1[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S1[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S1[e.w+i])<VaR_S1_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S1[,"N"]=j
Alpha1compare_S1[,"%"]=j/t.w
Alpha1compare_S1[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S1[,"VR"]=Alpha1compare_S1[,"N"]/Alpha1compare_S1[,"EN"]
Alpha1compare_S1 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR T-DISTRIBUTION
#FITTING T-DISTRIBUTION IN ORDER TO GET DF(DEGREE OF FREEDOM)
A=fitdistr(S1[1:e.w], "t", start = list(m=mean(S1[1:e.w]), s=sd(S1[1:e.w]), df=5), lower=c(-1,0.001,1))
A
A$estimate[3]

df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
Cal_TVaR <- function(x){
  TVaR <- mean(x)-(sd(x)*sqrt(e.w/(e.w-1)))*qt(p, df)
  return(TVaR)
}

TVaR_results_S1 <- rollapply(as.zoo(S1), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S1 <- lag(TVaR_results_S1,k=-1)
TVaR_results_S1 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S1_Alpha1 <- TVaR_results_S1[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S1[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S1[e.w+t]<TVaR_S1_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S1[,"N"]=s
Alpha1Tcompare_S1[,"%"]=s/t.w
Alpha1Tcompare_S1[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S1[,"VR"]=Alpha1Tcompare_S1[,"N"]/Alpha1Tcompare_S1[,"EN"]
Alpha1Tcompare_S1 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S1_Alpha2 <- VaR_results_S1[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S1[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S1[e.w+i])<VaR_S1_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S1[,"N"]=j
Alpha2compare_S1[,"%"]=j/t.w
Alpha2compare_S1[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S1[,"VR"]=Alpha2compare_S1[,"N"]/Alpha2compare_S1[,"EN"]
Alpha2compare_S1 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S1_Alpha2 <- TVaR_results_S1[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S1[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S1[e.w+t]<TVaR_S1_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S1[,"N"]=s
Alpha2Tcompare_S1[,"%"]=s/t.w
Alpha2Tcompare_S1[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S1[,"VR"]=Alpha2Tcompare_S1[,"N"]/Alpha2Tcompare_S1[,"EN"]
Alpha2Tcompare_S1 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S1_Alpha3 <- VaR_results_S1[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S1[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S1[e.w+i])<VaR_S1_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S1[,"N"]=j
Alpha3compare_S1[,"%"]=j/t.w
Alpha3compare_S1[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S1[,"VR"]=Alpha3compare_S1[,"N"]/Alpha3compare_S1[,"EN"]
Alpha3compare_S1 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S1_Alpha3 <- TVaR_results_S1[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S1[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S1[e.w+t]<TVaR_S1_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S1[,"N"]=s
Alpha3Tcompare_S1[,"%"]=s/t.w
Alpha3Tcompare_S1[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S1[,"VR"]=Alpha3Tcompare_S1[,"N"]/Alpha3Tcompare_S1[,"EN"]
Alpha3Tcompare_S1 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S1_Alpha4 <- VaR_results_S1[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S1[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S1[e.w+i])<VaR_S1_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S1[,"N"]=j
Alpha4compare_S1[,"%"]=j/t.w
Alpha4compare_S1[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S1[,"VR"]=Alpha4compare_S1[,"N"]/Alpha4compare_S1[,"EN"]
Alpha4compare_S1 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S1_Alpha4 <- TVaR_results_S1[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S1[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S1[e.w+t]<TVaR_S1_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S1[,"N"]=s
Alpha4Tcompare_S1[,"%"]=s/t.w
Alpha4Tcompare_S1[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S1[,"VR"]=Alpha4Tcompare_S1[,"N"]/Alpha4Tcompare_S1[,"EN"]
Alpha4Tcompare_S1 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S1_Alpha5 <- VaR_results_S1[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S1[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S1[e.w+i])<VaR_S1_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S1[,"N"]=j
Alpha5compare_S1[,"%"]=j/t.w
Alpha5compare_S1[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S1[,"VR"]=Alpha5compare_S1[,"N"]/Alpha5compare_S1[,"EN"]
Alpha5compare_S1 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S1_Alpha5 <- TVaR_results_S1[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S1[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S1[e.w+t]<TVaR_S1_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S1[,"N"]=s
Alpha5Tcompare_S1[,"%"]=s/t.w
Alpha5Tcompare_S1[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S1[,"VR"]=Alpha5Tcompare_S1[,"N"]/Alpha5Tcompare_S1[,"EN"]
Alpha5Tcompare_S1 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S1_Alpha6 <- VaR_results_S1[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S1[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S1[e.w+i])<VaR_S1_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S1[,"N"]=j
Alpha6compare_S1[,"%"]=j/t.w
Alpha6compare_S1[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S1[,"VR"]=Alpha6compare_S1[,"N"]/Alpha6compare_S1[,"EN"]
Alpha6compare_S1 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S1_Alpha6 <- TVaR_results_S1[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S1 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S1) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S1[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S1[e.w+t]<TVaR_S1_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S1[,"N"]=s
Alpha6Tcompare_S1[,"%"]=s/t.w
Alpha6Tcompare_S1[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S1[,"VR"]=Alpha6Tcompare_S1[,"N"]/Alpha6Tcompare_S1[,"EN"]
Alpha6Tcompare_S1 # the matrix after executing the program
```

#LOG-LIKELIHOOD  AND INDEPENDENCE TEST
```{r}
##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S1[e.w+1:t.w], VaR = VaR_S1_Alpha6, conf.level = p[6])[8:12]
LR_IN6



##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S1[e.w+1:t.w], VaR = TVaR_S1_Alpha6, conf.level = p[6])[8:12]
TLR_IN6
```


#STATISTICAL TESTS
```{r}

dailyreturns <- S1 #investment return data

#TEST FOR NORMALITY
shapiro.test(dailyreturns) #Shapiro-Wilk test
jarque.bera.test(dailyreturns) #Jarque-Bera test 

#TEST FOR T-DISTRIBUTION
t.test(dailyreturns) #student t-test

#QQ PLOTS
qqPlot(dailyreturns, distribution="norm") #QQ plot for normal distribution
qqPlot(dailyreturns, distribution="t", df=df) #QQ plot for t-distribution


##TEST FOR AUTOCORRELATION INDEPENDENCY OF RETURNS
pacf(dailyreturns, lag.max = 20, plot = T, col= "red", type = "o") # shows that returns a positive return is most likely followed by another postive return, same thing applies to negative returns
Box.test(dailyreturns, lag=1, type="Ljung-Box") #returns are not iid i.e. not normally distributed since p-value > chi-squared (X-squared)
```


#GRAPHS
```{r}
#plot for returns
plot(S1, ylab = "dailyreturns",type="l", main = "Apple Returns", col="blue")

#plot for volatility
plot(Sigma_values_S1, ylab = "volatility", main = "Return volatility", col="red")


#plot for trend
plot(mu_values_S1, ylab = "trend", main = "Return trend", col="red")

#plot for NormalVaR
plot(VaR_results_S1[,1], ylab = "NormalVaR", main = "5%VaR", col="blue")

#plot for Studentt-VaR
plot(TVaR_results_S1[,1], ylab = "Student's t VaR", main = "5%VaR", col="blue")


#histogram
hist(dailyreturns, main = "histogram of Apple returns")

#probability density function
density(dailyreturns)
plot(density(dailyreturns),main="Density estimate of Apple")

#Empirical Cummulative distribution function
ecdf(dailyreturns)
plot(ecdf(dailyreturns), ylab = "dailyreturns", main="ECDF for Apple", col.01line="blue", pch = 19)

##FITTING NORMAL DISTRIBUTION CURVE
h<-hist(dailyreturns,breaks=15)
xhist<-c(min(h$breaks),h$breaks)
yhist<-c(0,h$density,0)
xfit<-seq(min(dailyreturns),max(dailyreturns),length=40)
yfit<-dnorm(xfit,mean=mean(dailyreturns),sd=sd(dailyreturns))
plot(xhist,yhist,type="s",ylim=c(0,max(yhist,yfit)), main="Normal pdf and histogram")
lines(xfit,yfit, col="red") #the curve does not cover the whole histogram, hence investment returns are not normally distributed.
```

#DESCIPTIVE STATISTICS
```{r}
descdist(dailyreturns, discrete=FALSE, boot=500) #gives information about the descriptive statistics for apple
```

#FOR INTEL CORPORATION
```{r}
#set up estimation window (e.w) and testing window (t.w)
S2 <- data$INTEL #returns for INTEL

#DESCRIPTIVE STATISTICS
descdist(S2, discrete=FALSE, boot=500) #gives information about the descriptive statistics

VaR_results_S2 <- rollapply(as.zoo(S2), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S2 <- lag(VaR_results_S2,k=-1)
VaR_results_S2 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S2_Alpha1 <- VaR_results_S2[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S2[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S2[e.w+i])<VaR_S2_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S2[,"N"]=j
Alpha1compare_S2[,"%"]=j/t.w
Alpha1compare_S2[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S2[,"VR"]=Alpha1compare_S2[,"N"]/Alpha1compare_S2[,"EN"]
Alpha1compare_S2 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#Another method best method
A=fitdistr(S2[1:252], "t", start = list(m=mean(S2[1:252]), s=sd(S2[1:252]), df=5), lower=c(-1,0.001,1))
A


df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S2 <- rollapply(as.zoo(S2), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S2 <- lag(TVaR_results_S2,k=-1)
TVaR_results_S2 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S2_Alpha1 <- TVaR_results_S2[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S2[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S2[e.w+t]<TVaR_S2_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S2[,"N"]=s
Alpha1Tcompare_S2[,"%"]=s/t.w
Alpha1Tcompare_S2[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S2[,"VR"]=Alpha1Tcompare_S2[,"N"]/Alpha1Tcompare_S2[,"EN"]
Alpha1Tcompare_S2 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S2_Alpha2 <- VaR_results_S2[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S2[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S2[e.w+i])<VaR_S2_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S2[,"N"]=j
Alpha2compare_S2[,"%"]=j/t.w
Alpha2compare_S2[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S2[,"VR"]=Alpha2compare_S2[,"N"]/Alpha2compare_S2[,"EN"]
Alpha2compare_S2 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S2_Alpha2 <- TVaR_results_S2[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S2[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S2[e.w+t]<TVaR_S2_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S2[,"N"]=s
Alpha2Tcompare_S2[,"%"]=s/t.w
Alpha2Tcompare_S2[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S2[,"VR"]=Alpha2Tcompare_S2[,"N"]/Alpha2Tcompare_S2[,"EN"]
Alpha2Tcompare_S2 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S2_Alpha3 <- VaR_results_S2[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S2[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S2[e.w+i])<VaR_S2_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S2[,"N"]=j
Alpha3compare_S2[,"%"]=j/t.w
Alpha3compare_S2[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S2[,"VR"]=Alpha3compare_S2[,"N"]/Alpha3compare_S2[,"EN"]
Alpha3compare_S2 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S2_Alpha3 <- TVaR_results_S2[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S2[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S2[e.w+t]<TVaR_S2_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S2[,"N"]=s
Alpha3Tcompare_S2[,"%"]=s/t.w
Alpha3Tcompare_S2[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S2[,"VR"]=Alpha3Tcompare_S2[,"N"]/Alpha3Tcompare_S2[,"EN"]
Alpha3Tcompare_S2 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S2_Alpha4 <- VaR_results_S2[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S2[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S2[e.w+i])<VaR_S2_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S2[,"N"]=j
Alpha4compare_S2[,"%"]=j/t.w
Alpha4compare_S2[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S2[,"VR"]=Alpha4compare_S2[,"N"]/Alpha4compare_S2[,"EN"]
Alpha4compare_S2 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S2_Alpha4 <- TVaR_results_S2[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S2[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S2[e.w+t]<TVaR_S2_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S2[,"N"]=s
Alpha4Tcompare_S2[,"%"]=s/t.w
Alpha4Tcompare_S2[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S2[,"VR"]=Alpha4Tcompare_S2[,"N"]/Alpha4Tcompare_S2[,"EN"]
Alpha4Tcompare_S2 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S2_Alpha5 <- VaR_results_S2[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S2[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S2[e.w+i])<VaR_S2_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S2[,"N"]=j
Alpha5compare_S2[,"%"]=j/t.w
Alpha5compare_S2[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S2[,"VR"]=Alpha5compare_S2[,"N"]/Alpha5compare_S2[,"EN"]
Alpha5compare_S2 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S2_Alpha5 <- TVaR_results_S2[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S2[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S2[e.w+t]<TVaR_S2_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S2[,"N"]=s
Alpha5Tcompare_S2[,"%"]=s/t.w
Alpha5Tcompare_S2[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S2[,"VR"]=Alpha5Tcompare_S2[,"N"]/Alpha5Tcompare_S2[,"EN"]
Alpha5Tcompare_S2 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S2_Alpha6 <- VaR_results_S2[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S2[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S2[e.w+i])<VaR_S2_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S2[,"N"]=j
Alpha6compare_S2[,"%"]=j/t.w
Alpha6compare_S2[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S2[,"VR"]=Alpha6compare_S2[,"N"]/Alpha6compare_S2[,"EN"]
Alpha6compare_S2 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S2_Alpha6 <- TVaR_results_S2[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S2 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S2) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S2[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S2[e.w+t]<TVaR_S2_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S2[,"N"]=s
Alpha6Tcompare_S2[,"%"]=s/t.w
Alpha6Tcompare_S2[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S2[,"VR"]=Alpha6Tcompare_S2[,"N"]/Alpha6Tcompare_S2[,"EN"]
Alpha6Tcompare_S2 # the matrix after executing the program


#FOR LOG-LIKELIHOOD  AND INDEPENDENCE TEST
##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S2[e.w+1:t.w], VaR = VaR_S2_Alpha6, conf.level = p[6])[8:12]
LR_IN6



##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S2[e.w+1:t.w], VaR = TVaR_S2_Alpha6, conf.level = p[6])[8:12]
TLR_IN6

```


#FOR MICROSOFT
```{r}
#set up estimation window (e.w) and testing window (t.w)
S3 <- data$MICROSOFT #returns for MICROSOFT

#DESCRIPTIVE STATISTICS
descdist(S3, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S3 <- rollapply(as.zoo(S3), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S3 <- lag(VaR_results_S3,k=-1)
VaR_results_S3 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S3_Alpha1 <- VaR_results_S3[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S3[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S3[e.w+i])<VaR_S3_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S3[,"N"]=j
Alpha1compare_S3[,"%"]=j/t.w
Alpha1compare_S3[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S3[,"VR"]=Alpha1compare_S3[,"N"]/Alpha1compare_S3[,"EN"]
Alpha1compare_S3 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#Another method best method
A=fitdistr(S3[1:252], "t", start = list(m=mean(S3[1:252]), s=sd(S3[1:252]), df=4), lower=c(-1,0.001,1))
A

df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S3 <- rollapply(as.zoo(S3), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S3 <- lag(TVaR_results_S3,k=-1)
TVaR_results_S3 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S3_Alpha1 <- TVaR_results_S3[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S3[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S3[e.w+t]<TVaR_S3_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S3[,"N"]=s
Alpha1Tcompare_S3[,"%"]=s/t.w
Alpha1Tcompare_S3[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S3[,"VR"]=Alpha1Tcompare_S3[,"N"]/Alpha1Tcompare_S3[,"EN"]
Alpha1Tcompare_S3 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S3_Alpha2 <- VaR_results_S3[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S3[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S3[e.w+i])<VaR_S3_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S3[,"N"]=j
Alpha2compare_S3[,"%"]=j/t.w
Alpha2compare_S3[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S3[,"VR"]=Alpha2compare_S3[,"N"]/Alpha2compare_S3[,"EN"]
Alpha2compare_S3 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S3_Alpha2 <- TVaR_results_S3[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S3[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S3[e.w+t]<TVaR_S3_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S3[,"N"]=s
Alpha2Tcompare_S3[,"%"]=s/t.w
Alpha2Tcompare_S3[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S3[,"VR"]=Alpha2Tcompare_S3[,"N"]/Alpha2Tcompare_S3[,"EN"]
Alpha2Tcompare_S3 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S3_Alpha3 <- VaR_results_S3[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S3[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S3[e.w+i])<VaR_S3_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S3[,"N"]=j
Alpha3compare_S3[,"%"]=j/t.w
Alpha3compare_S3[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S3[,"VR"]=Alpha3compare_S3[,"N"]/Alpha3compare_S3[,"EN"]
Alpha3compare_S3 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S3_Alpha3 <- TVaR_results_S3[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S3[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S3[e.w+t]<TVaR_S3_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S3[,"N"]=s
Alpha3Tcompare_S3[,"%"]=s/t.w
Alpha3Tcompare_S3[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S3[,"VR"]=Alpha3Tcompare_S3[,"N"]/Alpha3Tcompare_S3[,"EN"]
Alpha3Tcompare_S3 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S3_Alpha4 <- VaR_results_S3[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S3[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S3[e.w+i])<VaR_S3_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S3[,"N"]=j
Alpha4compare_S3[,"%"]=j/t.w
Alpha4compare_S3[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S3[,"VR"]=Alpha4compare_S3[,"N"]/Alpha4compare_S3[,"EN"]
Alpha4compare_S3 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S3_Alpha4 <- TVaR_results_S3[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S3[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S3[e.w+t]<TVaR_S3_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S3[,"N"]=s
Alpha4Tcompare_S3[,"%"]=s/t.w
Alpha4Tcompare_S3[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S3[,"VR"]=Alpha4Tcompare_S3[,"N"]/Alpha4Tcompare_S3[,"EN"]
Alpha4Tcompare_S3 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S3_Alpha5 <- VaR_results_S3[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S3[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S3[e.w+i])<VaR_S3_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S3[,"N"]=j
Alpha5compare_S3[,"%"]=j/t.w
Alpha5compare_S3[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S3[,"VR"]=Alpha5compare_S3[,"N"]/Alpha5compare_S3[,"EN"]
Alpha5compare_S3 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S3_Alpha5 <- TVaR_results_S3[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S3[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S3[e.w+t]<TVaR_S3_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S3[,"N"]=s
Alpha5Tcompare_S3[,"%"]=s/t.w
Alpha5Tcompare_S3[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S3[,"VR"]=Alpha5Tcompare_S3[,"N"]/Alpha5Tcompare_S3[,"EN"]
Alpha5Tcompare_S3 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S3_Alpha6 <- VaR_results_S3[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S3[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S3[e.w+i])<VaR_S3_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S3[,"N"]=j
Alpha6compare_S3[,"%"]=j/t.w
Alpha6compare_S3[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S3[,"VR"]=Alpha6compare_S3[,"N"]/Alpha6compare_S3[,"EN"]
Alpha6compare_S3 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S3_Alpha6 <- TVaR_results_S3[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S3 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S3) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S3[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S3[e.w+t]<TVaR_S3_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S3[,"N"]=s
Alpha6Tcompare_S3[,"%"]=s/t.w
Alpha6Tcompare_S3[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S3[,"VR"]=Alpha6Tcompare_S3[,"N"]/Alpha6Tcompare_S3[,"EN"]
Alpha6Tcompare_S3 # the matrix after executing the program


##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S3[e.w+1:t.w], VaR = VaR_S3_Alpha6, conf.level = p[6])[8:12]
LR_IN6



##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha1, conf.level = p[1])[8:12]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha2, conf.level = p[2])[8:12]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha3, conf.level = p[3])[8:12]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha4, conf.level = p[4])[8:12]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha5, conf.level = p[5])[8:12]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S3[e.w+1:t.w], VaR = TVaR_S3_Alpha6, conf.level = p[6])[8:12]
TLR_UC6


```

#FOR MICRON
```{r}
#set up estimation window (e.w) and testing window (t.w)
S4 <- data$MICRON #returns for MICRON

#DESCRIPTIVE STATISTICS
descdist(S4, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S4 <- rollapply(as.zoo(S4), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S4 <- lag(VaR_results_S4,k=-1)
VaR_results_S4 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S4_Alpha1 <- VaR_results_S4[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S4[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S4[e.w+i])<VaR_S4_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S4[,"N"]=j
Alpha1compare_S4[,"%"]=j/t.w
Alpha1compare_S4[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S4[,"VR"]=Alpha1compare_S4[,"N"]/Alpha1compare_S4[,"EN"]
Alpha1compare_S4 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FITTING T-DISTRIBUTION
A=fitdistr(S4[1:252], "t", start = list(m=mean(S4[1:252]), s=sd(S4[1:252]), df=5), lower=c(-1,0.001,1))
A

df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S4 <- rollapply(as.zoo(S4), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S4 <- lag(TVaR_results_S4,k=-1)
TVaR_results_S4 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S4_Alpha1 <- TVaR_results_S4[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S4[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S4[e.w+t]<TVaR_S4_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S4[,"N"]=s
Alpha1Tcompare_S4[,"%"]=s/t.w
Alpha1Tcompare_S4[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S4[,"VR"]=Alpha1Tcompare_S4[,"N"]/Alpha1Tcompare_S4[,"EN"]
Alpha1Tcompare_S4 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S4_Alpha2 <- VaR_results_S4[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S4[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S4[e.w+i])<VaR_S4_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S4[,"N"]=j
Alpha2compare_S4[,"%"]=j/t.w
Alpha2compare_S4[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S4[,"VR"]=Alpha2compare_S4[,"N"]/Alpha2compare_S4[,"EN"]
Alpha2compare_S4 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S4_Alpha2 <- TVaR_results_S4[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S4[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S4[e.w+t]<TVaR_S4_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S4[,"N"]=s
Alpha2Tcompare_S4[,"%"]=s/t.w
Alpha2Tcompare_S4[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S4[,"VR"]=Alpha2Tcompare_S4[,"N"]/Alpha2Tcompare_S4[,"EN"]
Alpha2Tcompare_S4 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S4_Alpha3 <- VaR_results_S4[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S4[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S4[e.w+i])<VaR_S4_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S4[,"N"]=j
Alpha3compare_S4[,"%"]=j/t.w
Alpha3compare_S4[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S4[,"VR"]=Alpha3compare_S4[,"N"]/Alpha3compare_S4[,"EN"]
Alpha3compare_S4 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S4_Alpha3 <- TVaR_results_S4[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S4[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S4[e.w+t]<TVaR_S4_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S4[,"N"]=s
Alpha3Tcompare_S4[,"%"]=s/t.w
Alpha3Tcompare_S4[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S4[,"VR"]=Alpha3Tcompare_S4[,"N"]/Alpha3Tcompare_S4[,"EN"]
Alpha3Tcompare_S4 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S4_Alpha4 <- VaR_results_S4[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S4[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S4[e.w+i])<VaR_S4_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S4[,"N"]=j
Alpha4compare_S4[,"%"]=j/t.w
Alpha4compare_S4[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S4[,"VR"]=Alpha4compare_S4[,"N"]/Alpha4compare_S4[,"EN"]
Alpha4compare_S4 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S4_Alpha4 <- TVaR_results_S4[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S4[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S4[e.w+t]<TVaR_S4_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S4[,"N"]=s
Alpha4Tcompare_S4[,"%"]=s/t.w
Alpha4Tcompare_S4[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S4[,"VR"]=Alpha4Tcompare_S4[,"N"]/Alpha4Tcompare_S4[,"EN"]
Alpha4Tcompare_S4 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S4_Alpha5 <- VaR_results_S4[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S4[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S4[e.w+i])<VaR_S4_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S4[,"N"]=j
Alpha5compare_S4[,"%"]=j/t.w
Alpha5compare_S4[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S4[,"VR"]=Alpha5compare_S4[,"N"]/Alpha5compare_S4[,"EN"]
Alpha5compare_S4 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S4_Alpha5 <- TVaR_results_S4[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S4[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S4[e.w+t]<TVaR_S4_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S4[,"N"]=s
Alpha5Tcompare_S4[,"%"]=s/t.w
Alpha5Tcompare_S4[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S4[,"VR"]=Alpha5Tcompare_S4[,"N"]/Alpha5Tcompare_S4[,"EN"]
Alpha5Tcompare_S4 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S4_Alpha6 <- VaR_results_S4[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S4[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S4[e.w+i])<VaR_S4_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S4[,"N"]=j
Alpha6compare_S4[,"%"]=j/t.w
Alpha6compare_S4[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S4[,"VR"]=Alpha6compare_S4[,"N"]/Alpha6compare_S4[,"EN"]
Alpha6compare_S4 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S4_Alpha6 <- TVaR_results_S4[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S4 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S4) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S4[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S4[e.w+t]<TVaR_S4_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S4[,"N"]=s
Alpha6Tcompare_S4[,"%"]=s/t.w
Alpha6Tcompare_S4[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S4[,"VR"]=Alpha6Tcompare_S4[,"N"]/Alpha6Tcompare_S4[,"EN"]
Alpha6Tcompare_S4 # the matrix after executing the program


##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S4[e.w+1:t.w], VaR = VaR_S4_Alpha6, conf.level = p[6])[8:12]
LR_IN6



##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S4[e.w+1:t.w], VaR = TVaR_S4_Alpha6, conf.level = p[6])[8:12]
TLR_IN6


```


#FOR SIRIUS
```{r}
#set up estimation window (e.w) and testing window (t.w)
S5 <- data$SIRIUS #returns for SIRIUS

#DESCRIPTIVE STATISTICS
descdist(S5, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S5 <- rollapply(as.zoo(S5), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S5 <- lag(VaR_results_S5,k=-1)
VaR_results_S5 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S5_Alpha1 <- VaR_results_S5[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S5[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S5[e.w+i])<VaR_S5_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S5[,"N"]=j
Alpha1compare_S5[,"%"]=j/t.w
Alpha1compare_S5[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S5[,"VR"]=Alpha1compare_S5[,"N"]/Alpha1compare_S5[,"EN"]
Alpha1compare_S5 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FITTING T-DISTRIBUTION
#Another method best method
A=fitdistr(S5[1:252], "t", start = list(m=mean(S5[1:252]), s=sd(S5[1:252]), df=6), lower=c(-1,0.001,1))
A

df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S5 <- rollapply(as.zoo(S5), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S5 <- lag(TVaR_results_S5,k=-1)
TVaR_results_S5 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S5_Alpha1 <- TVaR_results_S5[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S5[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S5[e.w+t]<TVaR_S5_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S5[,"N"]=s
Alpha1Tcompare_S5[,"%"]=s/t.w
Alpha1Tcompare_S5[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S5[,"VR"]=Alpha1Tcompare_S5[,"N"]/Alpha1Tcompare_S5[,"EN"]
Alpha1Tcompare_S5 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S5_Alpha2 <- VaR_results_S5[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S5[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S5[e.w+i])<VaR_S5_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S5[,"N"]=j
Alpha2compare_S5[,"%"]=j/t.w
Alpha2compare_S5[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S5[,"VR"]=Alpha2compare_S5[,"N"]/Alpha2compare_S5[,"EN"]
Alpha2compare_S5 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S5_Alpha2 <- TVaR_results_S5[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S5[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S5[e.w+t]<TVaR_S5_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S5[,"N"]=s
Alpha2Tcompare_S5[,"%"]=s/t.w
Alpha2Tcompare_S5[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S5[,"VR"]=Alpha2Tcompare_S5[,"N"]/Alpha2Tcompare_S5[,"EN"]
Alpha2Tcompare_S5 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S5_Alpha3 <- VaR_results_S5[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S5[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S5[e.w+i])<VaR_S5_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S5[,"N"]=j
Alpha3compare_S5[,"%"]=j/t.w
Alpha3compare_S5[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S5[,"VR"]=Alpha3compare_S5[,"N"]/Alpha3compare_S5[,"EN"]
Alpha3compare_S5 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S5_Alpha3 <- TVaR_results_S5[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S5[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S5[e.w+t]<TVaR_S5_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S5[,"N"]=s
Alpha3Tcompare_S5[,"%"]=s/t.w
Alpha3Tcompare_S5[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S5[,"VR"]=Alpha3Tcompare_S5[,"N"]/Alpha3Tcompare_S5[,"EN"]
Alpha3Tcompare_S5 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S5_Alpha4 <- VaR_results_S5[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S5[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S5[e.w+i])<VaR_S5_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S5[,"N"]=j
Alpha4compare_S5[,"%"]=j/t.w
Alpha4compare_S5[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S5[,"VR"]=Alpha4compare_S5[,"N"]/Alpha4compare_S5[,"EN"]
Alpha4compare_S5 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S5_Alpha4 <- TVaR_results_S5[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S5[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S5[e.w+t]<TVaR_S5_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S5[,"N"]=s
Alpha4Tcompare_S5[,"%"]=s/t.w
Alpha4Tcompare_S5[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S5[,"VR"]=Alpha4Tcompare_S5[,"N"]/Alpha4Tcompare_S5[,"EN"]
Alpha4Tcompare_S5 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S5_Alpha5 <- VaR_results_S5[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S5[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S5[e.w+i])<VaR_S5_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S5[,"N"]=j
Alpha5compare_S5[,"%"]=j/t.w
Alpha5compare_S5[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S5[,"VR"]=Alpha5compare_S5[,"N"]/Alpha5compare_S5[,"EN"]
Alpha5compare_S5 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S5_Alpha5 <- TVaR_results_S5[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S5[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S5[e.w+t]<TVaR_S5_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S5[,"N"]=s
Alpha5Tcompare_S5[,"%"]=s/t.w
Alpha5Tcompare_S5[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S5[,"VR"]=Alpha5Tcompare_S5[,"N"]/Alpha5Tcompare_S5[,"EN"]
Alpha5Tcompare_S5 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S5_Alpha6 <- VaR_results_S5[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S5[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S5[e.w+i])<VaR_S5_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S5[,"N"]=j
Alpha6compare_S5[,"%"]=j/t.w
Alpha6compare_S5[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S5[,"VR"]=Alpha6compare_S5[,"N"]/Alpha6compare_S5[,"EN"]
Alpha6compare_S5 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S5_Alpha6 <- TVaR_results_S5[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S5 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S5) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S5[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S5[e.w+t]<TVaR_S5_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S5[,"N"]=s
Alpha6Tcompare_S5[,"%"]=s/t.w
Alpha6Tcompare_S5[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S5[,"VR"]=Alpha6Tcompare_S5[,"N"]/Alpha6Tcompare_S5[,"EN"]
Alpha6Tcompare_S5 # the matrix after executing the program


##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha1, conf.level = p[1])[1:7]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha2, conf.level = p[2])[1:7]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha3, conf.level = p[3])[1:7]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha4, conf.level = p[4])[1:7]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha5, conf.level = p[5])[1:7]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S5[e.w+1:t.w], VaR = VaR_S5_Alpha6, conf.level = p[6])[1:7]
LR_IN6



##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S5[e.w+1:t.w], VaR = TVaR_S5_Alpha6, conf.level = p[6])[8:12]
TLR_IN6


```


#FOR POPEYES
```{r}
#set up estimation window (e.w) and testing window (t.w)
S6 <- data$POPEYES #returns for POPEYES

#DESCRIPTIVE STATISTICS
descdist(S6, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S6 <- rollapply(as.zoo(S6), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S6 <- lag(VaR_results_S6,k=-1)
VaR_results_S6 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S6_Alpha1 <- VaR_results_S6[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S6[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S6[e.w+i])<VaR_S6_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S6[,"N"]=j
Alpha1compare_S6[,"%"]=j/t.w
Alpha1compare_S6[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S6[,"VR"]=Alpha1compare_S6[,"N"]/Alpha1compare_S6[,"EN"]
Alpha1compare_S6 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR T-DISTRIBUTION
#Another method best method
A=fitdistr(S6[1:252], "t", start = list(m=mean(S6[1:252]), s=sd(S6[1:252]), df=6), lower=c(-1,0.001,1))
A

df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S6 <- rollapply(as.zoo(S6), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S6 <- lag(TVaR_results_S6,k=-1)
TVaR_results_S6 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S6_Alpha1 <- TVaR_results_S6[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S6[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S6[e.w+t]<TVaR_S6_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S6[,"N"]=s
Alpha1Tcompare_S6[,"%"]=s/t.w
Alpha1Tcompare_S6[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S6[,"VR"]=Alpha1Tcompare_S6[,"N"]/Alpha1Tcompare_S6[,"EN"]
Alpha1Tcompare_S6 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S6_Alpha2 <- VaR_results_S6[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S6[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S6[e.w+i])<VaR_S6_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S6[,"N"]=j
Alpha2compare_S6[,"%"]=j/t.w
Alpha2compare_S6[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S6[,"VR"]=Alpha2compare_S6[,"N"]/Alpha2compare_S6[,"EN"]
Alpha2compare_S6 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S6_Alpha2 <- TVaR_results_S6[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S6[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S6[e.w+t]<TVaR_S6_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S6[,"N"]=s
Alpha2Tcompare_S6[,"%"]=s/t.w
Alpha2Tcompare_S6[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S6[,"VR"]=Alpha2Tcompare_S6[,"N"]/Alpha2Tcompare_S6[,"EN"]
Alpha2Tcompare_S6 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S6_Alpha3 <- VaR_results_S6[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S6[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S6[e.w+i])<VaR_S6_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S6[,"N"]=j
Alpha3compare_S6[,"%"]=j/t.w
Alpha3compare_S6[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S6[,"VR"]=Alpha3compare_S6[,"N"]/Alpha3compare_S6[,"EN"]
Alpha3compare_S6 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S6_Alpha3 <- TVaR_results_S6[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S6[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S6[e.w+t]<TVaR_S6_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S6[,"N"]=s
Alpha3Tcompare_S6[,"%"]=s/t.w
Alpha3Tcompare_S6[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S6[,"VR"]=Alpha3Tcompare_S6[,"N"]/Alpha3Tcompare_S6[,"EN"]
Alpha3Tcompare_S6 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S6_Alpha4 <- VaR_results_S6[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S6[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S6[e.w+i])<VaR_S6_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S6[,"N"]=j
Alpha4compare_S6[,"%"]=j/t.w
Alpha4compare_S6[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S6[,"VR"]=Alpha4compare_S6[,"N"]/Alpha4compare_S6[,"EN"]
Alpha4compare_S6 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S6_Alpha4 <- TVaR_results_S6[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S6[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S6[e.w+t]<TVaR_S6_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S6[,"N"]=s
Alpha4Tcompare_S6[,"%"]=s/t.w
Alpha4Tcompare_S6[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S6[,"VR"]=Alpha4Tcompare_S6[,"N"]/Alpha4Tcompare_S6[,"EN"]
Alpha4Tcompare_S6 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S6_Alpha5 <- VaR_results_S6[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S6[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S6[e.w+i])<VaR_S6_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S6[,"N"]=j
Alpha5compare_S6[,"%"]=j/t.w
Alpha5compare_S6[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S6[,"VR"]=Alpha5compare_S6[,"N"]/Alpha5compare_S6[,"EN"]
Alpha5compare_S6 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S6_Alpha5 <- TVaR_results_S6[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S6[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S6[e.w+t]<TVaR_S6_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S6[,"N"]=s
Alpha5Tcompare_S6[,"%"]=s/t.w
Alpha5Tcompare_S6[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S6[,"VR"]=Alpha5Tcompare_S6[,"N"]/Alpha5Tcompare_S6[,"EN"]
Alpha5Tcompare_S6 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S6_Alpha6 <- VaR_results_S6[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S6[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S6[e.w+i])<VaR_S6_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S6[,"N"]=j
Alpha6compare_S6[,"%"]=j/t.w
Alpha6compare_S6[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S6[,"VR"]=Alpha6compare_S6[,"N"]/Alpha6compare_S6[,"EN"]
Alpha6compare_S6 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S6_Alpha6 <- TVaR_results_S6[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S6 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S6) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S6[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S6[e.w+t]<TVaR_S6_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S6[,"N"]=s
Alpha6Tcompare_S6[,"%"]=s/t.w
Alpha6Tcompare_S6[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S6[,"VR"]=Alpha6Tcompare_S6[,"N"]/Alpha6Tcompare_S6[,"EN"]
Alpha6Tcompare_S6 # the matrix after executing the program



##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S6[e.w+1:t.w], VaR = VaR_S6_Alpha6, conf.level = p[6])[8:12]
LR_IN6


##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S6[e.w+1:t.w], VaR = TVaR_S6_Alpha6, conf.level = p[6])[8:12]
TLR_IN6


```


#FOR CISCO
```{r}
#set up estimation window (e.w) and testing window (t.w)
S7 <- data$CISCO #returns for CISCO


#DESCRIPTIVE STATISTICS
descdist(S7, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S7 <- rollapply(as.zoo(S7), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S7 <- lag(VaR_results_S7,k=-1)
VaR_results_S7 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S7_Alpha1 <- VaR_results_S7[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S7[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S7[e.w+i])<VaR_S7_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S7[,"N"]=j
Alpha1compare_S7[,"%"]=j/t.w
Alpha1compare_S7[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S7[,"VR"]=Alpha1compare_S7[,"N"]/Alpha1compare_S7[,"EN"]
Alpha1compare_S7 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR T-DISTRIBUTION
A=fitdistr(S7[1:252], "t", start = list(m=mean(S7[1:252]), s=sd(S7[1:252]), df=5), lower=c(-1,0.001,1))
A

df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S7 <- rollapply(as.zoo(S7), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S7 <- lag(TVaR_results_S7,k=-1)
TVaR_results_S7 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S7_Alpha1 <- TVaR_results_S7[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S7[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S7[e.w+t]<TVaR_S7_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S7[,"N"]=s
Alpha1Tcompare_S7[,"%"]=s/t.w
Alpha1Tcompare_S7[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S7[,"VR"]=Alpha1Tcompare_S7[,"N"]/Alpha1Tcompare_S7[,"EN"]
Alpha1Tcompare_S7 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S7_Alpha2 <- VaR_results_S7[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S7[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S7[e.w+i])<VaR_S7_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S7[,"N"]=j
Alpha2compare_S7[,"%"]=j/t.w
Alpha2compare_S7[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S7[,"VR"]=Alpha2compare_S7[,"N"]/Alpha2compare_S7[,"EN"]
Alpha2compare_S7 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S7_Alpha2 <- TVaR_results_S7[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S7[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S7[e.w+t]<TVaR_S7_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S7[,"N"]=s
Alpha2Tcompare_S7[,"%"]=s/t.w
Alpha2Tcompare_S7[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S7[,"VR"]=Alpha2Tcompare_S7[,"N"]/Alpha2Tcompare_S7[,"EN"]
Alpha2Tcompare_S7 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S7_Alpha3 <- VaR_results_S7[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S7[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S7[e.w+i])<VaR_S7_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S7[,"N"]=j
Alpha3compare_S7[,"%"]=j/t.w
Alpha3compare_S7[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S7[,"VR"]=Alpha3compare_S7[,"N"]/Alpha3compare_S7[,"EN"]
Alpha3compare_S7 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S7_Alpha3 <- TVaR_results_S7[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S7[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S7[e.w+t]<TVaR_S7_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S7[,"N"]=s
Alpha3Tcompare_S7[,"%"]=s/t.w
Alpha3Tcompare_S7[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S7[,"VR"]=Alpha3Tcompare_S7[,"N"]/Alpha3Tcompare_S7[,"EN"]
Alpha3Tcompare_S7 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S7_Alpha4 <- VaR_results_S7[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S7[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S7[e.w+i])<VaR_S7_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S7[,"N"]=j
Alpha4compare_S7[,"%"]=j/t.w
Alpha4compare_S7[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S7[,"VR"]=Alpha4compare_S7[,"N"]/Alpha4compare_S7[,"EN"]
Alpha4compare_S7 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S7_Alpha4 <- TVaR_results_S7[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S7[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S7[e.w+t]<TVaR_S7_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S7[,"N"]=s
Alpha4Tcompare_S7[,"%"]=s/t.w
Alpha4Tcompare_S7[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S7[,"VR"]=Alpha4Tcompare_S7[,"N"]/Alpha4Tcompare_S7[,"EN"]
Alpha4Tcompare_S7 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S7_Alpha5 <- VaR_results_S7[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S7[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S7[e.w+i])<VaR_S7_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S7[,"N"]=j
Alpha5compare_S7[,"%"]=j/t.w
Alpha5compare_S7[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S7[,"VR"]=Alpha5compare_S7[,"N"]/Alpha5compare_S7[,"EN"]
Alpha5compare_S7 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S7_Alpha5 <- TVaR_results_S7[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S7[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S7[e.w+t]<TVaR_S7_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S7[,"N"]=s
Alpha5Tcompare_S7[,"%"]=s/t.w
Alpha5Tcompare_S7[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S7[,"VR"]=Alpha5Tcompare_S7[,"N"]/Alpha5Tcompare_S7[,"EN"]
Alpha5Tcompare_S7 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S7_Alpha6 <- VaR_results_S7[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S7[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S7[e.w+i])<VaR_S7_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S7[,"N"]=j
Alpha6compare_S7[,"%"]=j/t.w
Alpha6compare_S7[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S7[,"VR"]=Alpha6compare_S7[,"N"]/Alpha6compare_S7[,"EN"]
Alpha6compare_S7 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S7_Alpha6 <- TVaR_results_S7[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S7 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S7) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S7[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S7[e.w+t]<TVaR_S7_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S7[,"N"]=s
Alpha6Tcompare_S7[,"%"]=s/t.w
Alpha6Tcompare_S7[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S7[,"VR"]=Alpha6Tcompare_S7[,"N"]/Alpha6Tcompare_S7[,"EN"]
Alpha6Tcompare_S7 # the matrix after executing the program



##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S7[e.w+1:t.w], VaR = VaR_S7_Alpha6, conf.level = p[6])[8:12]
LR_IN6


##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S7[e.w+1:t.w], VaR = TVaR_S7_Alpha6, conf.level = p[6])[8:12]
TLR_IN6


```


#FOR FACEBOOK
```{r}
#set up estimation window (e.w) and testing window (t.w)
S8 <- data$FACEBOOK #returns for FACEBOOK

#DESCRIPTIVE STATISTICS
descdist(S8, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S8 <- rollapply(as.zoo(S8), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S8 <- lag(VaR_results_S8,k=-1)
VaR_results_S8 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S8_Alpha1 <- VaR_results_S8[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S8[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S8[e.w+i])<VaR_S8_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S8[,"N"]=j
Alpha1compare_S8[,"%"]=j/t.w
Alpha1compare_S8[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S8[,"VR"]=Alpha1compare_S8[,"N"]/Alpha1compare_S8[,"EN"]
Alpha1compare_S8 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#Another method best method
A=fitdistr(S8[1:252], "t", start = list(m=mean(S8[1:252]), s=sd(S8[1:252]), df=5), lower=c(-1,0.001,1))
A

df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S8 <- rollapply(as.zoo(S8), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S8 <- lag(TVaR_results_S8,k=-1)
TVaR_results_S8 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S8_Alpha1 <- TVaR_results_S8[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S8[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S8[e.w+t]<TVaR_S8_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S8[,"N"]=s
Alpha1Tcompare_S8[,"%"]=s/t.w
Alpha1Tcompare_S8[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S8[,"VR"]=Alpha1Tcompare_S8[,"N"]/Alpha1Tcompare_S8[,"EN"]
Alpha1Tcompare_S8 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S8_Alpha2 <- VaR_results_S8[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S8[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S8[e.w+i])<VaR_S8_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S8[,"N"]=j
Alpha2compare_S8[,"%"]=j/t.w
Alpha2compare_S8[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S8[,"VR"]=Alpha2compare_S8[,"N"]/Alpha2compare_S8[,"EN"]
Alpha2compare_S8 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S8_Alpha2 <- TVaR_results_S8[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S8[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S8[e.w+t]<TVaR_S8_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S8[,"N"]=s
Alpha2Tcompare_S8[,"%"]=s/t.w
Alpha2Tcompare_S8[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S8[,"VR"]=Alpha2Tcompare_S8[,"N"]/Alpha2Tcompare_S8[,"EN"]
Alpha2Tcompare_S8 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S8_Alpha3 <- VaR_results_S8[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S8[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S8[e.w+i])<VaR_S8_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S8[,"N"]=j
Alpha3compare_S8[,"%"]=j/t.w
Alpha3compare_S8[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S8[,"VR"]=Alpha3compare_S8[,"N"]/Alpha3compare_S8[,"EN"]
Alpha3compare_S8 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S8_Alpha3 <- TVaR_results_S8[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S8[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S8[e.w+t]<TVaR_S8_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S8[,"N"]=s
Alpha3Tcompare_S8[,"%"]=s/t.w
Alpha3Tcompare_S8[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S8[,"VR"]=Alpha3Tcompare_S8[,"N"]/Alpha3Tcompare_S8[,"EN"]
Alpha3Tcompare_S8 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S8_Alpha4 <- VaR_results_S8[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S8[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S8[e.w+i])<VaR_S8_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S8[,"N"]=j
Alpha4compare_S8[,"%"]=j/t.w
Alpha4compare_S8[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S8[,"VR"]=Alpha4compare_S8[,"N"]/Alpha4compare_S8[,"EN"]
Alpha4compare_S8 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S8_Alpha4 <- TVaR_results_S8[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S8[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S8[e.w+t]<TVaR_S8_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S8[,"N"]=s
Alpha4Tcompare_S8[,"%"]=s/t.w
Alpha4Tcompare_S8[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S8[,"VR"]=Alpha4Tcompare_S8[,"N"]/Alpha4Tcompare_S8[,"EN"]
Alpha4Tcompare_S8 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S8_Alpha5 <- VaR_results_S8[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S8[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S8[e.w+i])<VaR_S8_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S8[,"N"]=j
Alpha5compare_S8[,"%"]=j/t.w
Alpha5compare_S8[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S8[,"VR"]=Alpha5compare_S8[,"N"]/Alpha5compare_S8[,"EN"]
Alpha5compare_S8 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S8_Alpha5 <- TVaR_results_S8[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S8[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S8[e.w+t]<TVaR_S8_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S8[,"N"]=s
Alpha5Tcompare_S8[,"%"]=s/t.w
Alpha5Tcompare_S8[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S8[,"VR"]=Alpha5Tcompare_S8[,"N"]/Alpha5Tcompare_S8[,"EN"]
Alpha5Tcompare_S8 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S8_Alpha6 <- VaR_results_S8[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S8[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S8[e.w+i])<VaR_S8_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S8[,"N"]=j
Alpha6compare_S8[,"%"]=j/t.w
Alpha6compare_S8[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S8[,"VR"]=Alpha6compare_S8[,"N"]/Alpha6compare_S8[,"EN"]
Alpha6compare_S8 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S8_Alpha6 <- TVaR_results_S8[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S8 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S8) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S8[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S8[e.w+t]<TVaR_S8_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S8[,"N"]=s
Alpha6Tcompare_S8[,"%"]=s/t.w
Alpha6Tcompare_S8[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S8[,"VR"]=Alpha6Tcompare_S8[,"N"]/Alpha6Tcompare_S8[,"EN"]
Alpha6Tcompare_S8 # the matrix after executing the program



##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S8[e.w+1:t.w], VaR = VaR_S8_Alpha6, conf.level = p[6])[8:12]
LR_IN6


##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S8[e.w+1:t.w], VaR = TVaR_S8_Alpha6, conf.level = p[6])[8:12]
TLR_IN6

```


#FOR ON-SEMICONDUCTOR
```{r}
#set up estimation window (e.w) and testing window (t.w)
S9 <- data$ON #returns for ON-SEMI


#DESCRIPTIVE STATISTICS
descdist(S9, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S9 <- rollapply(as.zoo(S9), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S9 <- lag(VaR_results_S9,k=-1)
VaR_results_S9 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S9_Alpha1 <- VaR_results_S9[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S9[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S9[e.w+i])<VaR_S9_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S9[,"N"]=j
Alpha1compare_S9[,"%"]=j/t.w
Alpha1compare_S9[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S9[,"VR"]=Alpha1compare_S9[,"N"]/Alpha1compare_S9[,"EN"]
Alpha1compare_S9 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS


#FITTING T-DISTRIBUTION FOR DF
A=fitdistr(S9[1:252], "t", start = list(m=mean(S9[1:252]), s=sd(S9[1:252]), df=5), lower=c(-1,0.001,1))
A

df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S9 <- rollapply(as.zoo(S9), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S9 <- lag(TVaR_results_S9,k=-1)
TVaR_results_S9 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S9_Alpha1 <- TVaR_results_S9[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S9[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S9[e.w+t]<TVaR_S9_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S9[,"N"]=s
Alpha1Tcompare_S9[,"%"]=s/t.w
Alpha1Tcompare_S9[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S9[,"VR"]=Alpha1Tcompare_S9[,"N"]/Alpha1Tcompare_S9[,"EN"]
Alpha1Tcompare_S9 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S9_Alpha2 <- VaR_results_S9[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S9[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S9[e.w+i])<VaR_S9_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S9[,"N"]=j
Alpha2compare_S9[,"%"]=j/t.w
Alpha2compare_S9[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S9[,"VR"]=Alpha2compare_S9[,"N"]/Alpha2compare_S9[,"EN"]
Alpha2compare_S9 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S9_Alpha2 <- TVaR_results_S9[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S9[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S9[e.w+t]<TVaR_S9_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S9[,"N"]=s
Alpha2Tcompare_S9[,"%"]=s/t.w
Alpha2Tcompare_S9[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S9[,"VR"]=Alpha2Tcompare_S9[,"N"]/Alpha2Tcompare_S9[,"EN"]
Alpha2Tcompare_S9 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S9_Alpha3 <- VaR_results_S9[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S9[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S9[e.w+i])<VaR_S9_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S9[,"N"]=j
Alpha3compare_S9[,"%"]=j/t.w
Alpha3compare_S9[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S9[,"VR"]=Alpha3compare_S9[,"N"]/Alpha3compare_S9[,"EN"]
Alpha3compare_S9 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S9_Alpha3 <- TVaR_results_S9[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S9[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S9[e.w+t]<TVaR_S9_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S9[,"N"]=s
Alpha3Tcompare_S9[,"%"]=s/t.w
Alpha3Tcompare_S9[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S9[,"VR"]=Alpha3Tcompare_S9[,"N"]/Alpha3Tcompare_S9[,"EN"]
Alpha3Tcompare_S9 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S9_Alpha4 <- VaR_results_S9[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S9[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S9[e.w+i])<VaR_S9_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S9[,"N"]=j
Alpha4compare_S9[,"%"]=j/t.w
Alpha4compare_S9[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S9[,"VR"]=Alpha4compare_S9[,"N"]/Alpha4compare_S9[,"EN"]
Alpha4compare_S9 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S9_Alpha4 <- TVaR_results_S9[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S9[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S9[e.w+t]<TVaR_S9_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S9[,"N"]=s
Alpha4Tcompare_S9[,"%"]=s/t.w
Alpha4Tcompare_S9[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S9[,"VR"]=Alpha4Tcompare_S9[,"N"]/Alpha4Tcompare_S9[,"EN"]
Alpha4Tcompare_S9 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S9_Alpha5 <- VaR_results_S9[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S9[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S9[e.w+i])<VaR_S9_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S9[,"N"]=j
Alpha5compare_S9[,"%"]=j/t.w
Alpha5compare_S9[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S9[,"VR"]=Alpha5compare_S9[,"N"]/Alpha5compare_S9[,"EN"]
Alpha5compare_S9 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S9_Alpha5 <- TVaR_results_S9[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S9[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S9[e.w+t]<TVaR_S9_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S9[,"N"]=s
Alpha5Tcompare_S9[,"%"]=s/t.w
Alpha5Tcompare_S9[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S9[,"VR"]=Alpha5Tcompare_S9[,"N"]/Alpha5Tcompare_S9[,"EN"]
Alpha5Tcompare_S9 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S9_Alpha6 <- VaR_results_S9[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S9[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S9[e.w+i])<VaR_S9_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S9[,"N"]=j
Alpha6compare_S9[,"%"]=j/t.w
Alpha6compare_S9[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S9[,"VR"]=Alpha6compare_S9[,"N"]/Alpha6compare_S9[,"EN"]
Alpha6compare_S9 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S9_Alpha6 <- TVaR_results_S9[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S9 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S9) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S9[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S9[e.w+t]<TVaR_S9_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S9[,"N"]=s
Alpha6Tcompare_S9[,"%"]=s/t.w
Alpha6Tcompare_S9[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S9[,"VR"]=Alpha6Tcompare_S9[,"N"]/Alpha6Tcompare_S9[,"EN"]
Alpha6Tcompare_S9 # the matrix after executing the program



##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S9[e.w+1:t.w], VaR = VaR_S9_Alpha6, conf.level = p[6])[8:12]
LR_IN6


##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S9[e.w+1:t.w], VaR = TVaR_S9_Alpha6, conf.level = p[6])[8:12]
TLR_IN6


```


#FOR FRONTIER
```{r}
#set up estimation window (e.w) and testing window (t.w)
S10 <- data$FRONTIER #returns for FRONTIER

#DESCRIPTIVE STATISTICS
descdist(S10, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S10 <- rollapply(as.zoo(S10), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S10 <- lag(VaR_results_S10,k=-1)
VaR_results_S10 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S10_Alpha1 <- VaR_results_S10[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S10[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S10[e.w+i])<VaR_S10_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S10[,"N"]=j
Alpha1compare_S10[,"%"]=j/t.w
Alpha1compare_S10[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S10[,"VR"]=Alpha1compare_S10[,"N"]/Alpha1compare_S10[,"EN"]
Alpha1compare_S10 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS


#FITTING T-DISTRIBUTION FOR DF
A=fitdistr(S10[1:252], "t", start = list(m=mean(S10[1:252]), s=sd(S10[1:252]), df=4), lower=c(-1,0.001,1))
A

df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S10 <- rollapply(as.zoo(S10), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S10 <- lag(TVaR_results_S10,k=-1)
TVaR_results_S10 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S10_Alpha1 <- TVaR_results_S10[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S10[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S10[e.w+t]<TVaR_S10_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S10[,"N"]=s
Alpha1Tcompare_S10[,"%"]=s/t.w
Alpha1Tcompare_S10[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S10[,"VR"]=Alpha1Tcompare_S10[,"N"]/Alpha1Tcompare_S10[,"EN"]
Alpha1Tcompare_S10 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S10_Alpha2 <- VaR_results_S10[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S10[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S10[e.w+i])<VaR_S10_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S10[,"N"]=j
Alpha2compare_S10[,"%"]=j/t.w
Alpha2compare_S10[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S10[,"VR"]=Alpha2compare_S10[,"N"]/Alpha2compare_S10[,"EN"]
Alpha2compare_S10 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S10_Alpha2 <- TVaR_results_S10[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S10[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S10[e.w+t]<TVaR_S10_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S10[,"N"]=s
Alpha2Tcompare_S10[,"%"]=s/t.w
Alpha2Tcompare_S10[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S10[,"VR"]=Alpha2Tcompare_S10[,"N"]/Alpha2Tcompare_S10[,"EN"]
Alpha2Tcompare_S10 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S10_Alpha3 <- VaR_results_S10[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S10[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S10[e.w+i])<VaR_S10_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S10[,"N"]=j
Alpha3compare_S10[,"%"]=j/t.w
Alpha3compare_S10[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S10[,"VR"]=Alpha3compare_S10[,"N"]/Alpha3compare_S10[,"EN"]
Alpha3compare_S10 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S10_Alpha3 <- TVaR_results_S10[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S10[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S10[e.w+t]<TVaR_S10_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S10[,"N"]=s
Alpha3Tcompare_S10[,"%"]=s/t.w
Alpha3Tcompare_S10[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S10[,"VR"]=Alpha3Tcompare_S10[,"N"]/Alpha3Tcompare_S10[,"EN"]
Alpha3Tcompare_S10 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S10_Alpha4 <- VaR_results_S10[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S10[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S10[e.w+i])<VaR_S10_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S10[,"N"]=j
Alpha4compare_S10[,"%"]=j/t.w
Alpha4compare_S10[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S10[,"VR"]=Alpha4compare_S10[,"N"]/Alpha4compare_S10[,"EN"]
Alpha4compare_S10 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S10_Alpha4 <- TVaR_results_S10[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S10[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S10[e.w+t]<TVaR_S10_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S10[,"N"]=s
Alpha4Tcompare_S10[,"%"]=s/t.w
Alpha4Tcompare_S10[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S10[,"VR"]=Alpha4Tcompare_S10[,"N"]/Alpha4Tcompare_S10[,"EN"]
Alpha4Tcompare_S10 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S10_Alpha5 <- VaR_results_S10[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S10[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S10[e.w+i])<VaR_S10_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S10[,"N"]=j
Alpha5compare_S10[,"%"]=j/t.w
Alpha5compare_S10[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S10[,"VR"]=Alpha5compare_S10[,"N"]/Alpha5compare_S10[,"EN"]
Alpha5compare_S10 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S10_Alpha5 <- TVaR_results_S10[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S10[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S10[e.w+t]<TVaR_S10_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S10[,"N"]=s
Alpha5Tcompare_S10[,"%"]=s/t.w
Alpha5Tcompare_S10[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S10[,"VR"]=Alpha5Tcompare_S10[,"N"]/Alpha5Tcompare_S10[,"EN"]
Alpha5Tcompare_S10 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S10_Alpha6 <- VaR_results_S10[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S10[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S10[e.w+i])<VaR_S10_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S10[,"N"]=j
Alpha6compare_S10[,"%"]=j/t.w
Alpha6compare_S10[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S10[,"VR"]=Alpha6compare_S10[,"N"]/Alpha6compare_S10[,"EN"]
Alpha6compare_S10 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S10_Alpha6 <- TVaR_results_S10[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S10 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S10) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S10[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S10[e.w+t]<TVaR_S10_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S10[,"N"]=s
Alpha6Tcompare_S10[,"%"]=s/t.w
Alpha6Tcompare_S10[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S10[,"VR"]=Alpha6Tcompare_S10[,"N"]/Alpha6Tcompare_S10[,"EN"]
Alpha6Tcompare_S10 # the matrix after executing the program



##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S10[e.w+1:t.w], VaR = VaR_S10_Alpha6, conf.level = p[6])[8:12]
LR_IN6


##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha6, conf.level = p[6])[1:7]
TLR_UC6

#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S10[e.w+1:t.w], VaR = TVaR_S10_Alpha6, conf.level = p[6])[8:12]
TLR_IN6

```


#FOR SPARTAN
```{r}
#set up estimation window (e.w) and testing window (t.w)
S11 <- data$SPARTAN #returns for SPARTAN

#DESCRIPTIVE STATISTICS
descdist(S11, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S11 <- rollapply(as.zoo(S11), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S11 <- lag(VaR_results_S11,k=-1)
VaR_results_S11 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S11_Alpha1 <- VaR_results_S11[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S11[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S11[e.w+i])<VaR_S11_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S11[,"N"]=j
Alpha1compare_S11[,"%"]=j/t.w
Alpha1compare_S11[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S11[,"VR"]=Alpha1compare_S11[,"N"]/Alpha1compare_S11[,"EN"]
Alpha1compare_S11 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS


#FITTING T-DISTRIBUTION FOR DF
A=fitdistr(S11[1:252], "t", start = list(m=mean(S11[1:252]), s=sd(S11[1:252]), df=5), lower=c(-1,0.001,1))
A
df=A$estimate[3]
#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S11 <- rollapply(as.zoo(S11), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S11 <- lag(TVaR_results_S11,k=-1)
TVaR_results_S11 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S11_Alpha1 <- TVaR_results_S11[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S11[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S11[e.w+t]<TVaR_S11_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S11[,"N"]=s
Alpha1Tcompare_S11[,"%"]=s/t.w
Alpha1Tcompare_S11[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S11[,"VR"]=Alpha1Tcompare_S11[,"N"]/Alpha1Tcompare_S11[,"EN"]
Alpha1Tcompare_S11 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S11_Alpha2 <- VaR_results_S11[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S11[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S11[e.w+i])<VaR_S11_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S11[,"N"]=j
Alpha2compare_S11[,"%"]=j/t.w
Alpha2compare_S11[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S11[,"VR"]=Alpha2compare_S11[,"N"]/Alpha2compare_S11[,"EN"]
Alpha2compare_S11 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S11_Alpha2 <- TVaR_results_S11[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S11[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S11[e.w+t]<TVaR_S11_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S11[,"N"]=s
Alpha2Tcompare_S11[,"%"]=s/t.w
Alpha2Tcompare_S11[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S11[,"VR"]=Alpha2Tcompare_S11[,"N"]/Alpha2Tcompare_S11[,"EN"]
Alpha2Tcompare_S11 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S11_Alpha3 <- VaR_results_S11[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S11[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S11[e.w+i])<VaR_S11_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S11[,"N"]=j
Alpha3compare_S11[,"%"]=j/t.w
Alpha3compare_S11[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S11[,"VR"]=Alpha3compare_S11[,"N"]/Alpha3compare_S11[,"EN"]
Alpha3compare_S11 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S11_Alpha3 <- TVaR_results_S11[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S11[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S11[e.w+t]<TVaR_S11_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S11[,"N"]=s
Alpha3Tcompare_S11[,"%"]=s/t.w
Alpha3Tcompare_S11[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S11[,"VR"]=Alpha3Tcompare_S11[,"N"]/Alpha3Tcompare_S11[,"EN"]
Alpha3Tcompare_S11 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S11_Alpha4 <- VaR_results_S11[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S11[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S11[e.w+i])<VaR_S11_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S11[,"N"]=j
Alpha4compare_S11[,"%"]=j/t.w
Alpha4compare_S11[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S11[,"VR"]=Alpha4compare_S11[,"N"]/Alpha4compare_S11[,"EN"]
Alpha4compare_S11 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S11_Alpha4 <- TVaR_results_S11[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S11[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S11[e.w+t]<TVaR_S11_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S11[,"N"]=s
Alpha4Tcompare_S11[,"%"]=s/t.w
Alpha4Tcompare_S11[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S11[,"VR"]=Alpha4Tcompare_S11[,"N"]/Alpha4Tcompare_S11[,"EN"]
Alpha4Tcompare_S11 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S11_Alpha5 <- VaR_results_S11[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S11[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S11[e.w+i])<VaR_S11_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S11[,"N"]=j
Alpha5compare_S11[,"%"]=j/t.w
Alpha5compare_S11[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S11[,"VR"]=Alpha5compare_S11[,"N"]/Alpha5compare_S11[,"EN"]
Alpha5compare_S11 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S11_Alpha5 <- TVaR_results_S11[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S11[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S11[e.w+t]<TVaR_S11_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S11[,"N"]=s
Alpha5Tcompare_S11[,"%"]=s/t.w
Alpha5Tcompare_S11[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S11[,"VR"]=Alpha5Tcompare_S11[,"N"]/Alpha5Tcompare_S11[,"EN"]
Alpha5Tcompare_S11 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S11_Alpha6 <- VaR_results_S11[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S11[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S11[e.w+i])<VaR_S11_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S11[,"N"]=j
Alpha6compare_S11[,"%"]=j/t.w
Alpha6compare_S11[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S11[,"VR"]=Alpha6compare_S11[,"N"]/Alpha6compare_S11[,"EN"]
Alpha6compare_S11 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S11_Alpha6 <- TVaR_results_S11[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S11 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S11) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S11[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S11[e.w+t]<TVaR_S11_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S11[,"N"]=s
Alpha6Tcompare_S11[,"%"]=s/t.w
Alpha6Tcompare_S11[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S11[,"VR"]=Alpha6Tcompare_S11[,"N"]/Alpha6Tcompare_S11[,"EN"]
Alpha6Tcompare_S11 # the matrix after executing the program



##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha1, conf.level = p[1])[8:12]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha2, conf.level = p[2])[8:12]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha3, conf.level = p[3])[8:12]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha4, conf.level = p[4])[8:12]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha5, conf.level = p[5])[8:12]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S11[e.w+1:t.w], VaR = VaR_S11_Alpha6, conf.level = p[6])[8:12]
LR_IN6


##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha6, conf.level = p[6])[1:7]
TLR_UC6


#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S11[e.w+1:t.w], VaR = TVaR_S11_Alpha6, conf.level = p[6])[8:12]
TLR_IN6

```


#FOR GILEAD
```{r}
#set up estimation window (e.w) and testing window (t.w)
S12 <- data$GILEAD #returns for GILEAD

#DESCRIPTIVE STATISTICS
descdist(S12, discrete=FALSE, boot=500) #gives information about the descriptive statistics

#FOR NORMAL VAR
VaR_results_S12 <- rollapply(as.zoo(S12), width=e.w, FUN=Cal_VaR, align="right")
VaR_results_S12 <- lag(VaR_results_S12,k=-1)
VaR_results_S12 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under normal distribution

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR NORMAL DISTRIBUTION
VaR_S12_Alpha1 <- VaR_results_S12[,1]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1compare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1compare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1compare_S12[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S12[e.w+i])<VaR_S12_Alpha1[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha1compare_S12[,"N"]=j
Alpha1compare_S12[,"%"]=j/t.w
Alpha1compare_S12[,"EN"]=(alpha[1]*t.w)
Alpha1compare_S12[,"VR"]=Alpha1compare_S12[,"N"]/Alpha1compare_S12[,"EN"]
Alpha1compare_S12 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS


#FITTING T-DISTRIBUTION FOR DF
A=fitdistr(S12[1:252], "t", start = list(m=mean(S12[1:252]), s=sd(S12[1:252]), df=5), lower=c(-1,0.001,1))
A

df=A$estimate[3]

#FOR VALUE AT RISK WITH NONCONSTANT TREND USING T-DISTRIBUTION
TVaR_results_S12 <- rollapply(as.zoo(S12), width=e.w, FUN=Cal_TVaR, align="right")
TVaR_results_S12 <- lag(TVaR_results_S12,k=-1)
TVaR_results_S12 # Value-at-Risk(VaR) for 3 years, 503 trading days testing window under t-distribution


#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.05 FOR T-DISTRIBUTION
TVaR_S12_Alpha1 <- TVaR_results_S12[,1]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha1Tcompare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha1Tcompare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha1Tcompare_S12[,"alpha"] <- alpha[1]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S12[e.w+t]<TVaR_S12_Alpha1[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha1Tcompare_S12[,"N"]=s
Alpha1Tcompare_S12[,"%"]=s/t.w
Alpha1Tcompare_S12[,"EN"]=(alpha[1]*t.w)
Alpha1Tcompare_S12[,"VR"]=Alpha1Tcompare_S12[,"N"]/Alpha1Tcompare_S12[,"EN"]
Alpha1Tcompare_S12 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.01 NORMAL DISTRIBUTION
VaR_S12_Alpha2 <- VaR_results_S12[,2]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2compare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2compare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2compare_S12[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S12[e.w+i])<VaR_S12_Alpha2[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha2compare_S12[,"N"]=j
Alpha2compare_S12[,"%"]=j/t.w
Alpha2compare_S12[,"EN"]=(alpha[2]*t.w)
Alpha2compare_S12[,"VR"]=Alpha2compare_S12[,"N"]/Alpha2compare_S12[,"EN"]
Alpha2compare_S12 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS UNDER 0.01 FOR T-DISTRIBUTION
TVaR_S12_Alpha2 <- TVaR_results_S12[,2]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha2Tcompare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha2Tcompare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha2Tcompare_S12[,"alpha"] <- alpha[2]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S12[e.w+t]<TVaR_S12_Alpha2[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha2Tcompare_S12[,"N"]=s
Alpha2Tcompare_S12[,"%"]=s/t.w
Alpha2Tcompare_S12[,"EN"]=(alpha[2]*t.w)
Alpha2Tcompare_S12[,"VR"]=Alpha2Tcompare_S12[,"N"]/Alpha2Tcompare_S12[,"EN"]
Alpha2Tcompare_S12 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 NORMAL DISTRIBUTION
VaR_S12_Alpha3 <- VaR_results_S12[,3]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3compare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3compare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3compare_S12[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S12[e.w+i])<VaR_S12_Alpha3[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha3compare_S12[,"N"]=j
Alpha3compare_S12[,"%"]=j/t.w
Alpha3compare_S12[,"EN"]=(alpha[3]*t.w)
Alpha3compare_S12[,"VR"]=Alpha3compare_S12[,"N"]/Alpha3compare_S12[,"EN"]
Alpha3compare_S12 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.02 FOR T-DISTRIBUTION
TVaR_S12_Alpha3 <- TVaR_results_S12[,3]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha3Tcompare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha3Tcompare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha3Tcompare_S12[,"alpha"] <- alpha[3]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S12[e.w+t]<TVaR_S12_Alpha3[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha3Tcompare_S12[,"N"]=s
Alpha3Tcompare_S12[,"%"]=s/t.w
Alpha3Tcompare_S12[,"EN"]=(alpha[3]*t.w)
Alpha3Tcompare_S12[,"VR"]=Alpha3Tcompare_S12[,"N"]/Alpha3Tcompare_S12[,"EN"]
Alpha3Tcompare_S12 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 NORMAL DISTRIBUTION
VaR_S12_Alpha4 <- VaR_results_S12[,4]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4compare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4compare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4compare_S12[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S12[e.w+i])<VaR_S12_Alpha4[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha4compare_S12[,"N"]=j
Alpha4compare_S12[,"%"]=j/t.w
Alpha4compare_S12[,"EN"]=(alpha[4]*t.w)
Alpha4compare_S12[,"VR"]=Alpha4compare_S12[,"N"]/Alpha4compare_S12[,"EN"]
Alpha4compare_S12 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.1 FOR T-DISTRIBUTION
TVaR_S12_Alpha4 <- TVaR_results_S12[,4]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha4Tcompare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha4Tcompare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha4Tcompare_S12[,"alpha"] <- alpha[4]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S12[e.w+t]<TVaR_S12_Alpha4[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha4Tcompare_S12[,"N"]=s
Alpha4Tcompare_S12[,"%"]=s/t.w
Alpha4Tcompare_S12[,"EN"]=(alpha[4]*t.w)
Alpha4Tcompare_S12[,"VR"]=Alpha4Tcompare_S12[,"N"]/Alpha4Tcompare_S12[,"EN"]
Alpha4Tcompare_S12 # the matrix after executing the program


## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 NORMAL DISTRIBUTION
VaR_S12_Alpha5 <- VaR_results_S12[,5]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5compare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5compare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5compare_S12[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S12[e.w+i])<VaR_S12_Alpha5[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha5compare_S12[,"N"]=j
Alpha5compare_S12[,"%"]=j/t.w
Alpha5compare_S12[,"EN"]=(alpha[5]*t.w)
Alpha5compare_S12[,"VR"]=Alpha5compare_S12[,"N"]/Alpha5compare_S12[,"EN"]
Alpha5compare_S12 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.001 FOR T-DISTRIBUTION
TVaR_S12_Alpha5 <- TVaR_results_S12[,5]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha5Tcompare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha5Tcompare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha5Tcompare_S12[,"alpha"] <- alpha[5]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S12[e.w+t]<TVaR_S12_Alpha5[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha5Tcompare_S12[,"N"]=s
Alpha5Tcompare_S12[,"%"]=s/t.w
Alpha5Tcompare_S12[,"EN"]=(alpha[5]*t.w)
Alpha5Tcompare_S12[,"VR"]=Alpha5Tcompare_S12[,"N"]/Alpha5Tcompare_S12[,"EN"]
Alpha5Tcompare_S12 # the matrix after executing the program

## 
#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 NORMAL DISTRIBUTION
VaR_S12_Alpha6 <- VaR_results_S12[,6]

##SUMMARY OF VALUE AT RISK FOR ESTIMATING FAILURE RATE
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6compare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6compare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6compare_S12[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
j=0
for (i in 1:t.w){
  if((S12[e.w+i])<VaR_S12_Alpha6[i])
    j=j+1
}
j #the number of violations or failure rates
#end of loop

Alpha6compare_S12[,"N"]=j
Alpha6compare_S12[,"%"]=j/t.w
Alpha6compare_S12[,"EN"]=(alpha[6]*t.w)
Alpha6compare_S12[,"VR"]=Alpha6compare_S12[,"N"]/Alpha6compare_S12[,"EN"]
Alpha6compare_S12 #the matrix after the executing the command
#end of COUNTING NUMBER OF VIOLATIONS

#FOR COUNTING NUMBER OF VIOLATIONS ALPHA=0.005 FOR T-DISTRIBUTION
TVaR_S12_Alpha6 <- TVaR_results_S12[,6]

#SUMMARY OF VALUE AT RISK
#creating a matrix for violation ratio and failure rate COUNTING NUMBER OF VIOLATIONSs
Alpha6Tcompare_S12 <- matrix(NA,nrow=1,ncol=5) #matrix for violation ratio(VR)
colnames(Alpha6Tcompare_S12) <- c("N","alpha","%","EN","VR") #names for each column of the matrix
Alpha6Tcompare_S12[,"alpha"] <- alpha[6]

#start loop COUNTING NUMBER OF VIOLATIONS of failure rate
s=0
for (t in 1:t.w){
  if(S12[e.w+t]<TVaR_S12_Alpha6[t])
    s=s+1
}
s #the number of violations or failure rates
#end loop COUNTING NUMBER OF VIOLATIONS

Alpha6Tcompare_S12[,"N"]=s
Alpha6Tcompare_S12[,"%"]=s/t.w
Alpha6Tcompare_S12[,"EN"]=(alpha[6]*t.w)
Alpha6Tcompare_S12[,"VR"]=Alpha6Tcompare_S12[,"N"]/Alpha6Tcompare_S12[,"EN"]
Alpha6Tcompare_S12 # the matrix after executing the program



##UNDER NORMAL DISTRIBUTION
#FOR LOG-LIKELIHOOD 
LR_UC1 <- VaRTest(alpha = alpha[1], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha1, conf.level = p[1])[1:7]
LR_UC1

LR_UC2 <- VaRTest(alpha = alpha[2], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha2, conf.level = p[2])[1:7]
LR_UC2

LR_UC3 <- VaRTest(alpha = alpha[3], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha3, conf.level = p[3])[1:7]
LR_UC3

LR_UC4 <- VaRTest(alpha = alpha[4], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha4, conf.level = p[4])[1:7]
LR_UC4

LR_UC5 <- VaRTest(alpha = alpha[5], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha5, conf.level = p[5])[1:7]
LR_UC5

LR_UC6 <- VaRTest(alpha = alpha[6], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha6, conf.level = p[6])[1:7]
LR_UC6


#FOR INDEPENDENCE TEST
LR_IN1 <- VaRTest(alpha = alpha[1], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha1, conf.level = p[1])[1:7]
LR_IN1

LR_IN2 <- VaRTest(alpha = alpha[2], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha2, conf.level = p[2])[1:7]
LR_IN2

LR_IN3 <- VaRTest(alpha = alpha[3], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha3, conf.level = p[3])[1:7]
LR_IN3

LR_IN4 <- VaRTest(alpha = alpha[4], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha4, conf.level = p[4])[1:7]
LR_IN4

LR_IN5 <- VaRTest(alpha = alpha[5], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha5, conf.level = p[5])[1:7]
LR_IN5

LR_IN6 <- VaRTest(alpha = alpha[6], actual = S12[e.w+1:t.w], VaR = VaR_S12_Alpha6, conf.level = p[6])[1:7]
LR_IN6

##UNDER T-DISTRIBUTION
#FOR LOG-LIKELIHOOD 
TLR_UC1 <- VaRTest(alpha = alpha[1], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha1, conf.level = p[1])[1:7]
TLR_UC1

TLR_UC2 <- VaRTest(alpha = alpha[2], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha2, conf.level = p[2])[1:7]
TLR_UC2

TLR_UC3 <- VaRTest(alpha = alpha[3], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha3, conf.level = p[3])[1:7]
TLR_UC3

TLR_UC4 <- VaRTest(alpha = alpha[4], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha4, conf.level = p[4])[1:7]
TLR_UC4

TLR_UC5 <- VaRTest(alpha = alpha[5], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha5, conf.level = p[5])[1:7]
TLR_UC5

TLR_UC6 <- VaRTest(alpha = alpha[6], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha6, conf.level = p[6])[1:7]
TLR_UC6


#FOR INDEPENDENCE TEST
TLR_IN1 <- VaRTest(alpha = alpha[1], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha1, conf.level = p[1])[8:12]
TLR_IN1

TLR_IN2 <- VaRTest(alpha = alpha[2], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha2, conf.level = p[2])[8:12]
TLR_IN2

TLR_IN3 <- VaRTest(alpha = alpha[3], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha3, conf.level = p[3])[8:12]
TLR_IN3

TLR_IN4 <- VaRTest(alpha = alpha[4], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha4, conf.level = p[4])[8:12]
TLR_IN4

TLR_IN5 <- VaRTest(alpha = alpha[5], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha5, conf.level = p[5])[8:12]
TLR_IN5

TLR_IN6 <- VaRTest(alpha = alpha[6], actual = S12[e.w+1:t.w], VaR = TVaR_S12_Alpha6, conf.level = p[6])[8:12]
TLR_IN6
```
