
@ ============================================================== @

proc full(th);
  /*  This proc performs filter, smoother, and evaluates the likelihood
     function based on the full likelihood function

   Input:
    th = (2k+3 x 1) vector of population parameters
   Output:
    if ks = 0, then proc returns -f0 = negative of log likelihood function
    if ks > 0, then proc returns a (ks x 2) vector whose first column is smoothed
       estimates of conditional mean at ks different values for x
       and whose second column is variance of first column; both
       conditional on parameter values used 
   Global variables:
    n = number of observations
    k = number of nonlinear explanatory variables (excluding constant term)
    klin = total number of explanatory variables (including linear and constant term)
    y = (n x 1) vector of dependent variables
    x = (n x k) matrix of nonlinear explanatory variables
    xwhole = (n x klin) matrix of all explanatory variables including constant
    ks = number of points at which smoothed inference is sought
         (ks = 0 means no smoothed inference is found, only likelihood
          function is evaluated)
    xs = (ks x k) matrix whose rows correspond to values of x at which
         smoothed inference is to be evaluated
    xswhole = (ks x klin) matrix whose rows correspond to complete values of explanatory
         variables for which smoothed inference is to be evaluated
    gamx = (k x 1) vector which gamma weights are proportional to

    Local variables:
    alphahat = (1+k x 1) vector of constant and linear drift terms
    gam = (k x 1) vector of weights associated with each explanatory
            variable 
    sigeps = population parameter for variance of y minus true E(y|x)
    P0 = (n x n) matrix whose row i col j value is H(h_ij)
    q = (ks x n) matrix whose row i gives prior covariance between the
        mean evaluated at the value of x represented by the ith row of xs
        and the values of x represented by observations 1 through n
    xq = (n x k) matrix which is the hadamard product of x with gamma
    epshat = estimated residuals
     */
local gam,sigeps,p0,f0,q,zeta,xq,
      f1, f2,xjunk,alphahat,epshat;

@ ======================================================@




@ read in parameters @

gam = th[1:k,1];
gam = gam .* gamx;
zeta  = th[k+1,1]^2; @ zeta is the squared ratio of lambda to sigma @
alphahat = th[k+2:rows(th)-1,1];
sigeps = th[rows(th),1]^2;

@ =====================================================@
@ set initial values @

xq = dist2(x,gam);
p0 = covary(k,xq);

@  =======================================================@
@ calculate log likelihood @

if zeta < 1;
     f1 = invpd(zeta*p0+eye(n));
     if ndpchk(16);
         goto fixit;
     endif;
     f2 = ln(detl);
     if ndpchk(16);
         goto fixit;
     endif;
else;
     f1 = (1/zeta)*invpd(p0+(eye(n)/zeta));
    if ndpchk(16);
         goto fixit;
     endif;
    if detl < 1.e-300;
         goto fixit;
   endif;
      f2 = n*ln(zeta)+ln(detl);
    if ndpchk(16);
         goto fixit;
          ndpclex;
     endif;
endif;  


if kc > 1;
   "constant term and linear coefficients";;alphahat';
   "scale coefficients for explanatory variables (g)";;gam';
   "variance of residual";;sigeps;
   "squared ratio of lambda to sigma";;zeta;
endif;

epshat = y - xwhole*alphahat;


f0 = -(1/2)*f2 - (n/2)*ln(2*pi) - (n/2)*ln(sigeps) - (1/(2*sigeps))*(epshat'*f1*epshat);

if kc > 1;
  "log likelihood:";;f0;
endif;

@ =========================================================@
@ this section finds smoothed inferences, if desired @

if ks > 0;
      xjunk = dist2(xs|x,gam);
      xjunk = zeta*covary(k,xjunk);
      q = xjunk[1:ks,ks+1:n+ks];
   f0 = xswhole*alphahat
        + q*f1*(y - xwhole*alphahat);
   p0 = xjunk[1:ks,1:ks] - q*f1*q';
    p0 = sigeps*diag(p0);
    f0 = -(f0 ~ p0);
endif;

goto getout;
    fixit:
    @ this code is executed to correct underflows encountered @
    "underflow in evaluating likelihood function or smoothed inference";
    "theta and psi vectors are";
    th';
    if ks == 0;
           f0 = ll0;
    else;
         f0 = xswhole*bhat;
         p0 = 1.e-6 * ones(ks,1);
         f0 = -(f0 ~ p0);
    endif;
getout:
retp(-f0);
endp;

