Skip to content

Random MarkovChain #154

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 50 commits into from
Aug 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e3ccb2c
random_markov_chain, random_stochastic_matrix, etc first version
oyamad May 18, 2015
22d3f9a
_random_choice renamed to random_choice_without_replacement and elabo…
oyamad May 19, 2015
093a339
test_random_mc.py created
oyamad May 19, 2015
12775b8
`external` added; examples added
oyamad May 19, 2015
49739b0
Some more tests added
oyamad May 19, 2015
4269721
Minor edits
oyamad May 20, 2015
5e77dbd
Reorg of Utilities into a util subpackge for categorization of utilit…
mmcky May 29, 2015
55bac4d
Merge branch 'reorg-utilities' into random_mc
oyamad May 29, 2015
a812bf3
Use `util.check_random_state`
oyamad May 29, 2015
d71c006
Move tests for utilities subpackage
mmcky May 29, 2015
159076d
Docstring updated
oyamad Jun 5, 2015
557d2fa
Merge pull request #161 from QuantEcon/test_mc_tools
sglyon Jul 29, 2015
bc8f1a6
Reorganize tests and add some more
oyamad Jul 31, 2015
c9ac0ee
Moving probvec and sample_without_replacement to utilities location
mmcky Aug 4, 2015
15cf645
Adding markov sub-package and reworked calls to utility function rand…
mmcky Aug 4, 2015
ab7eef5
Move markov tests to subpackage markov and updated calls to random_ch…
mmcky Aug 4, 2015
fce10cb
Fix for python 3 relative import statement
mmcky Aug 4, 2015
e7c6ae9
Migrating mc_tools.py to markov subpackage core.py file and associate…
mmcky Aug 4, 2015
36c794f
random_markov_chain, random_stochastic_matrix, etc first version
oyamad May 18, 2015
870f8d6
_random_choice renamed to random_choice_without_replacement and elabo…
oyamad May 19, 2015
0a24bbc
test_random_mc.py created
oyamad May 19, 2015
2a96cb7
`external` added; examples added
oyamad May 19, 2015
1d0ac00
Some more tests added
oyamad May 19, 2015
060300d
Minor edits
oyamad May 20, 2015
608e647
Reorg of Utilities into a util subpackge for categorization of utilit…
mmcky May 29, 2015
7b392b7
Use `util.check_random_state`
oyamad May 29, 2015
594fa4c
Move tests for utilities subpackage
mmcky May 29, 2015
23737b6
Docstring updated
oyamad Jun 5, 2015
b67c6b0
Reorganize tests and add some more
oyamad Jul 31, 2015
d94f192
Moving probvec and sample_without_replacement to utilities location
mmcky Aug 4, 2015
4fa00ef
Adding markov sub-package and reworked calls to utility function rand…
mmcky Aug 4, 2015
b58b5f9
Move markov tests to subpackage markov and updated calls to random_ch…
mmcky Aug 4, 2015
65b09e6
Fix for python 3 relative import statement
mmcky Aug 4, 2015
290739b
Migrating mc_tools.py to markov subpackage core.py file and associate…
mmcky Aug 4, 2015
95ec7dd
Merge branch 'random_mc' of https://github.com/QuantEcon/QuantEcon.py…
mmcky Aug 4, 2015
7b23b98
fix mc_tools to new core in markove subpackage
mmcky Aug 4, 2015
8efb7fb
Update example due to change in mc_tools
mmcky Aug 4, 2015
0bceec0
Separate out items that may be eligible for deprecation, leaving them…
mmcky Aug 4, 2015
ba80a61
Migrate gth_solve to new markov subpackage
mmcky Aug 5, 2015
1299070
minor update to fix reference in core to gth_solve in markov package
mmcky Aug 5, 2015
56327ff
Move gth_solve tests suite to markov subpackage
mmcky Aug 5, 2015
7cc36cb
Examples sections updated
oyamad Aug 6, 2015
0437763
Import probvec, sample_without_replacement from ..util.random
oyamad Aug 6, 2015
b1f2431
Create util/tests/test_random.py
oyamad Aug 6, 2015
2b3f8df
Reogranisation based on topics to include a random subpackage and mov…
mmcky Aug 6, 2015
6727218
Adding random subpackage to setup and fixing top level namespace to a…
mmcky Aug 6, 2015
0a05347
Migrate tauchen to markov subpackage and update associated api files
mmcky Aug 6, 2015
1979796
Migrate tauchen test to markov subpackage
mmcky Aug 6, 2015
62c4454
Migrate tauchen to approximation markov module and update api and tests
mmcky Aug 6, 2015
78b0593
Update solutions notebook due to change from approx_markov to tauchen
mmcky Aug 6, 2015
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
2 changes: 1 addition & 1 deletion examples/lucas_stokey.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from scipy.optimize import fmin_slsqp
from scipy.interpolate import UnivariateSpline
from quantecon import compute_fixed_point
from quantecon.mc_tools import mc_sample_path
from quantecon.markov import mc_sample_path


