Ejemplo n.º 1
0
    def _compute_stationary(self):
        """
        Store the stationary distributions in self._stationary_distributions.

        """
        if self.is_irreducible:
            stationary_dists = gth_solve(self.P).reshape(1, self.n)
        else:
            rec_classes = self.recurrent_classes
            stationary_dists = np.zeros((len(rec_classes), self.n))
            for i, rec_class in enumerate(rec_classes):
                stationary_dists[i, rec_class] = \
                    gth_solve(self.P[rec_class, :][:, rec_class])

        self._stationary_dists = stationary_dists
Ejemplo n.º 2
0
def test_stoch_matrix():
    """Test with stochastic matrices"""
    print(__name__ + '.' + test_stoch_matrix.__name__)
    matrices = Matrices()
    for matrix_dict in matrices.stoch_matrix_dicts:
        x = gth_solve(matrix_dict['A'])
        yield StationaryDistSumOne(), x
        yield StationaryDistNonnegative(), x
        yield StationaryDistEqualToKnown(), matrix_dict['stationary_dist'], x
Ejemplo n.º 3
0
def test_kmr_matrix():
    """Test with KMR matrices"""
    print(__name__ + '.' + test_kmr_matrix.__name__)
    matrices = Matrices()
    for matrix_dict in matrices.kmr_matrix_dicts:
        x = gth_solve(matrix_dict['A'])
        yield StationaryDistSumOne(), x
        yield StationaryDistNonnegative(), x
        yield StationaryDistLeftEigenVec(), matrix_dict['A'], x
Ejemplo n.º 4
0
def kmr_compute_stationary(n,
                           epsilon=0.01,
                           payoffs=[[6, 0, 0], [5, 7, 5], [0, 5, 8]]):
    """
    This function calculates the stationary state of KMR in genetal cases.
    n : int 
        The number of Players
    epsilon : float
        The probability of mutation
    Payoffs : list
        The payoff matrix(symmetric)
    """
    num_action = len(payoffs)
    states_cool_lex = cool_lex(n, num_action - 1)
    states = []
    num_zeros = n
    num_ones = num_action - 1
    num_states = int(
        factorial(num_zeros + num_ones) /
        (factorial(num_zeros) * factorial(num_ones)))
    tran_matrix = np.zeros([num_states, num_states])
    print states_cool_lex
    for i in states_cool_lex:
        profile = []
        for m, n in enumerate(i.split("1")):
            profile.append(len(n))
        states.append(profile)  # ex. [[0,2,0,1,0], ...]

    for s, i in enumerate(states):
        current_profile = np.array(i) / sum(i)
        expected_payoff = np.dot(payoffs, current_profile)
        best_reply = expected_payoff.argmax()
        state_no_before = s
        for x, y in enumerate(i):
            if y != 0:  # The player may be chosen
                for k in range(num_action):
                    adjacent_state = list(i)
                    adjacent_state[x] = adjacent_state[x] - 1
                    adjacent_state[k] = adjacent_state[k] + 1
                    state_no_after = states.index(adjacent_state)
                    if k == best_reply:
                        tran_matrix[state_no_before][
                            state_no_after] = tran_matrix[state_no_before][
                                state_no_after] + (y / num_zeros) * (
                                    1 - epsilon * (1 - 1 / float(num_action)))
                    else:
                        tran_matrix[state_no_before][
                            state_no_after] = tran_matrix[state_no_before][
                                state_no_after] + (y / num_zeros) * epsilon * (
                                    1 / float(num_action))
    print tran_matrix
    return gth_solve(tran_matrix)
Ejemplo n.º 5
0
def kmr_compute_stationary(n, epsilon=0.01, payoffs=[[6, 0, 0], [5, 7, 5], [0, 5, 8]]):
    """
    This function calculates the stationary state of KMR in genetal cases.
    n : int 
        The number of Players
    epsilon : float
        The probability of mutation
    Payoffs : list
        The payoff matrix(symmetric)
    """
    num_action = len(payoffs)
    states_cool_lex = cool_lex(n, num_action - 1)
    states = []
    num_zeros = n
    num_ones = num_action - 1
    num_states = int(factorial(num_zeros + num_ones) / (factorial(num_zeros) * factorial(num_ones)))
    tran_matrix = np.zeros([num_states, num_states])
    print states_cool_lex
    for i in states_cool_lex:
        profile = []
        for m, n in enumerate(i.split("1")):
            profile.append(len(n))
        states.append(profile)  # ex. [[0,2,0,1,0], ...]

    for s, i in enumerate(states):
        current_profile = np.array(i) / sum(i)
        expected_payoff = np.dot(payoffs, current_profile)
        best_reply = expected_payoff.argmax()
        state_no_before = s
        for x, y in enumerate(i):
            if y != 0:  # The player may be chosen
                for k in range(num_action):
                    adjacent_state = list(i)
                    adjacent_state[x] = adjacent_state[x] - 1
                    adjacent_state[k] = adjacent_state[k] + 1
                    state_no_after = states.index(adjacent_state)
                    if k == best_reply:
                        tran_matrix[state_no_before][state_no_after] = tran_matrix[state_no_before][state_no_after] + (
                            y / num_zeros
                        ) * (1 - epsilon * (1 - 1 / float(num_action)))
                    else:
                        tran_matrix[state_no_before][state_no_after] = tran_matrix[state_no_before][state_no_after] + (
                            y / num_zeros
                        ) * epsilon * (1 / float(num_action))
    print tran_matrix
    return gth_solve(tran_matrix)
Ejemplo n.º 6
0
def test_raises_value_error_non_sym():
    """Test with non symmetric input"""
    gth_solve(np.array([[0.4, 0.6]]))
Ejemplo n.º 7
0
def test_raises_value_error_non_2dim():
    """Test with non 2dim input"""
    gth_solve(np.array([0.4, 0.6]))