%   Define as equações que descrevem o steady-state do modelo com setor
%   publico detalhado da Conferencia DSGE, com 3 tipos de agentes:
%   ricardianos; n-ricardianos q trabalham; n-ricardianos q n-trab 

%   MODIFICACOES EM RELACAO AO "MOD_FISCAL_03_SS" ASSINALADAS ABAIXO


function F = mod_fiscal_04_ss(x)
global nr_i beta_i h_i sigma_c_i sigma_l_i eta_t_k_i eta_t_l_i eta_nt_k_i eta_nt_l_i delta_i delta_til_i gamma_i eta_i eta_t_g_i eta_nt_g_i mu_t_i mu_nt_i tao_star_i mu_w_i phi_i  y_star_i pt_star_i taoc_star_i g_i ig_i tl_i wm_i taoc_i taow_ss_i taok_ss_i taox_ss_i rho_b_i rho_i_i rho_R_star_i rho_pi_star_i rho_premium_i rho_a_t_i rho_a_nt_i rho_l_i rho_taoc_star_i rho_y_star_i rho_p_star_i rho_target_i theta_i a_i r_star_i g_ss_i ig_ss_i t_ss_i wm_ss_i taoc_ss_i gg_ss_i wmt_ss_i uwg_ss_i lg_llt_ss_i big_i bag_i ctb_y_ss_i ctl_y_ss_i uwt_ss_i nr2_i cct_ss_i;

%   Atribui valores aos parâmetros do modelo:
par_pop = nr_i;
par_pop2 = nr2_i;           %ADICIONADO
beta = beta_i;
h = h_i;
sigma_c = sigma_c_i;
sigma_l = sigma_l_i;
eta_t_k = eta_t_k_i;
eta_t_l = eta_t_l_i;
eta_nt_k = eta_nt_k_i;
eta_nt_l = eta_nt_l_i;
delta = delta_i;
delta_til = delta_til_i;
gamma = gamma_i;
eta = eta_i;
eta_t_g = eta_t_g_i;
eta_nt_g = eta_nt_g_i;
mu_t = mu_t_i;
mu_nt = mu_nt_i;
tao_star = tao_star_i;
mu_w = mu_w_i;
phi = phi_i;
theta = theta_i;
a = a_i;

big = big_i;
bag = bag_i;

A_t_K = 1/(((eta_t_l/eta_t_k)^(eta_t_l))*(((1-eta_t_k-eta_t_l)/eta_t_k)^(1-eta_t_k-eta_t_l)));
A_t_L = 1/(((eta_t_l/eta_t_k)^(eta_t_l-1))*(((1-eta_t_k-eta_t_l)/eta_t_k)^(1-eta_t_k-eta_t_l)));
A_t_Q = 1/(((eta_t_l/eta_t_k)^(eta_t_l))*((eta_t_k/(1-eta_t_k-eta_t_l))^(eta_t_k+eta_t_l)));
A_nt_K = 1/(((eta_nt_l/eta_nt_k)^(eta_nt_l))*(((1-eta_nt_k-eta_nt_l)/eta_nt_k)^(1-eta_nt_k-eta_nt_l)));
A_nt_L = 1/(((eta_nt_l/eta_nt_k)^(eta_nt_l-1))*(((1-eta_nt_k-eta_nt_l)/eta_nt_k)^(1-eta_nt_k-eta_nt_l)));
A_nt_Q = 1/(((eta_nt_l/eta_nt_k)^(eta_nt_l))*((eta_nt_k/(1-eta_nt_k-eta_nt_l))^(eta_nt_k+eta_nt_l)));

A_t = A_t_K + A_t_L + A_t_Q;
A_nt = A_nt_K + A_nt_L + A_nt_Q;

%   Atribui valores às variáveis exógenas no steady-state:

%   Setor externo
y_star = y_star_i;
pt_star = pt_star_i;
taoc_star = taoc_star_i;
r_star = r_star_i;

%   Variáveis fiscais
g = g_ss_i;
ig = ig_ss_i;
tl = t_ss_i;
wm = wm_ss_i;
taoc = taoc_ss_i;
taow = taow_ss_i;
taok = taok_ss_i;
taox = taox_ss_i;

