/
BlanchardPA2019.py
1403 lines (1138 loc) · 50.6 KB
/
BlanchardPA2019.py
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
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.4'
# jupytext_version: 1.2.4
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# # Public Debt and Low Interest Rates
#
# ## Replication of Blanchard (2019)
#
# #### by Julien Acalin - Johns Hopkins University
#
# This notebook fully replicates the analysis of the stochastic overlapping generations (OLG) model developed by Blanchard in his presidential address during the AEA meetings 2019. It takes about 3 minutes to run all the simulations in the notebook.
# +
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Show/Hide Code"></form>''')
# +
#############################
# Some initial setup
#############################
import numpy as np
from numpy import *
from numpy import array
from scipy.optimize import *
from scipy.optimize import minimize_scalar
from scipy import optimize,arange
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# %matplotlib inline
from joblib import Parallel, delayed
from scipy.stats import lognorm
import random
import time
import multiprocessing
start = time.time()
# +
#############################
# Linear Production Function
#############################
#############################
# I. Define some functions for the Linear Production Function
#############################
#############################
# a) Factors Returns
#############################
def rK_func(a,b):
#Eq(14)
"""
Define Return as Function of Productivity and Previous Capital (Linear)
"""
return alpha*a
def W_func(a,b):
#Eq(15)
"""
Define Wage as Function of Productivity and Previous Capital (Linear)
"""
return (1-alpha)*a
def FrK_func(a,b,c):
"""
Define Next Period (Future) Return as Function of Future Productivity, Previous Capital and Current Investment (Linear)
"""
return alpha*a
def FW_func(a,b,c):
"""
Define Next Period (Future) Wage as Function of Future Productivity, Previous Capital and Current Investment (Linear)
"""
return (1-alpha)*a
#############################
# b) Transfers Solver
#############################
def SSFast(MPKa,Rfa):
"""
Steady-State Values without government (Analytical solutions Linear)
Input: Annual MPK and Rf Rates
Output: Annual MPK and Rf Rates, mu, gamma, SS Capital, SS Wage
"""
#Compute Parameters
MPK = pow(MPKa/100+1,years)
Rf = pow(Rfa/100+1,years)
#Eq(19)
gamma = (np.log(MPK) - np.log(Rf))/(sigma*sigma)
#Eq(18)
mu = np.log(MPK) - np.log(alpha) - (sigma*sigma)/2
#Closed form Steady State Values
#Eq(20)
KSS = beta*(1-alpha)*(1+l)*np.exp(mu + (sigma*sigma)/2)
WSS = (1-alpha)*np.exp(mu + (sigma*sigma)/2)
return (round(MPKa,5), round(Rfa,5), round(mu,2), round(gamma,2), round(KSS,2), round(WSS,2))
def foc(inv,K,W,Xs,gamma,X,tr):
"""
Define FOC for Investment Linear
Input: inv (control variable), K, W, Xs, gamma, X (initial endowment), tr (transfer)
Output: result (which is equal to 0 when FOC holds)
"""
num = beta * pow(pi,-1/2) * sum(weights * (1 + FrK_func(Xs,K,inv) - delta) * (pow((1 + FrK_func(Xs,K,inv) - delta) * inv + (phiret * tauL * FW_func(Xs,K,inv)) + tr ,- gamma)))
den = pow(pi,-1/2) * sum(weights * pow((1 + FrK_func(Xs,K,inv) - delta) * inv + (phiret * tauL * FW_func(Xs,K,inv)) + tr,1-gamma))
#Eq(7)
result = num/den - (1-beta)/(W*(1-tauL) + X - tr - inv)
return result
def SSValues(MPKa,Rfa,r):
"""
Steady-State Values (Numerical solutions Linear)
Input: Annual MPK and Rf Rates, r (repetition index)
Output: Annual MPK and Rf Rates (Input), mu, gamma, SS Capital, SS Wage, SS Investment, Value function
"""
#Compute Parameters
MPK = pow(MPKa/100+1,years)
Rf = pow(Rfa/100+1,years)
#Eq(19)
gamma = (np.log(MPK) - np.log(Rf))/(sigma*sigma)
#Eq(18)
mu = np.log(MPK) - np.log(alpha) - (sigma*sigma)/2
Xs = exp(np.sqrt(2)*sigma*nodes+mu) #Gauss-Hermite
#Initialize Model
i = 0 #Reset period
X = l*WSS0list[r] #Non-stochastic endowment = l*WSS
tr = tau*beta*(1+l)*WSS0list[r] #Non-stochastic transfer = tau*(1+l)*ISS
K = beta*(1+l)*WSS0list[r] #Initial value for K
W = WSS0list[r] #Initial value for W
I = beta*(1+l)*WSS0list[r] #Initial value for I
#Create Empty lists
Klist=[]
cylist=[]
Ecolist=[]
Wlist=[]
Ilist=[]
while i != periods:
#Current Random Shock
random.seed(i)
np.random.seed(i)
z = np.random.lognormal(mu, sigma)
#Old
Eco = pow(pi,-1/2) * sum(weights * pow(I * (1 + rK_func(Xs,K) - delta) + phiret* tauL * W_func(Xs,K) + tr, 1-gamma))
#Current Wage
W = W_func(z,K)
#Young Optimal Investment Decision
I = least_squares(foc, (beta*(1+l)*W), bounds = (0,W*(1-tauL)-tr+X), args=(K,W,Xs,gamma,X,tr,))
I = round(I.x[0],50)
cy = W*(1-tauL) -tr - I + X
#Capital Motion
K = (1 - delta) * K + I
#Build Lists
Klist.append(K)
cylist.append(cy)
Ecolist.append(Eco)
Wlist.append(W)
Ilist.append(I)
i += 1
#Compute SS values
KSS = round(np.mean(Klist[drop:]),50)
WSS = round(np.mean(Wlist[drop:]),50)
ISS = round(np.mean(Ilist[drop:]),50)
#Compute Value function
cylist = [1] + cylist #Fix consumption for '1st generation' of old when were young to 1
cylist = cylist[:-1] #Remove last consumption young to make it consistent
Vlong = (1-beta)*np.log(np.asarray(cylist)) + beta / (1-gamma) * np.log(np.asarray(Ecolist))
V = np.mean(Vlong[drop:])
return (round(MPKa,5), round(Rfa,5), round(mu,2), round(gamma,2), round(KSS,5), round(WSS,5), round(ISS,5), round(V,5))
#############################
# c) Debt rollovers Solver
#############################
def Irf(irf,K,W,X,tr,aS,tax):
"""
Define FOC for Investment and Risk-free rate
Input: inv&rf (control variables), K, W, X (initial endowment), tr (transfer), aS (debt level), tax (if default)
Output: F (which is equal to 0 when FOCs hold)
"""
inv = irf[0]
rf = irf[1]
num = pow(pi,-1/2) * sum(weights * (1 + FrK_func(Xs,K,inv) - delta) * pow( (rf * aS) + (1 + FrK_func(Xs,K,inv) - delta) * inv + tr + phiret * tauL * FW_func(Xs,K,inv), -gamma))
den1 = pow(pi,-1/2) * sum(weights * pow( (rf * aS) + (1 + FrK_func(Xs,K,inv) - delta) * inv + tr + phiret * tauL * FW_func(Xs,K,inv), 1-gamma))
den2 = pow(pi,-1/2) * sum(weights * pow( (rf * aS) + (1 + FrK_func(Xs,K,inv) - delta) * inv + tr + phiret * tauL * FW_func(Xs,K,inv), -gamma))
F = empty((2))
#Eq(2)
F[0] = ( beta * num ) / ( den1 ) - (1-beta)/(W*(1-tauL) + X - aS - tr - inv - tax)
#Eq(6)
F[1] = ( num ) / ( den2 ) - rf
return F
def Rollover(sim):
"""
Debt Rollovers (Numerical solutions)
Input: sim (simulation index)
Output: Capital, Investment, Wage, Annual Rf and MPK Rates, Conso Young, Conso Old, EV Conso Old
aS (Safe asset level), aSW (Debt as share of income), taxW (Tax as share of income), aSI (Debt as share of Savings)
"""
#Initialize Model
i = 0 #Reset period
rgen=sim*10 #Random number generator
aS0 = tauaS * ISS #Initial debt rollover
aS = aS0 #Initial value for aS
W = WSS #Initial Value for W
K = KSS #Initial value for K
I = ISS #Initial value for I
X = l*WSS #Non-stochastic endowment
tr = tau*beta*(1+l)*WSS #Non-stochastic transfer
#Create Empty lists
Klist=[]
Ilist=[]
Wlist=[]
rflist=[]
rKlist=[]
cylist=[]
colist=[]
Ecolist=[]
aSlist=[]
aSWlist=[]
aSIlist=[]
taxWlist=[]
while i != generations:
#Current Random Shock
random.seed(rgen)
np.random.seed(rgen)
z = np.random.lognormal(mu, sigma)
#Save Values debt ratios
aSW = aS/(W+X)
aSI = aS/(I+aS)
#Old consume returns on savings, Transfers, and return on safe asset
Eco = pow(pi,-1/2) * sum(weights * pow(I * (1 + rK_func(Xs,K) - delta) + phiret* tauL * W_func(Xs,K) + aS + tr, 1-gamma))
co = I * (1 + rK_func(z,K) - delta) + phiret* tauL * W_func(z,K) + aS + tr
#Define a tax on young if debt rollover fails
tax = max(0,aS-upperl*aS0) / max(abs(aS-upperl*aS0),0.000001) * (aS - dtarget*aS0)
#Back to dtarget*aS0 if aS>upperl*aS0, equal to aS (ie unchanged) otherwise
aS = aS - tax
#Save Values
taxW = tax/(W+X)
#Young: Use the two optimality conditions to solve for the optimal Investment and consistent Rf
res = least_squares(Irf, (beta*(1+l)*W, 1), bounds = ((0, 0), (W*(1-tauL)-aS-tr-tax+X, 2)), args=(K,W,X,tr,aS,tax,))
#Note: Income = Endowment + Net Labor Income - tax - tr (if fixed transfers) - aS (if rollover debt)
I = round(res.x[0],50)
rf = round(res.x[1],50)
cy = W*(1-tauL) - I - aS - tr - tax + X
#Store Values
Klist.append(K)
Ilist.append(I)
rflist.append(rf)
cylist.append(cy)
colist.append(co)
Ecolist.append(Eco)
aSlist.append(aS)
aSWlist.append(aSW)
aSIlist.append(aSI)
taxWlist.append(taxW)
#Next Period Values
#Capital Motion
K = (1 - delta) * K + I
#Debt Motion
aS = rf * aS #Rollover Policy
#aS = rf * aS + aS0 #Extended Rollover Policy
#Factor Prices
W = W_func(z,K)
rK = rK_func(z,K)
rKlist.append(rK)
Wlist.append(W)
i += 1
rgen += 1
return (Klist, Ilist, Wlist, rflist, rKlist, cylist, colist, Ecolist, aSlist, aSWlist, taxWlist, aSIlist)
#############################
# II. Define the parameters
#############################
#############################
# a) Parameters
#############################
l=1 #Dummy =0 if no Non-Stochastic Initial Endowment, =1 if NSIC equal to WSS as in Blanchard(2019)
delta = 1 #Depreciation rate
alpha = 1/3 #Capital share in Cobb Douglas
sigma = 0.20 #Std of productivity shock
beta=0.325 #Discount factor (Linear)
years=25 #Length Generation for transfers
periods= 105 #Number simulated periods for transfers
drop=5 #Number of observations to drop for transfers
nsim = 1000 #Number simulations for debt rollovers
generations = 6 #Number generations for debt rollovers
upperl = 1.15 #Upper limit for Default
dtarget = 0.4 #Target value of Debt after Default
#############################
# b) Define the range values for MPK and EP (Transfers)
#############################
#rf = [1, 0.5, 0, -0.5, -1, -1.5, -2]
#mpk = [2.5, 2.25, 2, 1.75, 1.5, 1.25, 1, 0.75, 0.5, 0.25, 0]
rf = [1, 0.5, 0, -0.5, -1, -1.5, -2]
mpk = [4, 3.5, 3, 2.5, 2, 1.5, 1, 0.5, 0]
#############################
# c) Keep one specification (Debt rollovers)
#############################
MPKax = 2
Rfax = -1
#############################
# d) Computes the sample points and weights for Gauss-Hermite quadrature
#############################
degrees=100
nodes = np.polynomial.hermite.hermgauss(degrees)[0]
weights = np.polynomial.hermite.hermgauss(degrees)[1]
#############################
# III. Solve Transfers
#############################
#############################
# a) Solve for Analytical SS values without Government
#############################
#Solve Model for the range of values for MPK and EP
output_short=[]
for i in mpk:
for j in rf:
if j > i:
continue
out=SSFast(i, j)
output_short.append(out)
#Store Results
MPKa0list = array(output_short)[:,0]
Rfa0list = array(output_short)[:,1]
mu0list = array(output_short)[:,2]
gamma0list = array(output_short)[:,3]
KSS0list = array(output_short)[:,4]
WSS0list = array(output_short)[:,5]
#Number calibrations
x = len(KSS0list)
x = list(range(0, x))
#############################
# b) Solve for Numerical SS values without Government
#############################
#No Government
aS = 0 #Safe assets (government debt)
tauL = 0.0 #Tax rate on labor
tau = 0 #Transfers rate
phiret = 1 #Share of government spending allocated to old
#Solve Model for the range of values for MPK and EP
num_cores = multiprocessing.cpu_count()
calibration = Parallel(n_jobs=num_cores)(delayed(SSValues)(MPKa=MPKa0list[i], Rfa=Rfa0list[i], r=i) for i in x)
#Save Results
#np.savetxt("calibrationLinear.csv", calibration, delimiter=",", fmt='%s')
#Store Results
MPKalist = array(calibration)[:,0]
Rfalist = array(calibration)[:,1]
mulist = array(calibration)[:,2]
gammalist= array(calibration)[:,3]
KSSlist = array(calibration)[:,4]
WSSlist = array(calibration)[:,5]
ISSlist = array(calibration)[:,6]
Vlist = array(calibration)[:,7]
#############################
# c) Solve for Numerical SS values with Government - 5% of ISS
#############################
#Government
aS = 0 #Safe assets (government debt)
tauL = 0.0 #Tax rate on labor
tau = 0.05 #Transfers rate
phiret = 1 #Share of government spending allocated to old
#Solve Model for the range of values for MPK and EP
num_cores = multiprocessing.cpu_count()
result1 = Parallel(n_jobs=num_cores)(delayed(SSValues)(MPKa=MPKa0list[i], Rfa=Rfa0list[i], r=i) for i in x)
#Make lists
Vlisttr5ISS = array(result1)[:,7]
#############################
# d) Solve for Numerical SS values with Government - 20% of ISS
#############################
#Government
aS = 0 #Safe assets (government debt)
tauL = 0.0 #Tax rate on labor
tau = 0.20 #Transfers rate
phiret = 1 #Share of government spending allocated to old
#Solve Model for the range of values for MPK and EP
num_cores = multiprocessing.cpu_count()
result2 = Parallel(n_jobs=num_cores)(delayed(SSValues)(MPKa=MPKa0list[i], Rfa=Rfa0list[i], r=i) for i in x)
#Make lists
Vlisttr20ISS = array(result2)[:,7]
#############################
# e) Save Results
#############################
#Save Results
resL = [Vlist, (Vlisttr5ISS-Vlist)*100, (Vlisttr20ISS-Vlist)*100, MPKa0list, Rfa0list]
#np.savetxt("resultsLinear.csv", resL, delimiter=",", fmt='%s')
#############################
# IV. Solve Debt Rollovers
#############################
#############################
# a) Keep one specification for Debt rollovers
#############################
list_output = [item for item in calibration if item[0] < MPKax+0.05 and item[0] > MPKax-0.05]
list_output = [item for item in list_output if item[1] < Rfax+0.05 and item[1] > Rfax-0.05]
output_short = list_output
#Make lists Steady State Values without Government
MPKa = array(output_short)[0,0]
Rfa = array(output_short)[0,1]
mu = array(output_short)[0,2]
Xs = exp(np.sqrt(2)*sigma*nodes+mu)
gamma= array(output_short)[0,3]
KSS = array(output_short)[0,4]
WSS = array(output_short)[0,5]
ISS = array(output_short)[0,6]
ISSL = ISS
V = array(output_short)[0,7]
#############################
# b) Solve for Numerical SS values without Government
#############################
#No Government
tauaS = 0 #Initial debt rollover as share of ISS
tauL = 0.00 #Tax rate on labor
tau = 0.00 #Transfers rate
phiret = 1 #Share of government spending allocated to old
#Solve Model
DRcalibration=Parallel(n_jobs=num_cores)(delayed(Rollover)(i) for i in range(nsim))
#Format Results
DRcalibration=asarray(DRcalibration)
#Store Results
KM0 = DRcalibration[:,0,:]
IM0 = DRcalibration[:,1,:]
WM0 = DRcalibration[:,2,:]
rfM0 = DRcalibration[:,3,:]
rKM0 = DRcalibration[:,4,:]
cyM0 = DRcalibration[:,5,:]
coM0 = DRcalibration[:,6,:]
EcoM0 = DRcalibration[:,7,:]
aSM0 = DRcalibration[:,8,:]
aSWM0 = DRcalibration[:,9,:]
taxWM0 = DRcalibration[:,10,:]
aSIM0 = DRcalibration[:,11,:]
#Compute Expected Utility
aS0 = tauaS * ISS
EcoM0 = EcoM0[:,1:] #Remove first Expected consumption old in all simulations to make it time consistent
cyM0 = cyM0[:,0:-1] #Remove last consumption young in all simulations to make it time consistent
Vb0 = (1-beta)*np.log(np.asarray(cyM0)) + beta / (1-gamma) * np.log(np.asarray(EcoM0))
cold = np.log(pow(MPKa/100+1,25)*KSS + aS0)
VbL0 = np.insert(Vb0, 0, cold, axis=1) #Fix value function for 1st generation of old in all simulations
Vb0_mean = np.mean(Vb0, axis=0)
#############################
# c) Solve for Numerical SS values with Government
#############################
#No Government
tauaS = 0.15 #Initial debt rollover as share of ISS
tauL = 0.00 #Tax rate on labor
tau = 0.00 #Transfers rate
phiret = 1 #Share of government spending allocated to old
#Solve Model
DRcalibration=Parallel(n_jobs=num_cores)(delayed(Rollover)(i) for i in range(nsim))
#Fornat Results
DRcalibration=asarray(DRcalibration)
#Store Results
KM = DRcalibration[:,0,:]
IM = DRcalibration[:,1,:]
WM = DRcalibration[:,2,:]
rfM = DRcalibration[:,3,:]
rKM = DRcalibration[:,4,:]
cyM = DRcalibration[:,5,:]
coM = DRcalibration[:,6,:]
EcoM = DRcalibration[:,7,:]
aSM = DRcalibration[:,8,:]
aSWM = DRcalibration[:,9,:]
taxWM = DRcalibration[:,10,:]
aSIL = DRcalibration[:,11,:]
#Compute Expected Utility
aS0 = tauaS * ISS
EcoM = EcoM[:,1:] #Remove first Expected consumption old in all simulations to make it time consistent
cyM = cyM[:,0:-1] #Remove last consumption young in all simulations to make it time consistent
Vb = (1-beta)*np.log(np.asarray(cyM)) + beta / (1-gamma) * np.log(np.asarray(EcoM))
cold = np.log(pow(MPKa/100+1,25)*KSS + aS0)
VbL = np.insert(Vb, 0, cold, axis=1) #Fix value function for 1st generation of old in all simulations
Vb_mean = np.mean(Vb, axis=0)
# +
#############################
# Cobb-Douglas Production Function
#############################
#############################
# I. Define some functions for the Cobb-Douglas Production Function
#############################
#############################
# a) Factors Returns
#############################
def rK_func(a,b):
#Eq(22)
"""
Define Return as Function of Productivity and Previous Capital (Cobb-Douglas)
"""
return alpha*a*pow(b,alpha-1)
def W_func(a,b):
#Eq(23)
"""
Define Wage as Function of Productivity and Previous Capital (Cobb-Douglas)
"""
return (1-alpha)*a*pow(b,alpha)
def FrK_func(a,b,c):
"""
Define Next Period (Future) Return as Function of Future Productivity, Previous Capital and Current Investment (Cobb-Douglas)
"""
return alpha*a*pow((1 - delta)*b+c,alpha-1)
def FW_func(a,b,c):
"""
Define Next Period (Future) Wage as Function of Future Productivity, Previous Capital and Current Investment (Cobb-Douglas)
"""
return (1-alpha)*a*pow((1 - delta)*b+c,alpha)
#############################
# b) Transfers Solver
#############################
def SSFast(MPKa,Rfa):
"""
Steady-State Values without government (Analytical solutions Cobb-Douglas)
Input: Annual MPK and Rf Rates
Output: Annual MPK and Rf Rates, beta, gamma, SS Capital, SS Wage, test=0
"""
#Compute Parameters
#[Add References]
MPK = pow(MPKa/100+1,years)
Rf = pow(Rfa/100+1,years)
#Eq(36)
gamma = (np.log(MPK) - np.log(Rf))/(sigma*sigma)
#Eq(35)
beta = alpha / (MPK/np.exp(sigma*sigma/(1+alpha))) / ((1-alpha)*(1+l))
#Closed form Steady State Values
#Eq(30)
KSS = np.exp((np.log(beta*(1+l)*(1-alpha))+mu)/(1-alpha) + (sigma*sigma)/(2*(1-alpha*alpha)))
WSS = (1-alpha)*np.exp(mu + sigma*sigma/2)*np.exp((np.log(beta*(1+l)*(1-alpha))+mu)/(1-alpha)*alpha + (sigma*sigma)*(alpha*alpha)/(2*(1-alpha*alpha)))
return (round(MPKa,5), round(Rfa,5), round(beta,2), round(gamma,2), round(KSS,2), round(WSS,2))
def foc(inv,K,W,beta,gamma,X,tr):
"""
Define FOC for Investment Cobb-Douglas
Input: inv (control variable), K, W, beta, gamma, X (initial endowment), tr (transfer)
Output: result (which is equal to 0 when FOC holds)
"""
num = beta * pow(pi,-1/2) * sum(weights * (1 + FrK_func(Xs,K,inv) - delta) * (pow((1 + FrK_func(Xs,K,inv) - delta) * inv + (phiret * tauL * FW_func(Xs,K,inv)) + tr ,- gamma)))
den = pow(pi,-1/2) * sum(weights * pow((1 + FrK_func(Xs,K,inv) - delta) * inv + (phiret * tauL * FW_func(Xs,K,inv)) + tr,1-gamma))
#Eq(7)
result = num/den - (1-beta)/(W*(1-tauL) + X - tr - inv)
return result
def SSValues(MPKa,Rfa,r):
"""
Steady-State Values (Numerical solutions Cobb-Douglas beta)
Input: Annual MPK and Rf Rates, r (repetition index)
Output: Annual MPK and Rf Rates (Input), beta, gamma, SS Capital, SS Wage, SS Investment, Value function
"""
#Compute Parameters
MPK = pow(MPKa/100+1,years)
Rf = pow(Rfa/100+1,years)
#Eq(36)
gamma = (np.log(MPK) - np.log(Rf))/(sigma*sigma)
#Eq(35)
beta = alpha / (MPK/np.exp(sigma*sigma/(1+alpha))) / ((1-alpha)*(1+l))
#Initialize Model
i = 0 #Reset period
X = l*WSS0list[r] #Non-stochastic endowment = l*WSS
tr = tau*beta*(1+l)*WSS0list[r] #Non-stochastic transfer = tau*(1+l)*ISS
K = beta*(1+l)*WSS0list[r] #Initial value for K
W = WSS0list[r] #Initial value for W
I = beta*(1+l)*WSS0list[r] #Initial value for I
#Create Empty lists
Klist=[]
cylist=[]
Ecolist=[]
Wlist=[]
Ilist=[]
while i != periods:
#Current Random Shock
random.seed(i)
np.random.seed(i)
z = np.random.lognormal(mu, sigma)
#Old
Eco = pow(pi,-1/2) * sum(weights * pow(I * (1 + rK_func(Xs,K) - delta) + phiret* tauL * W_func(Xs,K) + tr, 1-gamma))
#Current Wage
W = W_func(z,K)
#Young Optimal Investment Decision
I = least_squares(foc, (beta*(1+l)*W), bounds = (0,W*(1-tauL)-tr+X), args=(K,W,beta,gamma,X,tr,)) #Cobb-Douglas
I = round(I.x[0],50)
cy = W*(1-tauL) -tr - I + X
#Capital Motion
K = (1 - delta) * K + I
#Build Lists
Klist.append(K)
cylist.append(cy)
Ecolist.append(Eco)
Wlist.append(W)
Ilist.append(I)
i += 1
#Compute SS values
KSS = round(np.mean(Klist[drop:]),50)
WSS = round(np.mean(Wlist[drop:]),50)
ISS = round(np.mean(Ilist[drop:]),50)
#Compute Value function
cylist = [1] + cylist #Fix consumption for '1st generation' of old when were young to 1
cylist = cylist[:-1] #Remove last consumption young to make it consistent
Vlong = (1-beta)*np.log(np.asarray(cylist)) + beta / (1-gamma) * np.log(np.asarray(Ecolist))
V = np.mean(Vlong[drop:])
return (round(MPKa,5), round(Rfa,5), round(beta,2), round(gamma,2), round(KSS,5), round(WSS,5), round(ISS,5), round(V,5))
#############################
# c) Debt rollovers Solver
#############################
def Irf(irf,K,W,X,tr,aS,tax):
"""
Define FOC for Investment and Risk-free rate
Input: inv&rf (control variables), K, W, X (initial endowment), tr (transfer), aS (debt level), tax (if default)
Output: F (which is equal to 0 when FOCs hold)
"""
inv = irf[0]
rf = irf[1]
num = pow(pi,-1/2) * sum(weights * (1 + FrK_func(Xs,K,inv) - delta) * pow( (rf * aS) + (1 + FrK_func(Xs,K,inv) - delta) * inv + tr + phiret * tauL * FW_func(Xs,K,inv), -gamma))
den1 = pow(pi,-1/2) * sum(weights * pow( (rf * aS) + (1 + FrK_func(Xs,K,inv) - delta) * inv + tr + phiret * tauL * FW_func(Xs,K,inv), 1-gamma))
den2 = pow(pi,-1/2) * sum(weights * pow( (rf * aS) + (1 + FrK_func(Xs,K,inv) - delta) * inv + tr + phiret * tauL * FW_func(Xs,K,inv), -gamma))
F = empty((2))
#Eq(2)
F[0] = ( beta * num ) / ( den1 ) - (1-beta)/(W*(1-tauL) + X - aS - tr - inv - tax)
#Eq(6)
F[1] = ( num ) / ( den2 ) - rf
return F
def Rollover(sim):
"""
Debt Rollovers (Numerical solutions)
Input: sim (simulation index)
Output: Capital, Investment, Wage, Annual Rf and MPK Rates, Conso Young, Conso Old, EV Conso Old
aS (Safe asset level), aSW (Debt as share of income), taxW (Tax as share of income), aSI (Debt as share of Savings)
"""
#Initialize Model
rgen=sim*10 #Random number generator
aS0 = tauaS * ISS #Initial debt rollover
aS = aS0 #Initial value for aS
i = 0 #Reset period
W = WSS #Initial Value for W
K = KSS #Initial value for K
I = ISS #Initial value for I
X = l*WSS #Non-stochastic endowment
tr = tau*beta*(1+l)*WSS #Non-stochastic transfer
#Create Empty lists
Klist=[]
Ilist=[]
Wlist=[]
rflist=[]
rKlist=[]
cylist=[]
colist=[]
Ecolist=[]
aSlist=[]
aSWlist=[]
aSIlist=[]
taxWlist=[]
while i != generations:
#Current Random Shock
random.seed(rgen)
np.random.seed(rgen)
z = np.random.lognormal(mu, sigma)
#Save Values
aSW = aS/(W+X)
aSI = aS/(I+aS)
#Old consume returns on savings, Transfers, and return on safe asset
Eco = pow(pi,-1/2) * sum(weights * pow(I * (1 + rK_func(Xs,K) - delta) + phiret* tauL * W_func(Xs,K) + aS + tr, 1-gamma))
co = I * (1 + rK_func(z,K) - delta) + phiret* tauL * W_func(z,K) + aS + tr
#Define a tax on young if debt rollover fails
tax = max(0,aS-upperl*aS0) / max(abs(aS-upperl*aS0),0.000001) * (aS - dtarget*aS0)
#Back to dtarget*aS0 if aS>upperl*aS0, equal to aS (ie unchanged) otherwise
aS = aS - tax
#Save Values
taxW = tax/(W+X)
#Young: Use the two optimality conditions to solve for the optimal Investment and consistent Rf
res = least_squares(Irf, (beta*(1+l)*W, 1), bounds = ((0, 0), (W*(1-tauL)-aS-tr-tax+X, 2)), args=(K,W,X,tr,aS,tax,))
#Note: Income = Endowment + Net Labor Income - tax - tr (if fixed transfers) - aS (if rollover debt)
I = round(res.x[0],50)
rf = round(res.x[1],50)
cy = W*(1-tauL) - I - aS - tr - tax + X
#Store Values
Klist.append(K)
Ilist.append(I)
rflist.append(rf)
cylist.append(cy)
colist.append(co)
Ecolist.append(Eco)
aSlist.append(aS)
aSWlist.append(aSW)
aSIlist.append(aSI)
taxWlist.append(taxW)
#Next Period Values
#Capital Motion
K = (1 - delta) * K + I
#Debt Motion
aS = rf * aS #Rollover Policy
#aS = rf * aS + aS0 #Extended Rollover Policy
#Factor Prices
W = W_func(z,K)
rK = rK_func(z,K)
rKlist.append(rK)
Wlist.append(W)
i += 1
rgen += 1
return (Klist, Ilist, Wlist, rflist, rKlist, cylist, colist, Ecolist, aSlist, aSWlist, taxWlist, aSIlist)
#############################
# II. Define the parameters
#############################
#############################
# a) Parameters
#############################
l=1 #Dummy =0 if no Non-Stochastic Initial Endowment, =1 if NSIC equal to WSS as in Blanchard(2019)
delta = 1 #Depreciation rate
alpha = 1/3 #Capital share in Cobb Douglas
sigma = 0.20 #Std of productivity shock
mu = 3 #Mean of productivity shock (Cobb-Douglas)
years=25 #Length Generation for transfers
periods= 105 #Number simulated periods for transfers
drop=5 #Number of observations to drop for transfers
nsim = 1000 #Number simulations for debt rollovers
generations = 6 #Number generations for debt rollovers
upperl = 1.15 #Upper limit for Default
dtarget = 0.4 #Target value of Debt after Default
#############################
# b) Define the range values for MPK and EP (Transfers)
#############################
#rf = [1, 0.5, 0, -0.5, -1, -1.5, -2]
#mpk = [2.5, 2.25, 2, 1.75, 1.5, 1.25, 1, 0.75, 0.5, 0.25, 0]
rf = [1, 0.5, 0, -0.5, -1, -1.5, -2]
mpk = [4, 3.5, 3, 2.5, 2, 1.5, 1, 0.5, 0]
#############################
# c) Keep one specification (Debt rollovers)
#############################
MPKax = 2
Rfax = -1
#############################
# d) Computes the sample points and weights for Gauss-Hermite quadrature
#############################
degrees=100
nodes = np.polynomial.hermite.hermgauss(degrees)[0]
weights = np.polynomial.hermite.hermgauss(degrees)[1]
Xs = exp(np.sqrt(2)*sigma*nodes+mu)
#############################
# III. Solve Transfers
#############################
#############################
# a) Solve for Analytical SS values without Government
#############################
#Solve Model for the range of values for MPK and EP
output_short=[]
for i in mpk:
for j in rf:
if j > i:
continue
out=SSFast(i, j)
output_short.append(out)
#Store Results
MPKa0list = array(output_short)[:,0]
Rfa0list = array(output_short)[:,1]
beta0list = array(output_short)[:,2]
gamma0list = array(output_short)[:,3]
KSS0list = array(output_short)[:,4]
WSS0list = array(output_short)[:,5]
#Number calibrations
x = len(KSS0list)
x = list(range(0, x))
#############################
# b) Solve for Numerical SS values without Government
#############################
#No Government
aS = 0 #Safe assets (government debt)
tauL = 0.0 #Tax rate on labor
tau = 0 #Transfers rate
phiret = 1 #Share of government spending allocated to old
#Solve Model for the range of values for MPK and EP
num_cores = multiprocessing.cpu_count()
calibration = Parallel(n_jobs=num_cores)(delayed(SSValues)(MPKa=MPKa0list[i], Rfa=Rfa0list[i], r=i) for i in x)
#Save Results
#np.savetxt("calibrationCobbDouglas.csv", calibration, delimiter=",", fmt='%s')
#Store Results
MPKalist = array(calibration)[:,0]
Rfalist = array(calibration)[:,1]
betalist = array(calibration)[:,2]
gammalist= array(calibration)[:,3]
KSSlist = array(calibration)[:,4]
WSSlist = array(calibration)[:,5]
ISSlist = array(calibration)[:,6]
Vlist = array(calibration)[:,7]
#############################
# c) Solve for Numerical SS values with Government - 5% of ISS
#############################
#Government
aS = 0 #Safe assets (government debt)
tauL = 0.0 #Tax rate on labor
tau = 0.05 #Transfers rate
phiret = 1 #Share of government spending allocated to old
#Solve Model for the range of values for MPK and EP
num_cores = multiprocessing.cpu_count()
result1 = Parallel(n_jobs=num_cores)(delayed(SSValues)(MPKa=MPKa0list[i], Rfa=Rfa0list[i], r=i) for i in x)
#Make lists
Vlisttr5ISS = array(result1)[:,7]