
Example 10  Problem Description
Nonparametric smoothing and extrapolation using
daily closing price of General Electric from 1/02/2003 to 1/17/2006
OMatrix source code for example 10
# Nonparametric smoothing and extrapolation.
#
# 1 data series: daily closing price of General Electric from 1/02/2003 to 1/17/2006
#
# Initialize
clear
ginit
clc
ranseed
# Read the data
N = 765.
ge = read("data\ge0306.prn","double",N,1)
# Perform smoothing and forecasting (trend extrapolation) using three different bandwidths and
# the local linear method. Nonparametric smoothing using local linear regression is better than
# simple smoothing using a moving average since it has better properties near the boundaries of
# the time series
#
# Holdout the last "fs" observations to extrapolate
#
fs = 20
gex = ge(1::endfs)
[fit1,res] = npsmooth(gex,0.025,"local linear",fs,0)
[fit2,res] = npsmooth(gex,0.050,"local linear",fs,0)
[fit3,res] = npsmooth(gex,0.150,"local linear",fs,0)
# Plot the original series and the smoothed series
gaddwin()
gxaxis("linear",0,Nfs+5,10)
gxgrid("major")
gygrid("minor")
gcolor({"black","red","green","blue"})
fit = [fit1,fit2,fit3]
gplot([gex,fit(1::endfs,:)])
# Plot the holdout "fs" observations and the trend forecasts
gaddwin()
gxaxis("linear",0,fs,10)
gxgrid("major")
gygrid("major")
gcolor({"black","red","green","blue"})
gplot([ge(endfs+1::end),fit(endfs+1::end,:)])
# Now perform a rolling forecasting evaluation using a window of 250 observations (about one year)
# to predict the next 10 observations (two weeks) using a bandwidth that tracks both the series
# (short term forecasting)and the general trend (longer term forecasting). You can experiment by
# changing the default value of 0.025 in the function below.
fs = 10
R = 250.
T = NR+1
f = zeros(Tfs,fs)
for i = 0 to Tfs1 begin
xi = ge(i+1::R+i)
[fit,res] = npsmooth(xi,0.085,"local linear",fs,0)
f(i+1,:) = fit(endfs+1::end)'
end
# Compute the forecast errors and the forecast error variance
y1 = [ge(R+1::endfs+1),ge(R+2::endfs+2),ge(R+3::endfs+3),ge(R+4::endfs+4),ge(R+5::endfs+5)]
y2 = [ge(R+6::endfs+6),ge(R+7::endfs+7),ge(R+8::endfs+8),ge(R+9::endfs+9),ge(R+10::endfs+10)]
y = [y1,y2]
print "Extrapolation model  RMSE of the forecasts from 1 to 10 days ahead = "
print colstd(yf)
# Compare the forecasting performance with the random walk (no change model)
frw = fillcols(ge(R::endfs),fs)
print "Random walk model  RMSE of the forecasts from 1 to 10 days ahead = "
print colstd(yfrw)
print
print "Its very difficult to beat the random walk model!!!"
Output from running example 10
