Commit 8c1b5864 authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

Merge pull request #422 from JohannesPfeifer/RecursiveForecasts

Documentation, bugfixing, and cosmetic changes of recursive forecasts 
parents 9f9c2c1f fdee69f7
......@@ -4183,7 +4183,7 @@ the file
@anchor{nobs1}
Runs a recursive estimation and forecast for samples of size ranging
of @var{INTEGER1} to @var{INTEGER2}. Option @code{forecast} must
also be specified
also be specified. The forecasts are stored in the @code{RecursiveForecast} field of the results structure. @xref{RecursiveForecast}
@item first_obs = @var{INTEGER}
@anchor{first_obs}
......@@ -4940,6 +4940,33 @@ Variable set by the @code{estimation} command. Stores parameter vector of final
Variable set by the @code{estimation} command. Stores log-posterior of final MCMC chain draw
@end defvr
@defvr {MATLAB/Octave variable} oo_.RecursiveForecast
@anchor{RecursiveForecast}
Variable set by the @code{forecast} option of the @code{estimation} command when used with the nobs = [@var{INTEGER1}:@var{INTEGER2}] option (@xref{nobs1,,nobs}).
Fields are of the form:
@example
@code{oo_.RecursiveForecast.@var{FORECAST_OBJECT}.@var{VARIABLE_NAME}}
@end example
where @var{FORECAST_OBJECT} is one of the following@footnote{See @ref{forecast} for more information}:
@table @code
@item Mean
Mean of the posterior forecast distribution
@item HPDinf/HPDsup
Upper/lower bound of the 90\% HPD interval taking into account only parameter uncertainty
@item HPDTotalinf/HPDTotalsup
Upper/lower bound of the 90\% HPD interval taking into account both parameter and future shock uncertainty
@end table
@var{VARIABLE_NAME} contains a matrix of the following size: number of time periods for which forecasts are requested using the nobs = [@var{INTEGER1}:@var{INTEGER2}] option times the number of forecast horizons requested by the @code{forecast} option. I.e., the row indicates the period at which the forecast is performed and the column the respective k-step ahead forecast. The starting periods are sorted in ascending order, not in declaration order.
@end defvr
@deffn Command model_comparison @var{FILENAME}[(@var{DOUBLE})]@dots{};
@deffnx Command model_comparison (marginal_density = laplace | modifiedharmonicmean) @var{FILENAME}[(@var{DOUBLE})]@dots{};
......
......@@ -39,7 +39,7 @@ var_list = check_list_of_variables(options_, M_, var_list);
options_.varlist = var_list;
if isfield(options_,'nobs')
nobs = options_.nobs;
nobs = sort(options_.nobs);
else
nobs = [];
end
......@@ -86,7 +86,8 @@ if nnobs > 1 && horizon > 0
mh_replic = options_.mh_replic;
rawdata = read_variables(options_.datafile,options_.varobs,[],options_.xls_sheet,options_.xls_range);
gend = options_.nobs;
rawdata = rawdata(options_.first_obs:options_.first_obs+gend-1,:);
data_plot_end_point=min(options_.first_obs+gend-1+horizon,size(rawdata,1)); %compute last observation that can be plotted
rawdata = rawdata(options_.first_obs:data_plot_end_point,:);
% Take the log of the variables if needed
if options_.loglinear && ~options_.logdata % and if the data are not in logs, then...
rawdata = log(rawdata);
......@@ -115,34 +116,32 @@ if nnobs > 1 && horizon > 0
IdObs = zeros(n_varobs,1);
for j=1:n_varobs
for i=1:nvar
iobs = strmatch(options_.varobs(j,:),var_list,'exact');
end
iobs = strmatch(options_.varobs(j,:),var_list,'exact');
if ~isempty(iobs)
IdObs(j,1) = iobs;
end
end
k = 3+min(nobs(end)-nobs(1)+horizon, ...
time_offset=min(3,gend-1); %for observables, plot 3 previous periods unless data is shorter
k = time_offset+min(nobs(end)-nobs(1)+horizon, ...
size(rawdata,1)-nobs(1));
data2 = rawdata(end-k+1:end,:);
[nbplt,nr,nc,lr,lc,nstar] = pltorg(nvar);
m = 1;
plot_index=0;
OutputDirectoryName = CheckPath('graphs',M_.fname);
for i = 1:size(var_list,1)
if mod(i,nstar) == 1
hfig = dyn_figure(options_,'Name','Out of sample forecasts');
plot_index=plot_index+1;
hfig = dyn_figure(options_,'Name',['Out of sample forecasts (',num2str(plot_index),')']);
m = 1;
end
subplot(nr,nc,m)
hold on
if any(i==IdObs)
k2 = find(i==IdObs);
if options_.loglinear == 1
plot(1:k,exp(data2(end-k+1:end,k2))','-k','linewidth',2);
else
plot(1:k,data2(end-k+1:end,k2)','-k','linewidth',2);
end
offsetx = 3;
plot(nobs(1)-offsetx+1:nobs(1)-offsetx+k,data2(end-k+1:end,k2)','-k','linewidth',2);
else
offsetx = 0;
end
......@@ -176,7 +175,8 @@ if nnobs > 1 && horizon > 0
'oo_recursive_{' int2str(nobs(j)) '}.forecast.HPDsup.' ...
vname ';']);
end
x = offsetx+nobs(j)-nobs(1)+(1:horizon);
x = nobs(1)+nobs(j)-nobs(1)+(1:horizon);
y = eval(['oo_.RecursiveForecast.Mean.' vname '(j,:)']);
y1 = eval(['oo_.RecursiveForecast.HPDinf.' vname '(j,:)']);
y2 = eval(['oo_.RecursiveForecast.HPDsup.' vname '(j,:)']);
......@@ -194,15 +194,13 @@ if nnobs > 1 && horizon > 0
plot(x,y4,'--r','linewidth',1.5)
end
end
% set(gca,'XTick',offsetx+[1 10 20 30 40 50 60 70 80 90]);
% set(gca,'XTickLabel',{'1';'10';'20';'30';'40';'50';'60';'70';'80';'90'});
% xlim([1 options_.forecast+10]);
if any(k==IdObs)
plot([offsetx+1 offsetx+1],ylim,'-c')
end
box on
title(vname,'Interpreter','none')
hold off
xlim([nobs(1)-offsetx nobs(end)+horizon])
m = m + 1;
if mod(i+1,nstar) == 1 || i ==size(var_list,1)
dyn_saveas(hfig,[M_.fname,filesep,'graphs',filesep M_.fname '_RecursiveForecasts_' int2str(plot_index)],options_);
end
end
end
......@@ -56,6 +56,9 @@ if dataset_.info.nvobs-size(rawdata,2)
'variables doesn''t match the number of variables in the database.'])
end
if size(rawdata,1)~=dataset_.info.ntobs
fprintf('Restricting the sample to observations %d to %d. Using in total %d observations. \n',first,dataset_.info.ntobs,dataset_.info.ntobs-first+1)
end
rawdata = rawdata(first:(first+dataset_.info.ntobs-1),:);
% Take the log (or anything else) of the variables if needed
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment