' UNIVARIATE GARCH-in-MEAN PROGRAM

'change path to program path
%path = @runpath
cd %path 

' load workfile
load qeret1ev.wf1

' dependent variables of all series must be continuous
series y1 = r17

' set sample 
' first observation of s1 need to be one or two periods after
' the first observation of s0 

sample s0 2 2362
sample s1 5 2362

' initialization of parameters and starting values
' change below only to change the specification of model 

smpl s0

'get starting values from univariate GARCH-in-mean

equation eq1.arch(archm=sd,m=100,c=1e-5) y1 c ar(1)
show eq1.output

'save the conditional variances

eq1.makegarch garch1 

' declare coef vectors to use in TVGARCHM model

coef(1) lambda
 lambda(1) = eq1.c(1)

coef(2) mu
 mu(1) = eq1.c(2)
 mu(2) = eq1.c(3)

coef(1) omega
 omega(1) = (abs(eq1.c(4)))^.5

coef(1) alpha
 alpha(1) = (eq1.c(5))^.5

coef(1) beta 
 beta(1) = (eq1.c(6))^.5 

' use sample var-cov as starting value of variance-covariance matrix

series var_y1 = @var(y1-lambda(1)*garch1)
series sqres1 = (y1-mu(1)-lambda(1)*garch1)^2

' ...........................................................
' LOG LIKELIHOOD
' set up the likelihood 
' 1) open a new blank likelihood object name tvgarch
' 2) specify the log likelihood model by append
' ...........................................................

logl tvgarchm

' squared errors and cross errors

tvgarchm.append @logl logl
tvgarchm.append sqres1 = (y1-lambda(1)*@sqrt(var_y1)-mu(1)-mu(2)*y1(-1))^2

' variance and covariance series 
tvgarchm.append var_y1  =  omega(1)^2 + beta(1)^2*var_y1(-1) + alpha(1)^2*sqres1(-1)

' log-likelihood series
tvgarchm.append logl = log(@dnorm(@sqrt(sqres1/var_y1)))-log(var_y1)/2

' estimate the model
smpl s1
tvgarchm.ml(showopts, m=100, c=1e-5)

' change below to display different output
show tvgarchm.output
graph var.line var_y1
show var


