*
* %LogSkewTGARCH(h,z) returns the log of the normalized (to variance h
* and zero mean) skew t density from Hansen (1994), "Autoregressive
* Conditional Density Estimation", International Economic Review, vol
* 35, no. 3, pp 705-730. For use with GARCH with the DENSITY option, the
* ETA and LAMBDA parameters are global (with specialized names to avoid
* name conflicts).
*
dec real %SkewT_eta %SkewT_lambda
*
function %LogSkewTGARCH h z
type real %LogSkewTGARCH h z
*
local real a b c zstd zn
local real eta lambda
*
compute eta=%SkewT_eta,lambda=%SkewT_lambda
*
if h<=0.or.eta<2.or.lambda>=1.or.lambda<=-1 {
   compute %LogSkewTGARCH=%na
   return
}
compute c=exp(%lngamma(.5*(eta+1))-%lngamma(.5*eta))/sqrt(%pi*(eta-2))
compute a=4*lambda*c*(eta-2)/(eta-1)
*
* Pick the sign for b that puts a/b on the same side as lambda
*
compute b=sqrt(1+3*lambda^2-a^2)
if a/b*lambda<0
   compute b=-b
*
* Standardize to unit variance
*
compute zstd=z/sqrt(h)
compute zn=(b*zstd+a)/%if(zstd<-a/b,1-lambda,1+lambda)
compute %LogSkewTGARCH=-.5*log(h)+log(b)+log(c)-.5*(eta+1)*log(1+zn^2/(eta-2))
end
