/*************** Parzen kernel *********************
Author: Michael Creel <mcreel@volcano.uab.es>
Date:   Thu, 29 Jan 1998


A parzen kernel routine for convariance matrix estimation.
Provided without performance guarantees for public,
non-commercial use.
*******************************************************/


/*
PARZEN
usage:      cov=parzen(x)
purpose:    calculates a heteroscedasticity/autocorrelation consistent
            variance covariance matrix using Parzen kernel
inputs:     x: the data, an nxk matrix, n is # of obs., k # of variables
outputs:    cov: the kxk covariance matrix. It is positive definite.
references: many available, one is A.R. Gallant, Nonlinear Statistical
Models,
            1987, pg. 533.

Written by Michael Creel, 27 Feb. 98.  mcreel@volcano.uab.es

The standard disclaimer applies:
     * the code is written and submitted for public, non-commercial use.
     * there are no performance guarantees.

*/

proc parzen(x);
local n,k,window_width,tau,cov,x,weight,s_n,s_n_lag;
n=rows(x);
k=cols(x);
window_width=round(n^.25);
tau=0;
cov=zeros(k,k);
do until tau>window_width;
    z=tau/window_width;
    weight=(1-6*z^2+6*z^3).*(z < 0.5)+(2*(1-z)^3).*(z >= 0.5);
    s_n=packr(x~lagn(x,tau));
    s_n_lag=s_n[.,k+1:cols(s_n)];
    s_n=s_n[.,1:k];
    s_n=(1/n)*s_n's_n_lag;
    @ add in transpose if not 0th autocovariance @
    cov=cov+weight*s_n+(1-tau==0)*weight*s_n';
    tau=tau+1;
    endo;
retp(cov);
endp;

