*  This is a stripped version of KERNEL.SRC (graphics and blanks were deleted)
*
*  KERNEL( options )  series  start  end  *xout *yout
*
*  KERNEL computes and graphs a nonparametric estimate of the
*  unconditional distribution of a single series.
*
* Parameters
*    series     Series to analyze.
*    start end  Range to use.  If none is given, it defaults to
*               the defined range of series
*    xout yout  Series to which the gridpoints and estimted distribution
*               may be saved, if desired.
*
* Options
*    KERNEL=[OPTIMAL]/GAUSSIAN
*       OPTIMAL uses the Epanechnikov kernel (the most efficient),
*       GAUSSIAN uses the standard normal PDF as the kernel, which
*       is 95.12% as efficient.
*
*    WINDOW=width of the window for the nonparametric estimation of
*       the distribution. As the window size increases, there is more
*       smoothing. Too large a width may cause too much detail to be
*       lost, e.g., missing a bimodal distribution. The window width
*       defaults to the optimal window for a normal distribution with
*       known variance:
*         (4/3)(1/5) x sigma x nobs**(-1/5)
*
*    NGRAPH/[NONGRAPH]
*       NGRAPH graphs the distribution centered with variance one, to
*       compare its shape to a N(O,1) distribution (which is also
*       plotted)
*
*    GRIDSIZE=the number of points to use [128].  Set it to 0 if you
*       want to use the ACTUAL series instead of an equispaced
*       grid of points in the domain of the series.
*
*    STYLE = [DOTS]/SYMBOL/LINES
*
*  Revision schedule:
*    ??/?? Written by Norman Morin
*    04/94 Revised by Tom Doan to speed up calculations
*
*
*  SOURCE: Silverman, B.W.(1986): "Density Estimation for Statistics
*     and Data Analysis:  Monographs on Statistics and Applied
*     Probability 26"  Chapman & Hall, London.
*
*
*****************************************************************************

PROCEDURE  KERNEL series start end xout yout

TYPE SERIES series  *xout *yout
TYPE INTEGER start end

OPTION CHOICE	kernel    2    gaussian optimal
OPTION REAL     window
OPTION INTEGER	gridsize
OPTION SWITCH   ngraph    0
OPTION CHOICE   style     3    dots symbols lines

LOCAL INTEGER   i j nval nobs start end startl endl
LOCAL REAL      width grid temp hival loval
LOCAL SERIES    nomiss series1 series2 nseries fhat fhat2 lhs n01
LOCAL STRING    header
LOCAL VECTOR    val

*
*  These are global variables. Change DECLARE to LOCAL if you do not
*  want them to be global
*

DECLARE REAL jbstat jbsignif

*
* Strip the missing values from series and shift it to entries 1 to nobs.
*

INQUIRE(SERIES=series) startl>>start endl>>end
STATISTICS(noprint) series startl endl
SET nomiss startl endl = %VALID(series)
SAMPLE(SMPL=nomiss) series startl endl nseries 1
COMPUTE nobs = %nobs

IF (ngraph)
   DIFF(STANDARDIZE) nseries 1 nobs

IF .not.%DEFINED(gridsize)
   COMPUTE nval = 128
ELSE IF (gridsize.le.0)
   COMPUTE nval = nobs
ELSE
   COMPUTE nval = gridsize

IF (gridsize.gt.0) {
   EXT(NOPRINT) nseries 1 nobs
   COMPUTE hival = %MAXIMUM
   COMPUTE loval = %MINIMUM
   COMPUTE grid  = (hival-loval)/(nval-1)
   SET series2 1 nval = loval + grid * (t-1)
   }
ELSE
   SET series2 1 nval = nseries


IF .not.%DEFINED(window)
   {
   STATISTICS(NOPRINT) nseries 1 nobs
   COMPUTE width = ((4./3.)**(.2)) * sqrt(%variance) * nobs**(-.2)
   }
ELSE
   COMPUTE width = window


STATISTICS(noprint) series startl endl

SET fhat 1 nval = 0.0

dim val(nobs)

DO i=1,nval
   if kernel.eq.1
      ewise val(j)=%density((series2(i)-nseries(j))/width)
   else
      ewise val(j)=%max(0.0,.335410196625*(1-.2*((series2(i)-nseries(j))/width)**2.0))
   compute fhat(i)=%sum(val)/(width*nobs)
end do i

IF %DEFINED(yout)
   SET yout 1 nval = fhat

CLEAR series2 nseries fhat fhat2 n01

END KERNEL



