*
* %LogSkewTDensity(z,eta,lambda) returns the log of the
* normalized (to unit variance and zero mean) skew t density from
* Hansen (1994), "Autoregressive Conditional Density Estimation",
* International Economic Review, vol 35, no. 3, pp 705-730
*
function %LogSkewTDensity z eta lambda
type real %LogSkewTDensity z eta lambda
*
local real a b c zn
*
if eta<2.or.lambda>=1.or.lambda<=-1 {
   compute %LogSkewTDensity=%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
compute zn=(b*z+a)/%if(z<-a/b,1-lambda,1+lambda)
compute %LogSkewTDensity=log(b)+log(c)-.5*(eta+1)*log(1+zn^2/(eta-2))
end

