@@ -551,34 +551,35 @@ class ClosedFormTrans:
551
551
self.α, self.β = α, β
552
552
553
553
def simulate(self,
554
- T, # length of transitional path to simulate
555
- init_ss, # initial steady state
556
- τ_pol=None, # sequence of tax rates
557
- D_pol=None, # sequence of government debt levels
558
- G_pol=None): # sequence of government purchases
559
-
554
+ T, # length of transitional path to simulate
555
+ init_ss, # initial steady state
556
+ τ_pol=None, # sequence of tax rates
557
+ D_pol=None, # sequence of government debt levels
558
+ G_pol=None): # sequence of government purchases
559
+
560
560
α, β = self.α, self.β
561
-
561
+
562
562
# unpack the steady state variables
563
563
K_hat, Y_hat, Cy_hat, Co_hat = init_ss[:4]
564
564
W_hat, r_hat = init_ss[4:6]
565
565
τ_hat, D_hat, G_hat = init_ss[6:9]
566
-
566
+
567
567
# initialize array containers
568
568
# K, Y, Cy, Co
569
569
quant_seq = np.empty((T+1, 4))
570
-
570
+
571
571
# W, r
572
572
price_seq = np.empty((T+1, 2))
573
-
573
+
574
574
# τ, D, G
575
575
policy_seq = np.empty((T+2, 3))
576
-
576
+
577
577
# t=0, starting from steady state
578
578
K0, Y0 = K_hat, Y_hat
579
579
W0, r0 = W_hat, r_hat
580
580
D0 = D_hat
581
-
581
+ δo = 0 # Define δo (set to 0 for this example, adjust as needed)
582
+
582
583
# fiscal policy
583
584
if τ_pol is None:
584
585
D1 = D_pol[1]
@@ -592,27 +593,27 @@ class ClosedFormTrans:
592
593
D1 = D_pol[1]
593
594
τ0 = τ_pol[0]
594
595
G0 = τ0 * (Y0 + r0 * D0) + D1 - (1 + r0) * D0
595
-
596
+
596
597
# optimal consumption plans
597
598
Cy0, Co0 = K_to_C(K0, D0, τ0, r0, α, β)
598
-
599
+
599
600
# t=0 economy
600
601
quant_seq[0, :] = K0, Y0, Cy0, Co0
601
602
price_seq[0, :] = W0, r0
602
603
policy_seq[0, :] = τ0, D0, G0
603
604
policy_seq[1, 1] = D1
604
-
605
+
605
606
# starting from t=1 to T
606
607
for t in range(1, T+1):
607
-
608
+
608
609
# transition of K
609
610
K_old, τ_old = quant_seq[t-1, 0], policy_seq[t-1, 0]
610
611
D = policy_seq[t, 1]
611
612
K = K_old ** α * (1 - τ_old) * (1 - α) * (1 - β) - D
612
-
613
+
613
614
# output, capital return, wage
614
615
Y, r, W = K_to_Y(K, α), K_to_r(K, α), K_to_W(K, α)
615
-
616
+
616
617
# to satisfy the government budget constraint
617
618
if τ_pol is None:
618
619
D = D_pol[t]
@@ -629,22 +630,32 @@ class ClosedFormTrans:
629
630
D_next = D_pol[t+1]
630
631
τ = τ_pol[t]
631
632
G = τ * (Y + r * D) + D_next - (1 + r) * D
632
-
633
+
633
634
# optimal consumption plans
634
- Cy, Co = K_to_C(K, D, τ, r, α, β)
635
-
635
+ result = minimize_scalar(lambda Cy: -Cy_val(Cy, W, r_next, τ, τ_next, δy, δo_next, β),
636
+ bounds=(1e-6, W*(1-τ)-δy-1e-6),
637
+ method='bounded')
638
+
639
+ Cy_opt = result.x
640
+ U_opt = -result.fun
641
+
642
+ Cy = Cy_opt
643
+ Co = (1 + r * (1 - τ)) * (K + D) - δo
644
+
636
645
# store time t economy aggregates
637
646
quant_seq[t, :] = K, Y, Cy, Co
638
647
price_seq[t, :] = W, r
639
648
policy_seq[t, 0] = τ
640
649
policy_seq[t+1, 1] = D_next
641
650
policy_seq[t, 2] = G
642
-
651
+
643
652
self.quant_seq = quant_seq
644
653
self.price_seq = price_seq
645
654
self.policy_seq = policy_seq
646
-
655
+
647
656
return quant_seq, price_seq, policy_seq
657
+
658
+
648
659
649
660
def plot(self):
650
661
@@ -967,57 +978,57 @@ class AK2():
967
978
self.α, self.β = α, β
968
979
969
980
def simulate(self,
970
- T, # length of transitional path to simulate
971
- init_ss, # initial steady state
972
- δy_seq, # sequence of lump sum tax for the young
973
- δo_seq, # sequence of lump sum tax for the old
974
- τ_pol=None, # sequence of tax rates
975
- D_pol=None, # sequence of government debt levels
976
- G_pol=None, # sequence of government purchases
977
- verbose=False,
978
- max_iter=500,
979
- tol=1e-5):
980
-
981
+ T, # length of transitional path to simulate
982
+ init_ss, # initial steady state
983
+ δy_seq, # sequence of lump sum tax for the young
984
+ δo_seq, # sequence of lump sum tax for the old
985
+ τ_pol=None, # sequence of tax rates
986
+ D_pol=None, # sequence of government debt levels
987
+ G_pol=None, # sequence of government purchases
988
+ verbose=False,
989
+ max_iter=500,
990
+ tol=1e-5):
991
+
981
992
α, β = self.α, self.β
982
-
993
+
983
994
# unpack the steady state variables
984
995
K_hat, Y_hat, Cy_hat, Co_hat = init_ss[:4]
985
996
W_hat, r_hat = init_ss[4:6]
986
997
τ_hat, D_hat, G_hat = init_ss[6:9]
987
-
998
+
988
999
# K, Y, Cy, Co
989
1000
quant_seq = np.empty((T+2, 4))
990
-
1001
+
991
1002
# W, r
992
1003
price_seq = np.empty((T+2, 2))
993
-
1004
+
994
1005
# τ, D, G
995
1006
policy_seq = np.empty((T+2, 3))
996
1007
policy_seq[:, 1] = D_pol
997
1008
policy_seq[:, 2] = G_pol
998
-
1009
+
999
1010
# initial guesses of prices
1000
1011
price_seq[:, 0] = np.ones(T+2) * W_hat
1001
1012
price_seq[:, 1] = np.ones(T+2) * r_hat
1002
-
1013
+
1003
1014
# initial guesses of policies
1004
1015
policy_seq[:, 0] = np.ones(T+2) * τ_hat
1005
-
1016
+
1006
1017
# t=0, starting from steady state
1007
1018
quant_seq[0, :2] = K_hat, Y_hat
1008
-
1019
+
1009
1020
if verbose:
1010
1021
# prepare to plot iterations until convergence
1011
1022
fig, axs = plt.subplots(1, 3, figsize=(14, 4))
1012
-
1023
+
1013
1024
# containers for checking convergence
1014
1025
price_seq_old = np.empty_like(price_seq)
1015
1026
policy_seq_old = np.empty_like(policy_seq)
1016
-
1027
+
1017
1028
# start iteration
1018
1029
i_iter = 0
1019
1030
while True:
1020
-
1031
+
1021
1032
if verbose:
1022
1033
# plot current prices at ith iteration
1023
1034
for i, name in enumerate(['W', 'r']):
@@ -1029,11 +1040,11 @@ class AK2():
1029
1040
axs[2].legend(bbox_to_anchor=(1.05, 1), loc='upper left')
1030
1041
axs[2].set_title('τ')
1031
1042
axs[2].set_xlabel('t')
1032
-
1043
+
1033
1044
# store old prices from last iteration
1034
1045
price_seq_old[:] = price_seq
1035
1046
policy_seq_old[:] = policy_seq
1036
-
1047
+
1037
1048
# start updating quantities and prices
1038
1049
for t in range(T+1):
1039
1050
K, Y = quant_seq[t, :2]
@@ -1043,40 +1054,41 @@ class AK2():
1043
1054
τ_next, D_next, G_next = policy_seq[t+1, :]
1044
1055
δy, δo = δy_seq[t], δo_seq[t]
1045
1056
δy_next, δo_next = δy_seq[t+1], δo_seq[t+1]
1046
-
1057
+
1047
1058
# consumption for the old
1048
1059
Co = (1 + r * (1 - τ)) * (K + D) - δo
1049
-
1060
+
1050
1061
# optimal consumption for the young
1051
- out = brent_max(Cy_val, 1e-6, W*(1-τ)-δy-1e-6,
1052
- args=(W, r_next, τ, τ_next,
1053
- δy, δo_next, β))
1054
- Cy = out[0]
1055
-
1062
+ result = minimize_scalar(lambda Cy: -Cy_val(Cy, W, r_next, τ, τ_next, δy, δo_next, β),
1063
+ bounds=(1e-6, W*(1-τ)-δy-1e-6),
1064
+ method='bounded')
1065
+
1066
+ Cy = result.x
1067
+
1056
1068
quant_seq[t, 2:] = Cy, Co
1057
1069
τ_num = ((1 + r) * D + G - D_next - δy - δo)
1058
1070
τ_denom = (Y + r * D)
1059
1071
policy_seq[t, 0] = τ_num / τ_denom
1060
-
1072
+
1061
1073
# saving of the young
1062
1074
A_next = W * (1 - τ) - δy - Cy
1063
-
1075
+
1064
1076
# transition of K
1065
1077
K_next = A_next - D_next
1066
1078
Y_next = K_to_Y(K_next, α)
1067
1079
W_next, r_next = K_to_W(K_next, α), K_to_r(K_next, α)
1068
-
1080
+
1069
1081
quant_seq[t+1, :2] = K_next, Y_next
1070
1082
price_seq[t+1, :] = W_next, r_next
1071
-
1083
+
1072
1084
i_iter += 1
1073
-
1085
+
1074
1086
if (np.max(np.abs(price_seq_old - price_seq)) < tol) & \
1075
1087
(np.max(np.abs(policy_seq_old - policy_seq)) < tol):
1076
1088
if verbose:
1077
1089
print(f"Converge using {i_iter} iterations")
1078
1090
break
1079
-
1091
+
1080
1092
if i_iter > max_iter:
1081
1093
if verbose:
1082
1094
print(f"Fail to converge using {i_iter} iterations")
@@ -1085,9 +1097,10 @@ class AK2():
1085
1097
self.quant_seq = quant_seq
1086
1098
self.price_seq = price_seq
1087
1099
self.policy_seq = policy_seq
1088
-
1100
+
1089
1101
return quant_seq, price_seq, policy_seq
1090
1102
1103
+
1091
1104
def plot(self):
1092
1105
1093
1106
quant_seq = self.quant_seq
0 commit comments