*
* LocalTrend
*
* Performs a "local smoothing regression".
*
* Syntax:
*
* @LocalTrend( option ) x start end y
*
* Parameters
*
*    x           dependent variable being analyzed
*    start end   estimation range (by default, the range of the x series)
*    y           series of fitted values
*
*  Reference:
*    See: Forecasting Methods and Applications, 3rd edition, by Makridakis, Wheelwright, and Hyndman (Wiley, 1998),
*         pp. 101-104
*
*  Revision Schedule:
*    07/05  Written by Tom Doan, Estima
*

procedure LocalTrend x start end y
type series x
type integer start end
type series *y
*
option integer span 13
*
local real u half
local integer startl endl time
local series trend spread
*
inquire(series=x) startl>>start endl>>end
compute half=fix(span/2)
*
set trend startl endl = t
set y     startl endl = 0.0
do time=startl,endl
   set spread = u=%iabs(t-time)/half,%if(u<1.0,1.0/(1-u**2)**2,%na)
   linreg(noprint,spread=spread) x startl endl
   # constant trend
   compute y(time)=%beta(1)+%beta(2)*time
end do time
end

