You are on page 1of 4

MBA19141_Assignment-3.

R
ARHUM

2021-01-12
rm(list=ls()) #clear the previous memory
setwd("F:/TERM 5/FRMM/Assignments") #set WD

data=read.table("Data.csv", header=T, sep=",")


data1=cbind(data[,1],data[,2])

# CCC GARCH for two assets (MRF & ITC)


library(ccgarch)
#Initial Values of the model coefficients
const=c(0.4,0.4)
arch_p=diag(2)
garch_p=diag(2)
corr_p=diag(2)
#Fit the model
garch_ccc <- eccc.estimation(const,arch_p,garch_p,corr_p,data1,
model="diagonal")
corr_1=garch_ccc$para.mat$R[1,2]
corr_1

## [1] 0.2172895

write.csv(garch_ccc$h,"CCC_GARCH_Var.csv")

#DCC GARCH for two assets (MRF & ITC)


#Initial Values of coeff
const=c(0.2,0.2) #Omega vector
arch_p=diag(2) # Alpha matrix
garch_p=diag(2) # Beta Matrix
corr_p=c(0.2,0.2)
garch_dcc <- dcc.estimation(const,arch_p,garch_p,corr_p,data,
model="diagonal")

## ****************************************************************
## * Estimation has been completed. *
## * The outputs are saved in a list with components: *
## * out : the estimates and their standard errors *
## * loglik : the value of the log-likelihood at the estimates *
## * h : a matrix of estimated conditional variances *
## * DCC : a matrix of DCC estimates *
## * std.resid : a matrix of the standardised residuals *
## * first : the results of the first stage estimation *
## * second : the results of the second stage estimation *
## ****************************************************************

corr_dcc=garch_dcc$DCC
var_dcc=garch_dcc$h
out=cbind(var_dcc,corr_dcc)
write.table(out,"Corr_out.csv",sep=",")

# CCC GARCH (10 Assets)


rm(list=ls())
data=read.table("Data_2.csv", header=T, sep=",")
data1=cbind(data[,1],data[,2],data[,3],data[,4],data[,5],data[,6],data[,7],da
ta[,8],data[,9],data[,10])
library(ccgarch)
const=c(0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1)
arch_p=diag(10)
garch_p=diag(10)
corr_p=diag(10)
garch_ccc <- eccc.estimation(const,arch_p,garch_p,corr_p,data1,
model="diagonal")
corr_1=garch_ccc$para.mat$R
corr_1

## [,1] [,2] [,3] [,4] [,5] [,6]


[,7]
## [1,] 1.0000000 0.2787031 0.3312094 0.2190469 0.2154500 0.3767476
0.2547717
## [2,] 0.2787031 1.0000000 0.2697694 0.1285787 0.1497216 0.3513145
0.2030133
## [3,] 0.3312094 0.2697694 1.0000000 0.2356960 0.1499763 0.2884464
0.3505973
## [4,] 0.2190469 0.1285787 0.2356960 1.0000000 0.2070800 0.1549705
0.1708267
## [5,] 0.2154500 0.1497216 0.1499763 0.2070800 1.0000000 0.1679034
0.1157942
## [6,] 0.3767476 0.3513145 0.2884464 0.1549705 0.1679034 1.0000000
0.1947588
## [7,] 0.2547717 0.2030133 0.3505973 0.1708267 0.1157942 0.1947588
1.0000000
## [8,] 0.4153580 0.2643596 0.3957444 0.2606296 0.1695326 0.3074126
0.3043417
## [9,] 0.2031492 0.1405086 0.1685097 0.1368272 0.1756435 0.1421285
0.1479371
## [10,] 0.2787056 0.1632716 0.2284578 0.1532455 0.1296109 0.2308730
0.1906480
## [,8] [,9] [,10]
## [1,] 0.4153580 0.2031492 0.2787056
## [2,] 0.2643596 0.1405086 0.1632716
## [3,] 0.3957444 0.1685097 0.2284578
## [4,] 0.2606296 0.1368272 0.1532455
## [5,] 0.1695326 0.1756435 0.1296109
## [6,] 0.3074126 0.1421285 0.2308730
## [7,] 0.3043417 0.1479371 0.1906480
## [8,] 1.0000000 0.1967012 0.2700480
## [9,] 0.1967012 1.0000000 0.1289611
## [10,] 0.2700480 0.1289611 1.0000000

