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

FIX: Fix period of reducible MarkovChain with custom state_values #684

Merged
merged 1 commit into from
Jan 3, 2023
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
4 changes: 1 addition & 3 deletions quantecon/markov/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,9 @@ def period(self):
if self.is_irreducible:
return self.digraph.period
else:
rec_classes = self.recurrent_classes

# Determine the period, the LCM of the periods of rec_classes
d = 1
for rec_class in rec_classes:
for rec_class in self.recurrent_classes_indices:
period = self.digraph.subgraph(rec_class).period
d = (d * period) // gcd(d, period)

Expand Down
14 changes: 11 additions & 3 deletions quantecon/markov/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import itertools
from numpy.testing import (
assert_allclose, assert_array_equal, assert_array_less, assert_raises,
assert_
assert_, assert_equal
)

from quantecon.markov import (
Expand Down Expand Up @@ -417,15 +417,17 @@ def setup_method(self):
'mc': MarkovChain([[1, 0, 0], [1, 0, 0], [0, 0, 1]],
state_values=state_values),
'coms': [[0], [1], [2]],
'recs': [[0], [2]]
'recs': [[0], [2]],
'period': 1
}

self.mc_periodic_dict = {
'mc': MarkovChain([[0, 1, 0], [0, 0, 1], [1, 0, 0]],
state_values=state_values),
'coms': [[0, 1, 2]],
'recs': [[0, 1, 2]],
'cycs': [[0], [1], [2]]
'cycs': [[0], [1], [2]],
'period': 3
}

def test_com_rec_classes(self):
Expand Down Expand Up @@ -471,6 +473,12 @@ def test_cyc_classes(self):
sorted(classes, key=key)
)

def test_period(self):
for mc_dict in [self.mc_reducible_dict, self.mc_periodic_dict]:
mc = mc_dict['mc']
period = mc_dict['period']
assert_equal(mc.period, period)

def test_simulate(self):
# Deterministic mc
mc = self.mc_periodic_dict['mc']
Expand Down