uwg_ss = uwg_ss_i;
lg_llt_ss = lg_llt_ss_i;

uwt_ss = uwt_ss_i;       % ADICIONADO

%   Sistema de equações que determina o steady_state:
%   TOTAL: 29+2+4+2 = 37
%   [209] (1-taow)*wc = ((lc/nr)^sigma_l)*(((1-taow)*wc*lc/nr + wm - h/PARPOP*cc)^sigma_c); % MODIFICADO
%   [210] cc = (1-taow)*wc*lc + wm*PARPOP; % MODIFICADO
%   [211] c = cl + cc + CCT; % MODIFICADO
%   [212] rk = (1-beta*(1-delta))/(beta*(1-taok)*(1+taoc));
%   [213] ynt = (1-gamma)*y*(ptilnt^(-(1+eta)/eta));
%   [214] yt = gamma*y*(ptilt^(-(1+eta)/eta));
%   [215] 1 = gamma*(ptilt^(-1/eta)) + (1-gamma)*(ptilnt^(-1/eta));
%   [216] ktilt = (yt + yt_star)*A_t_K*(rk^(eta_t_k-1))*(w^eta_t_l)*(ptx^(1-eta_t_k-eta_t_l));
%   [217] ktilnt = ynt*A_nt_K*(rk^(eta_nt_k-1))*(w^eta_nt_l)*(ptx^(1-eta_nt_k-eta_nt_l));
%   [218] lt = (yt + yt_star)*A_t_L*(rk^eta_t_k)*(w^(eta_t_l-1))*(ptx^(1-eta_t_k-eta_t_l));
%   [219] lnt = ynt*A_nt_L*(rk^eta_nt_k)*(w^(eta_nt_l-1))*(ptx^(1-eta_nt_k-eta_nt_l));
%   [220] qt = (yt + yt_star)*A_t_Q*(rk^eta_t_k)*(w^eta_t_l)*(ptx^(-eta_t_k-eta_t_l));
%   [221] qnt = ynt*A_nt_Q*(rk^eta_nt_k)*(w^eta_nt_l)*(ptx^(-eta_nt_k-eta_nt_l));
%   [222] il/delta = ktilt + ktilnt;
%   [223] ptx = ((1+taox)/(1+taoc_star))*er*pt_star;
%   [224] yt_star = (((ptilt*(1+taoc_star))/(er*(1+taoc)))^(-(1+tao_star)/tao_star))*y_star;
%   [225] ptilt = (1+mu_t)*(1+taoc)*((ig/delta_til)^(-eta_t_g))*A_t*(rk^eta_t_k)*(w^eta_t_l)*(ptx^(1-eta_t_k-eta_t_l));
%   [226] ptilnt = (1+mu_nt)*(1+taoc)*((ig/delta_til)^(-eta_nt_g))*A_nt*(rk^eta_nt_k)*(w^eta_nt_l)*(ptx^(1-eta_nt_k-eta_nt_l));
%   [227] wl = ((1+mu_w)/(1-taow))*(ll^sigma_l)*(((1-h)*cl)^sigma_c);
%   [228] ll = phi*((wl/w)^(-(1+a)/a))*l;
%   [229] lc = (1-phi)*((wc/w)^(-(1+a)/a))*l;
%   [230] l = LL + LC; % MODIFICADO
%   [231] 1 = phi*((wl/w)^(-1/a)) + (1-phi)*((wc/w)^(-1/a));
%   [232] q = qt + qnt;
%   [233] y = cl + cc + CCT + il + ig + g; % MODIFICADO
%   [234] (1/(beta*r_star)) = exp(-theta*er*b_star*(1/y));
%   [235] (1-beta)*er*b_star = (1/(1+taoc_star))*er*pt_star*q - (1/(1+taoc))*ptilt*yt_star;
%   [236] sp = (1 - beta)*b;
%   [237] sp = (taok/(1+taoc))*y + (taoc/(1+taoc))*cl + (taoc/(1+taoc))*(cc+CCT) +
%   (taow-taok)*w*l + (taok/(1+taoc))*er*ptilt*yt_star + (1/(1+taoc_star))*taox*er*pt_star*q +
%   (taox - taok*(1+taox))*(1/(1+taoc_star))*er*pt_star*q +
%   tl - (1/(1+taoc))*g - parpop*wm -PARPOP2*WMT - (1/(1+taoc))*ig -(1-taow)*x(33)*x(35); % MODIFICADO
%   [A.1] def*pib = p*(y+wg*lg) + (1/(1+taoc))* ptilt*yt_star - ptx*q;
%   [A.2] def = p^(p*(y+wg*lg)/(def*pib)) * ptilt^( (1/(1+taoc))*ptilt*yt_star/(def*pib))*ptx^(-ptx*q/(def*pib))
%   [EP.1] x(32) = x(24) + x(33)
%   [EP.2] x(34) = g + x(35)*x(33)
%   [EP.3] x(35) = u_wg_ss* x(23)
%   [EP.4] x(33) = lg_llt_ss*x(32)
%   [CCT.1] CCT = WMT*PARPOP2;
%   [CCT.2] WMT = UWT*WM;



