%% pre run dynare to load the macro (this should be done only once)
clear all 
close all
clc
dynare sw4;

%% setting the value for the parameter:

close all
clear all
clc

loadtheta

all_variabs    = strvcat('y_o','c_o','i_o','w_o','pi_o','r_o','h_o');
all_nvariabs   = {'y_o','c_o','i_o','w_o','pi_o','r_o','h_o'};
[comb]         = createcombinations(all_variabs,all_nvariabs);
combins        = [];
theta_0        = theta';

wb = waitbar(0, 'Computing the rank test for 35 combinations');
for co =1:35
    waitbar(co/35, wb);
    subset  = comb(co).var; 
    subsetn = comb(co).nvar; 
    combins = [combins; subsetn];
    result6  = [];
    for cc =6:29
        % compute the ABCD matrices. they are in minimal state form solution, see
        % complementary documents in Kumunijer and Ng (2011).
        [minA,minB,minC,minD,Sigma] = sw4_abcd(theta_0,comb(co).var);
        % Compute the derivatives
        [Delta_0,Delta_L,Delta_T,Delta_U] = ...
            delta_as(theta_0,minA,minB,minC,minD,Sigma,comb(co).var);
        n_x           = size(minA,1);
        n_eps         = size(minB,2);
        n_y           = size(minC,1);
        n_theta       = size(theta_0,1);
        % checks conditional identification under restrictions defined in Rmat
        Rmat{1} = [1 2 3 4 5 cc];
        tol1 = 1e-2;
        % prints the results in a table for all the restrictions in Rmat 
        % tolerance is set to tol1
        tabl_res = rank_test_res(Delta_0,n_theta,n_x,n_eps,n_y,tol1,Rmat);    
        if tabl_res(:,end)==1
            result6 = [result6, Rmat];
        else 
        end
    end     
    comb(co).restr6 = result6;
end
close(wb);

save sw4_oneextrares comb

paranames= {'\delta','\varepsilon_p','\varepsilon_w',' \lambda_w ',...
    'cg  ',' \beta ',' \phi_p','\psi ',' \alpha ',' h ',...
    ' \zeta_{\omega} ',' \zeta_p ',' i_{\omega} ',' i_p ',...
    ' \sigma_n ', ' \sigma_c $ ',' \varphi ', ' \rho_{\pi} ',...
    ' \rho_R ',' \rho_{y} ',' \rho_{\Delta y}',' \rho_{a} ',...
    ' \rho_{g}',' \rho_{i}',' \rho_{ga}',' \sigma_{a} ',...
    ' \sigma_{g} ',' \sigma_{i} ',' \sigma_{r} '};

%%
% output =[];
% 
% for i=1:size(comb,2)
%     for ii=1:size(comb(1,i).restr6,2)
%         output =[output; comb(1,i).nvar, paranames(comb(1,i).restr6{1,ii})];
%     end
% end

output =cell(size(comb,2),4+24);

for i=1:size(comb,2)
    output(i,1:4) = comb(1,i).nvar;
    for ii=1:size(comb(1,i).restr6,2)
        output(i,4+ii) = paranames(comb(1,i).restr6{1,ii}(1,end));
    end
end
