Skip to content

Commit 0bb1a7a

Browse files
committed
WIP
1 parent a8853dd commit 0bb1a7a

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ c_τ[~] <- b_τ[~]*r + y_τ[~]
5151

5252

5353
# exogenous processes
54+
5455
y[t] = y[~] + ρ_y*(y[t-1]-y[~])
5556
ζ[t] = ζ[~] + ρ_ζ*(ζ[t-1]-ζ[~])
5657

5758
a[t] = 1 + ρ_a*(a[t-1]-1)
5859

59-
d[t] = d[t-1] + e_d[t] # income shock for top earners (aggregate)
60-
d_b[t] = d_b[t-1] + e_d_b[t] # income shock for bottom earners (aggregate)
60+
d[t] = 0.99999*d[t-1] + e_d[t] # income shock for top earners (aggregate)
61+
d_b[t] = 0.99999*d_b[t-1] + e_d_b[t] # income shock for bottom earners (aggregate)
6162

6263
y_b[t] = a[t]*(1-y[t])*(1-ζ[t]) # labor income for bottom-earners (aggregate)
6364
y_τ[t] = a[t]*(1-y[t])*ζ[t] + (a[t]*y[t]) # top earners income (per capita)
@@ -66,7 +67,7 @@ c_b[t] = d_b[t] + b_b[t-1]*r - (b_b[t]-b_b[t-1])*p[t] + y_b[t] # consumption b
6667
c_τ[t] = d[t] + b_τ[t-1]*r - (b_τ[t]-b_τ[t-1])*p[t] + y_τ[t] # consumption top earners (aggregate)
6768

6869
W_τ[t] = q[t] + p[t]*(b_τ[t]) # wealth top earners (aggregate)
69-
W_b[t] = 1 + p[t]*(b_b[t]) # wealth bottom earners (aggregate)
70+
W_b[t] = 1 + p[t]*(b_b[t]) # wealth bottom earners (aggregate)
7071

7172
p[t] = β_b*(c_b[t+1]/c_b[t])^(-σ)*(r+p[t+1]) + φ_b*(W_b[t])^(-η_b)/(c_b[t])^(-σ)*p[t]
7273
p[t] = β_τ*(c_τ[t+1]/c_τ[t])^(-σ)*(r+p[t+1]) + φ_τ*(W_τ[t])^(-η )/(c_τ[t])^(-σ)*p[t]
@@ -83,11 +84,12 @@ r_w + p[t] = 1
8384

8485

8586
### Exogenous shocks
86-
# e_d[t] <- N(0, 0.01)
87-
# e_d_b[t] <- N(0, 0.01)
87+
# e_d[t] <- N(0, 0.01^2)
88+
# e_d_b[t] <- N(0, 0.01^2)
8889

8990
# exogenous shocks
9091
e_d[1] <- 0.01
92+
e_d[2] <- 0.0
9193
e_d_b[1] <- 0.0 # dummy assignment
9294

9395
# forall t, 1<=t<T+1: e_d[t] <- 0.01

ICAI/solve_icai.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dyno.symbolic_model import SymbolicModel
55
import pandas as pd
66

7-
model = SymbolicModel("ICAI/partial.dyno")
7+
model = SymbolicModel("ICAI/partial_deterministic.dyno")
88
model2 = SymbolicModel("ICAI/partial_shock.dyno")
99
dr2 = model2.solve()
1010
sim2 = dr2.irfs(type='level')['e_d'] # level seems to be ignored
@@ -17,13 +17,28 @@
1717
pd.DataFrame(v0, columns=model.symbols['variables'])
1818

1919
from dyno.solver import deterministic_solve
20+
from dyno.solver import solve as linsolve
2021
import time
2122

2223

23-
deterministic_solve(model)
24+
r0,A,B,C,D = model.jacobians
25+
X, evs = linsolve(A,B,C)
26+
r, J = model.deterministic_residuals_with_jacobian(v0.ravel(), sparsify=True)
27+
rr = r.reshape(v0.shape)
28+
29+
30+
31+
32+
33+
import numpy as np
34+
35+
np.isnan(r).sum()
36+
np.isnan(J.todense()).sum()
37+
38+
from scipy.sparse.linalg import spsolve
2439

2540
sims = [sim2]
26-
for t in [100,150,200]:
41+
for t in [150,200,250]:
2742

2843
m = model.recalibrate(T=t)
2944

@@ -33,27 +48,44 @@
3348
print("Elapsed time: ", t2 - t1)
3449
sims.append(sim)
3550

51+
sims.reverse()
52+
53+
54+
55+
3656

3757
plt.figure()
3858

3959
for i,sim in enumerate(sims):
40-
plt.subplot(131)
60+
plt.subplot(221)
4161
plt.plot(sim.index, sim['W_τ'], label=i)
62+
plt.ylim(2.4, 2.5)
4263
plt.legend()
4364
plt.xlim(0,50)
44-
plt.subplot(132)
65+
plt.subplot(222)
4566
plt.plot(sim.index, sim['c_τ'])
4667
plt.xlim(0,50)
47-
plt.subplot(133)
68+
plt.subplot(223)
4869
plt.plot(sim.index, sim['d'])
49-
plt.xlim(0,50)
70+
try:
71+
plt.subplot(224)
72+
plt.plot(sim.index, sim['e_d'])
73+
plt.ylim(-0.001, 0.01)
74+
except:
75+
pass
76+
5077

78+
plt.xlim(0,20)
5179
plt.tight_layout()
5280

5381
plt.legend()
5482

5583

5684

85+
86+
87+
88+
5789
model.jacobians[0]
5890

5991

dev_import_tests.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
from dyno.symbolic_model import SymbolicModel
66

7-
87
data = yaml.safe_load(open("import_tests.yaml"))
9-
108
model = SymbolicModel(txt=data['solvable']['rbc_deterministic'])
11-
129
model
1310

1411

src/dyno/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def solve(self: Self, **args):
227227
return sol
228228
else:
229229
dr = self.perturb(**args)
230+
return dr
230231

231232
def perturb(self: Self, method: Solver = "qz") -> RecursiveSolution:
232233
"""linearizes the model

src/dyno/solver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,8 @@ def deterministic_solve(model, x0=None, T=None, method="hybr", verbose=True, **a
499499
u0,
500500
jactype="sparse",
501501
verbose=verbose,
502-
maxit = args.get("maxit",10)
502+
maxit = args.get("maxit",10),
503+
tol=args.get("tol",1e-8)
503504
)
504505

505506
w0 = res.reshape(v0.shape)

0 commit comments

Comments
 (0)