%   Dicionário:
%   x(1)=wc, x(2)=lc, x(3)=cc; 
%   x(4)=c, x(5)=cl;
%   x(6)=rk; 
%   x(7)=ynt, x(8)=y, x(9)=ptilnt;
%   x(10)=yt, x(11)=ptilt;
%   x(12)=ktilt, x(13)=yt_star, x(14)=w, x(15)=ptx;
%   x(16)=ktilnt;
%   x(17)=lt, x(18)=lnt;
%   x(19)=qt, x(20)=qnt;
%   x(21)=il;
%   x(22)=er;
%   x(23)=wl, x(24)=ll, x(25)=l;
%   x(26)=q;
%   x(27)=b_star, x(28)=sp, x(29)=b;
%   x(30) = pib
%   x(31) = def
%   x(32) = llt
%   x(33) = lg
%   x(34) = gt
%   x(35) = wg
%   x(36) = CCT  % ADICIONADO
%   x(37) = WMT  % ADICIONADO

%   Equações reescritas após utilizar o dicionário:
%   [209] (1-taow)*x(1) = ((x(2)/par_pop)^sigma_l)*(((1-taow)*x(1)*x(2)/par_pop + wm - h/PARPOP2*x(3))^sigma_c);
%   [210] x(3) = (1-taow)*x(1)*x(2) + wm*PARPOP;
%   [211] x(4) = x(5) + x(3) + X(36);
%   [212] x(6) = (1-beta*(1-delta))/(beta*(1-taok)*(1+taoc));
%   [213] x(7) = (1-gamma)*x(8)*(x(9)^(-(1+eta)/eta));
%   [214] x(10) = gamma*x(8)*(x(11)^(-(1+eta)/eta));
%   [215] 1 = gamma*(x(11)^(-1/eta)) + (1-gamma)*(x(9)^(-1/eta));
%   [216] x(12) = (x(10) + x(13))*A_t_K*(x(6)^(eta_t_k-1))*(x(14)^eta_t_l)*(x(15)^(1-eta_t_k-eta_t_l));
%   [217] x(16) = x(7)*A_nt_K*(x(6)^(eta_nt_k-1))*(x(14)^eta_nt_l)*(x(15)^(1-eta_nt_k-eta_nt_l));
%   [218] x(17) = (x(10) + x(13))*A_t_L*(x(6)^eta_t_k)*(x(14)^(eta_t_l-1))*(x(15)^(1-eta_t_k-eta_t_l));
%   [219] x(18) = x(7)*A_nt_L*(x(6)^eta_nt_k)*(x(14)^(eta_nt_l-1))*(x(15)^(1-eta_nt_k-eta_nt_l));
%   [220] x(19) = (x(10) + x(13))*A_t_Q*(x(6)^eta_t_k)*(x(14)^eta_t_l)*(x(15)^(-eta_t_k-eta_t_l));
%   [221] x(20) = x(7)*A_nt_Q*(x(6)^eta_nt_k)*(x(14)^eta_nt_l)*(x(15)^(-eta_nt_k-eta_nt_l));
%   [222] x(21)/delta = x(12) + x(16);
%   [223] x(15) = ((1+taox)/(1+taoc_star))*x(22)*pt_star;
%   [224] x(13) = (((x(11)*(1+taoc_star))/(x(22)*(1+taoc)))^(-(1+tao_star)/tao_star))*y_star;
%   [225] x(11) = (1+mu_t)*(1+taoc)*((ig/delta_til)^(-eta_t_g))*A_t*(x(6)^eta_t_k)*(x(14)^eta_t_l)*(x(15)^(1-eta_t_k-eta_t_l));
%   [226] x(9)  = (1+mu_nt)*(1+taoc)*((ig/delta_til)^(-eta_nt_g))*A_nt*(x(6)^eta_nt_k)*(x(14)^eta_nt_l)*(x(15)^(1-eta_nt_k-eta_nt_l));
%   [227] x(23) = ((1+mu_w)/(1-taow))*(x(24)^sigma_l)*(((1-h)*x(5))^sigma_c);
%   [228] x(24) = phi*((x(23)/x(14))^(-(1+a)/a))*x(25);
%   [229] x(2) = (1-phi)*((x(1)/x(14))^(-(1+a)/a))*x(25);
%   [230] x(25) = x(24) + x(2);
%   [231] 1 = phi*((x(23)/x(14))^(-1/a)) + (1-phi)*((x(1)/x(14))^(-1/a))
%   [232] x(26) = x(19) + x(20);
%   [233] x(8) = x(5) + parpop*x(3) + x(21) + ig + g;
%   [234] (1/(beta*r_star)) = exp(-theta*x(22)*x(27)*(1/x(8)));
%   [235] (1-beta)*x(22)*x(27) = (1/(1+taoc_star))*x(22)*pt_star*x(26) - (1/(1+taoc))*x(11)*x(13);
%   [236] x(28) = (1 - beta)*x(29);
%   [237] x(28) = (taok/(1+taoc))*x(8) + (taoc/(1+taoc))*x(5) + (taoc/(1+taoc))*(x(3)+X(36)) +
%   (taow-taok)*x(14)*x(25) + (taok/(1+taoc))*x(22)*x(11)*x(13) + (1/(1+taoc_star))*taox*x(22)*pt_star*x(26) +
%   (taox - taok*(1+taox))*(1/(1+taoc_star))*x(22)*pt_star*x(26) +
%   tl - (1/(1+taoc))*g - parpop*wm -PARPOP2*WMT - (1/(1+taoc))*ig -(1-taow)*x(33)*x(35); 
%   [A.1] x(31)*x(30) = x(8)+ x(33)*x(35) + (1/(1+taoc))*x(11)*x(13) [- x(15)*x(26)];
%   [A.2] x(31) = 1 * x(11)^((1/(1+taoc))*x(11)*x(13)/(x(31)*x(30))) [*x(15)^(-x(15)*x(26)/(x(31)*x(30)))]
%   [EP.1] x(32) = x(24) + x(33)
%   [EP.2] x(34) = g + x(35)*x(33)
%   [EP.3] x(35) = u_wg_ss* x(23)
%   [EP.4] x(33) = lg_llt_ss*x(32)
%   [CCT.1] CCT = WMT*PARPOP2;
%   [CCT.2] WMT = UWT*WM;

