Skip to content

Commit

Permalink
FIX: Fix period of reducible MarkovChain with custom state_values (#684)
Browse files Browse the repository at this point in the history
Fix #676
  • Loading branch information
oyamad committed Jan 3, 2023
1 parent 372e251 commit 3279cf6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
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

0 comments on commit 3279cf6

Please sign in to comment.