forked from dgevans/Golosov-Sargent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunSimulations.m
251 lines (222 loc) · 7.81 KB
/
RunSimulations.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
function [sHist,gHist,u2btildHist,RHist,TauHist,YHist,TransHist,...
btildHist,c1Hist,c2Hist,l1Hist,l2Hist,IntHist,...
IncomeFromAssets_Agent1Hist,AfterTaxWageIncome_Agent1Hist,...
AfterTaxWageIncome_Agent2Hist,GShockDiffHist,TransDiffHist,...
LaborTaxAgent1DiffHist,LaborTaxAgent2DiffHist,DebtDiffHist,...
GiniCoeffHist]=RunSimulations(CoeffFileName,btild0,c10guess,c20guess,NumSim,Para,rHist0)
% This function plots the similation for NumSim periods starting brom
% btild0 and using coeff from endIter. If existing draw of s-shocks are to
% be used..use the argument sHist0
if nargin==7
flagUseExistingShocks='yes';
disp('Using existing shocks')
else
flagUseExistingShocks='no';
disp('yes')
end
close all;
olddatapath=Para.datapath;
oldtexpath=Para.texpath;
oldplotpath=Para.plotpath;
plotpath=oldplotpath;
datapath=olddatapath;
load(CoeffFileName)
disp('Govt Exp')
g=Para.g;
n1=Para.n1;
n2=Para.n2;
alpha_1=Para.alpha_1;
alpha_2=Para.alpha_2;
%disp('Govt Exp')
%g=Para.g
theta_1=Para.theta_1;
theta_2=Para.theta_2;
psi=Para.psi;
beta=Para.beta;
% SOLVE THE T-0 PROBLEM given btild(-1)
btild_1=btild0;
disp('Computed V...Now solving V0(btild_1) where btild_1 is')
disp(btild_1)
% c1 and c2 solve
options=optimset('Display','off');
[x,~,exitflagv0,~,~] = fminunc(@(x) getValue0(x, btild_1,1,Para,c,V),[ c10guess c20guess],options);
if ~(exitflagv0==1)
[x,~,exitflagv0,~,~] = fminunc(@(x) getValue0(x, btild_1,1,Para,c,V),[ 1 1/Para.RMax],options);
end
if ~(exitflagv0==1)
disp('Optimization failed for V0 once ..trying with fmincon')
opts = optimset('Algorithm', 'interior-point', 'Display','off', ...
'GradObj','off','GradConstr','off',...
'MaxIter',1000, ...
'TolX', Para.ctol/10, 'TolFun', Para.ctol, 'TolCon', Para.ctol,'MaxTime',200);
lb=[0.001 0.001];
ub=[10 10];
[x,fval,exitflagv0,output,lambda] =fmincon(@(x) getValue0(x, btild_1,1,Para,c,V),[ x ],[],[],[],[],lb,ub,[],opts);
%[x,~,exitflagv0,output,lambda] =ktrlink(@(x) getValue0(x, btild_1,1,Para,c,V),[ c10guess c20guess],[],[],[],[],lb,ub,[],opts);
end
c10 = x(1);
c20 = x(2);
R0=c10/c20;
TotalResources=(c10*n1+c20*n2+g(1));
FF=R0*theta_2/theta_1;
DenL2=n1*theta_1*FF+theta_2*n2;
l20=(TotalResources-n1*theta_1+n1*theta_1*FF)/(DenL2);
l10= 1-FF*(1-l20);
if theta_2==0
l20=0;
l10=TotalResources/theta_1;
end
BracketTerm=l20/(1-l20)-(l10/(1-l10))*R0;
u2btildprime0=(((1-psi)/(psi))*BracketTerm+btild_1/(beta*psi)+R0-1)*psi;
btildprime0=u2btildprime0/(c20^-1*psi) ;
Rprime0=c20^(-1)/c10^(-1);
% RUN SIMULATION
gHist=zeros(NumSim,1);
u2btildHist=zeros(NumSim,1);
btildHist=zeros(NumSim,1);
RHist=zeros(NumSim,1);
btildHist(1)=btild_1;
TauHist=zeros(NumSim,1);
YHist=zeros(NumSim,1);
TransHist=zeros(NumSim,1);
GMul=zeros(NumSim,1);
c1Hist=zeros(NumSim,1);
c2Hist=zeros(NumSim,1);
l1Hist=zeros(NumSim,1);
l2Hist=zeros(NumSim,1);
sHist=zeros(NumSim,1);
GiniCoeffHist=zeros(NumSim,1);
IntHist=zeros(NumSim-1,1);
IncomeFromAssets_Agent1Hist=zeros(NumSim-1,1);
AfterTaxWageIncome_Agent1Hist=zeros(NumSim,1);
AfterTaxWageIncome_Agent2Hist=zeros(NumSim,1);
GShockDiffHist=zeros(NumSim-1,1);
TransDiffHist=zeros(NumSim-1,1);
LaborTaxAgent1DiffHist=zeros(NumSim-1,1);
LaborTaxAgent2DiffHist=zeros(NumSim-1,1);
DebtDiffHist=zeros(NumSim-1,1);
% INITIALIZE - t=0
u2btildHist(1)=u2btildprime0;
ul20=(1-psi)/(1-l20);
ul10=(1-psi)/(1-l10);
uc20=psi/c20;
uc10=psi/c10;
c1Hist(1)=c10;
c2Hist(1)=c20;
l1Hist(1)=l10;
l2Hist(1)=l20;
btildHist(1)=btildprime0;
TauHist(1)=1-(ul10/(theta_1*uc10));
TransHist(1)=c20-l20*ul20/uc20;
RHist(1)=Rprime0;
YHist(1)=n1*c10+n2*c20+g(1);
sHist(1)=1;
gHist(1)=g(sHist(1));
AfterTaxWageIncome_Agent1Hist(1)=l10*ul10/uc10;
AfterTaxWageIncome_Agent2Hist(1)=l20*ul10/uc20;
tic
for i=1:NumSim-1
if mod(i,500)==0
disp('Running Simulation, t=')
disp(i)
toc
tic
end
% ------STATE (t) - x,R,s_ ------------------------------------------
u2btild=u2btildHist(i);
R=RHist(i);
s_=sHist(i);
% ----SOLVE THE BELLMAN EQUATION ------------------------------------
[PolicyRulesInit]=GetInitialApproxPolicy([u2btild R s_] ,x_state,PolicyRulesStore);
[PolicyRules, ~,exitflag,~]=CheckGradNAG(u2btild,R,s_,c,V,PolicyRulesInit,Para,0);
%---------------------------------------------------------------------------
% GET THE POLICY RULES -
%PolicyRules=[c1_1 c1_2 c2_1 c2_2 l1(1) l1(2) l2(1) l2(2) btildprime c2_1^(-1)/c1_1^(-1) c2_2^(-1)/c1_2^(-1) u2btildprime(1) u2btildprime(2)]
c1=PolicyRules(1:2);
c2=PolicyRules(3:4);
l1=PolicyRules(5:6);
l2=PolicyRules(7:8);
ul2=(1-psi)./(1-l2);
uc2=psi./c2;
ul1=(1-psi)./(1-l1);
uc1=psi./c1;
Rprime=PolicyRules(end-3:end-2);
% x' - u_c_2* btildprime
u2btildprime=PolicyRules(end-1:end);
% btildprime - x'/u_c2
btildprime=PolicyRules(9:10);
% Int-Rates
IntNum=psi/c2Hist(i); %marginal utility of consumption (Agent 2) in s_
DenNum=beta*sum(Para.P(sHist(i),:).*uc2); % expected marginal utility of consumption (Agent 2)
IntHist(i)=IntNum/DenNum;
% TAU - From the WAGE optimality of Agent 2
Tau=1-(ul1./(theta_1.*uc1));
% OUTPUT
y(1)=c1(1)*n1+c2(1)*n2+g(1);
y(2)=c1(2)*n1+c2(2)*n2+g(2);
% TRANSFERS
% These are transfers computed on the assumption that Agent 2 cannot
% borrow and lend. The transfers are the difference between his
% consumption and after tax earning (l . U_l/U_c)
Trans=c2-l2.*ul2./uc2;
% G MULTIPLIER - Computed using (yh-yl)/(gh-gl)
GMul(i)=(y(2)-y(1))/(g(2)-g(1));
% Income
AfterTaxWageIncome_Agent2=l2.*ul2./uc2+Trans;
AfterTaxWageIncome_Agent1=l1.*ul1./uc1+Trans;
% Gini Coeff
GiniCoeff=(AfterTaxWageIncome_Agent2 +2*AfterTaxWageIncome_Agent1)./(AfterTaxWageIncome_Agent2+AfterTaxWageIncome_Agent1)-3/2;
ExitFlag(i)=exitflag;
% DRAW THE s' ~ P(s,:) if flagUseExistingShocks is set to no
if strcmpi(flagUseExistingShocks,'yes')
if rHist0(i+1) < Para.P(sHist(i),1)
sHist(i+1)=1;
else
sHist(i+1)=2;
end
else
if rand < Para.P(sHist(i),1)
sHist(i+1)=1;
else
sHist(i+1)=2;
end
end
% UPDATE THE SIMULATION HISTORY
RHist(i+1)=Rprime(sHist(i+1));
u2btildHist(i+1)=u2btildprime(sHist(i+1)) ;
btildHist(i+1)=btildprime(sHist(i+1)) ;
TauHist(i+1)=Tau(sHist(i+1));
YHist(i+1)=y(sHist(i+1));
TransHist(i+1)=Trans(sHist(i+1));
c1Hist(i+1)=c1(sHist(i+1));
c2Hist(i+1)=c2(sHist(i+1));
l1Hist(i+1)=l1(sHist(i+1));
l2Hist(i+1)=l2(sHist(i+1));
IncomeFromAssets_Agent1Hist(i)=-btildHist(i).*(IntHist(i)-1);
AfterTaxWageIncome_Agent1Hist(i+1)=AfterTaxWageIncome_Agent1(sHist(i+1));
AfterTaxWageIncome_Agent2Hist(i+1)=AfterTaxWageIncome_Agent2(sHist(i+1));
gHist(i+1)=g(sHist(i+1));
% Diff in GBC
% diff in g_shock
GShockDiffHist(i)=g(2)-g(1);
% diff in trasnfers
TransDiffHist(i)=(Trans(2)-Trans(1));
% diff in labortax agent1
LaborTaxAgent1DiffHist(i)=theta_1*l1(2)*Tau(2)*n1 - theta_1*l1(1)*Tau(1)*n1;
% diff in labortax agent2
LaborTaxAgent2DiffHist(i)=theta_2*l2(2)*Tau(2)*n2 - theta_2*l2(1)*Tau(1)*n2;
% diff in borrowing
DebtDiffHist(i)=n1*(btildprime(2)-btildprime(1));
GiniCoeffHist(i+1)=GiniCoeff(sHist(i+1));
% if exitflag==1
% RHist(n)=Rprime(sHist(i+1));
% u2btildHist(n)=u2btildprime(sHist(i+1)) ;
% n=n+1;
% end
end
end
% c1Hist(3:5)-btildHist(3:5)-AfterTaxWageIncome_Agent1Hist(3:5)-IncomeFromAssets_Agent1Hist(2:4)
% c2Hist(3:5)-AfterTaxWageIncome_Agent2Hist(3:5)
% YHist(3:5)-n1*c1Hist(3:5)-n2*c2Hist(3:5)-gHist(3:5)
% YHist(3:5)-n1*l1Hist(3:5)*theta_1-n2*l2Hist(3:5)*theta_2
% gHist(3:5)+n1*btildHist(3:5)+TransHist(3:5)-TauHist(3:5).*(n1*theta_1*l1Hist(3:5)+n2*theta_2*l2Hist(3:5))-n1*btildHist(2:4).*IntHist(2:4)