-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
SS.py
1530 lines (1446 loc) · 43.9 KB
/
SS.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
# imports
from re import VERBOSE
import numpy as np
import scipy.optimize as opt
from dask import delayed, compute
import dask.multiprocessing
from ogcore import tax, pensions, household, firm, utils, fiscal
from ogcore import aggregates as aggr
from ogcore.constants import SHOW_RUNTIME
import os
import warnings
if not SHOW_RUNTIME:
warnings.simplefilter("ignore", RuntimeWarning)
"""
Set minimizer tolerance
"""
MINIMIZER_TOL = 1e-13
"""
Set flag for enforcement of solution check
"""
ENFORCE_SOLUTION_CHECKS = True
"""
Set flag for verbosity
"""
VERBOSE = True
"""
A global future for the Parameters object for client workers.
This is scattered once and place at module scope, then used
by the client in the inner loop.
"""
scattered_p = None
"""
------------------------------------------------------------------------
Define Functions
------------------------------------------------------------------------
"""
def euler_equation_solver(guesses, *args):
"""
Finds the euler errors for certain b and n, one ability type at a
time.
Args:
guesses (Numpy array): initial guesses for b and n, length 2S
args (tuple): tuple of arguments (r, w, p_tilde, bq, TR, factor, j, p)
r (scalar): real interest rate
w (scalar): real wage rate
p_tilde (scalar): composite good price
bq (Numpy array): bequest amounts by age, length S
rm (scalar): remittance amounts by age, length S
tr (scalar): government transfer amount by age, length S
ubi (vector): universal basic income (UBI) payment, length S
factor (scalar): scaling factor converting model units to dollars
p (OG-Core Specifications object): model parameters
Returns:
errros (Numpy array): errors from FOCs, length 2S
"""
(r, w, p_tilde, bq, rm, tr, ubi, factor, j, p) = args
b_guess = np.array(guesses[: p.S])
n_guess = np.array(guesses[p.S :])
b_s = np.array([0] + list(b_guess[:-1]))
b_splus1 = b_guess
theta = pensions.replacement_rate_vals(n_guess, w, factor, j, p)
error1 = household.FOC_savings(
r,
w,
p_tilde,
b_s,
b_splus1,
n_guess,
bq,
rm,
factor,
tr,
ubi,
theta,
p.rho[-1, :],
p.etr_params[-1],
p.mtry_params[-1],
None,
j,
p,
"SS",
)
error2 = household.FOC_labor(
r,
w,
p_tilde,
b_s,
b_splus1,
n_guess,
bq,
rm,
factor,
tr,
ubi,
theta,
p.chi_n[-1, :],
p.etr_params[-1],
p.mtrx_params[-1],
None,
j,
p,
"SS",
)
# Put in constraints for consumption and savings.
# According to the euler equations, they can be negative. When
# Chi_b is large, they will be. This prevents that from happening.
# I'm not sure if the constraints are needed for labor.
# But we might as well put them in for now.
mask1 = n_guess < 0
mask2 = n_guess > p.ltilde
mask3 = b_guess <= 0
mask4 = np.isnan(n_guess)
mask5 = np.isnan(b_guess)
error2[mask1] = 1e14
error2[mask2] = 1e14
error1[mask3] = 1e14
error1[mask5] = 1e14
error2[mask4] = 1e14
taxes = tax.net_taxes(
r,
w,
b_s,
n_guess,
bq,
factor,
tr,
ubi,
theta,
None,
j,
False,
"SS",
p.e[-1, :, j],
p.etr_params[-1],
p,
)
cons = household.get_cons(
r,
w,
p_tilde,
b_s,
b_splus1,
n_guess,
bq,
rm,
taxes,
p.e[-1, :, j],
p,
)
mask6 = cons < 0
error1[mask6] = 1e14
errors = np.hstack((error1, error2))
return errors
def inner_loop(outer_loop_vars, p, client):
"""
This function solves for the inner loop of the SS. That is, given
the guesses of the outer loop variables (r, w, TR, factor) this
function solves the households' problems in the SS.
Args:
outer_loop_vars (tuple): tuple of outer loop variables,
(bssmat, nssmat, r_p, r, w, p_m, BQ, RM, TR, factor) or
(bssmat, nssmat, r_p, r, w, p_m, BQ, RM, Y, TR, factor)
bssmat (Numpy array): initial guess at savings, size = SxJ
nssmat (Numpy array): initial guess at labor supply, size = SxJ
r_p (scalar): return on household investment portfolio
r (scalar): real interest rate
w (scalar): real wage rate
p_m (array_like): production goods prices
BQ (array_like): aggregate bequest amount(s)
TR (scalar): lump sum transfer amount
Y (scalar): real GDP
factor (scalar): scaling factor converting model units to dollars
p (OG-Core Specifications object): model parameters
client (Dask client object): client
Returns:
(tuple): results from household solution:
* euler_errors (Numpy array): errors terms from FOCs,
size = 2SxJ
* bssmat (Numpy array): savings, size = SxJ
* nssmat (Numpy array): labor supply, size = SxJ
* new_r (scalar): real interest rate on firm capital
* new_r_gov (scalar): real interest rate on government debt
* new_r_p (scalar): real interest rate on household
portfolio
* new_w (scalar): real wage rate
* new_p_i (array_like): good prices
* K_vec (array_like): capital demand for each industry
* L_vec (array_like): labor demand for each industry
* Y_vec (array_like): output from each industry
* new_TR (scalar): lump sum transfer amount
* new_Y (scalar): real GDP
* new_factor (scalar): scaling factor converting model
units to dollars
* new_BQ (array_like): aggregate bequest amount(s)
* average_income_model (scalar): average income in model
units
"""
# Retrieve the "scattered" Parameters object.
global scattered_p
if scattered_p is None:
scattered_p = client.scatter(p, broadcast=True) if client else p
# unpack variables to pass to function
bssmat, nssmat, r_p, r, w, p_m, Y, BQ, TR, Ig_baseline, factor = (
outer_loop_vars
)
p_m = np.array(p_m) # TODO: why is this a list otherwise?
p_i = np.dot(p.io_matrix, p_m)
BQ = np.array(BQ)
RM = np.array(aggr.get_RM(Y, p, "SS"))
# initialize array for euler errors
euler_errors = np.zeros((2 * p.S, p.J))
p_tilde = aggr.get_ptilde(p_i, p.tau_c[-1, :], p.alpha_c)
bq = household.get_bq(BQ, None, p, "SS")
rm = household.get_rm(RM, None, p, "SS")
tr = household.get_tr(TR, None, p, "SS")
ubi = p.ubi_nom_array[-1, :, :] / factor
lazy_values = []
for j in range(p.J):
guesses = np.append(bssmat[:, j], nssmat[:, j])
euler_params = (
r_p,
w,
p_tilde,
bq[:, j],
rm[:, j],
tr[:, j],
ubi[:, j],
factor,
j,
scattered_p,
)
lazy_values.append(
delayed(opt.root)(
euler_equation_solver,
guesses * 0.9,
args=euler_params,
method=p.FOC_root_method,
tol=MINIMIZER_TOL,
)
)
if client:
futures = client.compute(lazy_values, num_workers=p.num_workers)
results = client.gather(futures)
else:
results = results = compute(
*lazy_values,
scheduler=dask.multiprocessing.get,
num_workers=p.num_workers,
)
for j, result in enumerate(results):
euler_errors[:, j] = result.fun
bssmat[:, j] = result.x[: p.S]
nssmat[:, j] = result.x[p.S :]
b_splus1 = bssmat
b_s = np.array(list(np.zeros(p.J).reshape(1, p.J)) + list(bssmat[:-1, :]))
theta = pensions.replacement_rate_vals(nssmat, w, factor, None, p)
num_params = len(p.etr_params[-1][0])
etr_params_3D = [
[
[p.etr_params[-1][s][i] for i in range(num_params)]
for j in range(p.J)
]
for s in range(p.S)
]
net_tax = tax.net_taxes(
r_p,
w,
b_s,
nssmat,
bq,
factor,
tr,
ubi,
theta,
None,
None,
False,
"SS",
np.squeeze(p.e[-1, :, :]),
etr_params_3D,
p,
)
c_s = household.get_cons(
r_p,
w,
p_tilde,
b_s,
b_splus1,
nssmat,
bq,
rm,
net_tax,
np.squeeze(p.e[-1, :, :]),
p,
)
c_i = household.get_ci(c_s, p_i, p_tilde, p.tau_c[-1, :], p.alpha_c)
L = aggr.get_L(nssmat, p, "SS")
B = aggr.get_B(bssmat, p, "SS", False)
# Find gov't debt
r_gov = fiscal.get_r_gov(r, p, "SS")
D, D_d, D_f, new_borrowing, _, new_borrowing_f = fiscal.get_D_ss(
r_gov, Y, p
)
I_g = fiscal.get_I_g(Y, Ig_baseline, p, "SS")
K_g = fiscal.get_K_g(0, I_g, p, "SS")
# Find wage rate consistent with open economy interest rate
# this is an approximation - assumes only KL in rest of world
# production function
w_open = firm.get_w_from_r(p.world_int_rate[-1], p, "SS")
# Find output, labor demand, capital demand for M-1 industries
L_vec = np.zeros(p.M)
K_vec = np.zeros(p.M)
C_vec = np.zeros(p.I)
K_demand_open_vec = np.zeros(p.M)
for i_ind in range(p.I):
C_vec[i_ind] = aggr.get_C(c_i[i_ind, :, :], p, "SS")
Y_vec = np.dot(p.io_matrix.T, C_vec)
for m_ind in range(p.M - 1):
KYrat_m = firm.get_KY_ratio(r, p_m, p, "SS", m_ind)
K_vec[m_ind] = KYrat_m * Y_vec[m_ind]
L_vec[m_ind] = firm.solve_L(
Y_vec[m_ind], K_vec[m_ind], K_g, p, "SS", m_ind
)
K_demand_open_vec[m_ind] = firm.get_K(
p.world_int_rate[-1], w_open, L_vec[m_ind], p, "SS", m_ind
)
# Find output, labor demand, capital demand for industry M
L_M = max(0.001, L - L_vec.sum()) # make sure L_M > 0
K_demand_open_vec[-1] = firm.get_K(
p.world_int_rate[-1], w_open, L_M, p, "SS", -1
)
K, K_d, K_f = aggr.get_K_splits(
B, K_demand_open_vec.sum(), D_d, p.zeta_K[-1]
)
K_M = max(0.001, K - K_vec.sum()) # make sure K_M > 0
L_vec[-1] = L_M
K_vec[-1] = K_M
Y_vec[-1] = firm.get_Y(K_vec[-1], K_g, L_vec[-1], p, "SS", -1)
# Find GDP
Y = (p_m * Y_vec).sum()
I_g = fiscal.get_I_g(Y, Ig_baseline, p, "SS")
K_g = fiscal.get_K_g(0, I_g, p, "SS")
if p.zeta_K[-1] == 1.0:
new_r = p.world_int_rate[-1]
else:
new_r = firm.get_r(Y_vec[-1], K_vec[-1], p_m, p, "SS", -1)
new_w = firm.get_w(Y_vec[-1], L_vec[-1], p_m, p, "SS")
new_r_gov = fiscal.get_r_gov(new_r, p, "SS")
# now get accurate measure of debt service cost
(
D,
D_d,
D_f,
new_borrowing,
debt_service,
new_borrowing_f,
) = fiscal.get_D_ss(new_r_gov, Y, p)
MPKg_vec = np.zeros(p.M)
for m in range(p.M):
MPKg_vec[m] = firm.get_MPx(Y_vec[m], K_g, p.gamma_g[m], p, "SS", m)
new_r_p = aggr.get_r_p(
new_r, new_r_gov, p_m, K_vec, K_g, D, MPKg_vec, p, "SS"
)
average_income_model = (
(new_r_p * b_s + new_w * np.squeeze(p.e[-1, :, :]) * nssmat)
* p.omega_SS.reshape(p.S, 1)
* p.lambdas.reshape(1, p.J)
).sum()
if p.baseline:
new_factor = p.mean_income_data / average_income_model
else:
new_factor = factor
new_BQ = aggr.get_BQ(new_r_p, bssmat, None, p, "SS", False)
new_bq = household.get_bq(new_BQ, None, p, "SS")
new_RM = aggr.get_RM(Y, p, "SS")
new_rm = household.get_rm(new_RM, None, p, "SS")
tr = household.get_tr(TR, None, p, "SS")
theta = pensions.replacement_rate_vals(nssmat, new_w, new_factor, None, p)
# Find updated goods prices
new_p_m = firm.get_pm(new_w, Y_vec, L_vec, p, "SS")
new_p_m = new_p_m / new_p_m[-1] # normalize prices by industry M
new_p_i = np.dot(p.io_matrix, new_p_m)
new_p_tilde = aggr.get_ptilde(new_p_i, p.tau_c[-1, :], p.alpha_c)
num_params = len(p.etr_params[-1][0])
etr_params_3D = [
[
[p.etr_params[-1][s][i] for i in range(num_params)]
for j in range(p.J)
]
for s in range(p.S)
]
taxss = tax.net_taxes(
new_r_p,
new_w,
b_s,
nssmat,
new_bq,
factor,
tr,
ubi,
theta,
None,
None,
False,
"SS",
np.squeeze(p.e[-1, :, :]),
etr_params_3D,
p,
)
cssmat = household.get_cons(
new_r_p,
new_w,
new_p_tilde,
b_s,
bssmat,
nssmat,
new_bq,
new_rm,
taxss,
np.squeeze(p.e[-1, :, :]),
p,
)
(
total_tax_revenue,
_,
agg_pension_outlays,
UBI_outlays,
_,
_,
_,
_,
_,
_,
) = aggr.revenue(
new_r_p,
new_w,
b_s,
nssmat,
new_bq,
c_i,
Y_vec,
L_vec,
K_vec,
new_p_m,
factor,
ubi,
theta,
etr_params_3D,
np.squeeze(p.e[-1, :, :]),
p,
None,
"SS",
)
G = fiscal.get_G_ss(
Y,
total_tax_revenue,
agg_pension_outlays,
TR,
UBI_outlays,
I_g,
new_borrowing,
debt_service,
p,
)
new_TR = fiscal.get_TR(
Y,
TR,
G,
total_tax_revenue,
agg_pension_outlays,
UBI_outlays,
I_g,
p,
"SS",
)
G_vec = np.zeros(p.M)
G_vec[-1] = G
C_m_vec = np.dot(p.io_matrix.T, C_vec)
I_d_vec = np.zeros(p.M)
I_d = aggr.get_I(b_splus1, K_d, K_d, p, "SS")
I_d_vec[-1] = I_d
I_g_vec = np.zeros(p.M)
I_g_vec[-1] = I_g
debt_service_f = fiscal.get_debt_service_f(r_p, D_f)
net_capital_outflows = aggr.get_capital_outflows(
r_p, K_f, new_borrowing_f, debt_service_f, p
)
net_capital_outflows_vec = np.zeros(p.M)
net_capital_outflows_vec[-1] = net_capital_outflows
rc_error = (
Y_vec - C_m_vec - G_vec - I_d_vec - I_g_vec - net_capital_outflows_vec
)
return (
euler_errors,
bssmat,
nssmat,
new_r,
new_r_gov,
new_r_p,
new_w,
new_p_m,
K_vec,
L_vec,
Y_vec,
new_RM,
new_TR,
Y,
new_factor,
new_BQ,
average_income_model,
)
def SS_solver(
bmat,
nmat,
r_p,
r,
w,
p_m,
Y,
BQ,
TR,
Ig_baseline,
factor,
p,
client,
fsolve_flag=False,
):
"""
Solves for the steady state distribution of capital, labor, as well
as w, r, TR and the scaling factor, using functional iteration.
Args:
bmat (Numpy array): initial guess at savings, size = SxJ
nmat (Numpy array): initial guess at labor supply, size = SxJ
r_p (scalar): return on household investment portfolio
r (scalar): real interest rate
w (scalar): real wage rate
p_m (array_like): good prices
Y (scalar): real GDP
BQ (array_like): aggregate bequest amount(s)
TR (scalar): lump sum transfer amount
factor (scalar): scaling factor converting model units to dollars
p (OG-Core Specifications object): model parameters
client (Dask client object): client
Returns:
output (dictionary): dictionary with steady state solution
results
"""
dist = 10
iteration = 0
dist_vec = np.zeros(p.maxiter)
maxiter_ss = p.maxiter
nu_ss = p.nu
if fsolve_flag: # case where already solved via SS_fsolve
maxiter_ss = 1
if p.baseline_spending:
TR_baseline = p.alpha_bs_T[-1] * TR
if not p.budget_balance and not p.baseline_spending:
Y = TR / p.alpha_T[-1]
while (dist > p.mindist_SS) and (iteration < maxiter_ss):
# Solve for the steady state levels of b and n, given w, r,
# Y, BQ, TR, and factor
if p.baseline_spending:
TR = p.alpha_bs_T[-1] * TR_baseline
if not p.budget_balance and not p.baseline_spending:
Y = TR / p.alpha_T[-1]
outer_loop_vars = (
bmat,
nmat,
r_p,
r,
w,
p_m,
Y,
BQ,
TR,
Ig_baseline,
factor,
)
(
euler_errors,
new_bmat,
new_nmat,
new_r,
new_r_gov,
new_r_p,
new_w,
new_p_m,
new_K_vec,
new_L_vec,
new_Y_vec,
new_RM,
new_TR,
new_Y,
new_factor,
new_BQ,
average_income_model,
) = inner_loop(outer_loop_vars, p, client)
# update guesses for next iteration
bmat = utils.convex_combo(new_bmat, bmat, nu_ss)
nmat = utils.convex_combo(new_nmat, nmat, nu_ss)
r_p = utils.convex_combo(new_r_p, r_p, nu_ss)
r = utils.convex_combo(new_r, r, nu_ss)
w = utils.convex_combo(new_w, w, nu_ss)
p_m = utils.convex_combo(new_p_m, p_m, nu_ss)
factor = utils.convex_combo(new_factor, factor, nu_ss)
BQ = utils.convex_combo(new_BQ, BQ, nu_ss)
if p.baseline_spending:
Y = utils.convex_combo(new_Y, Y, nu_ss)
if Y != 0:
dist = np.array(
[utils.pct_diff_func(new_r, r)]
+ [utils.pct_diff_func(new_r_p, r_p)]
+ [utils.pct_diff_func(new_w, w)]
+ list(utils.pct_diff_func(new_p_m, p_m))
+ list(utils.pct_diff_func(new_BQ, BQ))
+ [utils.pct_diff_func(new_Y, Y)]
+ [utils.pct_diff_func(new_factor, factor)]
).max()
else:
# If Y is zero (if there is no output), a percent difference
# will throw NaN's, so we use an absolute difference
dist = np.array(
[utils.pct_diff_func(new_r, r)]
+ [utils.pct_diff_func(new_r_p, r_p)]
+ [utils.pct_diff_func(new_w, w)]
+ list(utils.pct_diff_func(new_p_m, p_m))
+ list(utils.pct_diff_func(new_BQ, BQ))
+ [abs(new_Y - Y)]
+ [utils.pct_diff_func(new_factor, factor)]
).max()
else:
if p.baseline_spending:
TR = p.alpha_bs_T[-1] * TR_baseline
else:
TR = utils.convex_combo(new_TR, TR, nu_ss)
dist = np.array(
[float(utils.pct_diff_func(new_r, r))]
+ [float(utils.pct_diff_func(new_r_p, r_p))]
+ [float(utils.pct_diff_func(new_w, w))]
+ list(utils.pct_diff_func(new_p_m, p_m))
+ list(utils.pct_diff_func(new_BQ, BQ))
+ [float(utils.pct_diff_func(new_TR, TR))]
+ [float(utils.pct_diff_func(new_factor, factor))]
).max()
dist_vec[iteration] = dist
# Similar to TPI: if the distance between iterations increases, then
# decrease the value of nu to prevent cycling
if iteration > 10:
if dist_vec[iteration] - dist_vec[iteration - 1] > 0:
nu_ss /= 2.0
print("New value of nu:", nu_ss)
iteration += 1
if VERBOSE:
print("Iteration: %02d" % iteration, " Distance: ", dist)
# Generate the SS values of variables, including euler errors
bssmat_s = np.append(np.zeros((1, p.J)), bmat[:-1, :], axis=0)
bssmat_splus1 = bmat
nssmat = nmat
rss = new_r
wss = new_w
K_vec_ss = new_K_vec
L_vec_ss = new_L_vec
Y_vec_ss = new_Y_vec
r_gov_ss = fiscal.get_r_gov(rss, p, "SS")
p_m_ss = new_p_m
p_i_ss = np.dot(p.io_matrix, p_m_ss)
p_tilde_ss = aggr.get_ptilde(p_i_ss, p.tau_c[-1, :], p.alpha_c)
RM_ss = new_RM
TR_ss = new_TR
Yss = new_Y
I_g_ss = fiscal.get_I_g(Yss, Ig_baseline, p, "SS")
K_g_ss = fiscal.get_K_g(0, I_g_ss, p, "SS")
Lss = aggr.get_L(nssmat, p, "SS")
Bss = aggr.get_B(bssmat_splus1, p, "SS", False)
(
Dss,
D_d_ss,
D_f_ss,
new_borrowing,
debt_service,
new_borrowing_f,
) = fiscal.get_D_ss(r_gov_ss, Yss, p)
print("SS debt = ", Dss, new_borrowing_f)
w_open = firm.get_w_from_r(p.world_int_rate[-1], p, "SS")
K_demand_open_ss = np.zeros(p.M)
for m in range(p.M):
K_demand_open_ss[m] = firm.get_K(
p.world_int_rate[-1], w_open, L_vec_ss[m], p, "SS", m
)
Kss, K_d_ss, K_f_ss = aggr.get_K_splits(
Bss, K_demand_open_ss.sum(), D_d_ss, p.zeta_K[-1]
)
# Yss = firm.get_Y(Kss, K_g_ss, Lss, p, 'SS')
I_g_ss = fiscal.get_I_g(Yss, Ig_baseline, p, "SS")
K_g_ss = fiscal.get_K_g(0, I_g_ss, p, "SS")
MPKg_vec = np.zeros(p.M)
for m in range(p.M):
MPKg_vec[m] = firm.get_MPx(
Y_vec_ss[m], K_g_ss, p.gamma_g[m], p, "SS", m
)
# r_p_ss = aggr.get_r_p(
# rss, r_gov_ss, p_m_ss, K_vec_ss, K_g_ss, Dss, MPKg_vec, p, "SS"
# )
r_p_ss = new_r_p
# Note that implicitly in this computation is that immigrants'
# wealth is all in the form of private capital
I_d_ss = aggr.get_I(bssmat_splus1, K_d_ss, K_d_ss, p, "SS")
Iss = aggr.get_I(bssmat_splus1, Kss, Kss, p, "SS")
BQss = new_BQ
factor_ss = factor
bqssmat = household.get_bq(BQss, None, p, "SS")
trssmat = household.get_tr(TR_ss, None, p, "SS")
rmssmat = household.get_rm(RM_ss, None, p, "SS")
ubissmat = p.ubi_nom_array[-1, :, :] / factor_ss
theta = pensions.replacement_rate_vals(nssmat, wss, factor_ss, None, p)
# Compute effective and marginal tax rates for all agents
num_params = len(p.etr_params[-1][0])
etr_params_3D = [
[
[p.etr_params[-1][s][i] for i in range(num_params)]
for j in range(p.J)
]
for s in range(p.S)
]
mtrx_params_3D = [
[
[p.mtrx_params[-1][s][i] for i in range(num_params)]
for j in range(p.J)
]
for s in range(p.S)
]
mtry_params_3D = [
[
[p.mtry_params[-1][s][i] for i in range(num_params)]
for j in range(p.J)
]
for s in range(p.S)
]
labor_noncompliance_rate_2D = np.tile(
np.reshape(p.labor_income_tax_noncompliance_rate[-1, :], (1, p.J)),
(p.S, 1),
)
capital_noncompliance_rate_2D = np.tile(
np.reshape(p.labor_income_tax_noncompliance_rate[-1, :], (1, p.J)),
(p.S, 1),
)
mtry_ss = tax.MTR_income(
r_p_ss,
wss,
bssmat_s,
nssmat,
factor,
True,
np.squeeze(p.e[-1, :, :]),
etr_params_3D,
mtry_params_3D,
capital_noncompliance_rate_2D,
p,
)
mtrx_ss = tax.MTR_income(
r_p_ss,
wss,
bssmat_s,
nssmat,
factor,
False,
np.squeeze(p.e[-1, :, :]),
etr_params_3D,
mtrx_params_3D,
labor_noncompliance_rate_2D,
p,
)
etr_ss = tax.ETR_income(
r_p_ss,
wss,
bssmat_s,
nssmat,
factor,
np.squeeze(p.e[-1, :, :]),
etr_params_3D,
labor_noncompliance_rate_2D,
capital_noncompliance_rate_2D,
p,
)
taxss = tax.net_taxes(
r_p_ss,
wss,
bssmat_s,
nssmat,
bqssmat,
factor_ss,
trssmat,
ubissmat,
theta,
None,
None,
False,
"SS",
np.squeeze(p.e[-1, :, :]),
etr_params_3D,
p,
)
cssmat = household.get_cons(
r_p_ss,
wss,
p_tilde_ss,
bssmat_s,
bssmat_splus1,
nssmat,
bqssmat,
rmssmat,
taxss,
np.squeeze(p.e[-1, :, :]),
p,
)
yss_before_tax_mat = household.get_y(
r_p_ss, wss, bssmat_s, nssmat, p, "SS"
)
Css = aggr.get_C(cssmat, p, "SS")
c_i_ss_mat = household.get_ci(
cssmat, p_i_ss, p_tilde_ss, p.tau_c[-1, :], p.alpha_c
)
C_vec_ss = np.zeros(p.I)
for i_ind in range(
p.I
): # TODO: update aggr.get_C to take full IxSxJ array
C_vec_ss[i_ind] = aggr.get_C(
c_i_ss_mat[i_ind, :, :],
p,
"SS",
)
(
total_tax_revenue,
iit_payroll_tax_revenue,
agg_pension_outlays,
UBI_outlays,
bequest_tax_revenue,
wealth_tax_revenue,
cons_tax_revenue,
business_tax_revenue,
payroll_tax_revenue,
iit_revenue,
) = aggr.revenue(
r_p_ss,
wss,
bssmat_s,
nssmat,
bqssmat,
c_i_ss_mat,
Y_vec_ss,
L_vec_ss,
K_vec_ss,
p_m,
factor,
ubissmat,
theta,
etr_params_3D,
np.squeeze(p.e[-1, :, :]),
p,
None,
"SS",
)
Gss = fiscal.get_G_ss(
Yss,
total_tax_revenue,
agg_pension_outlays,
TR_ss,
UBI_outlays,
I_g_ss,
new_borrowing,
debt_service,
p,
)
# Compute total investment (not just domestic)
Iss_total = aggr.get_I(None, Kss, Kss, p, "total_ss")
# solve resource constraint
# net foreign borrowing
debt_service_f = fiscal.get_debt_service_f(r_p_ss, D_f_ss)
net_capital_outflows = aggr.get_capital_outflows(
r_p_ss, K_f_ss, new_borrowing_f, debt_service_f, p
)
# Fill in arrays, noting that M-1 industries only produce consumption goods
G_vec_ss = np.zeros(p.M)
# Map consumption goods back to demands for production goods
print("IO: ", p.io_matrix.T.shape, ", C: ", C_vec_ss.shape)
C_m_vec_ss = np.dot(p.io_matrix.T, C_vec_ss)
G_vec_ss[-1] = Gss
I_d_vec_ss = np.zeros(p.M)
I_d_vec_ss[-1] = I_d_ss
I_g_vec_ss = np.zeros(p.M)
I_g_vec_ss[-1] = I_g_ss
net_capital_outflows_vec = np.zeros(p.M)
net_capital_outflows_vec[-1] = net_capital_outflows
RM_vec_ss = np.zeros(p.M)
RM_vec_ss[-1] = RM_ss
RC = aggr.resource_constraint(
Y_vec_ss,
C_m_vec_ss,
G_vec_ss,
I_d_vec_ss,
I_g_vec_ss,
net_capital_outflows_vec,
RM_vec_ss,
)
if VERBOSE:
print("Foreign debt holdings = ", D_f_ss)
print("Foreign capital holdings = ", K_f_ss)
print("resource constraint: ", RC)
if Gss < 0:
print(
"Steady state government spending is negative to satisfy"
+ " budget"
)
if ENFORCE_SOLUTION_CHECKS and (max(np.absolute(RC)) > p.RC_SS):
print("Resource Constraint Difference:", RC)
err = "Steady state aggregate resource constraint not satisfied"
raise RuntimeError(err)
# check constraints
household.constraint_checker_SS(bssmat_splus1, nssmat, cssmat, p.ltilde)
euler_savings = euler_errors[: p.S, :]
euler_labor_leisure = euler_errors[p.S :, :]
if VERBOSE:
print(
"Maximum error in labor FOC = ",
np.absolute(euler_labor_leisure).max(),
)
print(
"Maximum error in savings FOC = ", np.absolute(euler_savings).max()
)
# Return dictionary of SS results
output = {
"Kss": Kss,
"K_f_ss": K_f_ss,
"K_d_ss": K_d_ss,