Vous êtes sur la page 1sur 10

rm(list=ls())

library(gdata)

library(mnormt)

library(quantmod)

library(fBasics)

library(rmgarch)

library(parallel)

##### For generating Average weights for Inner validation of International Indices ########

fpath_1<-"F:/observations_international/"

fpath_2<-"F:/Observation Output/"

for(i in 1:160)

obs=read.csv(paste(fpath_1,"obs_",i,".csv",sep = ""))

####first principal component

pca=prcomp(obs, scale. = T) ## why did you do scale is equal to True??

##The paper has done the PCA based on covariance matrix as well as correlation matrix(this is to add
robustness in case correlation matrix is biased too much by previous data), but the R package will do it
based only on correlation matrix.By setting the scaling=True,the variables will be set as having zero mean
and unit variance.In that case, the covariance between two scaled variables becomes the correlation
coefficient-and thus both approaches become identical.Then,our R package gives the same results as
required by the cited literature.##

pc=pca[1,i] ## what does it retrieve??

##The formula says we need ith eigen value of ith principal component.This retrieves those values.

egn=eigen(pc)## what does it retrieve??

##This will retrieve the eigen values and eigen vectors required for calculating the first Principal
component.BY default it returns the eigen values only so assigning its values to variable egn will transfer
only eigenvalues.(If eigenvectors were needed, it could be done by egn=eigen$vectors(pc).But we do not
need eigenvectors for our calculation)

pc1=egn[,1]/sum(egn) ### what is the mathematical equation here that used?

##The mathematical equation used here is the one given in PPT to calculate 1st Principal component, it is
= λi/Summation λi

write.csv(pc1,file = paste(fpath_2,"First Principal Component/","1stPC_",i,".csv",sep = ""))

cc=cor(obs[c(-1)],method = "pearson")

write.csv(cc,file = paste(fpath_2,"Correlation Matrix/","cor_",i,".csv",sep = ""))

####forbes rigobon

vr=var(obs)

cv=cov(obs)

cvm=mean(cv)

vrm=mean(vr)

ccm=mean(cc)

### I dont think the equation below is correct

### You wrote this: we compute the variances over a 4 week rolling window, ###and then find the
difference between variance of given time period and ###minimum variance. This determines the d??.

##There has been an error in computing this measure. The necessary corrections are given below.

### I dont think the equation below finds this??

fbr=ccm/(sqrt(1+vrm*(1-(ccm*ccm))))

####If you see the forbes rigobon equation, it has pt on the numerator, which is pearson's correlation.This
is represented by ccm variable.Iin the denominator, there is another factor delta.this delta is the
difference between the variance of that period and minimum variance.the vrm factor finds the variance
of that period.To find the minimum variance, we will run a loop below
for (i in 1:160)

obs=read.csv(paste(fpath_1,"obs_",i,".csv",sep = ""))

flag=var(obs)

j=1+1

obs1=read.csv(paste(fpath_1,"obs_",j,".csv",sep = ""))

minvar=var(obs1)

if(flag<minvar)

minvar=flag

##This loop will compute the variances of all the measures and keep comparing it with minvar, which we
have set to as the variance of immediate next data.If the minimum variance in data set is lesser than this
value,it will get replaced.Else,it will stay with its past value.After completion of the loop,minvar will hold
the minimum variance value.This difference of variance and minvar is equal to delta.Now we will modify
the forbes rigobon equation with correct variables

delta=cv-minvar

fbr=ccm/(sqrt(1+delta*(1-(ccm*ccm))))

####DCC GARCH

### which package you are using to find this??

###There are two packages available, BayesDCCGarch and rmGARCH. For bivariate GARCH, rmGARCH
package is best suited which I have used.

data=obs

xspec = ugarchspec(mean.model = list(armaOrder = c(1, 1)), variance.model = list(garchOrder = c(1,1),


model = 'sGARCH'), distribution.model = 'norm')

uspec = multispec(replicate(4, xspec)) ## what does this function do ### explain??

## The ugarchspec function above will create a bivariate GARCH specification object-this object is the
representation of our data that needs to be fit in the GARCH model. The multispec function will now
replicate this to all columns of our data. What this step will do is to ensure uniform object creation from
all data points. The output of this step is then passed on for fitting of GARCH model.

spec1 = dccspec(uspec = uspec, dccOrder = c(1, 1), distribution = 'mvnorm')

## what does this function do ### explain??

##Once the output of multispec function has been received,it will now be transformed into dccgarch
model.This spec1 will convert this as per dccgarch model specifications.

spec1a = dccspec(uspec = uspec, dccOrder = c(1, 1), model='aDCC', distribution = 'mvnorm')

## what does this function do ### explain??

