Académique Documents
Professionnel Documents
Culture Documents
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 = ""))
##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.##
##The formula says we need ith eigen value of ith principal component.This retrieves those values.
##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)
##The mathematical equation used here is the one given in PPT to calculate 1st Principal component, it is
= λi/Summation λi
cc=cor(obs[c(-1)],method = "pearson")
####forbes rigobon
vr=var(obs)
cv=cov(obs)
cvm=mean(cv)
vrm=mean(vr)
ccm=mean(cc)
### 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.
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
###There are two packages available, BayesDCCGarch and rmGARCH. For bivariate GARCH, rmGARCH
package is best suited which I have used.
data=obs
## 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.
##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)
##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.
##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)
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)
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
data=obs
## 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
##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.
##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.
bekk = rshape(fit)
##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.
###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
### 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?
#### 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.
####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?
P[k] = (1-K[k]) * Pminus[k]### what is the equation you are using ###here?
)
par(mfrow=c(2,1))### why it is required??
####The function F is used to run the Broyden method after Kalman filtering.
F <- function(x) {
par(obs)
### 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.
mean(abs(Dynamic_SC[lower.tri(Dynamic_SC)]))
avgcor(cor(obs))
adjr.lm = lm(data=obs)
adr=summary(adjr.lm)$adj.r.squared
number_of_obs=160
mean_vector=c()
vector_obs=c()
for (i in 1:number_of_obs)
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_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