write.csv(garch_ccc$h,"CCC_GARCH_Var_10.csv")
write.csv(corr_1,"Corr_Matrix_10_CCC.csv")

#DCC GARCH (10 Assets)


const=c(0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1)
arch_p=diag(10)
garch_p=diag(10)
corr_p=c(0.2,0.2)
garch_dcc <- dcc.estimation(const,arch_p,garch_p,corr_p,data,
model="diagonal")

## ****************************************************************
## * Estimation has been completed. *
## * The outputs are saved in a list with components: *
## * out : the estimates and their standard errors *
## * loglik : the value of the log-likelihood at the estimates *
## * h : a matrix of estimated conditional variances *
## * DCC : a matrix of DCC estimates *
## * std.resid : a matrix of the standardised residuals *
## * first : the results of the first stage estimation *
## * second : the results of the second stage estimation *
## ****************************************************************

corr_dcc=garch_dcc$DCC
var_dcc=garch_dcc$h
out=cbind(var_dcc,corr_dcc)
write.table(out,"Corr_Dcc_out.csv",sep=",")

#Monte-Carlo 10 Assets
#This code is to generate correlated Random Numbers
correl=matrix(c(1, 0.291299026793378, 0.371342320625672,
0.251947662913924, 0.225018893793893, 0.401811186637609,
0.270793686729448, 0.442170247126175, 0.250151556771484,
0.292439442357404,
0.291299026793378, 1, 0.272397648218998,
0.141975702500437, 0.15023634850428, 0.371896133942635,
0.207554308432132, 0.251972412983337, 0.15565792650742,
0.154892377023923,
0.371342320625672, 0.272397648218998, 1, 0.25689759648536,
0.149151506681028, 0.324000733781084, 0.376482313288284,
0.430089193388842, 0.201933596472416, 0.245897926544068,
0.251947662913924, 0.141975702500437, 0.25689759648536,
1, 0.222313433202035, 0.194980964851881, 0.17855860694452,
0.264999207626123, 0.180017153809134, 0.165863293859964,
0.225018893793893, 0.15023634850428, 0.149151506681028,
0.222313433202035, 1, 0.17682063298195, 0.10989287155797,
0.165860116989631, 0.18876679003336, 0.137310312939035,
0.401811186637609, 0.371896133942635, 0.324000733781084,
0.194980964851881, 0.17682063298195, 1, 0.216919923880998,
0.308499724267648, 0.188892454493974, 0.247922978615251,
0.270793686729448, 0.207554308432132, 0.376482313288284,
0.17855860694452, 0.10989287155797, 0.216919923880998, 1,
0.322184722468269, 0.155707225617494, 0.195147784261079,
0.442170247126175, 0.251972412983337, 0.430089193388842,
0.264999207626123, 0.165860116989631, 0.308499724267648,
0.322184722468269, 1, 0.226649656954128, 0.292069736533152,
0.250151556771484, 0.15565792650742, 0.201933596472416,
0.180017153809134, 0.18876679003336, 0.188892454493974,
0.155707225617494, 0.226649656954128, 1, 0.147352084480652,
0.292439442357404, 0.154892377023923, 0.245897926544068,
0.165863293859964, 0.137310312939035, 0.247922978615251,
0.195147784261079, 0.292069736533152, 0.147352084480652, 1),10,10)
#Using Cholesky Decomposition
choles=chol(correl)
#Generating three series of Independent Random Nos from Standard Normal
Distribution
z1=rnorm(1000,0,1)
z2=rnorm(1000,0,1)
z3=rnorm(1000,0,1)
z4=rnorm(1000,0,1)
z5=rnorm(1000,0,1)
z6=rnorm(1000,0,1)
z7=rnorm(1000,0,1)
z8=rnorm(1000,0,1)
z9=rnorm(1000,0,1)
z10=rnorm(1000,0,1)
z=cbind(z1,z2,z3,z4,z5,z6,z7,z8,z9,z10)
#Matrix multiplication of Independent Random Number and Cholesky
x=z%*%choles
write.csv(x,"correl_Random_Number_10Assets.csv")

You might also like