Skip to content
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

MarkovChain.simulate API change; random_state option added #166

Merged
merged 12 commits into from
Aug 26, 2015
Prev Previous commit
Next Next commit
random_state moved from __init__ to simulate
based on discussion #146 (comment)
  • Loading branch information
oyamad committed Aug 26, 2015
commit 83ebebfb686627b4d4869b82db71e10b69f7dbec
28 changes: 13 additions & 15 deletions quantecon/markov/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,6 @@ class MarkovChain(object):
P : array_like(float, ndim=2)
The transition matrix. Must be of shape n x n.

random_state : scalar(int) or np.random.RandomState,
optional(default=None)
Random seed (integer) or np.random.RandomState instance to set
the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState is
used.

Attributes
----------
P : see Parameters
Expand Down Expand Up @@ -160,7 +153,7 @@ class MarkovChain(object):

"""

def __init__(self, P, random_state=None):
def __init__(self, P):
self.P = np.asarray(P)

# Check Properties
Expand All @@ -179,9 +172,6 @@ def __init__(self, P, random_state=None):
# The number of states
self.n = self.P.shape[0]

# random state
self.random_state = random_state

# To analyze the structure of P as a directed graph
self._digraph = None

Expand Down Expand Up @@ -288,7 +278,7 @@ def cdfs(self):
self._cdfs = cdfs
return self._cdfs

def simulate(self, ts_length, init=None, num_reps=None):
def simulate(self, ts_length, init=None, num_reps=None, random_state=None):
"""
Simulate time series of state transitions.

Expand All @@ -306,6 +296,13 @@ def simulate(self, ts_length, init=None, num_reps=None):
Number of simulations. Relevant only when init is a scalar
or None.

random_state : scalar(int) or np.random.RandomState,
optional(default=None)
Random seed (integer) or np.random.RandomState instance to set
the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState is
used.

Returns
-------
X : ndarray(int, ndim=1 or 2)
Expand All @@ -315,7 +312,7 @@ def simulate(self, ts_length, init=None, num_reps=None):
init is an array_like, otherwise k = num_reps.

"""
random_state = check_random_state(self.random_state)
random_state = check_random_state(random_state)

try:
k = len(init) # init is an array
Expand Down Expand Up @@ -439,5 +436,6 @@ def mc_sample_path(P, init=0, sample_size=1000, random_state=None):
u_0 = random_state.random_sample()
X_0 = searchsorted(cdf0, u_0)

mc = MarkovChain(P, random_state=random_state)
return mc.simulate(ts_length=sample_size, init=X_0)
mc = MarkovChain(P)
return mc.simulate(ts_length=sample_size, init=X_0,
random_state=random_state)