Skip to content

Remove optional numba installation code #246

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

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b01b0ab
Add node labels to DiGraph
oyamad Mar 15, 2016
b43039f
Edit and add docstrings
oyamad Mar 16, 2016
0176d3e
Add tests for node labels
oyamad Mar 16, 2016
3c103d6
Add decorator `annotate_nodes` to get_*_components methods
oyamad Mar 17, 2016
01fb48b
Delete unused method
oyamad Mar 17, 2016
3d243f1
Allow setting node_labels to None
oyamad Mar 17, 2016
43632eb
Add state values to MarkovChain
oyamad Mar 17, 2016
b0ad892
Fix bugs
oyamad Mar 17, 2016
b5cacb9
Add tests for state values
oyamad Mar 17, 2016
84fdbaa
Add a testcase
oyamad Mar 18, 2016
457578b
MarkovChain: Remove util.numba_installed
oyamad Mar 19, 2016
8850b31
MarkovChain: Fix bug
oyamad Mar 19, 2016
8d3176c
MarkovChain: Set default `return_values=True`
oyamad Mar 20, 2016
2d3aa9a
Remove optional numba installation code and make numba jit a strict d…
mmcky Mar 21, 2016
f32e468
Remove python27 support for xrange due to migration to python3.5+
mmcky Mar 23, 2016
ecfbc8f
Disallow non-homogeneous node_labels/state_values
oyamad Mar 28, 2016
5bc78d5
MarkovChain: Add get_index
oyamad Mar 28, 2016
bfc91bb
MarkovChain.get_index: Accept array_like of values
oyamad Mar 28, 2016
193f625
Fix typo
oyamad Mar 29, 2016
d825975
MarkovChain: Add `simulate_values`
oyamad Mar 29, 2016
3a4265c
DiscreteDP: Allow beta=1
oyamad Apr 6, 2016
c0397df
MarkovChain: Add test for simulate_values with init_value=None
oyamad Apr 11, 2016
d761cfb
MarkovChain: Fix bug in simulate_values
oyamad Apr 11, 2016
78e7e67
DiGraph: Add *_components_indices which return indices
oyamad Apr 11, 2016
f0c681f
MarkovChain: Add *_classes_indices which return indices
oyamad Apr 11, 2016
05e8ad8
MarkovChain: Change simulate_values to simulate
oyamad Apr 11, 2016
4eade8d
Merge pull request #237 from QuantEcon/add_nodes_states
jstac Apr 12, 2016
f40981a
Remove optional numba installation code and make numba jit a strict d…
mmcky Mar 21, 2016
f3c5976
Remove python27 support for xrange due to migration to python3.5+
mmcky Mar 23, 2016
d4861b2
Fix merge conflicts
mmcky Apr 14, 2016
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
99 changes: 87 additions & 12 deletions quantecon/graph_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
from fractions import gcd


# Decorator for *_components properties
def annotate_nodes(func):
def new_func(self):
list_of_components = func(self)
if self.node_labels is not None:
return [self.node_labels[c] for c in list_of_components]
return list_of_components
return new_func


class DiGraph(object):
r"""
Class for a directed graph. It stores useful information about the
Expand All @@ -27,6 +37,11 @@ class DiGraph(object):
weighted : bool, optional(default=False)
Whether to treat `adj_matrix` as a weighted adjacency matrix.

node_labels : array_like(default=None)
Array_like of length n containing the labels associated with the
nodes, which must be homogeneous in type. If None, the labels
default to integers 0 through n-1.

Attributes
----------
csgraph : scipy.sparse.csr_matrix
Expand All @@ -38,16 +53,26 @@ class DiGraph(object):
num_strongly_connected_components : int
The number of the strongly connected components.

strongly_connected_components : list(ndarray(int))
strongly_connected_components_indices : list(ndarray(int))
List of numpy arrays containing the indices of the strongly
connected components.

strongly_connected_components : list(ndarray)
List of numpy arrays containing the strongly connected
components.
components, where the nodes are annotated with their labels (if
`node_labels` is not None).

num_sink_strongly_connected_components : int
The number of the sink strongly connected components.

sink_strongly_connected_components : list(ndarray(int))
sink_strongly_connected_components_indices : list(ndarray(int))
List of numpy arrays containing the indices of the sink strongly
connected components.

sink_strongly_connected_components : list(ndarray)
List of numpy arrays containing the sink strongly connected
components.
components, where the nodes are annotated with their labels (if
`node_labels` is not None).

is_aperiodic : bool
Indicate whether the digraph is aperiodic.
Expand All @@ -56,8 +81,14 @@ class DiGraph(object):
The period of the digraph. Defined only for a strongly connected
digraph.

cyclic_components : list(ndarray(int))
List of numpy arrays containing the cyclic components.
cyclic_components_indices : list(ndarray(int))
List of numpy arrays containing the indices of the cyclic
components.

cyclic_components : list(ndarray)
List of numpy arrays containing the cyclic components, where the
nodes are annotated with their labels (if `node_labels` is not
None).

References
----------
Expand All @@ -70,7 +101,7 @@ class DiGraph(object):

"""

