Skip to content

Commit bc8232c

Browse files
oyamadQBatista
andauthored
FIX: Updates for Numba 0.49.0 (#531)
* FIX: Fix ModuleNotFoundError for Numba 0.49.0 * FIX: Fix errors in tests for `quad.py` when Numba 0.49.0 is used * FIX: Fix errors in tests for `nelder_mead.py` when Numba 0.49.0 is used * RFC: Fix PEP8 errors Co-authored-by: QBatista <[email protected]>
1 parent c4ade35 commit bc8232c

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

quantecon/optimize/nelder_mead.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ def _nelder_mead_algorithm(fun, vertices, bounds=np.array([[], []]).T,
220220
break
221221

222222
# Step 2: Reflection
223-
x_r = x_bar + ρ * (x_bar - vertices[worst_val_idx])
223+
# https://github.com/QuantEcon/QuantEcon.py/issues/530
224+
temp = ρ * (x_bar - vertices[worst_val_idx])
225+
x_r = x_bar + temp
224226
f_r = _neg_bounded_fun(fun, bounds, x_r, args=args)
225227

226228
if f_r >= f_val[best_val_idx] and f_r < f_val[sort_ind[n-1]]:
@@ -230,7 +232,9 @@ def _nelder_mead_algorithm(fun, vertices, bounds=np.array([[], []]).T,
230232

231233
# Step 3: Expansion
232234
elif f_r < f_val[best_val_idx]:
233-
x_e = x_bar + χ * (x_r - x_bar)
235+
# https://github.com/QuantEcon/QuantEcon.py/issues/530
236+
temp = χ * (x_r - x_bar)
237+
x_e = x_bar + temp
234238
f_e = _neg_bounded_fun(fun, bounds, x_e, args=args)
235239
if f_e < f_r: # Greedy minimization
236240
vertices[worst_val_idx] = x_e
@@ -242,11 +246,13 @@ def _nelder_mead_algorithm(fun, vertices, bounds=np.array([[], []]).T,
242246
# Step 4 & 5: Contraction and Shrink
243247
else:
244248
# Step 4: Contraction
249+
# https://github.com/QuantEcon/QuantEcon.py/issues/530
250+
temp = γ * (x_r - x_bar)
245251
if f_r < f_val[worst_val_idx]: # Step 4.a: Outside Contraction
246-
x_c = x_bar + γ * (x_r - x_bar)
252+
x_c = x_bar + temp
247253
LV_ratio_update = ργ
248254
else: # Step 4.b: Inside Contraction
249-
x_c = x_bar - γ * (x_r - x_bar)
255+
x_c = x_bar - temp
250256
LV_ratio_update = γ
251257

252258
f_c = _neg_bounded_fun(fun, bounds, x_c, args=args)

quantecon/quad.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'qnwsimp', 'qnwtrap', 'qnwunif', 'quadrect', 'qnwbeta',
2222
'qnwgamma']
2323

24+
2425
@vectorize(nopython=True)
2526
def gammaln(x):
2627
return math.lgamma(x)
@@ -681,6 +682,7 @@ def _make_multidim_func(one_d_func, n, *args):
681682
nodes = gridmake(*nodes)
682683
return nodes, weights
683684

685+
684686
@jit(nopython=True)
685687
def _qnwcheb1(n, a, b):
686688
"""
@@ -729,6 +731,7 @@ def _qnwcheb1(n, a, b):
729731

730732
return nodes, weights
731733

734+
732735
@jit(nopython=True)
733736
def _qnwlege1(n, a, b):
734737
"""
@@ -784,7 +787,10 @@ def _qnwlege1(n, a, b):
784787
p2 = p1
785788
p1 = ((2 * j - 1) * z * p2 - (j - 1) * p3) / j
786789

787-
pp = n * (z * p1 - p2)/(z * z - 1.0)
790+
# https://github.com/QuantEcon/QuantEcon.py/issues/530
791+
top = n * (z * p1 - p2)
792+
bottom = z ** 2 - 1.0
793+
pp = top / bottom
788794
z1 = z.copy()
789795
z = z1 - p1/pp
790796
if np.all(np.abs(z - z1) < 1e-14):
@@ -796,11 +802,13 @@ def _qnwlege1(n, a, b):
796802
nodes[i] = xm - xl * z
797803
nodes[- i - 1] = xm + xl * z
798804

799-
weights[i] = 2 * xl / ((1 - z * z) * pp * pp)
805+
# https://github.com/QuantEcon/QuantEcon.py/issues/530
806+
weights[i] = 2 * xl / ((1 - z ** 2) * pp * pp)
800807
weights[- i - 1] = weights[i]
801808

802809
return nodes, weights
803810

811+
804812
@jit(nopython=True)
805813
def _qnwnorm1(n):
806814
"""
@@ -879,6 +887,7 @@ def _qnwnorm1(n):
879887

880888
return nodes, weights
881889

890+
882891
@jit(nopython=True)
883892
def _qnwsimp1(n, a, b):
884893
"""
@@ -927,6 +936,7 @@ def _qnwsimp1(n, a, b):
927936

928937
return nodes, weights
929938

939+
930940
@jit(nopython=True)
931941
def _qnwtrap1(n, a, b):
932942
"""
@@ -974,6 +984,7 @@ def _qnwtrap1(n, a, b):
974984

975985
return nodes, weights
976986

987+
977988
@jit(nopython=True)
978989
def _qnwbeta1(n, a=1.0, b=1.0):
979990
"""
@@ -1090,13 +1101,14 @@ def _qnwbeta1(n, a=1.0, b=1.0):
10901101
weights[i] = temp/(pp*p2)
10911102

10921103
nodes = (1-nodes)/2
1093-
weights = weights * math.exp(gammaln(a+n) + gammaln(b+n)
1094-
- gammaln(n+1) - gammaln(n+ab+1))
1095-
weights = weights / (2*math.exp(gammaln(a+1) + gammaln(b+1)
1096-
- gammaln(ab+2)))
1104+
weights = weights * math.exp(gammaln(a+n) + gammaln(b+n) -
1105+
gammaln(n+1) - gammaln(n+ab+1))
1106+
weights = weights / (2*math.exp(gammaln(a+1) + gammaln(b+1) -
1107+
gammaln(ab+2)))
10971108

10981109
return nodes, weights
10991110

1111+
11001112
@jit(nopython=True)
11011113
def _qnwgamma1(n, a=1.0, b=1.0, tol=3e-14):
11021114
"""

quantecon/util/numba.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"""
55
import numpy as np
66
from numba import jit, generated_jit, types
7-
from numba.targets.linalg import _LAPACK
7+
try:
8+
from numba.np.linalg import _LAPACK # for Numba >= 0.49.0
9+
except ModuleNotFoundError:
10+
from numba.targets.linalg import _LAPACK # for Numba < 0.49.0
811

912

1013
# BLAS kinds as letters

0 commit comments

Comments
 (0)