4
4
"""
5
5
6
6
import numpy as np
7
- import quantecon as qe
8
7
import time
9
8
10
- from scipy .spatial import ConvexHull
11
- from scipy .optimize import linprog , minimize , minimize_scalar
12
- from scipy .interpolate import UnivariateSpline
9
+ from scipy .optimize import linprog , minimize
13
10
import numpy .polynomial .chebyshev as cheb
14
11
15
12
@@ -30,7 +27,7 @@ def __init__(self, β, mbar, h_min, h_max, n_h, n_m, N_g):
30
27
self .N_a = self .n_h * self .n_m
31
28
32
29
# Utility and production functions
33
- uc = lambda c : np .log (c )
30
+ uc = lambda c : np .log (np . maximum ( c , 1e-10 )) # Clip to 1e-10 to avoid log(0) or log(-ve )
34
31
uc_p = lambda c : 1 / c
35
32
v = lambda m : 1 / 500 * (mbar * m - 0.5 * m ** 2 )** 0.5
36
33
v_p = lambda m : 0.5 / 500 * (mbar * m - 0.5 * m ** 2 )** (- 0.5 ) * (mbar - m )
@@ -306,7 +303,7 @@ def solve_bellman(self, θ_min, θ_max, order, disp=False, tol=1e-7, maxiters=10
306
303
mbar = self .mbar
307
304
308
305
# Utility and production functions
309
- uc = lambda c : np .log (c )
306
+ uc = lambda c : np .log (np . maximum ( c , 1e-10 )) # Clip to 1e-10 to avoid log(0) or log(-ve )
310
307
uc_p = lambda c : 1 / c
311
308
v = lambda m : 1 / 500 * (mbar * m - 0.5 * m ** 2 )** 0.5
312
309
v_p = lambda m : 0.5 / 500 * (mbar * m - 0.5 * m ** 2 )** (- 0.5 ) * (mbar - m )
@@ -343,13 +340,13 @@ def p_fun(x):
343
340
scale = - 1 + 2 * (x [2 ] - θ_min )/ (θ_max - θ_min )
344
341
p_fun = - (u (x [0 ], x [1 ]) \
345
342
+ self .β * np .dot (cheb .chebvander (scale , order - 1 ), c ))
346
- return p_fun
343
+ return p_fun . item ()
347
344
348
345
def p_fun2 (x ):
349
346
scale = - 1 + 2 * (x [1 ] - θ_min )/ (θ_max - θ_min )
350
347
p_fun = - (u (x [0 ],mbar ) \
351
348
+ self .β * np .dot (cheb .chebvander (scale , order - 1 ), c ))
352
- return p_fun
349
+ return p_fun . item ()
353
350
354
351
cons1 = ({'type' : 'eq' , 'fun' : lambda x : uc_p (f (x [0 ], x [1 ])) * x [1 ]
355
352
* (x [0 ] - 1 ) + v_p (x [1 ]) * x [1 ] + self .β * x [2 ] - θ },
@@ -372,16 +369,18 @@ def p_fun2(x):
372
369
p_iter1 = np .zeros (order )
373
370
for i in range (order ):
374
371
θ = s [i ]
372
+ x0 = np .clip (lb1 + (ub1 - lb1 )/ 2 , lb1 , ub1 )
375
373
res = minimize (p_fun ,
376
- lb1 + ( ub1 - lb1 ) / 2 ,
374
+ x0 ,
377
375
method = 'SLSQP' ,
378
376
bounds = bnds1 ,
379
377
constraints = cons1 ,
380
378
tol = 1e-10 )
381
379
if res .success == True :
382
380
p_iter1 [i ] = - p_fun (res .x )
381
+ x0 = np .clip (lb2 + (ub2 - lb2 )/ 2 , lb2 , ub2 )
383
382
res = minimize (p_fun2 ,
384
- lb2 + ( ub2 - lb2 ) / 2 ,
383
+ x0 ,
385
384
method = 'SLSQP' ,
386
385
bounds = bnds2 ,
387
386
constraints = cons2 ,
@@ -416,8 +415,9 @@ def p_fun2(x):
416
415
h_grid = np .zeros (100 )
417
416
for i in range (100 ):
418
417
θ = θ_grid_fine [i ]
418
+ x0 = np .clip (lb1 + (ub1 - lb1 )/ 2 , lb1 , ub1 )
419
419
res = minimize (p_fun ,
420
- lb1 + ( ub1 - lb1 ) / 2 ,
420
+ x0 ,
421
421
method = 'SLSQP' ,
422
422
bounds = bnds1 ,
423
423
constraints = cons1 ,
@@ -428,8 +428,9 @@ def p_fun2(x):
428
428
θ_prime_grid [i ] = res .x [2 ]
429
429
h_grid [i ] = res .x [0 ]
430
430
m_grid [i ] = res .x [1 ]
431
+ x0 = np .clip (lb2 + (ub2 - lb2 )/ 2 , lb2 , ub2 )
431
432
res = minimize (p_fun2 ,
432
- lb2 + ( ub2 - lb2 ) / 2 ,
433
+ x0 ,
433
434
method = 'SLSQP' ,
434
435
bounds = bnds2 ,
435
436
constraints = cons2 ,
@@ -441,7 +442,8 @@ def p_fun2(x):
441
442
h_grid [i ] = res .x [0 ]
442
443
m_grid [i ] = self .mbar
443
444
scale = - 1 + 2 * (θ - θ_min )/ (θ_max - θ_min )
444
- resid_grid [i ] = np .dot (cheb .chebvander (scale , order - 1 ), c ) - p
445
+ resid_grid_val = np .dot (cheb .chebvander (scale , order - 1 ), c ) - p
446
+ resid_grid [i ] = resid_grid_val .item ()
445
447
446
448
self .resid_grid = resid_grid
447
449
self .θ_grid_fine = θ_grid_fine
@@ -465,13 +467,14 @@ def ValFun(x):
465
467
res = minimize (ValFun ,
466
468
(θ_min + θ_max )/ 2 ,
467
469
bounds = [(θ_min , θ_max )])
468
- θ_series [0 ] = res .x
470
+ θ_series [0 ] = res .x . item ()
469
471
470
472
# Simulate
471
473
for i in range (30 ):
472
474
θ = θ_series [i ]
475
+ x0 = np .clip (lb1 + (ub1 - lb1 )/ 2 , lb1 , ub1 )
473
476
res = minimize (p_fun ,
474
- lb1 + ( ub1 - lb1 ) / 2 ,
477
+ x0 ,
475
478
method = 'SLSQP' ,
476
479
bounds = bnds1 ,
477
480
constraints = cons1 ,
@@ -481,8 +484,9 @@ def ValFun(x):
481
484
h_series [i ] = res .x [0 ]
482
485
m_series [i ] = res .x [1 ]
483
486
θ_series [i + 1 ] = res .x [2 ]
487
+ x0 = np .clip (lb2 + (ub2 - lb2 )/ 2 , lb2 , ub2 )
484
488
res2 = minimize (p_fun2 ,
485
- lb2 + ( ub2 - lb2 ) / 2 ,
489
+ x0 ,
486
490
method = 'SLSQP' ,
487
491
bounds = bnds2 ,
488
492
constraints = cons2 ,
0 commit comments