Esempio n. 1
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. 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_issue591():
    """
    Test MarkovChasin.simulate for P with dtype=np.float32
    https://github.com/QuantEcon/QuantEcon.py/issues/591
    """
    num_states = 5
    transition_states = 4

    transition_seed = 2
    random_state = np.random.RandomState(transition_seed)
    transitions = random_state.uniform(0., 1., transition_states)
    transitions /= np.sum(transitions)
    P = np.zeros((num_states, num_states), dtype=np.float32)
    P[0, :transition_states] = transitions
    P[1:, 0] = 1.
    mc = MarkovChain(P=P)

    simulate_seed = 22220
    ts_length = 10000
    seq = mc.simulate(ts_length=ts_length,
                      init=0,
                      num_reps=1,
                      random_state=simulate_seed)
    max_state_in_seq = np.max(seq)

    assert_array_less(max_state_in_seq, num_states)
Esempio n. 4
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. 5
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. 6
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. 7
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. 8
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. 9
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)