Backtest of Forecasting

3 views (last 30 days)
Patrick
Patrick on 31 May 2011
I'd like to backtest my forecasting. I used the template from "forecsting the US economy". Instead of dates I used observations. Since I'm still a beginner with Matlab, I'd appreciate if someone helped me. I don't really get the last part of the code and don't know how to convert it. I'm sorry.
The data I used is stored in a double array, containing 5372 observations.
%% Backtest
syy = Return(3000); % start year for backtest
eyy = Return(5372); % end year for backtest
Horizon = 1; % number of quarters for forecast horizon
[T, n] = size(Y);
FError = NaN(eyy - syy + 1, n);
FDates = zeros(eyy - syy + 1, 1);
for yy = syy:eyy
StartDate = Return(3000);
EndDate = Return(5372);
% Locate indexes in data for specified date range
iStart = find(StartDate <= dates,1);
if iStart < 1
iStart = 1;
end
iEnd = find(EndDate <= dates,1);
if iEnd > numel(dates)
iEnd = numel(dates);
end
if iStart > 1
Y0 = Y(1:iStart-1,:);
else
Y0 = [];
end
Y1 = Y(iStart:iEnd,:);
iY1 = iY(iStart:iEnd,:);
% Set up model and estimate coefficients
spec = garchset('VarianceModel','GJR',...
'R',1,'M',1,'P',1,'Q',1);
spec = garchset(spec,'Display','off','Distribution','T');
[coeff,errors,LLF,eFit,sFit] = garchfit(spec,Return);
% Do forecasts
horizon = 1 [sigmaForecast,meanForecast,sigmaTotal,meanRMSE] = ... garchpred(coeff,Return,horizon);
% Assess Forecast Quality
Ow = max(0,min(Horizon,(size(Y,1) - iEnd)));
if Ow >= Horizon
h = Horizon;
else
h = [];
end
FDates(yy-syy+1) = yy;
if ~isempty(h)
Yerr = iY(iEnd+1:iEnd+Ow,:) - eFY(1:Ow,:);
Ym2 = Yerr(1:h,:) .^ 2;
Yrmse = sqrt(mean(Ym2,1));
fprintf('%12s',datestr(EndDate,1));
for i = 1:n
fprintf(' %7.2f',100*Yrmse(i));
end
FError(yy-syy+1,:) = 100*Yrmse';
fprintf('\n');
end
end
% Accuracy
mFError = NaN(size(FError));
sFError = NaN(size(FError));
for i = 1:n
mFError(:,i) = nanmean(FError(:,i));
sFError(:,i) = nanstd(FError(:,i));
end
for i = 1:n
subplot(ceil(n/2),2,i,'align');
plot(FDates,FError(:,i));
hold on
plot(FDates,mFError(:,i),'g');
plot(FDates,[mFError(:,i) - sFError(:,i),mFError(:,i) + sFError(:,i)],':r');
dateaxis('x',1);
if i == 1000
title(['\bfForecast Accuracy for ' sprintf('%g',Horizon) '-Year Horizon']);
end
end

Answers (0)

Categories

Find more on Risk Management Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!