F = [-(1-taow)*x(1) + ((x(2)/par_pop)^sigma_l)*(((1-taow)*x(1)*x(2)/par_pop + wm - h*x(3)/par_pop)^sigma_c);
     -x(3) + (1-taow)*x(1)*x(2) + wm*par_pop;
     -x(4) + x(5) + x(3) + x(36);
     -x(6) + (1-beta*(1-delta))/(beta*(1-taok)*(1+taoc));
     -x(7) + (1-gamma)*x(8)*(x(9)^(-(1+eta)/eta));
     -x(10) + gamma*x(8)*(x(11)^(-(1+eta)/eta));
     -1 + gamma*(x(11)^(-1/eta)) + (1-gamma)*(x(9)^(-1/eta));
     -x(12) + (x(10) + x(13))*A_t_K*(x(6)^(eta_t_k-1))*(x(14)^eta_t_l)*(x(15)^(1-eta_t_k-eta_t_l));
     -x(16) + x(7)*A_nt_K*(x(6)^(eta_nt_k-1))*(x(14)^eta_nt_l)*(x(15)^(1-eta_nt_k-eta_nt_l));
     -x(17) + (x(10) + x(13))*A_t_L*(x(6)^eta_t_k)*(x(14)^(eta_t_l-1))*(x(15)^(1-eta_t_k-eta_t_l));
     -x(18) + x(7)*A_nt_L*(x(6)^eta_nt_k)*(x(14)^(eta_nt_l-1))*(x(15)^(1-eta_nt_k-eta_nt_l));
     -x(19) + (x(10) + x(13))*A_t_Q*(x(6)^eta_t_k)*(x(14)^eta_t_l)*(x(15)^(-eta_t_k-eta_t_l));
     -x(20) + x(7)*A_nt_Q*(x(6)^eta_nt_k)*(x(14)^eta_nt_l)*(x(15)^(-eta_nt_k-eta_nt_l));
     -x(21)/delta + x(12) + x(16);
     -x(15) + ((1+taox)/(1+taoc_star))*x(22)*pt_star;
     -x(13) + (((x(11)*(1+taoc_star))/(x(22)*(1+taoc)))^(-(1+tao_star)/tao_star))*y_star;
     -x(11) + (1+mu_t)*(1+taoc)*((ig/delta_til)^(-eta_t_g))*A_t*(x(6)^eta_t_k)*(x(14)^eta_t_l)*(x(15)^(1-eta_t_k-eta_t_l));
     -x(9) + (1+mu_nt)*(1+taoc)*((ig/delta_til)^(-eta_nt_g))*A_nt*(x(6)^eta_nt_k)*(x(14)^eta_nt_l)*(x(15)^(1-eta_nt_k-eta_nt_l));
     -x(23) + ((1+mu_w)/(1-taow))*(x(32)^sigma_l)*(((1-h)*x(5))^sigma_c);
     -x(24) + phi*((x(23)/x(14))^(-(1+a)/a))*x(25);
     -x(2) + (1-phi)*((x(1)/x(14))^(-(1+a)/a))*x(25);
     -x(25) + x(17) + x(18);
     -1 + phi*((x(23)/x(14))^(-1/a)) + (1-phi)*((x(1)/x(14))^(-1/a));
     -x(26) + x(19) + x(20);
     -x(8) + x(4) + x(21) + ig + g;
     -(1/(beta*r_star)) + exp(-theta*x(22)*x(27)*(1/x(8)));
     -(1-beta)*x(22)*x(27) + (1/(1+taoc_star))*x(22)*pt_star*x(26) - (1/(1+taoc))*x(11)*x(13);
     -x(28) + (1 - beta)*x(29);
     -x(28)+(taok/(1+taoc))*x(8)+(taoc/(1+taoc))*(x(5)+x(3)+x(36))+(taow-taok)*x(14)*x(25)+(taok/(1+taoc))*x(22)*x(11)*x(13)+(1/(1+taoc_star))*taox*x(22)*pt_star*x(26)+(taox-taok*(1+taox))*(1/(1+taoc_star))*x(22)*pt_star*x(26)+tl-(1/(1+taoc))*g-par_pop*wm-par_pop2*x(37)-(1/(1+taoc))*ig-(1-taow)*x(33)*x(35);
     -x(31)*x(30) + x(8) + x(33)*x(35) + (1/(1+taoc))*x(11)*x(13) - x(15)*x(26);
     -x(31) + 1 * x(11)^((1/(1+taoc))*x(11)*x(13)/(x(31)*x(30)))*x(15)^(-x(15)*x(26)/(x(31)*x(30))); 
     -x(32) + x(24) + x(33);
     -x(34) + g + x(35)*x(33);
     -x(35) + uwg_ss_i* x(23);
     -x(33) + lg_llt_ss_i*x(32);
     -x(36) + x(37)*par_pop2 ;
     -x(37) + uwt_ss_i*wm ];
