Skip to content

Commit 17898f1

Browse files
authored
Update lecture code (#46)
1 parent 452ce5e commit 17898f1

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lectures/_static/lecture_specific/amss/recursive_allocation.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class RecursiveAllocationAMSS:
88

9-
def __init__(self, model, μgrid, tol_diff=1e-4, tol=1e-4):
9+
def __init__(self, model, μgrid, tol_diff=1e-7, tol=1e-7):
1010

1111
self.β, self.π, self.G = model.β, model.π, model.G
1212
self.mc, self.S = MarkovChain(self.π), len(model.π) # Number of states
@@ -227,6 +227,20 @@ def objf(z):
227227

228228
return -π[s_] @ (U(c, n) + β * Vprime)
229229

230+
def objf_prime(x):
231+
232+
epsilon = 1e-7
233+
x0 = np.asfarray(x)
234+
f0 = np.atleast_1d(objf(x0))
235+
jac = np.zeros([len(x0), len(f0)])
236+
dx = np.zeros(len(x0))
237+
for i in range(len(x0)):
238+
dx[i] = epsilon
239+
jac[i] = (objf(x0+dx) - f0)/epsilon
240+
dx[i] = 0.0
241+
242+
return jac.transpose()
243+
230244
def cons(z):
231245
c, n, xprime, T = z[:S], z[S:2 * S], z[2 * S:3 * S], z[3 * S:]
232246
u_c = Uc(c, n)
@@ -243,8 +257,8 @@ def cons(z):
243257
[self.xbar] * S + [(0., 0.)] * S
244258
out, fx, _, imode, smode = fmin_slsqp(objf, self.z0[x, s_],
245259
f_eqcons=cons, bounds=bounds,
246-
full_output=True, iprint=0,
247-
acc=self.tol, iter=self.maxiter)
260+
fprime=objf_prime, full_output=True,
261+
iprint=0, acc=self.tol, iter=self.maxiter)
248262

249263
if imode > 0:
250264
raise Exception(smode)

0 commit comments

Comments
 (0)