-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOLGIntermediaryModel.m
2387 lines (1991 loc) · 99.7 KB
/
OLGIntermediaryModel.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef OLGIntermediaryModel < model.DSGEModel
properties (SetAccess=protected)
NSTEN % number of endogenous state variables: Vfct.Ndim-1
NSTEX % number of exogenous state variables
NSOL % number of solution vars
NV % number of forecasting variables
NADD % number of additional endogenous variables
NCOND % number of conditional expectations of period-ahead variables
Sol_names % NSOLx1 cell array with names of solution variables
% must be in order of functions of Pfct
V_names % NVx1 cell array with names of forecasting variables
% must be in order of functions of Vfct
Sol_baseguess
En_names % NSTENx1 cell array with names of endog state variables
Ex_names % NSTEXx1 cell array with names of exog state variables
Add_names % NADDx1 cell array with names of additional vars
Cond_names %NCONDx1 cell array with names of conditional variables
Params % structure array with parameters
Exogenv % structure array with fields mtrans, pts_perm, pts_all
% specifying exog. Markov processes
Vfct % ApproxFunction object for iteration-relevant functions
Pfct % ApproxFunction object for solution jump variables
Tfct % ApproxFunction object for transition of state variable(s) (optional)
Ffct
Basegrid % base grid
nodes % for Gaussian integration
weights % for Gaussian integration
Addit % Additional Variables
end
methods
% constructor
function obj=OLGIntermediaryModel(params,endogenv,exogenv,vfct,pfct,tfct,ffct,basegrid,nodes,weights,addvar)
% call superclass constructor
[email protected](params,endogenv,exogenv,vfct,pfct,tfct,ffct);
obj.Basegrid=basegrid;
obj.NCOND=length(endogenv.condnames);
obj.Cond_names=reshape(endogenv.condnames,1,obj.NCOND);
obj.nodes=nodes;
obj.weights=weights;
% Add this to get the extra variables, needed to compute the
% PTI constraints.
obj.Addit = addvar;
end
function [nextst,outstr]=calcStateTransition(obj,point,solvec,mode,varargin)
params=obj.Params;
exogenv=obj.Exogenv;
if ~isempty(varargin)
if length(varargin)>1
params=varargin{2};
exogenv=varargin{3};
end
end
% unpack relevant params
theta=params.theta;
nu=params.nu;
tau=params.tau;
Hbar=params.Hbar;
deltaH=params.deltaH;
phi=params.phi;
gamma=params.gamma;
chi=params.chi;
piM=params.piM;
G=params.mu_G;
rental = params.rental;
tax = params.tax;
if isfield(params,'MITshockloss')
MITshockloss = params.MITshockloss;
else
MITshockloss =0;
end
if rental
kappaY = params.kappaY;
end
% compute next period's states
if isempty(varargin)
State_next=obj.evaluateTrans(point);
else
if mode>1
% simulation mode with different Tfct
thistfct=varargin{1};
State_next=thistfct.evaluateAt(point);
else
% solution mode
State_next=varargin{1};
end
end
% extract state variables
exst=point(1);
whatM=point(2);
eI=point(3);
Y=exogenv.pts_perm(exst,1);
sig_epsY=exogenv.pts_perm(exst,2);
sig_epsM=exogenv.pts_perm(exst,3);
% extract solution variables
R=exp(solvec(1));
P=exp(solvec(2));
q=exp(solvec(3));
pY=exp(solvec(4));
pM=exp(solvec(5));
dM=exp(solvec(6));
dY=exp(solvec(7));
cM=exp(solvec(8));
cY=exp(solvec(9));
if rental
hY=solvec(10);
else
hY=exp(solvec(10));
end
Itilde=exp(solvec(13));
RebMort=solvec(17);
% Market clearing
DI = dY + dM;
hM = Hbar - hY;
if rental
sY = (Hbar)/(1+(cM/cY));
sM = Hbar - sY;
rhoM = theta*cM/((1-theta)*sM);
rhoY = rhoM;
else
sM = hM;
sY = hY;
rhoM = theta*cM/((1-theta)*sM);
rhoY = theta*cY/((1-theta)*sY);
end
% Wealth distribution
Yhat = Y;
I = (1-Itilde)/chi;
xI = tau*eI - I;
WMO = whatM + q+xI + pM;
Phi = phi^(1/gamma)/(1+phi^(1/gamma));
% Values for the old agents
WO = piM * WMO;
BO = Phi * WO;
cO = WO - BO;
% Change: the taxation and the demand for assets affects the
% wealth of the agents.
whatY = (1-deltaH)*P + BO + RebMort + Yhat - whatM - eI - MITshockloss;
WY = whatY + pY;
WM = WMO - WO;
fullendogvars=[whatM,eI];
% Budget Constraints
QYHH = cY + rhoY*sY + (P-rhoY)*hY + dY/R + pY - WY;
QMHH = cM + rhoM*sM + (P-rhoM)*hM + q + pM + dM/R - WM;
SM = (P-rhoM)*hM + q + pM + dM/R - QMHH; % implied savings
SY = (P-rhoY)*hY + pY + dY/R - QYHH; % implied savings
% tax adjustment for mortgage rate
QY = QYHH/(1+tax);
QM = QMHH/(1+tax);
% Find the Rebate:
%point_state = [exst,mobj.whatM,eI];
Rebate_list=obj.evaluateForec(point)';
Rebate_today = Rebate_list(exst);
Y_star_today = Y + BO + RebMort + Rebate_today;
if mode>0
% simulation, mode contains number of next period's state
exst=mode;
nst=exogenv.exnpt;
cind=exogenv.pts_all(exst,end);
whatMnext=State_next(exst,1);
eInext=State_next(nst+exst,1);
nextst=[cind,whatMnext,eInext];
else
% solution mode, compute next period's state variables for all
% possible Markov states
cind=exogenv.pts_all(:,end);
nst=exogenv.exnpt;
whatMnext=State_next(1:nst,1);
eInext=State_next(nst+(1:nst),1);
% matrix of next period states
nextst=[cind,whatMnext,eInext];
end
addvars=struct('WY',WY,...
'WM',WM,...
'DI',DI,...
'hM',hM,...
'sM',sM,...
'rhoM',rhoM,...
'sY',sY,...
'rhoY',rhoY,...
'QYHH',QYHH,...
'QMHH',QMHH,...
'QY',QY,...
'QM',QM,...
'I',I,...
'xI',xI,...
'SM', SM, ...
'SY', SY, ...
'cO',cO,...
'BO',BO,...
'Y',Y,...
'end_yieldM',obj.Addit.end_yieldM,...
'end_yieldY',obj.Addit.end_yieldY,...
'Rebate_today',Rebate_today,...
'Y_star_today',Y_star_today);
outstr=struct;
outstr.addvars=addvars;
outstr.exstvec=[Y;sig_epsY;sig_epsM];
outstr.fullendogvars=fullendogvars;
end
function [fx,J,V]=calcEquations(obj,exst,nextst,solvec,instr,mode,checkTrans,varargin)
J =[]; % compatibility with older version
params=obj.Params;
% unpack params
beta=params.beta;
betaO=params.betaO;
gamma=params.gamma;
theta=params.theta;
psiY=params.psiY;
psiM=params.psiM;
chi=params.chi;
tau=params.tau;
nu=params.nu;
lambdaY=params.lambdaY;
lambdaM=params.lambdaM;
xi=params.xi;
xiDWL=params.xiDWL;
piM=params.piM;
piY=params.piY;
ebarY=params.ebarY;
ebarM=params.ebarM;
deltaH=params.deltaH;
Hbar=params.Hbar;
G=params.mu_G;
phi=params.phi;
delta_eta=params.delta_eta;
zeta=params.zeta;
alpha=params.alpha;
tax=params.tax;
hardconstr_LTV = params.LTV;
hardconstr_PTI = params.PTI;
%hardconstr=params.hardconstr;
rental = params.rental;
if rental
kappaY = params.kappaY;
end
%%%%%%%%%%%%%%%%%%%%%%
% Parameters LTV,PTI %
%%%%%%%%%%%%%%%%%%%%%%
thetaM_LTV=params.thetaM_LTV;
thetaY_LTV=params.thetaY_LTV;
% Change the value of theta PTI here!
thetaM_PTI=params.thetaM_PTI;
thetaY_PTI=params.thetaY_PTI;
% extract endogeous variables
R=exp(solvec(1));
P=exp(solvec(2));
q=exp(solvec(3));
pY=exp(solvec(4));
pM=exp(solvec(5));
dM=exp(solvec(6));
dY=exp(solvec(7));
if rental
hY=solvec(10);
else
hY=exp(solvec(10));
end
mY=exp(solvec(11));
mM=exp(solvec(12));
vM=exp(solvec(14));
vY=exp(solvec(15));
muI=solvec(16);
RebMort=solvec(17);
var_number = 17;
if rental
var_number = var_number + 1;
muI_rentY=solvec(var_number);
end
if hardconstr_LTV || hardconstr_PTI
var_number = var_number + 1;
if hardconstr_LTV && hardconstr_PTI
lamM_LTV=solvec(var_number);
lamY_LTV=solvec(var_number+1);
lamM_PTI=solvec(var_number+2);
lamY_PTI=solvec(var_number+3);
var_number = var_number + 3;
elseif hardconstr_LTV && not(hardconstr_PTI)
lamM_LTV=solvec(var_number);
lamY_LTV=solvec(var_number+1);
var_number = var_number + 1;
elseif not(hardconstr_LTV) && hardconstr_PTI
lamM_PTI=solvec(var_number);
lamY_PTI=solvec(var_number+1);
var_number = var_number + 1;
end
end
exnpt=obj.Exogenv.exnpt;
if checkTrans
WMtrans_check = solvec(var_number+(1:exnpt));
eItrans_check = solvec(var_number+exnpt+(1:exnpt));
end
% allocate result
fx=zeros(obj.NSOL+checkTrans*2*exnpt,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Multiplier transformations %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Financial Intermediaries
muIplus=max(0,muI)^3;
muIminus=max(0,-muI)^3;
% Non-Negativity const. housing of the young agents.
if rental
muIplus_rentY=max(0,muI_rentY)^3;
muIminus_rentY=max(0,-muI_rentY)^3;
end
if hardconstr_LTV || hardconstr_PTI
if hardconstr_LTV && hardconstr_PTI
% LTV
lamMplus_LTV=max(0,lamM_LTV)^3;
lamMminus_LTV=max(0,-lamM_LTV)^3;
lamYplus_LTV=max(0,lamY_LTV)^3;
lamYminus_LTV=max(0,-lamY_LTV)^3;
% PTI
lamMplus_PTI=max(0,lamM_PTI)^3;
lamMminus_PTI=max(0,-lamM_PTI)^3;
lamYplus_PTI=max(0,lamY_PTI)^3;
lamYminus_PTI=max(0,-lamY_PTI)^3;
elseif hardconstr_LTV && not(hardconstr_PTI)
% LTV
lamMplus_LTV=max(0,lamM_LTV)^3;
lamMminus_LTV=max(0,-lamM_LTV)^3;
lamYplus_LTV=max(0,lamY_LTV)^3;
lamYminus_LTV=max(0,-lamY_LTV)^3;
% PTI
lamMplus_PTI=0;
lamYplus_PTI=0;
elseif not(hardconstr_LTV) && hardconstr_PTI
% LTV
lamMplus_LTV=0;
lamYplus_LTV=0;
% PTI
lamMplus_PTI=max(0,lamM_PTI)^3;
lamMminus_PTI=max(0,-lamM_PTI)^3;
lamYplus_PTI=max(0,lamY_PTI)^3;
lamYminus_PTI=max(0,-lamY_PTI)^3;
end
else
%LTV
lamMplus_LTV=0;
lamYplus_LTV=0;
% PTI
lamMplus_PTI=0;
lamYplus_PTI=0;
end
% extract some other state-dependent variables
envec=instr.addvars;
DI=envec.DI;
rhoM=envec.rhoM;
rhoY=envec.rhoY;
QYHH=envec.QYHH;
QMHH=envec.QMHH;
QY=envec.QY;
QM=envec.QM;
I=envec.I;
xI=envec.xI;
SM=envec.SM;
SY=envec.SY;
hM=envec.hM;
eI=instr.fullendogvars(2);
BO=envec.BO;
WM=envec.WM;
WY=envec.WY;
Y_star_today = envec.Y_star_today;
% Variables needed for the DTI constraints:
ym = (1-nu)*Y_star_today;
yy = nu*Y_star_today;
% probabilities and states to compute expectation terms
prnext=obj.Exogenv.mtrans(exst,:);
Ynext=obj.Exogenv.pts_perm(:,1);
sigeps_nextY=obj.Exogenv.pts_perm(:,2);
sigeps_nextM=obj.Exogenv.pts_perm(:,3);
mueps_nextY=-.5*sigeps_nextY.^2;
mueps_nextM=-.5*sigeps_nextM.^2;
% projection evaluation
if nargin>=8 && (~checkTrans)
% 8 or more arguments were passed.
% Means expectations have already been computed (fast
% solution mode)
Pol_next = varargin{1};
if mode==3
% credit surface computation
Yvar = varargin{2};
Mvar = varargin{3};
end
else
% Nothing passed in ((conditional moments and EE errors in
% simulation) or consistency equations
if checkTrans
nextst(:,2) = WMtrans_check;
nextst(:,3) = eItrans_check;
end
Pol_next = obj.evaluateVal(nextst)';
if size(Pol_next,1)==1
prnext=1;
end
forec = obj.evaluateForec([exst,instr.fullendogvars]);
Pol_next=[Pol_next,forec];
end
q_next = Pol_next(:,1);
I_next = Pol_next(:,2);
vM_next = Pol_next(:,3);
vY_next = Pol_next(:,4);
P_next = Pol_next(:,5);
pY_next = Pol_next(:,6);
pM_next = Pol_next(:,7);
BO_next = Pol_next(:,8);
R_next = Pol_next(:,9);
RebMort_next = Pol_next(:,10);
Reb_next = Pol_next(:,11);
xI_next=tau*nextst(:,3)-I_next;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HERE I start using the equations %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Mortgage payoff and default for young
Ystar_next = Ynext + Reb_next + BO_next + RebMort_next;
ystar_next = (1-nu)*Ystar_next;
wYYdef_next = dY./G + nu*Ystar_next + pY_next;
% Change: eliminate BO_next
wYMdef_next = dY./G + (delta_eta*ystar_next)/piY;
epsbarYY_next = (mY/G - lambdaY*wYYdef_next)./((1-deltaH)*P_next*hY);
epsbarYM_next = (mY/G - lambdaY*wYMdef_next)./((1-deltaH)*P_next*hY);
% First for the individuals that stay young.
[Feps_nextYY,feps_nextYY,FepsplusYY,FepsminusYY,...
RFepsVFYY,RFepsFOC1YY,RFepsFOC2YY]= OLGIntermediaryModel.integEps(epsbarYY_next,mueps_nextY,sigeps_nextY,gamma,...
G*(1-deltaH)*P_next*hY/SY,(G*wYYdef_next-mY)/SY,obj.nodes,obj.weights,0);
[Feps_nextYM,feps_nextYM,FepsplusYM,FepsminusYM,...
RFepsVFYM,RFepsFOC1YM,RFepsFOC2YM]= OLGIntermediaryModel.integEps(epsbarYM_next,mueps_nextY,sigeps_nextY,gamma,...
G*(1-deltaH)*P_next*hY/SY,(G*wYMdef_next-mY)/SY,obj.nodes,obj.weights,0);
%%%%%%%%%%%%%%%%%%%%%%%
% Payoffs Calculations%
%%%%%%%%%%%%%%%%%%%%%%%
% YOUNG
Def_nextY = (1-piY)*Feps_nextYY + piY*Feps_nextYM;
FepsminusY = (1-piY)*FepsminusYY + piY*FepsminusYM;
Payoff_nextY = (1-Def_nextY).*mY./G + (1-deltaH)*P_next*hY.*(alpha + (1-xi)*FepsminusY);
wMdef_next = dM./G + q_next+xI_next + ((1-delta_eta)*ystar_next+pM_next);
epsbarM_next = (mM/G - lambdaM*wMdef_next)./((1-deltaH)*P_next*hM);
[Feps_nextM,feps_nextM,FepsplusM,FepsminusM,...
RFepsVFM,RFepsFOC1M,RFepsFOC2M]= OLGIntermediaryModel.integEps(epsbarM_next,mueps_nextM,sigeps_nextM,gamma,...
G*(1-deltaH)*P_next*hM/SM,(G*wMdef_next-mM)/SM,obj.nodes,obj.weights,0);
Payoff_nextM = (1-Feps_nextM).*mM./G + (1-deltaH)*P_next*hM.*(alpha + (1-xi)*FepsminusM);
%%%%%%%%%%%%%%%%%%
% Financial Int. %
%%%%%%%%%%%%%%%%%%
eInext = Payoff_nextY + Payoff_nextM - DI./G;
[min_eInext,minstate] = min(eInext);
min_PayoffY=Payoff_nextY(minstate);
min_PayoffM=Payoff_nextM(minstate);
%%%%%%%%%%%%%%%%%%%%%%
% Compute Middle SDF %
%%%%%%%%%%%%%%%%%%%%%%
Phi = (1/(1+ phi^(1/gamma)))^(1-gamma) + phi*(phi^(1/gamma)/(1+ phi^(1/gamma)))^(1-gamma) * betaO;
RMdef_next = G*(1-lambdaM)*wMdef_next/SM;
ThetaZ = ((1-theta)^(1-theta) * (theta/rhoM)^theta)^(1-gamma);
AZ = psiM*(dM/SM)^(1-gamma) + ...
beta*prnext*((RFepsVFM + Feps_nextM.*RMdef_next.^(1-gamma)) .* (piM*Phi + (1-piM)*vM_next));
BZ = AZ^(1/gamma)/( AZ^(1/gamma) + ThetaZ^(1/gamma));
MMnodef_m = beta * BZ^(-gamma)/vM * RFepsFOC1M .* (piM*Phi + (1-piM)*vM_next);
MMnodef_h = beta * BZ^(-gamma)/vM * RFepsFOC2M .* (piM*Phi + (1-piM)*vM_next);
MMdef = beta * BZ^(-gamma)/vM * RMdef_next.^(-gamma) .* (piM*Phi + (1-piM)*vM_next);
SDFM = MMnodef_m + (1-lambdaM)*Feps_nextM.*MMdef;
% With this we can compute the SDF of the financial
% intermediares.
SDFI = SDFM.*(tau + (1-tau)./(1-chi*I_next))*(1-chi*I);
%%%%%%%%%%%%%%%%%%%%%%
% Derivatives of q_M %
%%%%%%%%%%%%%%%%%%%%%%
QMdiff_term = feps_nextM.*(mM - G*(1-xi)*(1-deltaH)*hM*P_next.*epsbarM_next);
QMm_term = QMdiff_term./(G*(1-deltaH)*P_next*hM) + (1-deltaH)*P_next*hM/mM .*(alpha + (1-xi)*FepsminusM);
QMh_term = G*(1-deltaH)*P_next.*(alpha + (1-xi)*FepsminusM + QMdiff_term.*epsbarM_next./(G*(1-deltaH)*P_next*hM));
QMd_term = lambdaM*QMdiff_term./(G*(1-deltaH)*P_next*hM);
QMb_term = QMd_term.*(q_next+xI_next)*G;
QMeta_term = QMd_term.*((1-delta_eta)*ystar_next+pM_next)*G;
QMh = (1+tax)*((1-ebarM)*muIplus*QMh_term(minstate) + prnext*(SDFI.*QMh_term))/(1+zeta);
QMm = (1+tax)*((1-ebarM)*muIplus*QMm_term(minstate) + prnext*(SDFI.*QMm_term))/(1+zeta);
QMd = (1+tax)*((1-ebarM)*muIplus*QMd_term(minstate) + prnext*(SDFI.*QMd_term))/(1+zeta);
QMb = (1+tax)*((1-ebarM)*muIplus*QMb_term(minstate) + prnext*(SDFI.*QMb_term))/(1+zeta);
QMeta = (1+tax)*((1-ebarM)*muIplus*QMeta_term(minstate) + prnext*(SDFI.*QMeta_term))/(1+zeta);
%%%%%%%%%%%%%%%%%%%%%%
% Compute Young SDF %
%%%%%%%%%%%%%%%%%%%%%%
RYYdef_next = G*(1-lambdaY)*wYYdef_next/SY;
RYMdef_next = G*(1-lambdaY)*wYMdef_next/SY;
ThetaY = ((1-theta)^(1-theta) * (theta/rhoY)^theta)^(1-gamma);
AY = psiY*(dY/SY)^(1-gamma) ...
+ beta*(1-piY)*prnext*((RFepsVFYY + Feps_nextYY.*RYYdef_next.^(1-gamma)) .* vY_next) ...
+ beta*piY*prnext*((RFepsVFYM + Feps_nextYM.*RYMdef_next.^(1-gamma)) .* vM_next);
BY = AY^(1/gamma)/( AY^(1/gamma) + ThetaY^(1/gamma));
MYnodef_m = beta * BY^(-gamma)/vY * ((1-piY)*RFepsFOC1YY.*vY_next + piY*RFepsFOC1YM.*vM_next);
MYnodef_h = beta * BY^(-gamma)/vY * ((1-piY)*RFepsFOC2YY.*vY_next + piY*RFepsFOC2YM.*vM_next);
MYdef = beta * BY^(-gamma)/vY * ((1-piY)*Feps_nextYY.*RYYdef_next.^(-gamma).*vY_next + piY*Feps_nextYM.*RYMdef_next.^(-gamma).*vM_next );
SDFY = MYnodef_m + (1-lambdaY)*MYdef;
MYY = beta * BY^(-gamma)/vY * (1-piY)*(RFepsFOC1YY + (1-lambdaY)*Feps_nextYY.*RYYdef_next.^(-gamma)) .* vY_next;
MYM = beta * BY^(-gamma)/vY * piY*(RFepsFOC1YM + (1-lambdaY)*Feps_nextYM.*RYMdef_next.^(-gamma)) .* vM_next;
%%%%%%%%%%%%%%%%%%%%%%
% Derivatives of q_Y %
%%%%%%%%%%%%%%%%%%%%%%
epshatYY = mY - G*(1-xi)*(1-deltaH)*hY*P_next.*epsbarYY_next;
epshatYM = mY - G*(1-xi)*(1-deltaH)*hY*P_next.*epsbarYM_next;
fhat_nextY = (piY*feps_nextYM.*epshatYM + (1-piY)*feps_nextYY.*epshatYY)./(G*(1-deltaH)*P_next*hY);
QYm_term = fhat_nextY + (1-deltaH)*P_next*hY/mY .* (alpha + (1-xi)*FepsminusY);
QYd_term = lambdaY*fhat_nextY;
fhh_nextY = (piY*feps_nextYM.*epshatYM.*epsbarYM_next ...
+ (1-piY)*feps_nextYY.*epshatYY.*epsbarYY_next)./(G*(1-deltaH)*P_next*hY);
QYh_term = G*(1-deltaH)*P_next.*(alpha + FepsminusY*(1-xi)+ fhh_nextY);
fhhh_nextY = (piY*((delta_eta*ystar_next)/piY).*feps_nextYM.*epshatYM ...
+ (1-piY)*feps_nextYY.*(nu*Ystar_next + pY_next).*epshatYY)./(G*(1-deltaH)*P_next*hY);
QYeta_term = G*lambdaY*fhhh_nextY;
QYeta = (1+tax)*((1-ebarY)*muIplus*QYeta_term(minstate) + prnext*(SDFI.*QYeta_term))/(1+zeta);
QYm = (1+tax)*((1-ebarY)*muIplus*QYm_term(minstate) + prnext*(SDFI.*QYm_term))/(1+zeta);
QYh = (1+tax)*((1-ebarY)*muIplus*QYh_term(minstate) + prnext*(SDFI.*QYh_term))/(1+zeta);
QYd = (1+tax)*((1-ebarY)*muIplus*QYd_term(minstate) + prnext*(SDFI.*QYd_term))/(1+zeta);
%%%%%%%%%%%%%%%%%%%%%%
% FOC's Middle aged %
%%%%%%%%%%%%%%%%%%%%%%
% Effective returns on housing
Rh_eff_next = G*(1-deltaH)*P_next/(P - rhoM - QMh);
% Effective returns to mortgages
Rm_eff_next = 1/(QMHH/mM - QMm);
% Effective returns to deposits.
Rd_eff = R/(1-R*QMd);
% Effective returns to equity.
Re_eff_next = G*(q_next+xI_next)/(q - QMb);
% Effective returns to endowment.
Reta_eff_next = G*((1-delta_eta)*ystar_next+pM_next)/(pM - QMeta);
% This is the first element of equation (42)
cyieldM = psiM*(dM*BZ/SM)^(-gamma)/vM;
% The FOC written as no-excess return equations.
fx(1) = prnext*(SDFM.*Re_eff_next - MMnodef_h.*Rh_eff_next) ...
- lamMplus_LTV*thetaM_LTV*(BZ^(-gamma)/vM)*P/(P - rhoM - QMh);
fx(2) = Rd_eff*cyieldM + prnext*(SDFM.*(Rd_eff - Re_eff_next));
fx(3) = prnext*(MMnodef_h.*Rh_eff_next - MMnodef_m.*Rm_eff_next) ...
+lamMplus_LTV*(BZ^(-gamma)/vM)*(thetaM_LTV*P/(P - rhoM - QMh) - Rm_eff_next)...
-lamMplus_PTI*(BZ^(-gamma)/vM)*Rm_eff_next;
payoffend_m = ym;
fx(4) = prnext*(SDFM.*(Re_eff_next - Reta_eff_next))...
-lamMplus_PTI*payoffend_m*thetaM_PTI*(BZ^(-gamma)/vM)*(1/(pM - QMeta));
% Defintion of the savings for the middle aged.
fx(5) = SM - BZ*WM;
% This is the defintion of v_m(Z) in the appenix.
fx(6) = vM - ThetaZ*(1-BZ)^(1-gamma) - AZ*BZ^(1-gamma);
%%%%%%%%%%%%%%%%%%%%%%%%
% FOC's Intermdiaries %
%%%%%%%%%%%%%%%%%%%%%%%%
fx(7)=1/R - muIplus/G - prnext*SDFI;
fx(8)=(1+zeta)*QY - (1-ebarY)*muIplus*min_PayoffY - G*prnext*(SDFI.*Payoff_nextY);
fx(9)=(1+zeta)*QM - (1-ebarM)*muIplus*min_PayoffM - G*prnext*(SDFI.*Payoff_nextM);
%%%%%%%%%%%%%%%%%%%%%%%%
% FOC's Young agents %
%%%%%%%%%%%%%%%%%%%%%%%%
% Effective returns on housing
RYh_eff_next = G*(1-deltaH)*P_next/(P - rhoY - QYh);
% Effective returns on mortgages.
RYm_eff_next = 1/(QYHH/mY - QYm);
% Effective returns on deposits (not an expectation!)
RYd_eff = R/(1-R*QYd);
% Effective returns of the endwoment asset if you don't age.
RYYeta_eff_next = G*(nu*Ystar_next + pY_next)/(pY - QYeta);
% Change: eliminate BO_next
% Effective returns of the endwoment asset if you age.
RYMeta_eff_next = G*((delta_eta*ystar_next)/piY)/(pY - QYeta);
% Auxiliary variable: first part of equation (42)
cyieldY = psiY*(dY*BY/SY)^(-gamma)/vY;
% The FOC written as no-excess return equations.
fx(10)= cyieldY*RYd_eff + prnext*(SDFY.*RYd_eff - MYnodef_m.*RYm_eff_next)...
- lamYplus_LTV*(BY^(-gamma)/vY)*RYm_eff_next...
- lamYplus_PTI*(BY^(-gamma)/vY)*RYm_eff_next;
if rental
fx(11)= prnext*(MYnodef_m.*RYm_eff_next - MYnodef_h.*RYh_eff_next) ...
+ lamYplus_LTV*(BY^(-gamma)/vY)*(RYm_eff_next - thetaY_LTV*P/(P - rhoY - QYh))...
+ lamYplus_PTI*(BY^(-gamma)/vY)*RYm_eff_next...
- (muIplus_rentY+(kappaY*(hY)^(-gamma))) * (BY^(-gamma)/vY)*(1/(P - rhoY - QYh));
else
fx(11)= prnext*(MYnodef_m.*RYm_eff_next - MYnodef_h.*RYh_eff_next) ...
+ lamYplus_LTV*(BY^(-gamma)/vY)*(RYm_eff_next - thetaY_LTV*P/(P - rhoY - QYh))...
+ lamYplus_PTI*(BY^(-gamma)/vY)*RYm_eff_next;
end
payoffend_y = yy;
if rental
fx(12)= prnext*(MYnodef_h.*RYh_eff_next - (MYY.*RYYeta_eff_next + MYM.*RYMeta_eff_next)) ...
+ lamYplus_LTV*(BY^(-gamma)/vY)*thetaY_LTV*P/(P - rhoY - QYh)...
- lamYplus_PTI*(BY^(-gamma)/vY)*thetaY_PTI*(payoffend_y/(pY - QYeta))...
+ (muIplus_rentY+(kappaY*(hY)^(-gamma))) * (BY^(-gamma)/vY)*(1/(P - rhoY - QYh));
else
fx(12)= prnext*(MYnodef_h.*RYh_eff_next - (MYY.*RYYeta_eff_next + MYM.*RYMeta_eff_next)) ...
+ lamYplus_LTV*(BY^(-gamma)/vY)*thetaY_LTV*P/(P - rhoY - QYh)...
- lamYplus_PTI*(BY^(-gamma)/vY)*thetaY_PTI*(payoffend_y/(pY - QYeta));
end
% Defintion of the savings for the middle aged.
fx(13)= SY - BY*WY;
% This is the defintion of v_y(Z) in the appenix.
fx(14)= vY - ThetaY*(1-BY)^(1-gamma) - AY*BY^(1-gamma);
% CC for the Intermediary
fx(15)=(1-ebarY)*min_PayoffY+(1-ebarM)*min_PayoffM - DI/G - muIminus;
% Budget Constraint of the Intermediary.
fx(16)= (1+zeta)*(QY + QM) - ((1-tau)*eI + I - chi*I^2/2 + DI/R);
% Rebates
fx(17)= RebMort - ( zeta*(QY+QM) - (QMHH-QM) - (QYHH-QY) );
eq_number = 17;
if rental
eq_number = eq_number+1;
fx(eq_number) = hY - muIminus_rentY;
end
if hardconstr_LTV || hardconstr_PTI
eq_number = eq_number+1;
if hardconstr_LTV && hardconstr_PTI
% LTV and PTI
fx(eq_number)=thetaM_LTV*P*hM - mM-lamMminus_LTV;
fx(eq_number+1)=thetaY_LTV*P*hY - mY-lamYminus_LTV;
fx(eq_number+2)=thetaM_PTI*payoffend_m - mM-lamMminus_PTI;
fx(eq_number+3)=thetaY_PTI*payoffend_y - mY-lamYminus_PTI;
eq_number = eq_number+3;
elseif hardconstr_LTV && not(hardconstr_PTI)
% LTV and PTI
fx(eq_number)=thetaM_LTV*P*hM - mM-lamMminus_LTV;
fx(eq_number+1)=thetaY_LTV*P*hY - mY-lamYminus_LTV;
eq_number = eq_number+1;
elseif not(hardconstr_LTV) && hardconstr_PTI
% LTV and PTI
fx(eq_number)=thetaM_PTI*payoffend_m - mM-lamMminus_PTI;
fx(eq_number+1)=thetaY_PTI*payoffend_y - mY-lamYminus_PTI;
eq_number = eq_number+1;
end
end
%%%%%%%%%%%%%%%
% Transitions %
%%%%%%%%%%%%%%%
% Aggregate income of the young defaulters:
WYYdef_next = (1-piY)*(dY./G + nu*Ystar_next + pY_next);
% Change: eliminate BO_next
WYMdef_next = piY*dY./G + (delta_eta*ystar_next);
% Aggregate income of the middle aged defaulters:
WMdef_next = dM./G + q_next+xI_next + (1-delta_eta)*ystar_next + pM_next;
RebateY = (1-piY)*lambdaY*Feps_nextYY.*WYYdef_next + lambdaY*Feps_nextYM.*WYMdef_next + (xi-xiDWL)*(1-deltaH)*FepsminusY.*P_next*hY;
RebateM = (xi-xiDWL)*(1-deltaH)*FepsminusM.*P_next*hM + lambdaM*Feps_nextM.*WMdef_next;
RebateI = -alpha*P_next*Hbar;
% Change: eliminate BO_next
WYMnext = (delta_eta*ystar_next + piY*dY/G).*(1-lambdaY.*Feps_nextYM)...
+ piY*((1-deltaH)*FepsplusYM.*P_next*hY - (1-Feps_nextYM)*mY/G);
% Total wealth of the middle age and old age people:
WMnext = WYMnext + (1-deltaH)*FepsplusM.*P_next*hM - (1-Feps_nextM)*mM/G + (1-lambdaM*Feps_nextM).*WMdef_next;
% This is the value of the state variable:
%Yhat_next = Ynext - lumptax_next;
WMtrans = WMnext - (q_next+xI_next) - pM_next;
eItrans = eInext;
if checkTrans
fx(eq_number+(1:exnpt)) = WMtrans - WMtrans_check;
fx(eq_number+exnpt+(1:exnpt)) = eItrans - eItrans_check;
end
V=cell(3,1);
% marginal value functions
if mode==1
% Output new values for time iteration
Vnext=zeros(obj.Vfct.Nof,1);
Vnext(1)=q;
Vnext(2)=I;
Vnext(3)=vM;
Vnext(4)=vY;
Vnext(5)=P;
Vnext(6)=pY;
Vnext(7)=pM;
Vnext(8)=BO;
Vnext(9)=R;
Vnext(10)=RebMort;
V{1}=Vnext;
% state transition
V{2}=[WMtrans; eItrans]';
% forecasting function for rebates
V{3}=(RebateY+RebateM+RebateI)';
elseif mode==2
% Evaluation during simulation. Output conditional
% variables
% SDFs
SDF.SDFM = SDFM;
SDF.SDFI = SDFI;
SDF.SDFY = SDFY;
% Define returns
NetPayoff_nextM = Payoff_nextM - alpha.*(1-deltaH).*P_next*hM;
NetPayoff_nextY = Payoff_nextY - alpha.*(1-deltaH).*P_next*hY;
retMM = G*NetPayoff_nextM/QM;
retMY = G*NetPayoff_nextY/QY;
retH = G.*(1-deltaH)*P_next/(P-rhoM);
retBext = G.*(q_next + xI_next)/q;
retBin =(G*(Payoff_nextM+Payoff_nextY) - DI)./((1+zeta)*(QY+QM) - DI/R);
expR_MM = prnext * retMM;
expR_MY = prnext * retMY;
expR_H = prnext * retH;
expR_Bext = prnext * retBext;
expR_Bin = prnext * retBin;
% Expected default rates
EDefY = prnext * ((1-piY)*Feps_nextYY + piY*Feps_nextYM);
EDefM = prnext * Feps_nextM;
condvars = struct('expR_MM',expR_MM, ...
'expR_MY',expR_MY, ...
'expR_H',expR_H,...
'expR_Bext',expR_Bext,...
'expR_Bin',expR_Bin,...
'min_eInext',min_eInext,...
'cyieldM',cyieldM,...
'cyieldY',cyieldY,...
'EDefY',EDefY,...
'EDefM',EDefM);
% Wtrans.WY = WYtrans;
Wtrans.WM = WMtrans;
Wtrans.eI = eItrans;
V = {condvars,Wtrans,SDF};
elseif mode==3
% compute credit surface
% first young
npty=size(Yvar,1);
QYmat=zeros(npty,4);
for yi=1:npty
hYi = Yvar(yi,1);
levYi = Yvar(yi,2);
mYi=P*hYi*levYi;
levYWi = Yvar(yi,3);
wYYdef_next = mYi/levYWi;
wYMdef_next = mYi/levYWi;
epsbarYY_next = (mYi/G - lambdaY*wYYdef_next)./((1-deltaH)*P_next*hYi);
epsbarYM_next = (mYi/G - lambdaY*wYMdef_next)./((1-deltaH)*P_next*hYi);
[Feps_nextYY,~,~,FepsminusYY]= OLGIntermediaryModel.integEps(epsbarYY_next,mueps_nextY,sigeps_nextY,gamma,[],[],obj.nodes,obj.weights,1);
[Feps_nextYM,~,~,FepsminusYM]= OLGIntermediaryModel.integEps(epsbarYM_next,mueps_nextY,sigeps_nextY,gamma,[],[],obj.nodes,obj.weights,1);
Def_nextY = (1-piY)*Feps_nextYY + piY*Feps_nextYM;
FepsminusY = (1-piY)*FepsminusYY + piY*FepsminusYM;
Payoff_nextYi = (1-Def_nextY).*mYi./G + (1-xi)*(1-deltaH)*FepsminusY.*P_next*hYi;
% price this mortgage
QYmat(yi,1)=(1-ebarY)*muIplus*Payoff_nextYi(minstate) + G*prnext*(SDFI.*Payoff_nextYi);
QYmat(yi,2)=mYi; % mortgage
QYmat(yi,3)=mYi/QYmat(yi,1); % spread
QYmat(yi,4)=mYi/QYmat(yi,1) - R; % spread
end
% then middle aged
nptm=size(Mvar,1);
QMmat=zeros(nptm,4);
for mi=1:nptm
hMi = Mvar(mi,1);
levMi = Mvar(mi,2);
mMi=P*hMi*levMi;
levMWi = Mvar(mi,3);
wMdef_next = mMi/levMWi;
epsbarM_next = (mMi/G - lambdaM*wMdef_next)./((1-deltaH)*P_next*hMi);
[Feps_nextM,~,~,FepsminusM]= OLGIntermediaryModel.integEps(epsbarM_next,mueps_nextM,sigeps_nextM,gamma,[],[],obj.nodes,obj.weights,1);
Payoff_nextMi = (1-Feps_nextM).*mMi./G + (1-xi)*(1-deltaH)*FepsminusM.*P_next*hMi;
% price this mortgage
QMmat(mi,1)=(1-ebarM)*muIplus*Payoff_nextMi(minstate) + G*prnext*(SDFI.*Payoff_nextMi);
QMmat(mi,2)=mMi;
QMmat(mi,3)=mMi/QMmat(mi,1);
QMmat(mi,4)=mMi/QMmat(mi,1) - R;
end
V={QYmat,QMmat};
end
end
function [errmat,solmat,condmat,Wshtrans,SDFmat]=calcEEError(obj,pointmat)
% function to compute Euler equation error at points in state
% space given by pointmat
nst=size(obj.Exogenv.pts_all,1);
errmat=zeros(size(pointmat,1),obj.Pfct.Nof);
solmat=zeros(size(errmat));
condmat=zeros(size(pointmat,1),obj.NCOND);
SDFmat=zeros(size(pointmat,1),3*nst);
Wshtrans=zeros(size(pointmat,1),obj.NSTEN*nst);
evaluatePol = @(point)obj.evaluatePol(point);
calcStateTransition = @(point,soltmp)obj.calcStateTransition(point,soltmp,0);
calcEquations = @(exst,nextst,soltmp,outstr)obj.calcEquations(exst,nextst,soltmp,outstr,2,0);
params=obj.Params;
% Should be parfor. Use for when debugging only
parfor i=1:size(errmat,1)
% for i=1:size(errmat,1)
point=pointmat(i,:);
soltmp=evaluatePol(point);
% transition
[nextst,outstr]=calcStateTransition(point,soltmp);
% equations
[fx,~,V]=calcEquations(point(1),nextst,soltmp,outstr);
R=exp(soltmp(1));
P=exp(soltmp(2));
vM=exp(soltmp(14));
vY=exp(soltmp(15));
Reb=soltmp(17);
QY=outstr.addvars.QY;
QM=outstr.addvars.QM;
DI=outstr.addvars.DI;
SM=outstr.addvars.SM;
SY=outstr.addvars.SY;
if params.rental
normvec=[R,R,R,R,SM,vM,1/R,QY,QM,R,R,R,SY,vY,DI,1,Reb,1];
else
normvec=[R,R,R,R,SM,vM,1/R,QY,QM,R,R,R,SY,vY,DI,1,Reb];
end
if params.PTI || params.LTV
if params.LTV && params.PTI
% LTV and PTI
normvec=[normvec,P,P,P,P];
elseif params.LTV && not(params.PTI)
% LTV and PTI
normvec=[normvec,P,P];
elseif not(params.LTV) && params.PTI
% LTV and PTI
normvec=[normvec,P,P];
end
end
condvars=V{1};
% WYtrans=V{2}.WY;
WMtrans=V{2}.WM;
eItrans=V{2}.eI;
SDFI=V{3}.SDFI;
SDFM=V{3}.SDFM;
SDFY=V{3}.SDFY;
errmat(i,:)=fx'./normvec;
solmat(i,:)=soltmp';
condmat(i,:)=model.DSGEModel.structToVec(condvars)';
Wshtrans(i,:) = [WMtrans', eItrans'];
SDFmat(i,:) = [SDFM',SDFI',SDFY'];
end
end
function [QYmat,QMmat,baseQ]=calcCSurf(obj,Yvar,Mvar)
% function to compute credit surface at equilibrium
% intermediary SDF
npt=obj.Vfct.SSGrid.Npt;
ynpt=size(Yvar,1);
mnpt=size(Mvar,1);
NDIM=obj.Vfct.SSGrid.Ndim;
exnpt=size(obj.Exogenv.pts_perm,1);
pointmat=obj.Vfct.SSGrid.Pointmat;
QYmat=zeros(npt,ynpt,4);
QMmat=zeros(npt,mnpt,4);
baseQ=zeros(npt,2);
% transitions
transmat=obj.evaluateTrans(pointmat)';
forecmat=obj.evaluateForec(pointmat)';
transpts=reshape([repmat(1:exnpt,npt,1),transmat],npt*exnpt,NDIM);
Vtrans = obj.evaluateVal(transpts)';
% add forecasting points
forecpts=reshape(forecmat,npt*exnpt,1); % stack columns representing different states
Vtrans = [Vtrans, forecpts];
% index matching for transitions
refidx=kron((1:npt)',ones(1,exnpt));
refidx=refidx(:);
evaluatePol = @(point)obj.evaluatePol(point);