|
10 | 10 | import numpy as np
|
11 | 11 | from scipy import sparse
|
12 | 12 | import itertools
|
13 |
| -from numpy.testing import assert_allclose, assert_array_equal, assert_raises |
| 13 | +from numpy.testing import ( |
| 14 | + assert_allclose, assert_array_equal, assert_array_less, assert_raises |
| 15 | +) |
14 | 16 | from nose.tools import eq_, ok_, raises
|
15 | 17 |
|
16 | 18 | from quantecon.markov import (
|
@@ -339,6 +341,33 @@ def test_simulate_for_matrices_with_C_F_orders():
|
339 | 341 | assert_array_equal(computed_F, sample_path)
|
340 | 342 |
|
341 | 343 |
|
| 344 | +def test_simulate_issue591(): |
| 345 | + """ |
| 346 | + Test MarkovChasin.simulate for P with dtype=np.float32 |
| 347 | + https://github.com/QuantEcon/QuantEcon.py/issues/591 |
| 348 | + """ |
| 349 | + num_states = 5 |
| 350 | + transition_states = 4 |
| 351 | + |
| 352 | + transition_seed = 2 |
| 353 | + random_state = np.random.RandomState(transition_seed) |
| 354 | + transitions = random_state.uniform(0., 1., transition_states) |
| 355 | + transitions /= np.sum(transitions) |
| 356 | + P = np.zeros((num_states, num_states), dtype=np.float32) |
| 357 | + P[0, :transition_states] = transitions |
| 358 | + P[1:, 0] = 1. |
| 359 | + mc = MarkovChain(P=P) |
| 360 | + |
| 361 | + simulate_seed = 22220 |
| 362 | + ts_length = 10000 |
| 363 | + seq = mc.simulate( |
| 364 | + ts_length=ts_length, init=0, num_reps=1, random_state=simulate_seed |
| 365 | + ) |
| 366 | + max_state_in_seq = np.max(seq) |
| 367 | + |
| 368 | + assert_array_less(max_state_in_seq, num_states) |
| 369 | + |
| 370 | + |
342 | 371 | def test_mc_sample_path():
|
343 | 372 | P = [[0.4, 0.6], [0.2, 0.8]]
|
344 | 373 | Ps = [P, sparse.csr_matrix(P)]
|
|
0 commit comments