*
* Multivariate GARCH with two-step DCC estimator
*
all 2246
open data e1_data126v4_anal_data_daily.rat
data(format = rats) / rut25all lsut25all rub25all lsub25all $
				ewretd d20073_20082 d20083_20092 d20093_20094 d10yt

compute n=2
dec vect[series] ret(n) size(n)
compute i=0
dofor [string] port = 'ut25all' 'ub25all'
	compute return = 'r'+port , mv = 'ls'+port , i = i+1
	set ret(i) = %s(return)
	set size(i) = %s(mv)
end dofor

table

compute betas = 10
*
dec vect[series] eps(n)
dec vect fullbeta(betas*n+2)
*
* Do univariate GARCH models. Save the standardized residuals
* into eps(i). Copy the coefficients into the proper slots in
* the full beta matrix.
*

do i=1,n
   garch(p=1,q=1,resids=r,hseries=h,regressors,xregressors) / ret(i)
   # constant ewretd d10yt
   # d20073_20082 d20083_20092 d20093_20094 size(i)
   set eps(i) = r/sqrt(h)
   compute params = %rows(%beta)
   do j=1,params
      compute fullbeta(n*(j-1)+i)=%beta(j)
   end do j
end do i
display %beta
display fullbeta
*
* Compute the covariance matrix of the standardized residuals
*
vcv(matrix=rr)
# eps
*
* Create the series[symm] uu (outer product of residuals). Make
* it the unconditional value prior to the sample.
*
dec series[symm] uu q
gset uu %regstart() %regend() = %outerxx(%xt(eps,t))
gset uu 1 %regstart()-1 = rr
gset q  = rr
*
* Log likelihood for the DCC phase, taking the residuals as given
*
nonlin a b
dec frml[symm] qf
frml qf   = (qx=(1-a-b)*rr+a*uu{1}+b*q{1})
frml logl = q=qf,%logdensity(%cvtocorr(q),%xt(eps,t))
compute b=.80,a=.10
maximize logl 2 *
*
* Compute the estimates into the final two slots in fullbeta
*
compute fullbeta(betas*n+1)=%beta(1),fullbeta(betas*n+2)=%beta(2)
*
* Do one iteration of the full model with METHOD=BHHH to get
* the grand covariance matrix.
*

equation eq1 ret(1)
# constant ewretd d10yt
equation eq2 ret(2)
# constant ewretd d10yt

group twofactor eq1 eq2

garch(p=1, q=1, mv=dcc , model=twofactor , method=bfgs , initial=fullbeta , iters=1 , $
	regressors , xregressors) / ret
# d20073_20082 d20083_20092 d20093_20094 size

display fullbeta
display %rows(fullbeta)

