-
Notifications
You must be signed in to change notification settings - Fork 1
/
bellman.py
391 lines (344 loc) · 12.3 KB
/
bellman.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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 14:49:42 2013
@author: dgevans
"""
from scipy.integrate import ode
from scipy.optimize import brentq
from scipy.special import erf
from numpy import *
from scipy.integrate import quad
class bellman(object):
'''
Bellman equation object for the time 1 onwards problem
'''
def __init__(self,Para,u0_min):
'''
Constructs the bellman equation class, with object Para that holds parameter
information
'''
self.Para = Para
self.u0_min = u0_min
self.theta_min = 0.01
def __call__(self,Vf,wf,w2f):
'''
Returns a new function based on the coninuation
'''
self.Vf = Vf
self.wf = wf
self.w2f = w2f
def Vnew(state):
lambda_1,lambda_2_hat,theta_ = state
lambda_2 = lambda_2_hat*theta_
state_alt = array([lambda_1,lambda_2,theta_])
return self.computeValue(state_alt)
return Vnew
def computeValue(self,state):
'''
Computes the value and continuation terms associated with a state
'''
#first compute lambda_1
u0 = self.find_u0(state)
return self.computeExpectations(state,u0)[:3]
def computeExpectations(self,state,u0):
'''
Computes the expectations of various terms
'''
if not self.Vf ==None:
Fs = self.Vf,self.wf,self.w2f
else:
Fs = None
theta_ = state[2]
thetavec,w = self.Para.integration_nodes(theta_)
yvec = self.integrateODE(state,thetavec,u0)
if yvec[-1,1] > 0.:
return nan*ones(4)
obj = zeros((len(w),4))
for i,theta in enumerate(thetavec):
V,c,l,tau,Uc = self.Para.quantities(yvec[i],theta,state,Fs)
obj[i,0] = V
obj[i,1] = yvec[i,0]
obj[i,2] = yvec[i,0]*self.Para.f2(theta,theta_)/self.Para.f(theta,theta_)
obj[i,3] = 1./Uc
return w.dot(obj)
def getPolicies(self,state,thetavec,u0=None):
'''
Computes the expectations of various terms
'''
if u0 == None:
u0 = self.find_u0(state)
if not self.Vf ==None:
Fs = self.Vf,self.wf,self.w2f
else:
Fs = None
yvec = self.integrateODE(state,thetavec,u0)
if yvec[-1,1] > 0.:
return nan*ones(4)
pol = zeros((len(yvec),3))
for i,theta in enumerate(thetavec):
V,c,l,tau,Uc = self.Para.quantities(yvec[i],theta,state,Fs)
pol[i,:] = [c,l,tau]
return pol
def integrateODE(self,state,thetavec,u0):
'''
Integrates ODE over the gridpoints theta
'''
if not self.Vf ==None:
Fs = self.Vf,self.wf,self.w2f
else:
Fs = None
def dy_dtheta(theta,y):
dy = self.Para.differentialEquation(y,theta,state,Fs)
return dy
lambda_1,lambda_2,theta_ = state
mu_tilde = -lambda_2/theta_
if thetavec[0] > self.theta_min:
thetavec = hstack((self.theta_min,thetavec))
y0 = self.getInitial_y(u0,mu_tilde,thetavec[0],state)
r = ode(dy_dtheta).set_integrator('vode',rtol=1e-10,atol = 1e-10,nsteps=1000)
r.set_initial_value(y0,thetavec[0])
y = ones((len(thetavec),2))
y[0] = y0
for i,theta in enumerate(thetavec[1:]):
y[i+1] = r.integrate(theta)
if y[i+1,1] >0:
break
if not r.successful():
print "ode failed"
break
if thetavec[0] == self.theta_min:
return y[1:]
else:
return y
def integrateODEverbose(self,state,thetavec,u0):
'''
Integrates ODE over the gridpoints theta
'''
if not self.Vf ==None:
Fs = self.Vf,self.wf,self.w2f
else:
Fs = None
def dy_dtheta(theta,y):
print theta,y
dy = self.Para.differentialEquation(y,theta,state,Fs)
print dy
return dy
lambda_1,lambda_2,theta_ = state
mu_tilde = -lambda_2/theta_
if thetavec[0] > self.theta_min:
thetavec = hstack((self.theta_min,thetavec))
y0 = self.getInitial_y(u0,mu_tilde,thetavec[0],state)
r = ode(dy_dtheta).set_integrator('vode',rtol=1e-10,atol = 1e-10,nsteps=1000)
r.set_initial_value(y0,thetavec[0])
y = ones((len(thetavec),2))
y[0] = y0
for i,theta in enumerate(thetavec[1:]):
y[i+1] = r.integrate(theta)
if y[i+1,1] >0:
break
if not r.successful():
break
if thetavec[0] == self.theta_min:
return y[1:]
else:
return y
def getInitial_y(self,u0,mu_tilde,theta_0,state):
'''
Computes initial y for ode
'''
lambda_1,lambda_2,theta_ = state
F = lambda Uc: self.Para.residuals_end(Uc,u0,mu_tilde,0)
Uc0 = bracket_and_solve(F,1.)
mu0 = (1/Uc0-lambda_1)*self.Para.F(theta_0,theta_)-lambda_2*self.Para.F2(theta_0,theta_)
return array([u0,mu0])
def find_lambda_1_min(self,state_2):
'''
Computes the lambda_1 asssociated with u_min for lambda_2,theta
'''
lambda_2,theta_ = state_2
thetavec,_ = self.Para.integration_nodes(theta_)
u0_min = self.u0_min
def f(lambda_1):
state = hstack((lambda_1,state_2))
y = self.integrateODE(state,thetavec,u0_min)[-1]
if y[1] > 0:
return 1.
return y[1] + self.getTargetMu(state,y,thetavec[-1])
lambda_1 = bracket_and_solve(f,1.)
state = hstack((lambda_1,state_2))
while self.integrateODE(state,thetavec,u0_min)[-1,1] > 0.:
lambda_1 *= 1.000001
state = hstack((lambda_1,state_2))
# state = hstack((lambda_1,state_2))
return lambda_1
def find_u0(self,state):
'''
Finds the u0 associated with the optimal allocation
'''
theta_ = state[2]
thetavec,_ = self.Para.integration_nodes(theta_)
def f(u0_diff):
u0 = self.u0_min + u0_diff
y = self.integrateODE(state,thetavec,u0)[-1]
if y[1] > 0:
return -1.
return -(y[1] + self.getTargetMu(state,y,thetavec[-1]))
f0 = f(0.)
if(f0<=0.) and f0 != -1.:
return self.u0_min
u0_diff = bracket_and_solve(f,1.)
while self.integrateODE(state,thetavec,self.u0_min+u0_diff)[-1,1] > 0.:
u0_diff *= 0.999999
return self.u0_min+u0_diff
def getTargetMu(self,state,y,theta):
'''
Get target mu.
'''
lambda_1,lambda_2,theta_ = state
if self.Vf == None:
Fs = None
else:
Fs = self.Vf,self.wf,self.w2f
V,c,l,tau,Uc = self.Para.quantities(y,theta,state,Fs)
alpha = c/theta
f = lambda theta: (1./self.Para.Uc(alpha*theta,l)-lambda_1) * self.Para.f(theta,theta_) - lambda_2 * self.Para.f2(theta,theta_)
return quad(f,theta,inf)[0]
class time0_BellmanMap(object):
'''
The bellman map for the time zero bellman equation
'''
def __init__(self,Para,lambda_2_max):
self.Para = Para
self.lambda_2_max = lambda_2_max
def __call__(self,Vf,wf,w2f):
'''
Returns a new function based on the coninuation
'''
self.Vf = Vf
self.wf = wf
self.w2f = w2f
return self.computeValue
def computeValue(self,u0):
'''
Computes the value of the associated mu0
'''
Fs = self.Vf,self.wf,self.w2f
lambda_ = self.find_lambda_(u0)
return self.computeExpectation(u0,lambda_)[0]
def computeExpectation(self,u0,lambda_):
'''
Computes expectations given u0 and lambda_
'''
Fs = self.Vf,self.wf,self.w2f
thetavec,w = self.Para.integration_nodes0()
yvec = self.integrateODE(thetavec,u0,lambda_)
if yvec[-1,1] > 0.:
return nan*ones(2)
obj = zeros((len(w),2))
for i,theta in enumerate(thetavec):
if self.lambda_2_max <0:
yvec[i,1] = max(self.lambda_2_max*self.Para.f0(theta)*theta,yvec[i,1])
V,c,l,tau,Uc = self.Para.quantities0(yvec[i],theta,Fs)
obj[i,:] = [V,1./Uc]
return w.dot(obj)
def getPolicies(self,thetavec,u0,lambda_=None):
'''
Computes policies at vector thetavec
'''
if lambda_ == None:
lambda_ = self.find_lambda_(u0)
Fs = self.Vf,self.wf,self.w2f
yvec = self.integrateODE(thetavec,u0,lambda_)
if yvec[-1,1] > 0.:
return nan*ones(2)
policies = zeros((len(thetavec),3))
stateprime = zeros((len(thetavec),3))
Para = self.Para
for i,theta in enumerate(thetavec):
V,c,l,tau,Uc = self.Para.quantities0(yvec[i],theta,Fs)
policies[i,:] = [c,l,tau]
f_ = self.Para.f0(theta)
lambda_2 = yvec[i,1]/(f_)*(Para.beta/Para.delta)
lambda_1 = (1./Uc)*(Para.beta/Para.delta)
stateprime[i,:] = [lambda_1,lambda_2,theta]
return policies,stateprime
def find_lambda_(self,u0):
'''
Computes the lambda_ associated with u0
'''
thetavec,_ = self.Para.integration_nodes0()
def f(lambda_):
y = self.integrateODE(thetavec,u0,lambda_)[-1]
if y[1] > 0:
return 1.
theta = thetavec[-1]
mu_target = self.getTargetMu(y,theta,lambda_)
return y[1]+mu_target
lambda_ = bracket_and_solve(f,1.)
state = hstack((lambda_1,state_2))
while self.integrateODE(state,thetavec,u0)[-1,1] > 0.:
lambda_1 *= 1.000001
state = hstack((lambda_1,state_2))
return lambda_
def integrateODE(self,thetavec,u0,lambda_):
'''
Integrates ODE over the gridpoints theta
'''
Fs = self.Vf,self.wf,self.w2f
def dy_dtheta(theta,y):
if self.lambda_2_max <0:
y[1] = max(self.lambda_2_max*self.Para.f0(theta)*theta,y[1])
dy = self.Para.differentialEquation0(y,theta,lambda_,Fs)
return dy
y0 = self.getInitial_y(u0,lambda_,thetavec[0])
r = ode(dy_dtheta).set_integrator('vode',rtol=1e-10,atol = 1e-10,nsteps=1000)
r.set_initial_value(y0,thetavec[0])
y = ones((len(thetavec),2))
y[0] = y0
for i,theta in enumerate(thetavec[1:]):
y[i+1] = r.integrate(theta)
if y[i+1,1] >0:
break
if not r.successful():
break
return y
def getInitial_y(self,u0,lambda_,theta0):
'''
Computes initial y for ode
'''
sigma= self.Para.sigma
if sigma == 1.:
c0 = exp(u0)
Uc0 = 1./c0
else:
Uc0 = ( (1-sigma)*u0 + 1 )**(sigma/(sigma-1))
mu0 = 1./Uc0*self.Para.F0(theta0)-lambda_*self.Para.AlphaF0(theta0)
return array([u0,mu0])
def getTargetMu(self,y,theta,lambda_):
'''
Get target mu.
'''
Fs = self.Vf,self.wf,self.w2f
if(y[1]/(theta*self.Para.f0(theta)) < self.lambda_2_max ):
return 0.
V,c,l,tau,Uc = self.Para.quantities0(y,theta,Fs)
alpha = c/theta
f = lambda theta: (1./self.Para.Uc(alpha*theta,l)-lambda_) * self.Para.f0(theta)
return quad(f,theta,inf)[0]
def bracket_and_solve(F,x0,scale = 2.):
'''
Brackets the root of F and solves under assumption that F is decreasing
'''
xmin = x0
xmax = x0
if(F(x0) > 0.):
xmax *= scale
while(F(xmax) >0.):
xmin = xmax
xmax *= scale
else:
xmin /= scale
while(F(xmin) <0.):
xmax = xmin
xmin /= scale
return brentq(F,xmin,xmax)