% Dynare code for solving the model from the paper 'Uncertainty Shocks Are
% Aggregate Demand Shocks'.


% Qazi Haque
% The University of Adelaide
% September 2014


%----------------------------------------------------------------
% 1. Defining variables
%----------------------------------------------------------------

var Z sigma_z C lambda pie m q_u q_v N u U Y R v q J_F w_N w tou;
varexo e e_sigma;

%----------------------------------------------------------------
% 2. Defining parameters
%----------------------------------------------------------------

parameters beta chi eta alpha mu rho phi kappa b gamma omega pie_ss phi_pie phi_y rho_z mean_sigma_z rho_sigma_z sigma_sigma_z; 

%----------------------------------------------------------------
% 3. Calibration
%----------------------------------------------------------------

% Structural parameters

beta = 0.99;      % Household's discount factor
chi = 0.523;      % Scale of disutility of working
eta = 10;         % Elasticity of substitution between differentiated goods  
alpha = 0.5;      % Share parameter in matching function
mu = 0.65;        % Matching efficiency
rho = 0.10;       % Job seperation rate
phi = 0.25;       % Flow value of unemployment
kappa = 0.14;     % Vacancy cost
b = 0.5;          % Nash barganing weight
gamma = 0.8;      % Real wage rigidity
omega = 112;      % Price adustment cost
pie_ss = 1.005;   % Steady state inflation (or inflation target)
phi_pie = 1.5;    % Taylor-rule coefficient for inflation
phi_y = 0.2;      % Taylor-rule coefficient for output

% Shock parameters

rho_z = 0.9;      % Persistency of first moment shock
mean_sigma_z = 0.01;   % Mean value of volatility of shock
rho_sigma_z = 0.76; % Persistence of uncertainty shock
sigma_sigma_z = 0.67; % Standard deviation of uncertainty shock


%----------------------------------------------------------------
% 4. Model
%----------------------------------------------------------------

model;

% 1. Household's bond Euler equation 

1 = beta*(lambda(+1)/lambda)*(R/pie(+1));

% 2. Marginal Utility of Consumption

lambda = 1/C;

% 3. Retail firms optimal pricing decision

q = ((eta-1)/eta) + (omega/eta)*((pie/pie_ss)*((pie/pie_ss)-1) - ((beta*lambda(+1))/(lambda))*(Y(+1)/Y)*(pie(+1)/pie_ss)*((pie(+1)/pie_ss)-1));

% 4. Matching function

m = mu*u^alpha*v^(1-alpha);

% 5. Job finding rate

q_u = m/u;

% 6. Vacancy filling rate

q_v = m/v;

% 7. Employment Dynamics

N = (1-rho)*N(-1) + m;

% 8. Number of searching workers

u = 1 - (1-rho)*N(-1);

% 9. Unemployment

U = 1 - N;

% 10. Aggregate Production Function

Y = Z*N;

% 11. Taylor rule

R = 1.0101* 1.005*(pie/1.005)^(phi_pie)*(Y/0.94)^(phi_y);

% 12. Aggregate Resourse Constraint

C + kappa*v + (omega/2)*((pie/pie_ss)-1)^2*Y = Y;

% 13. Optimal Vacancy Posting

kappa/q_v = (1-tou)*(q*Z-w) + beta*(lambda(+1)/lambda)*(1-rho)*(kappa/q_v(+1));

% 14. Firm Value

J_F = kappa/q_v;

% 15. Nash bargaining wage

(b/(1-b))*J_F = w_N - phi - (chi/lambda) + (1-rho)*beta*(lambda(+1)/lambda)*(1-q_u(+1))*(b/(1-b))*J_F(+1);

% 16. Actual real wage (with real wage rigidity)

w = w(-1)^gamma*w_N^(1-gamma);

% 17. Dividend tax rate

tou*(q*Z-w)*N = phi*(1-N);

% 18. Stochastic process for technology shock

log(Z) = rho_z*log(Z(-1)) + sigma_z*e;

% 19. Stochastic process for second moment shock

log(sigma_z) = (1-rho_sigma_z)*log(mean_sigma_z) + rho_sigma_z*log(sigma_z(-1)) + sigma_sigma_z*e_sigma;


end;


initval;

Z = 1;
sigma_z = mean_sigma_z;
C = 0.92;
lambda = 1.085;
pie = 1.005;
m = 0.094;
q_u = 0.605;
q_v = 0.7;
N = 0.94;
u = 0.155;
U = 0.06;
Y = 0.94;
R = 1.01515;
v = 0.134;
q = 0.9;
J_F = 0.2;
w_N = 0.862;
w = w_N;
tou = 0.42;
e = 0;
e_sigma = 0;


end;

shocks;
var e = 1;
var e_sigma = 1;
end;

steady;

options_.pruning=1;
stoch_simul(order = 3, periods = 2096, drop = 2000);


ergo_mean = mean(oo_.endo_simul,2);

ex_ = zeros(20,2);

out = simult_(ergo_mean,oo_.dr,ex_,3);

ex_(1,2) = 1 ;
output = simult_(ergo_mean,oo_.dr,ex_,3);


Unemployment = ((output(11,:)- out(11,:)));
PercentagedeviationUnemployment = (Unemployment./ergo_mean(11))*100;

Firmvalue = ((output(16,:)- out(16,:))./ergo_mean(16))*100;
Matchvalue = 2*Firmvalue;

%real_interest = out(13,:)./out(5,:);
%ergo_mean_realinterestrate = ergo_mean(13)/ergo_mean(5);

%RealInterestRate = real_interest_rate - ergo_mean_realinterestrate;

Inflation = ((output(5,:)- out(5,:))./ergo_mean(5))*100;

NominalInterestRate = ((output(13,:)- out(13,:))./ergo_mean(13))*100;

Consumption = ((output(3,:)- out(3,:))./ergo_mean(3))*100;

Employment = ((output(9,:)- out(9,:))./ergo_mean(9))*100;

GDP = ((output(12,:)- out(12,:))./ergo_mean(12))*100;

RelativePrice = ((output(15,:)- out(15,:))./ergo_mean(13))*100;

Labor_market_tightness = output(14,:)./output(10,:);
Labor_tightness = out(14,:)./out(10,:);
ergo_mean_LaborMarketTightness = ergo_mean(14)/ergo_mean(10);
LaborMarketTightness = ((Labor_market_tightness - Labor_tightness)./ergo_mean_LaborMarketTightness)*100;

Technologyshock = (output(1,:)- out(1,:));
Uncertaintyshock = (output(2,:)- out(2,:));

close all;


figure;
subplot(4,2,1)
plot(PercentagedeviationUnemployment);
legend('Percentage deviation of Unemployment')


subplot(4,2,2)
plot(Inflation);
legend('Inflation')


subplot(4,2,3)
plot(NominalInterestRate);
legend('Nominal interest rate')

subplot(4,2,4)
plot(RelativePrice);
legend('Relative Price')

subplot(4,2,5)
plot(Firmvalue);
legend('Match value')

subplot(4,2,6)
plot(LaborMarketTightness);
legend('Labor Market Tightness')

subplot(4,2,7)
plot(Technologyshock);
legend('Z')


subplot(4,2,8)
plot(Uncertaintyshock);
legend('sigma_z')

