%%%%%%%%%%%%%%%% MAIN LOOP %%%%%%%%%%%%%%%%%%%%%%%%%%%
index = 0; % time index for the rolling window
constr = 0; % unconstrained paramters
flag1=1 % 1 for Klaassen and 0 for Gray (default)
max_forecast = 22; % max number of forecasts
% Elapsed minutes from beginning
minutes = etime(clock,seconds)/60;
eval(['save junk/',strcat('res_',modelname,distr_name),' errortype constr']);
for timeindex = T:N
data = dataf(1+index:timeindex);
sec=clock; % clock call for the main loop
% Setting the lower bounds and upper bounds for optimization (once for all)
% Using fmincon
infinity = inf;
hundred = 100;
ten = 10;
ptiny = 1e-6;
tiny = 1e-8;
ttiny = 1e-8;
max_gamma = 171.6;
ged_nu_tiny = 1/max_gamma;
tdist_tiny = 1e-3; % New
switch errortype
case 1 % Normal case
lowerBounds = [-hundred(ones(2,1)) ; tiny(ones(2,1)) ; ttiny(ones(2+2,1)); ptiny(ones(2,1))];
upperBounds = [ hundred(ones(2,1)) ; ten(ones(2,1)) ; ones(2+2,1) ; 1-ptiny(ones(2,1))];
case {2} % t with 2 Dof
lowerBounds = [-.5*hundred(ones(2,1)) ; tiny(ones(2,1)) ; tiny(ones(2+2,1)); ptiny(ones(2,1)) ; (2+tdist_tiny)+tiny(ones(2,1))]; % New
upperBounds = [ .5*hundred(ones(2,1)) ; ten(ones(2,1)) ; ones(2+2,1) ; 1-ptiny(ones(2,1)) ; 2*max_gamma-1+tiny(ones(2,1))];
case 4 % t with 1 DoF
lowerBounds = [-.5*hundred(ones(2,1)) ; tiny(ones(2,1)) ; zeros(2+2,1); ptiny(ones(2,1)) ; 2+tiny+tdist_tiny]; % New
upperBounds = [ .5*hundred(ones(2,1)) ; ten(ones(2,1)) ; ones(2+2,1) ; 1-ptiny(ones(2,1)) ; 2*max_gamma-1+tiny];
case 5 % GED with 1 DoF
lowerBounds = [-2*ten(ones(2,1)) ; tiny(ones(2,1)) ; tiny(ones(2+2,1)); ptiny(ones(2,1)) ; ged_nu_tiny*3+tiny];
upperBounds = [ 2*ten(ones(2,1)) ; 2*(ones(2,1)) ; ones(2+2,1); 1-ptiny(ones(2,1)) ; max_gamma+tiny];
otherwise
end
% Constraints for fmincon
A = zeros(10,size(x0));
A(1,5) = -1;
A(2,6) = -1;
A(3,7) = -1;
A(4,8) = -1;
A(5,9) = -1;
A(6,9) = 1;
A(7,10) = -1;
A(8,10) = 1;
A(9,[5 7]) = 1;
A(10,[6 8]) = 1;
b = [zeros(1,2) zeros(1,2) [0 1 0 1] ones(1,2)*[1 - 1e-6]];
% For next call to the optimization procedure fmincon: Setting the options
% Options for fmincon
options = optimset('fmincon');
if timeindex == T
max_st_v = 99; % max # of starting values. Default=99
switch errortype
case 1 %N
values1 = [ x0 [abs(rand(size(x0,1)-2,500));.98*(ones(2,500))]];
case 2 %t2DF
values1 = [ x0 [abs(rand(size(x0,1)-4,500));.98*(ones(2,500));2+6*abs(rand(2,500))]];
case 4 %t1DF
values1 = [ x0 [abs(rand(size(x0,1)-3,500));.98*(ones(2,500));2+6*abs(rand(1,500))]];
case 5 %G2DF
values1 = [ x0 [abs(rand(size(x0,1)-3,500));.98*(ones(2,500));abs(randn(1,500))]];
otherwise
end
inndd1=(A(9:10,:)*values1>1);
inndd2=sum(inndd1);
inndd2(find(inndd2==2))=1;
values1(:,(find(inndd2==1)))=[];
starting_values = values1(:,1:max_st_v);
clear inndd*;
clear values1;
clear values1;
param0 = repmat(zeros(size(x0)),1,max_st_v);
options = optimset(options , 'TolFun' , 1e-7);
options = optimset(options , 'TolX' , 1e-7);
options = optimset(options , 'TolCon' , 1e-6);
options = optimset(options , 'Display' , 'iter');
options = optimset(options , 'Diagnostics' , 'on');
options = optimset(options , 'LargeScale' , 'off');
options = optimset(options , 'MaxIter' , 2500);
options = optimset(options , 'HessUpdate' ,'bfgs');
options = optimset(options , 'LevenbergMarquardt' ,'on');
options = optimset(options , 'LineSearchType' ,'quadcubic');
options = optimset(options , 'Jacobian' ,'off');
options = optimset(options , 'MeritFunction' ,'singleobj');
options = optimset(options , 'MaxFunEvals' , 2000);
fprintf(fid,'Optimization for STARTING VALUES \n',index);
LL=zeros(max_st_v,1);
for j = 1:max_st_v
fprintf(1,'Index is %-3.3f \n',j);
fprintf(fid,'Index is %-3.3f \n',j);
x0 = starting_values(:,j);
fprintf(fid,'x0 \n');
fprintf(fid,'%-3.5f \n',x0);
LL(j,1)=swgarchlik(x0,data,errortype,flag1,constr);
fprintf(fid,'LL is \n');
fprintf(fid,'%-3.5f \n',LL(j,1));
[parameters, LL(j,1)] = fmincon('swgarchlik', x0 ,A, b, [],[],lowerBounds, upperBounds, [], options, data, errortype, flag1, constr);
fprintf(fid,'LL is %-3.5f and param_x0 is \n',LL(j,1));
fprintf(fid,'%-3.5f \n',parameters);
param0(:,j) = parameters;
end
[minLL,minIndex] = min(LL,[],1)
x0 = [];
x0 = param0(:,minIndex)
fprintf(fid,'Initial x0 \n');
fprintf(fid,'%-3.5f \n',x0);
end
if timeindex == T
options = optimset(options , 'TolFun' , 1e-008);
options = optimset(options , 'TolX' , 1e-008);
else
switch errortype
case {1,5}
options = optimset(options , 'TolFun' , 1e-008);
options = optimset(options , 'TolX' , 1e-008);
otherwise
options = optimset(options , 'TolFun' , 1e-007);
options = optimset(options , 'TolX' , 1e-007);
end
end
options = optimset(options , 'TolCon' , 1e-008);
options = optimset(options , 'TolPCG' , 1e-2);
options = optimset(options , 'Display' , 'iter');
options = optimset(options , 'Diagnostics' , 'on');
options = optimset(options , 'LargeScale' , 'off');
options = optimset(options , 'MaxIter' , 2500);
options = optimset(options , 'HessUpdate' ,'bfgs');
options = optimset(options , 'LevenbergMarquardt' ,'off');
options = optimset(options , 'LineSearchType' ,'cubicpoly');
options = optimset(options , 'Jacobian' ,'off');
options = optimset(options , 'MeritFunction' ,'multiobj');
options = optimset(options , 'MaxFunEvals' , 5000);
fprintf(1,'\n\n Index is %-3.0f \n',index);
fprintf(1,'\n ERRORTYPE is %-3.0f \n',errortype);
fprintf(1,'\n Minutes from latest optimization are %-3.2f \n',minutes);
fprintf(1,'Time from latest optimization is %-15s \n',datestr(clock,0));
[parameters, LLF, EXITFLAG, OUTPUT, Lambda, GRAD, HESSIAN] = fmincon('swgarchlik', x0 ,A, b, [],[],lowerBounds, upperBounds, [], options, data, errortype, flag1, constr);
if EXITFLAG<=0
EXITFLAG
fprintf(1,'Convergence has been not successful! \n')
end
% Add check for parameter constraints if fminunc is used
% Printing
fprintf(fid,'timeindex \t\t LogLike \t\t Exitflag(<=0_if_unsuccessful) \n');
fprintf(fid,'%-3.1f \t\t %-5.5f \t\t %-3.3f \n',[timeindex,LLF,EXITFLAG]);
[LLF,likelihoods,e,p1,p1t,p1sm,h1,h2] = swgarchlik(parameters,data,errortype,flag1,constr);
LLF=-LLF;
grad = gradt('swgarchlik',parameters,data,errortype,flag1,constr);
hess = hessian('swgarchlik',parameters,data,errortype,flag1,constr);
% asymptotic standard errors
stder_hess = sqrt(diag((hess)^(-1))); % from calculated hessian
tstat_hess = parameters./stder_hess;