@@ -199,6 +199,28 @@ def test_DiscreteMarkovChain():
199
199
assert recurrence == (True , True , False , True , False )
200
200
assert periods == (1 , 1 , 1 , 1 , 1 )
201
201
202
+ # test canonical form
203
+ # see https://www.dartmouth.edu/~chance/teaching_aids/books_articles/probability_book/Chapter11.pdf
204
+ # example 11.13
205
+ T = Matrix ([[1 , 0 , 0 , 0 , 0 ],
206
+ [S (1 ) / 2 , 0 , S (1 ) / 2 , 0 , 0 ],
207
+ [0 , S (1 ) / 2 , 0 , S (1 ) / 2 , 0 ],
208
+ [0 , 0 , S (1 ) / 2 , 0 , S (1 ) / 2 ],
209
+ [0 , 0 , 0 , 0 , S (1 )]])
210
+ DW = DiscreteMarkovChain ('DW' , [0 , 1 , 2 , 3 , 4 ], T )
211
+ states , A , B , C = DW .decompose ()
212
+ assert states == [0 , 4 , 1 , 2 , 3 ]
213
+ assert A == Matrix ([[1 , 0 ], [0 , 1 ]])
214
+ assert B == Matrix ([[S (1 )/ 2 , 0 ], [0 , 0 ], [0 , S (1 )/ 2 ]])
215
+ assert C == Matrix ([[0 , S (1 )/ 2 , 0 ], [S (1 )/ 2 , 0 , S (1 )/ 2 ], [0 , S (1 )/ 2 , 0 ]])
216
+ states , new_matrix = DW .canonical_form ()
217
+ assert states == [0 , 4 , 1 , 2 , 3 ]
218
+ assert new_matrix == Matrix ([[1 , 0 , 0 , 0 , 0 ],
219
+ [0 , 1 , 0 , 0 , 0 ],
220
+ [S (1 )/ 2 , 0 , 0 , S (1 )/ 2 , 0 ],
221
+ [0 , 0 , S (1 )/ 2 , 0 , S (1 )/ 2 ],
222
+ [0 , S (1 )/ 2 , 0 , S (1 )/ 2 , 0 ]])
223
+
202
224
# test custom state space
203
225
Y10 = DiscreteMarkovChain ('Y' , [1 , 2 , 3 ], TO2 )
204
226
tuples = Y10 .communication_classes ()
0 commit comments