Skip to content

Commit 37cff82

Browse files
authored
[chang_ramsey] Fix Runtime and Deprecation warnings (#204)
* Fix Runtime and Deprecation warnings * fix bounds warning * install cvxopt
1 parent bfdf491 commit 37cff82

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

lectures/_static/lecture_specific/chang_credible/changecon.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
"""
55

66
import numpy as np
7-
import quantecon as qe
87
import time
98

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
1310
import numpy.polynomial.chebyshev as cheb
1411

1512

@@ -30,7 +27,7 @@ def __init__(self, β, mbar, h_min, h_max, n_h, n_m, N_g):
3027
self.N_a = self.n_h*self.n_m
3128

3229
# 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)
3431
uc_p = lambda c: 1/c
3532
v = lambda m: 1/500 * (mbar * m - 0.5 * m**2)**0.5
3633
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
306303
mbar = self.mbar
307304

308305
# 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)
310307
uc_p = lambda c: 1 / c
311308
v = lambda m: 1 / 500 * (mbar * m - 0.5 * m**2)**0.5
312309
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):
343340
scale = -1 + 2 * (x[2] - θ_min)/(θ_max - θ_min)
344341
p_fun = - (u(x[0], x[1]) \
345342
+ self.β * np.dot(cheb.chebvander(scale, order - 1), c))
346-
return p_fun
343+
return p_fun.item()
347344

348345
def p_fun2(x):
349346
scale = -1 + 2*(x[1] - θ_min)/(θ_max - θ_min)
350347
p_fun = - (u(x[0],mbar) \
351348
+ self.β * np.dot(cheb.chebvander(scale, order - 1), c))
352-
return p_fun
349+
return p_fun.item()
353350

354351
cons1 = ({'type': 'eq', 'fun': lambda x: uc_p(f(x[0], x[1])) * x[1]
355352
* (x[0] - 1) + v_p(x[1]) * x[1] + self.β * x[2] - θ},
@@ -372,16 +369,18 @@ def p_fun2(x):
372369
p_iter1 = np.zeros(order)
373370
for i in range(order):
374371
θ = s[i]
372+
x0 = np.clip(lb1 + (ub1-lb1)/2, lb1, ub1)
375373
res = minimize(p_fun,
376-
lb1 + (ub1-lb1) / 2,
374+
x0,
377375
method='SLSQP',
378376
bounds=bnds1,
379377
constraints=cons1,
380378
tol=1e-10)
381379
if res.success == True:
382380
p_iter1[i] = -p_fun(res.x)
381+
x0 = np.clip(lb2 + (ub2-lb2)/2, lb2, ub2)
383382
res = minimize(p_fun2,
384-
lb2 + (ub2-lb2) / 2,
383+
x0,
385384
method='SLSQP',
386385
bounds=bnds2,
387386
constraints=cons2,
@@ -416,8 +415,9 @@ def p_fun2(x):
416415
h_grid = np.zeros(100)
417416
for i in range(100):
418417
θ = θ_grid_fine[i]
418+
x0 = np.clip(lb1 + (ub1-lb1)/2, lb1, ub1)
419419
res = minimize(p_fun,
420-
lb1 + (ub1-lb1) / 2,
420+
x0,
421421
method='SLSQP',
422422
bounds=bnds1,
423423
constraints=cons1,
@@ -428,8 +428,9 @@ def p_fun2(x):
428428
θ_prime_grid[i] = res.x[2]
429429
h_grid[i] = res.x[0]
430430
m_grid[i] = res.x[1]
431+
x0 = np.clip(lb2 + (ub2-lb2)/2, lb2, ub2)
431432
res = minimize(p_fun2,
432-
lb2 + (ub2-lb2)/2,
433+
x0,
433434
method='SLSQP',
434435
bounds=bnds2,
435436
constraints=cons2,
@@ -441,7 +442,8 @@ def p_fun2(x):
441442
h_grid[i] = res.x[0]
442443
m_grid[i] = self.mbar
443444
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()
445447

446448
self.resid_grid = resid_grid
447449
self.θ_grid_fine = θ_grid_fine
@@ -465,13 +467,14 @@ def ValFun(x):
465467
res = minimize(ValFun,
466468
(θ_min + θ_max)/2,
467469
bounds=[(θ_min, θ_max)])
468-
θ_series[0] = res.x
470+
θ_series[0] = res.x.item()
469471

470472
# Simulate
471473
for i in range(30):
472474
θ = θ_series[i]
475+
x0 = np.clip(lb1 + (ub1-lb1)/2, lb1, ub1)
473476
res = minimize(p_fun,
474-
lb1 + (ub1-lb1)/2,
477+
x0,
475478
method='SLSQP',
476479
bounds=bnds1,
477480
constraints=cons1,
@@ -481,8 +484,9 @@ def ValFun(x):
481484
h_series[i] = res.x[0]
482485
m_series[i] = res.x[1]
483486
θ_series[i+1] = res.x[2]
487+
x0 = np.clip(lb2 + (ub2-lb2)/2, lb2, ub2)
484488
res2 = minimize(p_fun2,
485-
lb2 + (ub2-lb2)/2,
489+
x0,
486490
method='SLSQP',
487491
bounds=bnds2,
488492
constraints=cons2,

lectures/chang_ramsey.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ In addition to what's in Anaconda, this lecture will need the following librarie
2626
---
2727
tags: [hide-output]
2828
---
29-
!pip install polytope
29+
!pip install polytope cvxopt
3030
```
3131

3232
## Overview
@@ -947,7 +947,7 @@ def plot_competitive(ChangModel):
947947
# Add point showing Ramsey Plan
948948
idx_Ramsey = np.where(ext_C[:, 0] == max(ext_C[:, 0]))[0][0]
949949
R = ext_C[idx_Ramsey, :]
950-
ax.scatter(R[0], R[1], 150, 'black', 'o', zorder=1)
950+
ax.scatter(R[0], R[1], 150, 'black', marker='o', zorder=1)
951951
w_min = min(ext_C[:, 0])
952952
953953
# Label Ramsey Plan slightly to the right of the point

0 commit comments

Comments
 (0)