class Planners_Allocation_Sequential(object):
Expand Down
9 changes: 5 additions & 4 deletions quantecon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@
from .estspec import smooth, periodogram, ar_periodogram
from .graph_tools import DiGraph
from .gridtools import cartesian, mlinspace
from .gth_solve import gth_solve
from .kalman import Kalman
from .lae import LAE
from .arma import ARMA
from .lqcontrol import LQ
from .lqnash import nnash
from .lss import LinearStateSpace
from .matrix_eqn import solve_discrete_lyapunov, solve_discrete_riccati
from .mc_tools import MarkovChain, mc_compute_stationary, mc_sample_path
from .quadsums import var_quadratic_sum, m_quadratic_sum
#->Propose Delete From Top Level
from .markov import MarkovChain, random_markov_chain, random_stochastic_matrix, gth_solve, tauchen #Promote to keep current examples working
from .markov import mc_compute_stationary, mc_sample_path #Imports that Should be Deprecated with markov package
#<-
from .rank_nullspace import rank_est, nullspace
from .robustlq import RBLQ
from .tauchen import approx_markov
from . import quad as quad
from .utilities import searchsorted
from .util import searchsorted

#Add Version Attribute
from .version import version as __version__
9 changes: 9 additions & 0 deletions quantecon/markov/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Markov Chain SubPackge API
"""

from .core import MarkovChain
from .core import mc_compute_stationary, mc_sample_path #-Future Deprecation-#
from .gth_solve import gth_solve
from .random import random_markov_chain, random_stochastic_matrix
from .approximation import tauchen
6 changes: 4 additions & 2 deletions quantecon/tauchen.py → quantecon/markov/approximation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
Filename: tauchen.py
Filename: approximation.py

Authors: Thomas Sargent, John Stachurski

tauchen
-------
Discretizes Gaussian linear AR(1) processes via Tauchen's method

"""
Expand All @@ -11,7 +13,7 @@
from scipy.stats import norm


def approx_markov(rho, sigma_u, m=3, n=7):
def tauchen(rho, sigma_u, m=3, n=7):
"""
Computes the Markov matrix associated with a discretized version of
the linear Gaussian AR(1) process
Expand Down
8 changes: 4 additions & 4 deletions quantecon/mc_tools.py → quantecon/markov/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Authors: Chase Coleman, Spencer Lyon, Daisuke Oyama, Tom Sargent,
John Stachurski

Filename: mc_tools.py
Filename: core.py

This file contains some useful objects for handling a finite-state
discrete-time Markov chain.
Expand Down Expand Up @@ -88,12 +88,12 @@
import numpy as np
from fractions import gcd
import sys
from .graph_tools import DiGraph
from .gth_solve import gth_solve
from ..graph_tools import DiGraph

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


class MarkovChain(object):
Expand Down
2 changes: 1 addition & 1 deletion quantecon/gth_solve.py → quantecon/markov/gth_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
from numba import jit

from .external import numba_installed, jit
from ..external import numba_installed, jit
if not numba_installed:
try:
xrange
Expand Down
141 changes: 141 additions & 0 deletions quantecon/markov/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"""
Filename: random.py

Author: Daisuke Oyama

Generate a MarkovChain randomly.

"""
import numpy as np
import scipy.sparse

from .core import MarkovChain
from ..external import numba_installed, jit
from ..util import check_random_state
from ..random import (
probvec, sample_without_replacement
)


def random_markov_chain(n, k=None, sparse=False, random_state=None):
"""
Return a randomly sampled MarkovChain instance with n states, where
each state has k states with positive transition probability.

Parameters
----------
n : scalar(int)
Number of states.

k : scalar(int), optional(default=None)
Number of states that may be reached from each state with
positive probability. Set to n if not specified.