def __init__(self, adj_matrix, weighted=False):
def __init__(self, adj_matrix, weighted=False, node_labels=None):
if weighted:
dtype = None
else:
Expand All @@ -83,6 +114,9 @@ def __init__(self, adj_matrix, weighted=False):

self.n = n # Number of nodes

# Call the setter method
self.node_labels = node_labels

self._num_scc = None
self._scc_proj = None
self._sink_scc_labels = None
Expand All @@ -95,6 +129,26 @@ def __repr__(self):
def __str__(self):
return "Directed Graph:\n - n(number of nodes): {n}".format(n=self.n)

@property
def node_labels(self):
return self._node_labels

@node_labels.setter
def node_labels(self, values):
if values is None:
self._node_labels = None
else:
values = np.asarray(values)
if (values.ndim < 1) or (values.shape[0] != self.n):
raise ValueError(
'node_labels must be an array_like of length n'
)
if np.issubdtype(values.dtype, np.object_):
raise ValueError(
'data in node_labels must be homogeneous in type'
)
self._node_labels = values

def _find_scc(self):
"""
Set ``self._num_scc`` and ``self._scc_proj``
Expand Down Expand Up @@ -170,21 +224,31 @@ def num_sink_strongly_connected_components(self):
return len(self.sink_scc_labels)

@property
def strongly_connected_components(self):
def strongly_connected_components_indices(self):
if self.is_strongly_connected:
return [np.arange(self.n)]
else:
return [np.where(self.scc_proj == k)[0]
for k in range(self.num_strongly_connected_components)]

@property
def sink_strongly_connected_components(self):
@annotate_nodes
def strongly_connected_components(self):
return self.strongly_connected_components_indices

@property
def sink_strongly_connected_components_indices(self):
if self.is_strongly_connected:
return [np.arange(self.n)]
else:
return [np.where(self.scc_proj == k)[0]
for k in self.sink_scc_labels.tolist()]

@property
@annotate_nodes
def sink_strongly_connected_components(self):
return self.sink_strongly_connected_components_indices

def _compute_period(self):
"""
Set ``self._period`` and ``self._cyclic_components_proj``.
Expand Down Expand Up @@ -256,13 +320,18 @@ def is_aperiodic(self):
return (self.period == 1)

@property
def cyclic_components(self):
def cyclic_components_indices(self):
if self.is_aperiodic:
return [np.arange(self.n)]
else:
return [np.where(self._cyclic_components_proj == k)[0]
for k in range(self.period)]

@property
@annotate_nodes
def cyclic_components(self,):
return self.cyclic_components_indices

def subgraph(self, nodes):
"""
Return the subgraph consisting of the given nodes and edges
Expand All @@ -271,7 +340,7 @@ def subgraph(self, nodes):
Parameters
----------
nodes : array_like(int, ndim=1)
Array of nodes.
Array of node indices.

Returns
-------
Expand All @@ -282,7 +351,13 @@ def subgraph(self, nodes):
adj_matrix = self.csgraph[nodes, :][:, nodes]

weighted = True # To copy the dtype
return DiGraph(adj_matrix, weighted=weighted)

if self.node_labels is not None:
node_labels = self.node_labels[nodes]
else:
node_labels = None

return DiGraph(adj_matrix, weighted=weighted, node_labels=node_labels)


def _csr_matrix_indices(S):
Expand Down
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
2 changes: 1 addition & 1 deletion quantecon/markov/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
from .random import random_markov_chain, random_stochastic_matrix, \
random_discrete_dp
from .approximation import tauchen
from .ddp import DiscreteDP
from .ddp import DiscreteDP, backward_induction
Loading