Пример #1
0
class TestLogitDynamics:
    '''Test the methods of LogitDynamics'''

    def setUp(self):
        '''Setup a LogitDynamics instance'''
        # symmetric 2x2 coordination game
        payoff_matrix = [[4, 0],
                         [3, 2]]
        beta = 4.0
        g = NormalFormGame(payoff_matrix)
        self.ld = LogitDynamics(g, beta=beta)

    def test_set_init_actions_with_given_init_actions(self):
        init_actions = (0, 1)
        self.ld.set_init_actions(init_actions)
        assert_array_equal(self.ld.current_actions, init_actions)

    def test_set_init_actions_when_init_action_dist_None(self):
        self.ld.set_init_actions()  # Action dist randomly chosen
        init_actions = self.ld.current_actions

        ok_(all(
            action in list(range(2)) for action in self.ld.current_actions)
            )

    def test_simulate_seed(self):
        np.random.seed(291)
        seq = self.ld.simulate(ts_length=10, init_actions=(0, 0))
        assert_array_equal(
            seq,
            [[0, 0],
             [0, 0],
             [0, 0],
             [0, 1],
             [1, 1],
             [1, 1],
             [1, 1],
             [1, 1],
             [1, 1],
             [1, 1]]
        )

    def test_simulate_lln(self):
        n = 100
        T = 1000
        seq = self.ld.replicate(T=T, num_reps=n)
        count = 0
        for i in range(n):
            if all(seq[i, :] == [1, 1]):
                count += 1
        frequency = count / n

        # 0.981367209 = prob that the stationary distribution assigns to [1, 1]
        ok_(np.abs(frequency-0.981367209) < 0.05)
Пример #2
0
class TestLogitDynamics:
    '''Test the methods of LogitDynamics'''

    def setUp(self):
        '''Setup a LogitDynamics instance'''
        # symmetric 2x2 coordination game
        payoff_matrix = [[4, 0],
                         [3, 2]]
        beta = 4.0
        g = NormalFormGame(payoff_matrix)
        self.ld = LogitDynamics(g, beta=beta)

    def test_set_init_actions_with_given_init_actions(self):
        init_actions = (0, 1)
        self.ld.set_init_actions(init_actions)
        assert_array_equal(self.ld.current_actions, init_actions)

    def test_set_init_actions_when_init_action_dist_None(self):
        self.ld.set_init_actions()  # Action dist randomly chosen
        init_actions = self.ld.current_actions

        ok_(all(
            action in list(range(2)) for action in self.ld.current_actions)
            )

    def test_simulate_seed(self):
        np.random.seed(291)
        seq = self.ld.simulate(ts_length=10, init_actions=(0, 0))
        assert_array_equal(
            seq,
            [[0, 0],
             [0, 0],
             [0, 0],
             [0, 1],
             [1, 1],
             [1, 1],
             [1, 1],
             [1, 1],
             [1, 1],
             [1, 1]]
        )

    def test_simulate_lln(self):
        n = 100
        T = 1000
        seq = self.ld.replicate(T=T, num_reps=n)
        count = 0
        for i in range(n):
            if all(seq[i, :] == [1, 1]):
                count += 1
        frequency = count / n

        # 0.981367209 = prob that the stationary distribution assigns to [1, 1]
        ok_(np.abs(frequency-0.981367209) < 0.05)