##################################################################### # # # Macro: OPDES.MAC # # Version: 2.00 # # Date : June, 1999 # # Author: F. R. B. Cruz, O. F. Neves, and E. M. M. Toscano # # Departamento de Estatistica - ICEx # # UFMG - Brazil # # Note: This macro implements the one parameter # # double exponential smoothing as described by: # # B. L. Bowerman and R. T. O'Connell # # Forecasting and Time Series: An Applied Approach # # Duxbury Press, Belmont, USA, 1993 # # # ##################################################################### # macro opdes Series; weight Alpha; origin InitPt; number NuOfFor; smoothed Smooth; forecasts Forecast; levels Level; lowers Lower; uppers Upper; SumSqError SumSqEr; NoPlot. # # declarations mcolumn Series Smooth Forecast Lower Upper mconstants Alpha InitPt NuOfFor Level SumSqEr mcolumn SAux time c2 c3 c4 AbsError mconstants SSize Size Beta0 Beta1 Index S S2 StartPt EndPt Delta V Dt Aux Z mconstants Epsilon F A B W GV GW default Alpha=-1 InitPt=-1 NuOfFor=0 Level=0.95 # noecho brief 2 mtitle 'One Parameter Double Exponential Smoothing' note Macro running ... please wait brief 0 # erase Smooth erase Forecast erase Upper erase Lower count Series SSize nmiss Series if InitPt=-1 let InitPt = SSize endif if InitPt < SSize let SSize = InitPt endif if Alpha > 0 note do know Alpha, computing SumSqError right away # compute beta0 and beta1 # create vector of time # if NuOfFor = 0 # note assuming I optimize, I shall use part of data copy Series SAux; Use 1:6. set Time 1:6 end # else # note assuming optimum was found, I shall use all data # copy Series SAux; # Use 1:SSize. # set Time # 1:SSize # end # endif # perform regression Regress SAux 1 Time; Coefficients c2; Constant. let beta0 = c2(1) let beta1 = c2(2) copy Series SAux; Use 1:SSize. # compute sum of square errors let S = beta0 - (1-Alpha)*beta1/Alpha let S2 = beta0 - 2*(1-Alpha)*beta1/Alpha do Index = 1:SSize let Smooth(Index) = (2+Alpha/(1-Alpha))*S - (1+Alpha/(1-Alpha))*S2 let S = Alpha*SAux(Index) + (1-Alpha)*S let S2 = Alpha*S + (1-Alpha)*S2 enddo let AbsError = ABS(SAux - Smooth) let SumSqEr = sum(AbsError**2) # compute Forecast and confidence intervals for NuOfFor steps ahead # compute Delta let Delta = SUM(AbsError)/SSize let V = 1 - Alpha let Aux = (1+Level)/2 InvCDF Aux Z; Normal 0.0 1.0. # compute forecasts and confidence intervals let Index = InitPt+1 let EndPt = InitPt+NuOfFor while ( Index <= EndPt ) let Forecast(Index) = (2+Alpha*(Index-SSize)/(1-Alpha))*S - (1+Alpha*(Index-SSize)/(1-Alpha))*S2 let Dt = 1 + Alpha/(1+V)**3*(1+4*v + 5*V**2 + 2*Alpha*(1+3*V)*(Index-SSize) + 2*Alpha**2*(Index-SSize)**2) let Dt = 1.25*SQRT(Dt/(1+Alpha/(1+V)**3*(1+4*v + 5*V**2 + 2*Alpha*(1+3*V) + 2*Alpha**2))) let Lower(Index) = Forecast(Index)-Z*Dt*Delta let Upper(Index) = Forecast(Index)+Z*Dt*Delta let Index = Index + 1 endwhile brief 2 print Alpha SumSqEr brief 0 if noplot = 0 # plot graphs brief 2 note Ploting graph ... please wait brief 0 TSPlot Series Smooth Forecast Lower Upper; TDisplay 11; Symbol; Connect; Title "One Parameter Double Exponential Smoothing for Series"; Overlay; Axis 11; Label "Time"; Axis 2; Label "Series". endif else # don't know Alpha, optimize Alpha first (Golden Section Method) note don't know Alpha, optimizing first copy Series SAux; use 1:SSize. let Epsilon = 0.005 let F = (SQRT(5)-1)/2 let A = 0.0 let B = 1.0 let V = B - F*(B-A) let W = A + F*(B-A) %opdes SAux; weight V; smoothed Smooth; SumSqError GV; NoPlot. %opdes SAux; weight W; smoothed Smooth; SumSqError GW; NoPlot. while (F*(B-A) > Epsilon) if GV < GW let B = W let W = V let GW = GV let V = B - F*(B-A) %opdes SAux; weight V; smoothed Smooth; SumSqError GV; NoPlot. else let A = V let V = W let GV = GW let W = A + F*(B-A) %opdes SAux; weight W; smoothed Smooth; SumSqError GW; NoPlot. endif endwhile if GV < GW let B = W let Alpha = B - F*(B-A) else let A = V let Alpha = A + F*(B-A) endif if NoPlot = 0 %opdes Series; weight Alpha; origin InitPt; number NuOfFor; smoothed Smooth; forecasts Forecast; levels Level; lowers Lower; uppers Upper; SumSqError SumSqEr. else %opdes Series; weight Alpha; origin InitPt; number NuOfFor; smoothed Smooth; forecasts Forecast; levels Level; lowers Lower; uppers Upper; SumSqError SumSqEr; NoPlot. endif endif brief 2 # exit note Macro exiting ... endmacro