##After the dccgarch model has been created, the cited literature (of Billio) was concerned about one
factor governing all results in dccgarch model.So they have made it unsymmetric (notice the model has
changed from dcc to “aDCC”. This will nullify the effect of any single factor affecting entire model.

cl = makePSOCKcluster(4)

## what does this function do ### explain??

##This creates several copies of R running in parallel and communicating with each other. For bivariate
garch modelling, we will need such instances. General practice is to double the number of cluster sockets
for each variable for faster computation, and thus for bivariate model, 4 such clusters are created. The
rmgarch package will automatically assign processing load over these clusters and obtain results via inter
communication.

multf = multifit(uspec, Dat, cluster = cl)

## what does this function do ### explain??

##Now, the dccgarch model is being fit using the data we have supplied from the uspec function.Notice
that the clusters are now being utilized for computation.

fit1 = dccfit(spec1, data = Dat, fit.control = list(eval.se = TRUE), fit = multf, cluster = cl)

## what does this function do ### explain??

This function will create an object that has now been fit accordingly into a GARCH model. The fit.control
will control the arguments passed to the fitting function. The eval.se is set to be true because the cited
literature has computed the standard errors, so we have to incorporate that in our results as well.

fit_adcc = dccfit(spec1, data = Dat, fit.control = list(eval.se = TRUE), fit = multf, cluster = cl)

## what does this function do ### explain??

dkk=fit_adcc

##Once the results of the GARCH model are obtained above, the cited literature again makes it
unsymmetrical to make sure the output is not biased by one single factor. Notice that this was done while
feeding the input as well. So, by running this twice we have added robustness in our model-both the input
data as well as output model are free of any bias as far as possible through this package. The results are
then passed on to variable named dkk to be written on csv file.

####BEKK GARCH

### which package you are using to find this??

##The same package used for DCCGARCH, i.e rmGARCH package

data=obs

xspec1 = ugarchspec(mean.model = list(armaOrder = c(1, 1)), variance.model = list(garchOrder = c(1,1),


model = 'eGARCH'), distribution.model = 'norm')

## what does this function do ### explain??

uspec1 = multispec(replicate(10, xspec))

## what does this function do ### explain??

specs1 = dccspec(uspec = uspec, dccOrder = c(1, 1), distribution = 'mvnorm')

## what does this function do ### explain??

specs1a = dccspec(uspec = uspec, dccOrder = c(1, 1), model='aDCC', distribution = 'mvnorm')

## what does this function do ### explain??

## Combined answer for above questions : The ugarchspec function above will create a bivariate GARCH
specification object-this object is the representation of our data that needs to be fit in the GARCH model.
The multispec function will now replicate this to all columns of our data. What this step will do is to ensure
uniform object creation from all data points. The output of this step is then passed on for fitting of GARCH
model. ##Once the output of multispec function has been received,it will now be transformed into
dccgarch model.This spec1 will convert this as per dccgarch model specifications.

NOTE: Intial steps of both BEKK and DCC GARCH modelling are similar, so I have used the same answers
as explained in DCC GARCH

specs2 = dccspec(uspec = uspec, dccOrder = c(1, 1), distribution = 'mvlaplace')

## what does this function do ### explain??

##From here the difference in DCC GARCH and BEKK GARCH begins. BEKK GARCH is used as a dccgarch
model but with a laplace transform matrix. Another literature which I downloaded myself for additional
information (Do we really need both BEKK and DCC GARCH-A tale of two multivariate GARCH models –
Michael McAleer, Nov 2010, Koyoto Institute of Economic Reseach, paper no 738) discusses these
differences in great detail-although the main paper of Billio simply uses BEKK GARCH without going into
much details.

specs2a = dccspec(uspec = uspec, dccOrder = c(1, 1), model='aDCC', distribution = 'mvlaplace')


## what does this function do ### explain??

##Once the results of the BEKKGARCH model are obtained above, the cited literature again makes it
unsymmetrical to make sure the output is not biased by one single factor. This is again same as the
practices followed in DCC GARCH modelling. Notice that this was done while feeding the input as well. So,
by running this twice we have added robustness in our model-both the input data as well as output model
are free of any bias as far as possible through this package.

fit = dccfit(spec3a, data = Dat, fit.control = list(eval.se=FALSE), fit = multf)

bekk = rshape(fit)

## what does this function do ### explain??

##We now obtained the model fitted as per BEKK GARCH specifications. Since in BEKK GARCH the cited
literature does not consider the standard errors, eval.se is set to as FALSE. Results are then passed on to
variable BEKK for writing into csv file. Notice the rshape function-this is used to fit the model appropriately
if dimensionality becomes too large. If you read the paper (cited above in black italic letters)-it talks about
a “curse of dimensionality” that BEKK GARCH has. This function somewhat helps in its correction.

####CONDITIONAL BETA

et=bekk ####The BEKK GARCH model is already prepared earlier,we can use its value

### In the equation below, how did you calculate ri and rm as given in your ### ppt

### ri and rm represent the local equity and the global market as per the cited paper. However, in our
data this distinction is not clear so I have used the same data for both ri and rm. This may cause errors in
result, so if you can make a differentiation of local and global market as per data given then I will remodify
this suitably.

### What does vrm represent?

###vrm represents the variance of the data.

### what does et represent?

###et represents the factor Et[ri,t] mentioned in the ppt.It was obtained from BEKK GARCH modelling and
since we have computed that we can use its value directly here.

beta=(cvm/vrm)*et

####SC ASI

xhat = rep(0,count) ## why is this step required?

P = rep(0,count) ## why is this step required?


xhatminus=rep(0,count) ## why is this step required?

Pminus=rep(0,count) ## why is this step required?

K=rep(0,count) ## why is this step required?

### Combined answer: These steps are the standard techniques for Kalman filtering. It is an algorithm
that uses a series of measurements observed over time, and produces estimates of unknown variables
that tend to be more accurate than those based on a single measurement alone, by estimating a joint
probability distribution over the variables for each timeframe. The various variables here are using the
replicate function (rep) to assign equal vectors of length zero, thus to create transitional matrix T (as given
in ppt). These vector values will be updated as the filter is applied.

xhat[1] <- 0

P[1] <- 1

for (k in 1:160){

#time update

xhatminus[k] <- xhat[k-1] ## what is the equation you are using here?

#### var(αt) = H,

Pminus[k] <- P[k-1] + Q ## what is the equation you are using here?

####t = Tα t−1 + ηt,

#measurement update ## why is measurement update required??

#### the cited paper has used a linear state-space model in which they explicitly differentiate between
measured variables and their population counterparts. For this, after every cycle of equations above being
calculated, we need to update the value of transitional matrix T with these.

K[k] = Pminus[k] / (Pminus[k] + R)

## what is the equation you are using ###here?

####T=t/t+ ηt (They have used but not mentioned formula in cited literature, this is used to update values
of kalman filter on our data)

xhat[k] = xhatminus[k] + K[k] * (z[k] - xhatminus[k]) ####what is the equation you are using ###here?

#### H= yt +t* (α t−1- yt)

P[k] = (1-K[k]) * Pminus[k]### what is the equation you are using ###here?

#### yt = (1- α t−1)*t

)
par(mfrow=c(2,1))### why it is required??

