Esempio n. 1
0
def test_simulate_init_array_num_reps():
    P = [[0.4, 0.6], [0.2, 0.8]]
    mc = MarkovChain(P)

    ts_length = 10
    init=[0, 1]
    num_reps=3

    X = mc.simulate(ts_length, init, num_reps)
    assert_array_equal(X[:, 0], init*num_reps)
Esempio n. 2
0
def test_simulate_init_type():
    P = [[0.4, 0.6], [0.2, 0.8]]
    mc = MarkovChain(P)

    seed = 0
    ts_length = 3
    init = 0  # int
    X = mc.simulate(ts_length, init=init, random_state=seed)

    inits_np_int = [t(init) for t in [np.int32, np.int64]]
    for init in inits_np_int:
        X_np_int = mc.simulate(ts_length, init=init, random_state=seed)
        assert_array_equal(X_np_int, X)
Esempio n. 3
0
def test_simulate_ergodicity():
    P = [[0.4, 0.6], [0.2, 0.8]]
    stationary_dist = [0.25, 0.75]
    init = 0
    mc = MarkovChain(P)

    seed = 4433
    ts_length = 100
    num_reps = 300
    tol = 0.1

    x = mc.simulate(ts_length, init=init, num_reps=num_reps, random_state=seed)
    frequency_1 = x[:, -1].mean()
    ok_(np.abs(frequency_1 - stationary_dist[1]) < tol)
Esempio n. 4
0
def test_simulate_shape():
    P = [[0.4, 0.6], [0.2, 0.8]]
    mc = MarkovChain(P)

    (ts_length, init, num_reps) = (10, None, None)
    assert_array_equal(mc.simulate(ts_length, init, num_reps).shape,
                       (ts_length,))

    (ts_length, init, num_reps) = (10, [0, 1], None)
    assert_array_equal(mc.simulate(ts_length, init, num_reps).shape,
                       (len(init), ts_length))

    (ts_length, init, num_reps) = (10, [0, 1], 3)
    assert_array_equal(mc.simulate(ts_length, init, num_reps).shape,
                       (len(init)*num_reps, ts_length))

    for (ts_length, init, num_reps) in [(10, None, 3), (10, None, 1)]:
        assert_array_equal(mc.simulate(ts_length, init, num_reps).shape,
                           (num_reps, ts_length))
Esempio n. 5
0
def test_get_index():
    P = [[0.4, 0.6], [0.2, 0.8]]
    mc = MarkovChain(P)

    eq_(mc.get_index(0), 0)
    eq_(mc.get_index(1), 1)
    assert_raises(ValueError, mc.get_index, 2)
    assert_array_equal(mc.get_index([1, 0]), [1, 0])
    assert_raises(ValueError, mc.get_index, [[1]])

    mc.state_values = [1, 2]
    eq_(mc.get_index(1), 0)
    eq_(mc.get_index(2), 1)
    assert_raises(ValueError, mc.get_index, 0)
    assert_array_equal(mc.get_index([2, 1]), [1, 0])
    assert_raises(ValueError, mc.get_index, [[1]])

    mc.state_values = [[1, 2], [3, 4]]
    eq_(mc.get_index([1, 2]), 0)
    assert_raises(ValueError, mc.get_index, 1)
    assert_array_equal(mc.get_index([[3, 4], [1, 2]]), [1, 0])