Skip to content

Commit

Permalink
FIX: Updates for Numba 0.49.0 (#531)
Browse files Browse the repository at this point in the history
* 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 <quentin.batista@mail.mcgill.ca>
  • Loading branch information
oyamad and QBatista committed Apr 21, 2020
1 parent c4ade35 commit bc8232c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
14 changes: 10 additions & 4 deletions quantecon/optimize/nelder_mead.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ def _nelder_mead_algorithm(fun, vertices, bounds=np.array([[], []]).T,
break

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

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

# Step 3: Expansion
elif f_r < f_val[best_val_idx]:
x_e = x_bar + χ * (x_r - x_bar)
# https://github.com/QuantEcon/QuantEcon.py/issues/530
temp = χ * (x_r - x_bar)
x_e = x_bar + temp
f_e = _neg_bounded_fun(fun, bounds, x_e, args=args)
if f_e < f_r: # Greedy minimization
vertices[worst_val_idx] = x_e
Expand All @@ -242,11 +246,13 @@ def _nelder_mead_algorithm(fun, vertices, bounds=np.array([[], []]).T,
# Step 4 & 5: Contraction and Shrink
else:
# Step 4: Contraction
# https://github.com/QuantEcon/QuantEcon.py/issues/530
temp = γ * (x_r - x_bar)
if f_r < f_val[worst_val_idx]: # Step 4.a: Outside Contraction
x_c = x_bar + γ * (x_r - x_bar)
x_c = x_bar + temp
LV_ratio_update = ργ
else: # Step 4.b: Inside Contraction
x_c = x_bar - γ * (x_r - x_bar)
x_c = x_bar - temp
LV_ratio_update = γ

f_c = _neg_bounded_fun(fun, bounds, x_c, args=args)
Expand Down
24 changes: 18 additions & 6 deletions quantecon/quad.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'qnwsimp', 'qnwtrap', 'qnwunif', 'quadrect', 'qnwbeta',
'qnwgamma']


@vectorize(nopython=True)
def gammaln(x):
return math.lgamma(x)
Expand Down Expand Up @@ -681,6 +682,7 @@ def _make_multidim_func(one_d_func, n, *args):
nodes = gridmake(*nodes)
return nodes, weights


@jit(nopython=True)
def _qnwcheb1(n, a, b):
"""
Expand Down Expand Up @@ -729,6 +731,7 @@ def _qnwcheb1(n, a, b):

return nodes, weights


@jit(nopython=True)
def _qnwlege1(n, a, b):
"""
Expand Down Expand Up @@ -784,7 +787,10 @@ def _qnwlege1(n, a, b):
p2 = p1
p1 = ((2 * j - 1) * z * p2 - (j - 1) * p3) / j

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

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

return nodes, weights


@jit(nopython=True)
def _qnwnorm1(n):
"""
Expand Down Expand Up @@ -879,6 +887,7 @@ def _qnwnorm1(n):

return nodes, weights


@jit(nopython=True)
def _qnwsimp1(n, a, b):
"""
Expand Down Expand Up @@ -927,6 +936,7 @@ def _qnwsimp1(n, a, b):

return nodes, weights


@jit(nopython=True)
def _qnwtrap1(n, a, b):
"""
Expand Down Expand Up @@ -974,6 +984,7 @@ def _qnwtrap1(n, a, b):

return nodes, weights


@jit(nopython=True)
def _qnwbeta1(n, a=1.0, b=1.0):
"""
Expand Down Expand Up @@ -1090,13 +1101,14 @@ def _qnwbeta1(n, a=1.0, b=1.0):
weights[i] = temp/(pp*p2)

nodes = (1-nodes)/2
weights = weights * math.exp(gammaln(a+n) + gammaln(b+n)
- gammaln(n+1) - gammaln(n+ab+1))
weights = weights / (2*math.exp(gammaln(a+1) + gammaln(b+1)
- gammaln(ab+2)))
weights = weights * math.exp(gammaln(a+n) + gammaln(b+n) -
gammaln(n+1) - gammaln(n+ab+1))
weights = weights / (2*math.exp(gammaln(a+1) + gammaln(b+1) -
gammaln(ab+2)))

return nodes, weights


@jit(nopython=True)
def _qnwgamma1(n, a=1.0, b=1.0, tol=3e-14):
"""
Expand Down
5 changes: 4 additions & 1 deletion quantecon/util/numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"""
import numpy as np
from numba import jit, generated_jit, types
from numba.targets.linalg import _LAPACK
try:
from numba.np.linalg import _LAPACK # for Numba >= 0.49.0
except ModuleNotFoundError:
from numba.targets.linalg import _LAPACK # for Numba < 0.49.0


# BLAS kinds as letters
Expand Down

0 comments on commit bc8232c

Please sign in to comment.