####To create the values in transition matrix using column wise.

###what does the function F calculate

####The function F is used to run the Broyden method after Kalman filtering.

F <- function(x) {

par(obs)

scasi <- broyden(F,x0)

###what does the function broyden calculate

### This model uses Fisher transform for estimating the Sample correlation. After that, the population
correlation is obtained by computing the inverse of the Fisher transform using Broyden’s Method.

write.csv(fbr,file = paste(fpath_2,"Forbes Rigobon/","ForbesRigobon_",i,".csv",sep = ""))

write.csv(beta,file = paste(fpath_2,"Conditional Beta/","conditionalBeta_",i,".csv",sep = ""))

write.csv(scasi,file = paste(fpath_2,"SC ASI/","SCASI_",i,".csv",sep = ""))

write.csv(dkk,file = paste(fpath_2,"DKK GARCH/","DKKGARCH_",i,".csv",sep = ""))

write.csv(bkk,file = paste(fpath_2,"BEKK GARCH/","BEKKGARCH_",i,".csv",sep = ""))

cormat <- cor(obs[c(-1)],method = "pearson")

corlowtri <- cormat[lower.tri(cormat)]

avgcor <- function(Dynamic_SC)

mean(abs(Dynamic_SC[lower.tri(Dynamic_SC)]))

avgcor(cor(obs))

write.csv(avgcor(cor(obs)),file = paste(fpath_2,"Dynamic SC/","DynamicSC_",i,".csv",sep = ""))

adjr.lm = lm(data=obs)
adr=summary(adjr.lm)$adj.r.squared

write.csv(adr,file = paste(fpath_2,"Adjusted R Squared/","AdjustedRSquared_",i,".csv",sep = ""))

##########this code is not required##############

list_of_index<- read.csv( paste(fpath_1,"obs_2.csv",sep = ""))

number_of_obs=160

mean_vector=c()

vector_obs=c()

for (i in 1:number_of_obs)

vector_obs[i] <- paste("obs_",i)

for(i in 1:number_of_obs)

mean_matrix=matrix(0,number_of_obs,4)

wt=read.csv(paste(fpath_1,"obs_",i,".csv",sep = ""))

wt=wt[,-1]

mean_vector=append(mean_vector,mean(upperTriangle(wt, diag = FALSE)))

mean_matrix[,1]=vector_obs

mean_matrix[,2]=mean_vector

mean_matrix[1,3]=min(mean_vector)

mean_matrix[1,4]=max(mean_vector)

header<-c("Observations","Means","Minimum","Maximum")

colnames(mean_matrix)<-c(header)

write.csv(mean_matrix,paste(fpath_2,"Means/","obs_mean.csv",sep=""))

### why did you calculate the means, minimum and maximum

### what is the need of this step??


###This whole code in green was accidentally copied. This is actually a part of the practice codes you sent
me and has been inserted by mistake. Kindly ignore this, it has no effect on results of financial measures.

Vous aimerez peut-être aussi