-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
TPI.py
1410 lines (1303 loc) · 44.1 KB
/
TPI.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
"""
Time path iteration (TPI) module for OG-Core.
This module contains the following functions:
get_initial_SS_values()
firstdoughnutring()
twist_doughnut()
inner_loop()
run_TPI()
"""
# imports
import numpy as np
import pickle
import scipy.optimize as opt
from dask import delayed, compute
import dask.multiprocessing
from ogcore import tax, utils, household, firm, 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
"""
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
def get_initial_SS_values(p):
"""
Get values of variables for the initial period and the steady state
equilibrium values.
Args:
p (OG-Core Specifications object): model parameters
Returns:
(tuple): initial period and steady state values:
* initial_values (tuple): initial period variable values,
(b_sinit, b_splus1init, factor, initial_b, initial_n)
* ss_vars (dictionary): dictionary with steady state
solution results
* theta (Numpy array): steady-state retirement replacement
rates, length J
* baseline_values (tuple): (Ybaseline, TRbaseline, Gbaseline,
D0_baseline), GDP, lump sum transfer, and government spending
amounts from the baseline model run
"""
baseline_ss = os.path.join(p.baseline_dir, "SS", "SS_vars.pkl")
ss_baseline_vars = utils.safe_read_pickle(baseline_ss)
factor = ss_baseline_vars["factor_ss"]
B0 = aggr.get_B(ss_baseline_vars["bssmat_splus1"], p, "SS", True)
initial_b = ss_baseline_vars["bssmat_splus1"] * (
ss_baseline_vars["Bss"] / B0
)
initial_n = ss_baseline_vars["nssmat"]
Ybaseline = None
TRbaseline = None
Gbaseline = None
Ig_baseline = None
if not p.baseline:
baseline_tpi = os.path.join(p.baseline_dir, "TPI", "TPI_vars.pkl")
tpi_baseline_vars = utils.safe_read_pickle(baseline_tpi)
Ybaseline = tpi_baseline_vars["Y"]
if p.baseline_spending:
baseline_tpi = os.path.join(p.baseline_dir, "TPI", "TPI_vars.pkl")
tpi_baseline_vars = utils.safe_read_pickle(baseline_tpi)
TRbaseline = tpi_baseline_vars["TR"]
Gbaseline = tpi_baseline_vars["G"]
Ig_baseline = tpi_baseline_vars["I_g"]
if p.baseline:
ss_vars = ss_baseline_vars
else:
reform_ss_path = os.path.join(p.output_base, "SS", "SS_vars.pkl")
ss_vars = utils.safe_read_pickle(reform_ss_path)
theta = ss_vars["theta"]
"""
------------------------------------------------------------------------
Set other parameters and initial values
------------------------------------------------------------------------
"""
# Get an initial distribution of wealth with the initial population
# distribution. When small_open=True, the value of K0 is used as a
# placeholder for first-period wealth
B0 = aggr.get_B(initial_b, p, "SS", True)
b_sinit = np.array(
list(np.zeros(p.J).reshape(1, p.J)) + list(initial_b[:-1])
)
b_splus1init = initial_b
# Intial gov't debt and capital stock must match that in the baseline
if not p.baseline:
baseline_tpi = os.path.join(p.baseline_dir, "TPI", "TPI_vars.pkl")
tpi_baseline_vars = utils.safe_read_pickle(baseline_tpi)
D0_baseline = tpi_baseline_vars["D"][0]
Kg0_baseline = tpi_baseline_vars["K_g"][0]
else:
RM0_baseline = None
D0_baseline = None
Kg0_baseline = None
initial_values = (B0, b_sinit, b_splus1init, factor, initial_b, initial_n)
baseline_values = (
Ybaseline,
TRbaseline,
Gbaseline,
Ig_baseline,
D0_baseline,
Kg0_baseline,
)
return initial_values, ss_vars, theta, baseline_values
def firstdoughnutring(
guesses, r, w, p_tilde, bq, rm, tr, theta, factor, ubi, j, initial_b, p
):
"""
Solves the first entries of the upper triangle of the twist doughnut. This
is separate from the main TPI function because the values of b and n are
scalars, so it is easier to just have a separate function for these cases.
Args:
guesses (Numpy array): initial guesses for b and n, length 2
r (scalar): real interest rate
w (scalar): real wage rate
p_tilde (scalar): composite good price
bq (scalar): bequest amounts by age
rm (scalar): remittance amounts by age
tr (scalar): government transfer amount
theta (Numpy array): retirement replacement rates, length J
factor (scalar): scaling factor converting model units to dollars
ubi (scalar): individual UBI credit to household s=E+S of type j in
period 0
j (int): index of ability type
initial_b (Numpy array): SxJ matrix, savings of agents alive at T=0
p (OG-Core Specifications object): model parameters
Returns:
euler errors (Numpy array): errors from first order conditions,
length 2
"""
b_splus1 = float(guesses[0])
n = float(guesses[1])
b_s = float(initial_b[-2, j])
# Find errors from FOC for savings and FOC for labor supply
error1 = household.FOC_savings(
np.array([r]),
np.array([w]),
np.array([p_tilde]),
b_s,
np.array([b_splus1]),
np.array([n]),
np.array([bq]),
np.array([rm]),
factor,
np.array([tr]),
np.array([ubi]),
theta[j],
p.rho[0, -1],
p.etr_params[0][-1],
p.mtry_params[0][-1],
None,
j,
p,
"TPI_scalar",
)
error2 = household.FOC_labor(
np.array([r]),
np.array([w]),
np.array([p_tilde]),
b_s,
b_splus1,
np.array([n]),
np.array([bq]),
np.array([rm]),
factor,
np.array([tr]),
np.array([ubi]),
theta[j],
p.chi_n[0, -1],
p.etr_params[0][-1],
p.mtrx_params[0][-1],
None,
j,
p,
"TPI_scalar",
)
if n <= 0 or n >= 1:
error2 += 1e12
if b_splus1 <= 0:
error1 += 1e12
return [np.squeeze(error1)] + [np.squeeze(error2)]
def twist_doughnut(
guesses,
r,
w,
p_tilde,
bq,
rm,
tr,
theta,
factor,
ubi,
j,
s,
t,
etr_params,
mtrx_params,
mtry_params,
initial_b,
p,
):
"""
Solves the upper triangle of time path iterations. These are the agents who
are alive at time T=0 so that we do not solve for their full lifetime (so
of their life was before the model begins).
Args:
guesses (list): initial guesses for b and n, length 2s
r (Numpy array): real interest rate
w (Numpy array): real wage rate
p_tilde (Numpy array): composite good price
bq (Numpy array): bequest amounts by age, length s
rm (Numpy array): remittance amounts by age, length s
tr (Numpy array): government transfer amount
theta (Numpy array): retirement replacement rates, length J
factor (scalar): scaling factor converting model units to dollars
ubi (Numpy array): length remaining periods of life UBI payout to household
j (int): index of ability type
s (int): years of life remaining
t (int): model period
etr_params (list): ETR function parameters,
list of lists with size = sxsxnum_params
mtrx_params (list): labor income MTR function parameters,
list of lists with size = sxsxnum_params
mtry_params (list): capital income MTR function
parameters, lists of lists with size = sxsxnum_params
initial_b (Numpy array): savings of agents alive at T=0,
size = SxJ
p (OG-Core Specifications object): model parameters
Returns:
euler errors (Numpy array): errors from first order conditions,
length 2s
"""
length = int(len(guesses) / 2)
b_guess = np.array(guesses[:length])
n_guess = np.array(guesses[length:])
if length == p.S:
b_s = np.array([0] + list(b_guess[:-1]))
else:
b_s = np.array([(initial_b[-(s + 3), j])] + list(b_guess[:-1]))
b_splus1 = b_guess
w_s = w[t : t + length]
r_s = r[t : t + length]
p_tilde_s = p_tilde[t : t + length]
n_s = n_guess
chi_n_s = np.diag(p.chi_n[t : t + p.S, :], max(p.S - length, 0))
rho_s = np.diag(p.rho[t : t + p.S, :], max(p.S - length, 0))
error1 = household.FOC_savings(
r_s,
w_s,
p_tilde_s,
b_s,
b_splus1,
n_s,
bq,
rm,
factor,
tr,
ubi,
theta,
rho_s,
etr_params,
mtry_params,
t,
j,
p,
"TPI",
)
error2 = household.FOC_labor(
r_s,
w_s,
p_tilde_s,
b_s,
b_splus1,
n_s,
bq,
rm,
factor,
tr,
ubi,
theta,
chi_n_s,
etr_params,
mtrx_params,
t,
j,
p,
"TPI",
)
# Check and punish constraint violations
mask1 = n_guess < 0
error2[mask1] += 1e12
mask2 = n_guess > p.ltilde
error2[mask2] += 1e12
mask4 = b_guess <= 0
error2[mask4] += 1e12
mask5 = b_splus1 < 0
error2[mask5] += 1e12
return list(error1.flatten()) + list(error2.flatten())
def inner_loop(guesses, outer_loop_vars, initial_values, ubi, j, ind, p):
"""
Given path of economic aggregates and factor prices, solves
household problem. This has been termed the inner-loop (in
contrast to the outer fixed point loop that solves for GE factor
prices and economic aggregates).
Args:
guesses (tuple): initial guesses for b and n, (guesses_b,
guesses_n)
outer_loop_vars (tuple): values for factor prices and economic
aggregates used in household problem
(r_p, r, w, p_m, BQ, RM, TR, theta)
r_p (Numpy array): real interest rate on household portfolio
r (Numpy array): real interest rate on private capital
w (Numpy array): real wage rate
p_m (Numpy array): output goods prices
BQ (array_like): aggregate bequest amounts
TR (Numpy array): lump sum transfer amount
theta (Numpy array): retirement replacement rates, length J
initial_values (tuple): initial period variable values,
(b_sinit, b_splus1init, factor, initial_b, initial_n,
D0_baseline)
ubi (array_like): T+S x S x J array time series of UBI transfers in
model units for each type-j age-s household in every period t
j (int): index of ability type
ind (Numpy array): integers from 0 to S-1
p (OG-Core Specifications object): model parameters
Returns:
(tuple): household solution results:
* euler_errors (Numpy array): errors from FOCs, size = Tx2S
* b_mat (Numpy array): savings amounts, size = TxS
* n_mat (Numpy array): labor supply amounts, size = TxS
"""
(K0, b_sinit, b_splus1init, factor, initial_b, initial_n) = initial_values
guesses_b, guesses_n = guesses
r_p, r, w, p_m, BQ, RM, TR, theta = outer_loop_vars
# compute composite good price
p_i = (
np.tile(p.io_matrix.reshape(1, p.I, p.M), (p.T + p.S, 1, 1))
* np.tile(p_m.reshape(p.T + p.S, 1, p.M), (1, p.I, 1))
).sum(axis=2)
p_tilde = aggr.get_ptilde(p_i[:, :], p.tau_c[:, :], p.alpha_c, "TPI")
# compute bq
bq = household.get_bq(BQ, None, p, "TPI")
# compute tr
tr = household.get_tr(TR, None, p, "TPI")
# compute rm
rm = household.get_rm(RM, None, p, "TPI")
# initialize arrays
b_mat = np.zeros((p.T + p.S, p.S))
n_mat = np.zeros((p.T + p.S, p.S))
euler_errors = np.zeros((p.T, 2 * p.S))
solutions = opt.root(
firstdoughnutring,
[guesses_b[0, -1], guesses_n[0, -1]],
args=(
r_p[0],
w[0],
p_tilde[0],
bq[0, -1, j],
rm[0, -1, j],
tr[0, -1, j],
theta * p.replacement_rate_adjust[0],
factor,
ubi[0, -1, j],
j,
initial_b,
p,
),
method=p.FOC_root_method,
tol=MINIMIZER_TOL,
)
b_mat[0, -1], n_mat[0, -1] = solutions.x[0], solutions.x[1]
for s in range(p.S - 2): # Upper triangle
ind2 = np.arange(s + 2)
b_guesses_to_use = np.diag(guesses_b[: p.S, :], p.S - (s + 2))
n_guesses_to_use = np.diag(guesses_n[: p.S, :], p.S - (s + 2))
theta_to_use = theta[j] * p.replacement_rate_adjust[: p.S]
bq_to_use = np.diag(bq[: p.S, :, j], p.S - (s + 2))
rm_to_use = np.diag(rm[: p.S, :, j], p.S - (s + 2))
tr_to_use = np.diag(tr[: p.S, :, j], p.S - (s + 2))
ubi_to_use = np.diag(ubi[: p.S, :, j], p.S - (s + 2))
num_params = len(p.etr_params[0][0])
temp_etr = [
[p.etr_params[t][p.S - s - 2 + t][i] for i in range(num_params)]
for t in range(s + 2)
]
etr_params_to_use = [
[temp_etr[i][j] for j in range(num_params)] for i in range(s + 2)
]
temp_mtrx = [
[p.mtrx_params[t][p.S - s - 2 + t][i] for i in range(num_params)]
for t in range(s + 2)
]
mtrx_params_to_use = [
[temp_mtrx[i][j] for j in range(num_params)] for i in range(s + 2)
]
temp_mtry = [
[p.mtry_params[t][p.S - s - 2 + t][i] for i in range(num_params)]
for t in range(s + 2)
]
mtry_params_to_use = [
[temp_mtry[i][j] for j in range(num_params)] for i in range(s + 2)
]
solutions = opt.root(
twist_doughnut,
list(b_guesses_to_use) + list(n_guesses_to_use),
args=(
r_p,
w,
p_tilde,
bq_to_use,
rm_to_use,
tr_to_use,
theta_to_use,
factor,
ubi_to_use,
j,
s,
0,
etr_params_to_use,
mtrx_params_to_use,
mtry_params_to_use,
initial_b,
p,
),
method=p.FOC_root_method,
tol=MINIMIZER_TOL,
)
b_vec = solutions.x[: int(len(solutions.x) / 2)]
b_mat[ind2, p.S - (s + 2) + ind2] = b_vec
n_vec = solutions.x[int(len(solutions.x) / 2) :]
n_mat[ind2, p.S - (s + 2) + ind2] = n_vec
for t in range(0, p.T):
b_guesses_to_use = 0.75 * np.diag(guesses_b[t : t + p.S, :])
n_guesses_to_use = np.diag(guesses_n[t : t + p.S, :])
theta_to_use = theta[j] * p.replacement_rate_adjust[t : t + p.S]
bq_to_use = np.diag(bq[t : t + p.S, :, j])
rm_to_use = np.diag(rm[t : t + p.S, :, j])
tr_to_use = np.diag(tr[t : t + p.S, :, j])
ubi_to_use = np.diag(ubi[t : t + p.S, :, j])
# initialize array of diagonal elements
num_params = len(p.etr_params[t][0])
etr_params_to_use = [
[p.etr_params[t + s][s][i] for i in range(num_params)]
for s in range(p.S)
]
mtrx_params_to_use = [
[p.mtrx_params[t + s][s][i] for i in range(num_params)]
for s in range(p.S)
]
mtry_params_to_use = [
[p.mtry_params[t + s][s][i] for i in range(num_params)]
for s in range(p.S)
]
solutions = opt.root(
twist_doughnut,
list(b_guesses_to_use) + list(n_guesses_to_use),
args=(
r_p,
w,
p_tilde,
bq_to_use,
rm_to_use,
tr_to_use,
theta_to_use,
factor,
ubi_to_use,
j,
None,
t,
etr_params_to_use,
mtrx_params_to_use,
mtry_params_to_use,
initial_b,
p,
),
method=p.FOC_root_method,
tol=MINIMIZER_TOL,
)
euler_errors[t, :] = solutions.fun
b_vec = solutions.x[: p.S]
b_mat[t + ind, ind] = b_vec
n_vec = solutions.x[p.S :]
n_mat[t + ind, ind] = n_vec
# print('Type ', j, ' max euler error = ',
# np.absolute(euler_errors).max())
return euler_errors, b_mat, n_mat
def run_TPI(p, client=None):
"""
Solve for transition path equilibrium of OG-Core.
Args:
p (OG-Core Specifications object): model parameters
client (Dask client object): client
Returns:
output (dictionary): dictionary with transition path solution
results
"""
global scattered_p
if client:
scattered_p = client.scatter(p, broadcast=True)
else:
scattered_p = p
# unpack tuples of parameters
initial_values, ss_vars, theta, baseline_values = get_initial_SS_values(p)
(B0, b_sinit, b_splus1init, factor, initial_b, initial_n) = initial_values
(
Ybaseline,
TRbaseline,
Gbaseline,
Ig_baseline,
D0_baseline,
Kg0_baseline,
) = baseline_values
# Create time path of UBI household benefits and aggregate UBI outlays
ubi = p.ubi_nom_array / factor
UBI = aggr.get_L(ubi[: p.T], p, "TPI")
print(
"Government spending breakpoints are tG1: ", p.tG1, "; and tG2:", p.tG2
)
# Initialize guesses at time paths
# Make array of initial guesses for labor supply and savings
guesses_b = utils.get_initial_path(
initial_b, ss_vars["bssmat_splus1"], p, "ratio"
)
guesses_n = utils.get_initial_path(
initial_n, ss_vars["nssmat"], p, "ratio"
)
b_mat = guesses_b
n_mat = guesses_n
ind = np.arange(p.S)
# Get path for aggregate savings and labor supply
L_init = np.ones((p.T + p.S,)) * ss_vars["Lss"]
B_init = np.ones((p.T + p.S,)) * ss_vars["Bss"]
L_init[: p.T] = aggr.get_L(n_mat[: p.T], p, "TPI")
B_init[1 : p.T] = aggr.get_B(b_mat[: p.T], p, "TPI", False)[: p.T - 1]
B_init[0] = B0
K_init = B_init * ss_vars["Kss"] / ss_vars["Bss"]
K = K_init
K_d = K_init * ss_vars["K_d_ss"] / ss_vars["Kss"]
K_f = K_init * ss_vars["K_f_ss"] / ss_vars["Kss"]
L = L_init
B = B_init
K_g = np.ones_like(K) * ss_vars["K_g_ss"]
Y = np.zeros_like(K)
Y[: p.T] = firm.get_Y(K[: p.T], K_g[: p.T], L[: p.T], p, "TPI")
Y[p.T :] = ss_vars["Yss"]
# path for industry specific aggregates
K_vec_init = np.ones((p.T + p.S, p.M)) * ss_vars["K_vec_ss"].reshape(
1, p.M
)
L_vec_init = np.ones((p.T + p.S, p.M)) * ss_vars["L_vec_ss"].reshape(
1, p.M
)
Y_vec_init = np.ones((p.T + p.S, p.M)) * ss_vars["Y_vec_ss"].reshape(
1, p.M
)
# compute w
w = np.ones_like(K) * ss_vars["wss"]
# compute goods prices
p_m = np.ones((p.T + p.S, p.M)) * ss_vars["p_m_ss"].reshape(1, p.M)
p_m[: p.T, :] = firm.get_pm(
w[: p.T], Y_vec_init[: p.T, :], L_vec_init[: p.T, :], p, "TPI"
)
p_m = p_m / p_m[:, -1].reshape(
p.T + p.S, 1
) # normalize prices by industry M
p_i = (
np.tile(p.io_matrix.reshape(1, p.I, p.M), (p.T + p.S, 1, 1))
* np.tile(p_m.reshape(p.T + p.S, 1, p.M), (1, p.I, 1))
).sum(axis=2)
p_tilde = aggr.get_ptilde(p_i[:, :], p.tau_c[:, :], p.alpha_c, "TPI")
if not any(p.zeta_K == 1):
w[: p.T] = np.squeeze(
firm.get_w(Y[: p.T], L[: p.T], p_m[: p.T, :], p, "TPI")
)
# repeat with updated w
p_m[: p.T, :] = firm.get_pm(
w[: p.T], Y_vec_init[: p.T, :], L_vec_init[: p.T, :], p, "TPI"
)
p_m = p_m / p_m[:, -1].reshape(
p.T + p.S, 1
) # normalize prices by industry M
p_i = (
np.tile(p.io_matrix.reshape(1, p.I, p.M), (p.T + p.S, 1, 1))
* np.tile(p_m.reshape(p.T + p.S, 1, p.M), (1, p.I, 1))
).sum(axis=2)
p_tilde = aggr.get_ptilde(p_i[:, :], p.tau_c[:, :], p.alpha_c, "TPI")
# path for interest rates
r = np.zeros_like(Y)
r[: p.T] = np.squeeze(
firm.get_r(Y[: p.T], K[: p.T], p_m[: p.T, :], p, "TPI")
)
r[p.T :] = ss_vars["rss"]
# For case where economy is small open econ
r[p.zeta_K == 1] = p.world_int_rate[p.zeta_K == 1]
# initial guesses at fiscal vars
if p.budget_balance:
if np.abs(ss_vars["TR_ss"]) < 1e-13:
TR_ss2 = 0.0 # sometimes SS is very small but not zero,
# even if taxes are zero, this get's rid of the
# approximation error, which affects the pct changes below
else:
TR_ss2 = ss_vars["TR_ss"]
TR = np.ones(p.T + p.S) * TR_ss2
total_tax_revenue = TR - ss_vars["agg_pension_outlays"]
G = np.zeros(p.T + p.S)
D = np.zeros(p.T + p.S)
D_d = np.zeros(p.T + p.S)
D_f = np.zeros(p.T + p.S)
I_g = fiscal.get_I_g(Y[: p.T], None, p, "TPI")
else:
if p.baseline_spending:
# Will set to TRbaseline here, but will be updated in TPI loop
# with call to fiscal.get_TR
TR = np.concatenate(
(TRbaseline[: p.T], np.ones(p.S) * ss_vars["TR_ss"])
)
# Will set to Ig_baseline here, but will be updated in TPI loop
# with call to fiscal.get_I_g
I_g = np.concatenate(
(Ig_baseline[: p.T], np.ones(p.S) * ss_vars["I_g_ss"])
)
# Will set to Gbaseline here, but will be updated in TPI loop
# with call to fiscal.D_G_path, which also does closure rule
G = np.concatenate(
(Gbaseline[: p.T], np.ones(p.S) * ss_vars["Gss"])
)
else:
TR = p.alpha_T * Y
G = np.ones(p.T + p.S) * ss_vars["Gss"]
I_g = np.ones(p.T + p.S) * ss_vars["I_g_ss"]
D = np.ones(p.T + p.S) * ss_vars["Dss"]
D_d = D * ss_vars["D_d_ss"] / ss_vars["Dss"]
D_f = D * ss_vars["D_f_ss"] / ss_vars["Dss"]
if p.baseline:
K_g0 = p.initial_Kg_ratio * Y[0]
else:
K_g0 = Kg0_baseline
K_g = fiscal.get_K_g(K_g0, I_g, p, "TPI")
total_tax_revenue = np.ones(p.T + p.S) * ss_vars["total_tax_revenue"]
# Compute other interest rates
r_gov = fiscal.get_r_gov(r, p, "TPI")
r_p = np.ones_like(r) * ss_vars["r_p_ss"]
MPKg = np.zeros((p.T, p.M))
for m in range(p.M):
MPKg[:, m] = np.squeeze(
firm.get_MPx(
Y_vec_init[: p.T, m], K_g[: p.T], p.gamma_g[m], p, "TPI", m
)
)
r_p[: p.T] = aggr.get_r_p(
r[: p.T],
r_gov[: p.T],
p_m[: p.T, :],
K_vec_init[: p.T, :],
K_g[: p.T],
D[: p.T],
MPKg,
p,
"TPI",
)
# Initialize bequests
BQ0 = aggr.get_BQ(r_p[0], initial_b, None, p, "SS", True)
if not p.use_zeta:
BQ = np.zeros((p.T + p.S, p.J))
for j in range(p.J):
BQ[:, j] = (
list(np.linspace(BQ0[j], ss_vars["BQss"][j], p.T))
+ [ss_vars["BQss"][j]] * p.S
)
BQ = np.array(BQ)
else:
BQ = (
list(np.linspace(BQ0, ss_vars["BQss"], p.T))
+ [ss_vars["BQss"]] * p.S
)
BQ = np.array(BQ)
# Initialize aggregate remittances
if p.baseline:
RM = aggr.get_RM(Y, p, "TPI")
else:
# This is the reform case and is based off of Ybaseline, but allows for
# remittance parameters to change in a reform and update the RM series
Ybaseline_ext = np.concatenate(
[Ybaseline, np.ones(p.S) * Ybaseline[-1]]
)
RM = aggr.get_RM(Ybaseline_ext, p, "TPI")
# Start transition path iteration (TPI)
TPIiter = 0
TPIdist = 10
euler_errors = np.zeros((p.T, 2 * p.S, p.J))
TPIdist_vec = np.zeros(p.maxiter)
# TPI loop
while (TPIiter < p.maxiter) and (TPIdist >= p.mindist_TPI):
outer_loop_vars = (r_p, r, w, p_m, BQ, RM, TR, theta)
# compute composite good price
p_i = (
np.tile(p.io_matrix.reshape(1, p.I, p.M), (p.T + p.S, 1, 1))
* np.tile(p_m.reshape(p.T + p.S, 1, p.M), (1, p.I, 1))
).sum(axis=2)
p_tilde = aggr.get_ptilde(p_i[:, :], p.tau_c[:, :], p.alpha_c, "TPI")
euler_errors = np.zeros((p.T, 2 * p.S, p.J))
lazy_values = []
for j in range(p.J):
guesses = (guesses_b[:, :, j], guesses_n[:, :, j])
lazy_values.append(
delayed(inner_loop)(
guesses,
outer_loop_vars,
initial_values,
ubi,
j,
ind,
scattered_p,
)
)
if client:
futures = client.compute(lazy_values, num_workers=p.num_workers)
results = client.gather(futures)
else:
results = compute(
*lazy_values,
scheduler=dask.multiprocessing.get,
num_workers=p.num_workers,
)
for j, result in enumerate(results):
euler_errors[:, :, j], b_mat[:, :, j], n_mat[:, :, j] = result
bmat_s = np.zeros((p.T, p.S, p.J))
bmat_s[0, 1:, :] = initial_b[:-1, :]
bmat_s[1:, 1:, :] = b_mat[: p.T - 1, :-1, :]
bmat_splus1 = np.zeros((p.T, p.S, p.J))
bmat_splus1[:, :, :] = b_mat[: p.T, :, :]
num_params = len(p.etr_params[0][0])
etr_params_4D = [
[
[
[p.etr_params[t][s][i] for i in range(num_params)]
for j in range(p.J)
]
for s in range(p.S)
]
for t in range(p.T)
]
bqmat = household.get_bq(BQ, None, p, "TPI")
rmmat = household.get_rm(RM, None, p, "TPI")
trmat = household.get_tr(TR, None, p, "TPI")
tax_mat = tax.net_taxes(
r_p[: p.T],
w[: p.T],
bmat_s,
n_mat[: p.T, :, :],
bqmat[: p.T, :, :],
factor,
trmat[: p.T, :, :],
ubi[: p.T, :, :],
theta,
0,
None,
False,
"TPI",
p.e,
etr_params_4D,
p,
)
r_p_path = utils.to_timepath_shape(r_p)
p_tilde_path = utils.to_timepath_shape(p_tilde)
wpath = utils.to_timepath_shape(w)
c_mat = household.get_cons(
r_p_path[: p.T, :, :],
wpath[: p.T, :, :],
p_tilde_path[: p.T, :, :],
bmat_s,
bmat_splus1,
n_mat[: p.T, :, :],
bqmat[: p.T, :, :],
rmmat[: p.T, :, :],
tax_mat,
p.e,
p,
)
C = aggr.get_C(c_mat, p, "TPI")
c_i = household.get_ci(
c_mat[: p.T, :, :],
p_i[: p.T, :],
p_tilde[: p.T],
p.tau_c[: p.T, :],
p.alpha_c,
"TPI",
)
y_before_tax_mat = household.get_y(
r_p_path[: p.T, :, :],
wpath[: p.T, :, :],
bmat_s[: p.T, :, :],
n_mat[: p.T, :, :],
p,
"TPI",
)
L[: p.T] = aggr.get_L(n_mat[: p.T], p, "TPI")
B[1 : p.T] = aggr.get_B(bmat_splus1[: p.T], p, "TPI", False)[: p.T - 1]
w_open = firm.get_w_from_r(p.world_int_rate[: p.T], p, "TPI")
# Find output, labor demand, capital demand for M-1 industries
L_vec = np.zeros((p.T, p.M))
K_vec = np.zeros((p.T, p.M))
C_vec = np.zeros((p.T, p.I))
K_demand_open_vec = np.zeros((p.T, p.M))
for i_ind in range(p.I):
C_vec[:, i_ind] = aggr.get_C(c_i[: p.T, i_ind, :, :], p, "TPI")
Y_vec = (
np.tile(p.io_matrix.reshape(1, p.I, p.M), (p.T, 1, 1))
* np.tile(C_vec[: p.T, :].reshape(p.T, p.I, 1), (1, 1, p.M))
).sum(axis=1)
for m_ind in range(p.M - 1):
KYrat_m = firm.get_KY_ratio(
r[: p.T], p_m[: p.T, :], p, "TPI", 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, "TPI", m_ind
)
K_demand_open_vec[:, m_ind] = firm.get_K(
p.world_int_rate[: p.T],
w_open[: p.T],
L_vec[: p.T, m_ind],
p,
"TPI",
m_ind,
)
# Find output, labor demand, capital demand for last industry
L_M = np.maximum(
np.ones(p.T) * 0.001, L[: p.T] - L_vec[: p.T, :].sum(-1)
) # make sure L_M > 0
K_demand_open_vec[:, -1] = firm.get_K(
p.world_int_rate[: p.T], w_open[: p.T], L_M[: p.T], p, "TPI", -1
)
K[: p.T], K_d[: p.T], K_f[: p.T] = aggr.get_K_splits(
B[: p.T],
K_demand_open_vec[: p.T, :].sum(-1),
D_d[: p.T],
p.zeta_K[: p.T],
)
K_M = np.maximum(
np.ones(p.T) * 0.001, K[: p.T] - K_vec[: p.T, :].sum(-1)
) # make sure K_M > 0
L_vec[:, -1] = L_M
K_vec[:, -1] = K_M
Y_vec[:, -1] = firm.get_Y(
K_vec[: p.T, -1], K_g[: p.T], L_vec[: p.T, -1], p, "TPI", -1
)
Y = (p_m[: p.T, :] * Y_vec[: p.T, :]).sum(-1)
(
total_tax_rev,
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[: p.T],
w[: p.T],
bmat_s,
n_mat[: p.T, :, :],
bqmat[: p.T, :, :],
c_i[: p.T, :, :, :],
Y_vec[: p.T, :],
L_vec[: p.T, :],
K_vec[: p.T, :],
p_m[: p.T, :],
factor,
ubi[: p.T, :, :],
theta,
etr_params_4D,
p.e,
p,
None,
"TPI",
)
total_tax_revenue[: p.T] = total_tax_rev
dg_fixed_values = (
Y,
total_tax_revenue,
agg_pension_outlays,
UBI_outlays,
TR,
I_g,
Gbaseline,
D0_baseline,
)
(
Dnew,
G[: p.T],
D_d[: p.T],
D_f[: p.T],
new_borrowing,
debt_service,
new_borrowing_f,
) = fiscal.D_G_path(r_gov, dg_fixed_values, p)
K[: p.T], K_d[: p.T], K_f[: p.T] = aggr.get_K_splits(
B[: p.T], K_demand_open_vec.sum(-1), D_d[: p.T], p.zeta_K[: p.T]
)