Skip to content

Remove code that supported optional numba installation #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions quantecon/lss.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import numpy as np
from numpy.random import multivariate_normal
from scipy.linalg import solve
from numba import jit

#-Check if Numba is Available-#
from .util import numba_installed, jit

@jit
def simulate_linear_model(A, x0, v, ts_length):
"""
This is a separate function for simulating a vector linear system of
Expand Down Expand Up @@ -55,8 +54,6 @@ def simulate_linear_model(A, x0, v, ts_length):
x[i, t+1] += A[i, j] * x[j, t] #Dot Product
return x

if numba_installed:
simulate_linear_model = jit(simulate_linear_model)

class LinearStateSpace(object):
"""
Expand Down
103 changes: 47 additions & 56 deletions quantecon/markov/gth_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@
import numpy as np
from numba import jit

from ..util import numba_installed, jit
if not numba_installed:
try:
xrange
except: # python3
xrange = range


def gth_solve(A, overwrite=False):
def gth_solve(A, overwrite=False, use_jit=True):
r"""
This routine computes the stationary distribution of an irreducible
Markov transition matrix (stochastic matrix) or transition rate
Expand Down Expand Up @@ -74,13 +66,13 @@ def gth_solve(A, overwrite=False):
n = A1.shape[0]
x = np.zeros(n)

if numba_installed:
if use_jit:
_gth_solve_jit(A1, x)
return x

# if not numba_installed
# if not using jit
# === Reduction === #
for k in xrange(n-1):
for k in range(n-1):
scale = np.sum(A1[k, k+1:n])
if scale <= 0:
# There is one (and only one) recurrent class contained in
Expand All @@ -94,7 +86,7 @@ def gth_solve(A, overwrite=False):

# === Backward substitution === #
x[n-1] = 1
for k in xrange(n-2, -1, -1):
for k in range(n-2, -1, -1):
x[k] = np.dot(x[k+1:n], A1[k+1:n, k])

# === Normalization === #
Expand All @@ -103,46 +95,45 @@ def gth_solve(A, overwrite=False):
return x


if numba_installed:
@jit(nopython=True)
def _gth_solve_jit(A, out):
"""
JIT complied version of the main routine of gth_solve.

Parameters
----------
A : numpy.ndarray(float, ndim=2)
Stochastic matrix or generator matrix. Must be of shape n x n.
Data will be overwritten.

out : numpy.ndarray(float, ndim=1)
Output array in which to place the stationary distribution of A.

"""
n = A.shape[0]

# === Reduction === #
for k in range(n-1):
scale = np.sum(A[k, k+1:n])
if scale <= 0:
# There is one (and only one) recurrent class contained in
# {0, ..., k};
# compute the solution associated with that recurrent class.
n = k+1
break
for i in range(k+1, n):
A[i, k] /= scale

for j in range(k+1, n):
A[i, j] += A[i, k] * A[k, j]

# === Backward substitution === #
out[n-1] = 1
for k in range(n-2, -1, -1):
for i in range(k+1, n):
out[k] += out[i] * A[i, k]

# === Normalization === #
norm = np.sum(out)
for k in range(n):
out[k] /= norm
@jit(nopython=True)
def _gth_solve_jit(A, out):
"""
JIT complied version of the main routine of gth_solve.

Parameters
----------
A : numpy.ndarray(float, ndim=2)
Stochastic matrix or generator matrix. Must be of shape n x n.
Data will be overwritten.

out : numpy.ndarray(float, ndim=1)
Output array in which to place the stationary distribution of A.

"""
n = A.shape[0]

# === Reduction === #
for k in range(n-1):
scale = np.sum(A[k, k+1:n])
if scale <= 0:
# There is one (and only one) recurrent class contained in
# {0, ..., k};
# compute the solution associated with that recurrent class.
n = k+1
break
for i in range(k+1, n):
A[i, k] /= scale

for j in range(k+1, n):
A[i, j] += A[i, k] * A[k, j]

# === Backward substitution === #
out[n-1] = 1
for k in range(n-2, -1, -1):
for i in range(k+1, n):
out[k] += out[i] * A[i, k]

# === Normalization === #
norm = np.sum(out)
for k in range(n):
out[k] /= norm
7 changes: 3 additions & 4 deletions quantecon/markov/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"""
import numpy as np
import scipy.sparse
from numba import jit

from .core import MarkovChain
from .ddp import DiscreteDP
from ..util import check_random_state, numba_installed, jit
from ..util import check_random_state
from ..random import probvec, sample_without_replacement


Expand Down Expand Up @@ -218,7 +219,7 @@ def random_discrete_dp(num_states, num_actions, beta=None,
ddp = DiscreteDP(R, Q, beta, s_indices, a_indices)
return ddp


@jit
def _sa_indices(num_states, num_actions):
L = num_states * num_actions
s_indices = np.empty(L, dtype=int)
Expand All @@ -233,5 +234,3 @@ def _sa_indices(num_states, num_actions):

return s_indices, a_indices

if numba_installed:
_sa_indices = jit(_sa_indices)
11 changes: 4 additions & 7 deletions quantecon/random/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"""

import numpy as np
from ..util import check_random_state, numba_installed, jit
from numba import jit

from ..util import check_random_state

#-Generating Arrays and Vectors-#

Expand Down Expand Up @@ -74,7 +76,7 @@ def _diff(r, out):
out[i, j] = r[i, j] - r[i, j-1]
out[i, n] = 1 - r[i, n-1]


@jit
def sample_without_replacement(n, k, num_trials=None, random_state=None):
"""
Randomly choose k integers without replacement from 0, ..., n-1.
Expand Down Expand Up @@ -141,8 +143,3 @@ def sample_without_replacement(n, k, num_trials=None, random_state=None):
return result[0]
else:
return result

if numba_installed:
docs = sample_without_replacement.__doc__
sample_without_replacement = jit(sample_without_replacement)
sample_without_replacement.__doc__ = docs
1 change: 0 additions & 1 deletion quantecon/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

from .array import searchsorted
from .external import jit, numba_installed
from .notebooks import fetch_nb_dependencies
from .random import check_random_state
from .timing import tic, tac, toc
14 changes: 3 additions & 11 deletions quantecon/util/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
"""

import numpy as np
from .external import numba_installed, jit
from numba import jit

# ----------------- #
# -ARRAY UTILITIES- #
# ----------------- #


def _searchsorted(a, v):
@jit(nopython=True)
def searchsorted(a, v):
"""
Custom version of np.searchsorted. Return the largest index `i` such
that `a[i-1] <= v < a[i]` (for `i = 0`, `v < a[0]`); if `v[n-1] <=
Expand Down Expand Up @@ -61,11 +61,3 @@ def _searchsorted(a, v):
else:
lo = m
return hi

if numba_installed:
searchsorted = jit(nopython=True)(_searchsorted)
else:
def searchsorted(a, v):
return np.searchsorted(a, v, side='right')

searchsorted.__doc__ = _searchsorted.__doc__
24 changes: 0 additions & 24 deletions quantecon/util/external.py

This file was deleted.