forked from dgevans/Golosov-Sargent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckGradNAG.m
executable file
·228 lines (182 loc) · 7.01 KB
/
CheckGradNAG.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
% Inputs - xInit, state variables - u2btild,,R,s_ coeff, value
% function, para
function [PolicyRules, V_new,exitflag,fvec]=CheckGradNAG(u2bdiff,RR,s,c,VV,xInit,Para,flagOpt)
global V Vcoef R u2btild Par s_ flagCons
%Get the initial guess for the uconstraint problem. With the simplification
%we need only c1_1,c1_2and c2_1
xInit=xInit(1:3);
Para.theta=[Para.theta_1 Para.theta_2];
Para.alpha=[Para.alpha_1 Para.alpha_2];
Par=Para;
u2btild=u2bdiff;
R=RR;
Vcoef{1}=c(1,:)';
Vcoef{2}=c(2,:)';
V=VV;
s_=s;
u2btildLL=Para.u2btildLL;
u2btildUL=Para.u2btildUL;
n1=Para.n1;
n2=Para.n2;
ctol=Para.ctol;
%% Now solve the unconstraint problem FOC using NAG
% use the last solution
warning('off', 'NAG:warning')
[x, fvec,~,ifail]=c05qb('BelObjectiveUncondGradNAGBGP',xInit);
switch ifail
case {0}
exitflag=1;
case {2, 3, 4}
exitflag=-2;
x=xInit;
end
if flagOpt==1
opts = optimset('Algorithm', 'interior-point', 'Display','off','TolX',1e-6);
xoptguess=x;
[x, fvec,exitflag]=ktrlink(@(x) -Value3cont(x) ,xoptguess,[],[],[],[],[],[], [],opts);
exitflag=exitflag+1;
end
%% GET THE Policy Rules
psi= Par.psi;
beta = Par.beta;
P = Par.P;
theta_1 = Par.theta(1);
theta_2 = Par.theta(2);
g = Par.g;
alpha = Par.alpha;
sigma = 1;
c1_1=x(1);
c1_2=x(2);
c2_1=x(3);
%compute components from unconstrained guess
[c2_2 grad_c2_2] = computeC2_2(c1_1,c1_2,c2_1,R,s_,P,sigma);
[l1 l1grad l2 l2grad] = computeL(c1_1,c1_2,c2_1,c2_2,grad_c2_2,...
theta_1,theta_2,g,n1,n2);
[btildprime grad_btildprime] = computeBtildeprime(c1_1,c1_2,c2_1,c2_2,grad_c2_2,l1,l2,l1grad,l2grad,...
u2btild,s_,psi,beta,P);
% x' - definition
u2btildprime=psi*[c2_1^(-1) c2_2^(-1)].*btildprime;
% State next period
X(1,:) = [psi*c2_1^(-1)*btildprime(1),c2_1^(-1)/c1_1^(-1)];%state next period
X(2,:) = [psi*c2_2^(-1)*btildprime(2),c2_2^(-1)/c1_2^(-1)];%state next period
% Compute the guess for the multipliers of the constraint problem
dV_x=[funeval(Vcoef{1},V(1),[u2btild R],[1 0])];
dV_R=[funeval(Vcoef{1},V(1),[u2btild R],[0 1])];
Lambda_I0=-dV_x;
MultiplierGuess=[Lambda_I0 Lambda_I0];
xInit=[c1_1 c1_2 c2_1 u2btildprime(1) u2btildprime(2) MultiplierGuess];
% set flagCons to interior solution
flagCons='ToCheck';
flagConsOld='SolveKKT';
while (strcmpi(flagCons,flagConsOld))==0
flagConsOld=flagCons;
flagCons='Int';
% Check the upper limits
% if upper limit binds for state 1 only
if u2btildprime(1)> u2btildUL && u2btildprime(2)< u2btildUL
flagCons='UL_';
xInit=[c1_1 c1_2 c2_1 (u2btildprime(1)-u2btildUL) u2btildprime(2) MultiplierGuess];
end
% if upper limit binds for state 2 only
if u2btildprime(1) < u2btildUL && u2btildprime(2)>u2btildUL
flagCons='_UL';
xInit=[c1_1 c1_2 c2_1 u2btildprime(1) (u2btildprime(2)-u2btildUL) MultiplierGuess];
end
% if upper limit binds for both the states
if u2btildprime(1)> u2btildUL && u2btildprime(2) > u2btildUL
flagCons='ULUL';
xInit=[c1_1 c1_2 c2_1 (u2btildprime(1)- u2btildUL) (u2btildprime(2) - u2btildUL) MultiplierGuess];
end
% Check the lower limits
% if lower limit binds for state 1 only
if u2btildprime(1)< u2btildLL && u2btildprime(2)> u2btildLL
flagCons='LL_';
xInit=[c1_1 c1_2 c2_1 (u2btildLL-u2btildprime(1)) u2btildprime(2) MultiplierGuess];
end
% if lower limit binds for state 2 only
if u2btildprime(1) > u2btildLL && u2btildprime(2) <u2btildLL
flagCons='_LL';
xInit=[c1_1 c1_2 c2_1 u2btildprime(1) (u2btildLL-u2btildprime(2)) MultiplierGuess];
end
% if lower limit binds for both the states
if u2btildprime(1) < u2btildLL && u2btildprime(2) <u2btildLL
flagCons='LLLL';
xInit=[c1_1 c1_2 c2_1 (u2btildLL-u2btildprime(1)) (u2btildLL-u2btildprime(2)) MultiplierGuess];
end
if ~(strcmpi(flagCons,'Int'))
%% RESOLVE with KKT conditions
warning('off', 'NAG:warning')
[x, fvec,~,ifail]=c05qb('resFOCBGP_alt',xInit);
switch ifail
case {0}
exitflag=1;
case {2, 3, 4}
exitflag=-2;
x=xInit;
end
MuU=zeros(1,2);
MuL=zeros(1,2);
c1_1=x(1);
c1_2=x(2);
c2_1=x(3);
%compute components from solution
[c2_2 grad_c2_2] = computeC2_2(c1_1,c1_2,c2_1,R,s_,P,sigma);
[l1 l1grad l2 l2grad] = computeL(c1_1,c1_2,c2_1,c2_2,grad_c2_2,...
theta_1,theta_2,g,n1,n2);
% update u2btildprime
switch flagCons
case 'LL_'
% lower limit binds for state 1 only
MuL(1)=x(4);
MuL(2)=0;
u2btildprime(1)=u2btildLL;
u2btildprime(2)=x(5);
case '_LL'
% lower limit binds for state 2 only
MuL(1)=0;
MuL(2)=x(5);
u2btildprime(1)=x(4);
u2btildprime(2)=u2btildLL;
case 'LLLL'
% lower limit binds for both the states
MuL(1)=x(4);
MuL(2)=x(5);
u2btildprime(1)=u2btildLL;
u2btildprime(2)=u2btildLL;
case 'UL_'
% upper limit binds for state 1 only
MuU(1)=x(4);
MuU(2)=0;
u2btildprime(1)=u2btildUL;
u2btildprime(2)=x(5);
case '_UL'
% upper limit binds for state 2 only
MuU(1)=0;
MuU(2)=x(5);
u2btildprime(1)=x(4);
u2btildprime(2)=u2btildUL;
case 'ULUL'
% upper limit binds for both the states
MuL(1)=x(4);
MuL(2)=x(5);
u2btildprime(1)=u2btildUL;
u2btildprime(2)=u2btildUL;
end
end
btildprime(1)=u2btildprime(1)/(psi*c2_1^(-1));
btildprime(2)=u2btildprime(2)/(psi*c2_2^(-1));
X(1,:) = [u2btildprime(1),c2_1^(-1)/c1_1^(-1)];%state next period
X(2,:) = [u2btildprime(2),c2_2^(-1)/c1_2^(-1)];%state next period
Lambda=x(11:end);
end
%compute objective
if ~isreal(X)
X=real(X);
end
Vobj = P(s_,1)*(alpha(1)*uBGP(c1_1,l1(1),psi)+alpha(2)*uBGP(c2_1,l2(1),psi)...
+beta*funeval(Vcoef{1},V(1),X(1,:)));
Vobj = Vobj + P(s_,2)*(alpha(1)*uBGP(c1_2,l1(2),psi)+alpha(2)*uBGP(c2_2,l2(2),psi)...
+beta*funeval(Vcoef{2},V(2),X(2,:)));
V_new=Vobj;
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)];
end