*
* %LogSkewGEDGARCH(h,x) returns the log density at x of the univariate
* skewed GED with mean zero and variance h. The skewness control
* (lambda) and kurtosis control (k) are global variables so this can be
* used with GARCH with the DENSITY option. These have specialized names
* to avoid name conflicts.
*
* Lambda takes values between -1 and 1, and k takes non-negative values.
* k=2,lambda=0 is a normal. Smaller values of k have fatter tails.
*
* From Panayiotis(2015), "Skewed Generalized Error Distribution of Financial
* Assets and Option Pricing," Multinational Finance Journal, vol. 19(4), pages
* 223-266.
*
* Revision Schedule:
*  07/2022 Written by Tom Doan, Estima
*
declare real %SkewGED_lambda %SkewGED_k
*
function %LogSkewGEDGARCH h x
*
type real h x
local real lambda k a1 a2 phi
compute lambda=%SkewGED_lambda,k=%SkewGED_k
*
* Return NA's on out of range parameters
*
if h<=0.or.lambda<-1.0.or.lambda>+1.0.or.k<=0 {
   compute %LogSkewGEDGARCH=%na
   return
}
*
* Back out the scaling phi from the input variance
*
compute a1=2*lambda*k^(1/k)*%gamma(2/k)/%gamma(1/k)
compute a2=(1+3*lambda^2)*k^(2/k)*%gamma(3/k)/%gamma(1/k)
compute phi=sqrt(h/(a2-a1^2))
*
compute %LogSkewGEDGARCH=(1-1/k)*log(k)-%lngamma(1/k)-log(2*phi)-$
  (1/k)*(abs(x)^k)/(((1+%sign(x)*lambda)*phi)^k)
end