sparse : bool, optional(default=False)
Whether to store the transition probability matrix in sparse
matrix form. (Sparse format is not yet implemented.)

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
-------
mc : MarkovChain

Examples
--------
>>> mc = qe.markov.random_markov_chain(3, random_state=1234)
>>> mc.P
array([[ 0.19151945, 0.43058932, 0.37789123],
[ 0.43772774, 0.34763084, 0.21464142],
[ 0.27259261, 0.5073832 , 0.22002419]])
>>> mc = qe.markov.random_markov_chain(3, k=2, random_state=1234)
>>> mc.P
array([[ 0.19151945, 0.80848055, 0. ],
[ 0. , 0.62210877, 0.37789123],
[ 0.56227226, 0. , 0.43772774]])

"""
if sparse:
raise NotImplementedError
P = random_stochastic_matrix(n, k, sparse, format='csr',
random_state=random_state)
mc = MarkovChain(P)
return mc


def random_stochastic_matrix(n, k=None, sparse=False, format='csr',
random_state=None):
"""
Return a randomly sampled n x n stochastic matrix with k nonzero
entries for each row.

Parameters
----------
n : scalar(int)
Number of states.

k : scalar(int), optional(default=None)
Number of nonzero entries in each row of the matrix. Set to n if
not specified.

sparse : bool, optional(default=False)
Whether to generate the matrix in sparse matrix form.

format : str in {'bsr', 'csr', 'csc', 'coo', 'lil', 'dia', 'dok'},
optional(default='csr')
Sparse matrix format. Relevant only when sparse=True.

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
-------
P : numpy ndarray or scipy sparse matrix (float, ndim=2)
Stochastic matrix.

See also
--------
random_markov_chain : Return a random MarkovChain instance.

"""
if not (isinstance(n, int) and n > 0):
raise ValueError('n must be a positive integer')
if k is None:
k = n
if not (isinstance(k, int) and 0 < k <= n):
raise ValueError('k must be an integer with 0 < k <= n')

# n prob vectors of dimension k, shape (n, k)
probvecs = probvec(n, k, random_state=random_state)

if k == n:
P = probvecs
if sparse:
return scipy.sparse.coo_matrix(P).asformat(format)
else:
return P

# if k < n:
rows = np.repeat(np.arange(n), k)
cols = \
sample_without_replacement(
n, k, num_trials=n, random_state=random_state
).ravel()
data = probvecs.ravel()

if sparse:
P = scipy.sparse.coo_matrix((data, (rows, cols)), shape=(n, n))
return P.asformat(format)
else:
P = np.zeros((n, n))
P[rows, cols] = data
return P
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
"""
Filename: test_tauchen.py
Filename: test_approximation.py
Authors: Chase Coleman
Date: 07/18/2014

Tests for tauchen.py file
Tests for approximation.py file (i.e. tauchen)

"""
import sys
import os
import unittest
import numpy as np
from numpy.testing import assert_allclose
from quantecon.tauchen import approx_markov
from quantecon.markov import tauchen


class TestApproxMarkov(unittest.TestCase):
class TestTauchen(unittest.TestCase):

def setUp(self):
self.rho, self.sigma_u = np.random.rand(2)
self.n = np.random.random_integers(3, 25)
self.m = np.random.random_integers(4)
self.tol = 1e-12

self.x, self.P = approx_markov(self.rho, self.sigma_u, self.m, self.n)
self.x, self.P = tauchen(self.rho, self.sigma_u, self.m, self.n)

def tearDown(self):
del self.x
Expand Down Expand Up @@ -51,5 +51,5 @@ def test_states_sum_0(self):


if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestApproxMarkov)
suite = unittest.TestLoader().loadTestsFromTestCase(TestTauchen)
unittest.TextTestRunner(verbosity=2, stream=sys.stderr).run(suite)
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
from numpy.testing import assert_allclose, assert_array_equal
from nose.tools import eq_, raises

from quantecon.mc_tools import (
from quantecon.markov import (
MarkovChain, mc_compute_stationary, mc_sample_path
)

from ..external import numba_installed, jit
from quantecon.external import numba_installed, jit


def list_of_array_equal(s, t):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from numpy.testing import assert_array_equal
from nose.tools import eq_, ok_, raises

from quantecon.gth_solve import gth_solve
from quantecon.markov import gth_solve


TOL = 1e-15
Expand Down
Loading