' example program for EViews LogL object
' Estimate EGARCH with generalized error distribution (Nelson's specification) 
'
' For discussion, see p. 668 of Hamilton, James D. (1994) Time Series Analysis, 
' Princeton University Press.


'change path to data path
%path = @runpath
cd %path

' load workfile
load price_volatility

series rklci = 100*dlog(klci)
series rhkx = 100*dlog(hkx)
series ridx = 100*dlog(idx) 
series rpse = 100*dlog(pse)  
series rset = 100*dlog(set)  
series rsse = 100*dlog(sse)  
series rsst = 100*dlog(sst)  
series rszse = 100*dlog(szse) 

' set sample to 1/1/00-31/12/10 (31/12/10 is obs 2870)
sample s0 2 3
sample s1 4 2870
smpl s1


' get starting values from Gaussian EGARCH
equation eq1
eq1.arch(2,2,asy=2,egarch) rklci c rhkx(-1) ridx(-1) rklci(-1) rpse(-1) rset(-1) rsse(-1) rsst(-1) rszse(-1)
show eq1.output


' declare and initialize parameters

' coefs on lagged variance
coef(2) delta
delta(1) = eq1.c(15)
delta(2) = eq1.c(16)

' coefs on lagged resids
coef(2) alpha 
alpha(1) = eq1.c(11)
alpha(2) = eq1.c(12)

' coef on asym term
coef(1) chi
chi(1) = eq1.c(12)/eq1.c(11)

' coefs on deterministic terms
coef(2) rho
rho(1) = 2*log(eq1.@se)
rho(2) = 0 

' 0<nu<2 is thick tails; nu>2 is thin tails
coef(1) nu = 2 

!pi = @acos(-1)

' set presample values of expressions in logl
smpl s0
series zeta = log(1+rho(2)*ndays)
series h = exp(rho(1))
series z = 0
series temp1 = @abs(z(-1)) + chi(1)*z(-1)


' set up EGARCH likelihood
' note that E|v{t-1}| is absorbed in overall constant rho(1) 
logl ll1
ll1.append @logl logl
'zeta = unconditional mean of log(ht)
ll1.append zeta = log(1+rho(2)*ndays)
'log(lambda) p.668 last equation
ll1.append loggam1 = @gammalog(1/nu(1))
ll1.append loglam = -log(2)/nu(1) + 0.5*( loggam1 - @gammalog(3/nu(1)) )
ll1.append temp1 = @abs(z(-1)) + chi(1)*z(-1)
ll1.append log(h) = rho(1) + zeta + delta(1)*(log(h(-1))-zeta(-1)) + delta(2)*(log(h(-2))-zeta(-2)) + alpha(1)*temp1 + alpha(2)*temp1(-1)
ll1.append res = rklci - c(1)*h - c(2) - c(3)*rhkx(-1) - c(4)*ridx(-1) - c(5)*rklci(-1) - c(6)*rpse(-1) - c(7)*rset(-1) - c(8)*rsse(-1) - c(9)*rsst(-1) - c(10)*rszse(-1)
ll1.append z = res/@sqrt(h)
ll1.append logl = log(nu(1)) - loglam - (1+1/nu(1))*log(2) - loggam1 - 0.5*@abs(z/exp(loglam))^nu(1) - 0.5*log(h)


' estimate and display output
smpl s1
ll1.ml(showopts, m=1000, c=1e-5)
show ll1.output

