Пример #1
0
def bomze_figures(N=60, beta=1, process="incentive", directory=None):
    """
    Makes plots of the stationary distribution and expected divergence for each
    of the plots in Bomze's classification.
    """

    if not directory:
        directory = "bomze_paper_figures_%s" % process
        ensure_directory(directory)
    for i, m in enumerate(bomze_matrices()):
        mu = 3./2 * 1./N
        fitness_landscape = linear_fitness_landscape(m)
        incentive = fermi(fitness_landscape, beta=beta)
        edges = incentive_process.multivariate_transitions(N, incentive,
                                                           num_types=3, mu=mu)
        d1 = stationary_distribution(edges)

        filename = os.path.join(directory, "%s_%s_stationary.eps" % (i, N))
        figure, tax = ternary.figure(scale = N)
        tax.heatmap(d1)
        tax.savefig(filename=filename)
        pyplot.close(figure)

        for q_d in [0., 1.]:
            d2 = expected_divergence(edges, q_d=q_d)
            filename = os.path.join(directory, "%s_%s_%s.eps"  % (i, N, q_d))
            figure, tax = ternary.figure(scale = N)
            tax.heatmap(d2)
            tax.savefig(filename=filename)
            pyplot.close(figure)
def compute_entropy_rate(N=30,
                         n=2,
                         m=None,
                         incentive_func=None,
                         beta=1.,
                         mu=None,
                         exact=False,
                         lim=1e-13,
                         logspace=False):
    if not m:
        m = np.ones((n, n))
    if not incentive_func:
        incentive_func = incentives.fermi
    if not mu:
        # mu = (n-1.)/n * 1./(N+1)
        mu = 1. / N

    fitness_landscape = incentives.linear_fitness_landscape(m)
    incentive = incentive_func(fitness_landscape, beta=beta, q=1)
    edges = incentive_process.multivariate_transitions(N,
                                                       incentive,
                                                       num_types=n,
                                                       mu=mu)
    s = stationary.stationary_distribution(edges,
                                           exact=exact,
                                           lim=lim,
                                           logspace=logspace)
    e = stationary.entropy_rate(edges, s)
    return e, s
Пример #3
0
def test_extrema_moran_3(lim=1e-12):
    """
    Test for extrema of the stationary distribution.
    """
    n = 2
    N = 100
    mu = 6. / 25
    m = [[1, 0], [0, 1]]
    maxes = set([(38, 62), (62, 38)])
    mins = set([(50, 50), (100, 0), (0, 100)])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = replicator(fitness_landscape)
    edges = incentive_process.multivariate_transitions(N,
                                                       incentive,
                                                       num_types=n,
                                                       mu=mu)
    s = stationary_distribution(edges, lim=lim)
    flow = inflow_outflow(edges)

    for q_d in [0, 1]:
        s2 = incentive_process.kl(edges, q_d=1)
        assert_equal(find_local_maxima(s), set(maxes))
        assert_equal(find_local_minima(s), set(mins))
        assert_equal(find_local_minima(s2), set([(50, 50), (40, 60),
                                                 (60, 40)]))
        assert_equal(find_local_maxima(flow), set(mins))
Пример #4
0
def test_extrema_moran_5(lim=1e-16):
    """
    Test for extrema of the stationary distribution.
    """
    n = 3
    N = 60
    mu = (3. / 2) * 1. / N
    m = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
    maxes = set([(20, 20, 20), (0, 0, 60), (0, 60, 0), (60, 0, 0), (30, 0, 30),
                 (0, 30, 30), (30, 30, 0)])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = fermi(fitness_landscape, beta=0.1)
    edges = incentive_process.multivariate_transitions(N,
                                                       incentive,
                                                       num_types=n,
                                                       mu=mu)

    s = stationary_distribution(edges, lim=lim)
    s2 = expected_divergence(edges, q_d=0)
    flow = inflow_outflow(edges)

    # These sets should all correspond
    assert_equal(find_local_maxima(s), set(maxes))
    assert_equal(find_local_minima(s2), set(maxes))
    assert_equal(find_local_minima(flow), set(maxes))

    # The minima are pathological
    assert_equal(find_local_minima(s), set([(3, 3, 54), (3, 54, 3),
                                            (54, 3, 3)]))
    assert_equal(find_local_maxima(s2),
                 set([(4, 52, 4), (4, 4, 52), (52, 4, 4)]))
    assert_equal(find_local_maxima(flow),
                 set([(1, 58, 1), (1, 1, 58), (58, 1, 1)]))
Пример #5
0
def test_extrema_moran_5(lim=1e-16):
    """
    Test for extrema of the stationary distribution.
    """
    n = 3
    N = 60
    mu = (3./2) * 1./N
    m = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
    maxes = set([(20, 20, 20), (0, 0, 60), (0, 60, 0), (60, 0, 0),
                 (30, 0, 30), (0, 30, 30), (30, 30, 0)])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = fermi(fitness_landscape, beta=0.1)
    edges = incentive_process.multivariate_transitions(
        N, incentive, num_types=n, mu=mu)

    s = stationary_distribution(edges, lim=lim)
    s2 = expected_divergence(edges, q_d=0)
    flow = inflow_outflow(edges)

    # These sets should all correspond
    assert_equal(find_local_maxima(s), set(maxes))
    assert_equal(find_local_minima(s2), set(maxes))
    assert_equal(find_local_minima(flow), set(maxes))

    # The minima are pathological
    assert_equal(find_local_minima(s),
                 set([(3, 3, 54), (3, 54, 3), (54, 3, 3)]))
    assert_equal(find_local_maxima(s2),
                 set([(4, 52, 4), (4, 4, 52), (52, 4, 4)]))
    assert_equal(find_local_maxima(flow),
                 set([(1, 58, 1), (1, 1, 58), (58, 1, 1)]))
Пример #6
0
def two_dim_transitions_figure(N, m, mu=0.01, incentive_func=replicator):
    """
    Plot transition entropies and stationary distributions.
    """

    n = len(m[0])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = incentive_func(fitness_landscape)
    if not mu:
        mu = 1./ N
    edges = incentive_process.multivariate_transitions(N, incentive, num_types=n, mu=mu)

    s = stationary_distribution(edges, exact=True)
    d = edges_to_edge_dict(edges)

    # Set up plots
    gs = gridspec.GridSpec(3, 1)
    ax1 = pyplot.subplot(gs[0, 0])
    ax1.set_title("Transition Probabilities")
    ups, downs, _ = two_dim_transitions(edges)
    xs = range(0, N+1)
    ax1.plot(xs, ups)
    ax1.plot(xs, downs)

    ax2 = pyplot.subplot(gs[1, 0])
    ax2.set_title("Relative Entropy")
    divs1 = expected_divergence(edges)
    divs2 = expected_divergence(edges, q_d=0)
    plot_dictionary(divs1, ax=ax2)
    plot_dictionary(divs2, ax=ax2)

    ax3 = pyplot.subplot(gs[2, 0])
    ax3.set_title("Stationary Distribution")
    plot_dictionary(s, ax=ax3)
    ax3.set_xlabel("Number of A individuals (i)")
Пример #7
0
def bomze_figures(N=60, beta=1, process="incentive", directory=None):
    """
    Makes plots of the stationary distribution and expected divergence for each
    of the plots in Bomze's classification.
    """

    if not directory:
        directory = "bomze_paper_figures_%s" % process
        ensure_directory(directory)
    for i, m in enumerate(bomze_matrices()):
        mu = 3. / 2 * 1. / N
        fitness_landscape = linear_fitness_landscape(m)
        incentive = fermi(fitness_landscape, beta=beta)
        edges = incentive_process.multivariate_transitions(N,
                                                           incentive,
                                                           num_types=3,
                                                           mu=mu)
        d1 = stationary_distribution(edges)

        filename = os.path.join(directory, "%s_%s_stationary.eps" % (i, N))
        figure, tax = ternary.figure(scale=N)
        tax.heatmap(d1)
        tax.savefig(filename=filename)
        pyplot.close(figure)

        for q_d in [0., 1.]:
            d2 = expected_divergence(edges, q_d=q_d)
            filename = os.path.join(directory, "%s_%s_%s.eps" % (i, N, q_d))
            figure, tax = ternary.figure(scale=N)
            tax.heatmap(d2)
            tax.savefig(filename=filename)
            pyplot.close(figure)
Пример #8
0
def test_incentive_process_k(lim=1e-14):
    """
    Compare stationary distribution computations to known analytic form for
    neutral landscape for the Moran process.
    """
    for k in [1, 2, 10,]:
        for n, N in [(2, 20), (2, 50), (3, 10), (3, 20)]:
            mu = (n-1.)/n * 1./(N+1)
            m = numpy.ones((n, n)) # neutral landscape
            fitness_landscape = linear_fitness_landscape(m)
            incentive = replicator(fitness_landscape)

            # Neutral landscape is the default
            edges = incentive_process.k_fold_incentive_transitions(
                N, incentive, num_types=n, mu=mu, k=k)
            stationary_1 = stationary_distribution(edges, lim=lim)

            # Check that the stationary distribution satisfies balance
            # conditions
            check_detailed_balance(edges, stationary_1)
            check_global_balance(edges, stationary_1)
            check_eigenvalue(edges, stationary_1)

            # Also check edge_func calculation
            edges = incentive_process.multivariate_transitions(
                N, incentive, num_types=n, mu=mu)
            states = states_from_edges(edges)
            edge_func = power_transitions(edges, k)
            stationary_2 = stationary_distribution(
                edge_func, states=states, lim=lim)

            for key in stationary_1.keys():
                assert_almost_equal(
                    stationary_1[key], stationary_2[key], places=5)
Пример #9
0
def four_dim_figures(N=30, beta=1., q=1.):
    """
    Four dimensional example. Three dimensional slices are plotted
    for illustation.
    """

    m = [[0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [0, 0, 0, 1]]
    num_types = len(m[0])
    fitness_landscape = linear_fitness_landscape(m)
    mu = 4. / 3 * 1. / N

    incentive = fermi(fitness_landscape, beta=beta, q=q)
    edges = incentive_process.multivariate_transitions(N,
                                                       incentive,
                                                       num_types=num_types,
                                                       mu=mu)

    d1 = expected_divergence(edges, q_d=0, boundary=True)
    d2 = stationary_distribution(edges)

    # We need to slice the 4dim dictionary into three-dim slices for plotting.
    for slice_index in range(4):
        for d in [d1, d2]:
            slice_dict = slice_dictionary(d, N, slice_index=3)
            figure, tax = ternary.figure(scale=N)
            tax.heatmap(slice_dict, style="d")
    pyplot.show()
Пример #10
0
def graphical_abstract_figures(N=60, q=1, beta=0.1):
    """
    Three dimensional process examples.
    """

    a = 0
    b = 1
    m = [[a, b, b], [b, a, b], [b, b, a]]
    mu = (3. / 2) * 1. / N
    fitness_landscape = linear_fitness_landscape(m)
    incentive = fermi(fitness_landscape, beta=beta, q=q)
    edges = incentive_process.multivariate_transitions(N,
                                                       incentive,
                                                       num_types=3,
                                                       mu=mu)
    d = stationary_distribution(edges, iterations=None)

    figure, tax = ternary.figure(scale=N)
    tax.heatmap(d, scale=N)
    tax.savefig(filename="ga_stationary.eps", dpi=600)

    d = expected_divergence(edges, q_d=0)
    figure, tax = ternary.figure(scale=N)
    tax.heatmap(d, scale=N)
    tax.savefig(filename="ga_d_0.eps", dpi=600)

    d = expected_divergence(edges, q_d=1)
    figure, tax = ternary.figure(scale=N)
    tax.heatmap(d, scale=N)
    tax.savefig(filename="ga_d_1.eps", dpi=600)
Пример #11
0
def graphical_abstract_figures(N=60, q=1, beta=0.1):
    """
    Three dimensional process examples.
    """

    a = 0
    b = 1
    m = [[a, b, b], [b, a, b], [b, b, a]]
    mu = (3. / 2 ) * 1. / N
    fitness_landscape = linear_fitness_landscape(m)
    incentive = fermi(fitness_landscape, beta=beta, q=q)
    edges = incentive_process.multivariate_transitions(N, incentive, num_types=3, mu=mu)
    d = stationary_distribution(edges, iterations=None)

    figure, tax = ternary.figure(scale=N)
    tax.heatmap(d, scale=N)
    tax.savefig(filename="ga_stationary.eps", dpi=600)

    d = expected_divergence(edges, q_d=0)
    figure, tax = ternary.figure(scale=N)
    tax.heatmap(d, scale=N)
    tax.savefig(filename="ga_d_0.eps", dpi=600)

    d = expected_divergence(edges, q_d=1)
    figure, tax = ternary.figure(scale=N)
    tax.heatmap(d, scale=N)
    tax.savefig(filename="ga_d_1.eps", dpi=600)
Пример #12
0
def test_extrema_moran_2(lim=1e-16):
    """
    Test for extrema of the stationary distribution.
    """
    n = 2
    N = 100
    mu = 1. / 1000
    m = [[1, 2], [3, 1]]
    maxes = set([(33, 67), (100,0), (0, 100)])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = replicator(fitness_landscape)
    edges = incentive_process.multivariate_transitions(N, incentive, num_types=n, mu=mu)
    s = stationary_distribution(edges, lim=lim)
    s2 = expected_divergence(edges, q_d=0)

    assert_equal(find_local_maxima(s), set(maxes))
    assert_equal(find_local_minima(s2), set(maxes))
Пример #13
0
def tournament_stationary_3(N, mu=None):
    """
    Example for a tournament selection matrix.
    """

    if not mu:
        mu = 3./2 * 1./N
    m = [[1,1,1], [0,1,1], [0,0,1]]
    num_types = len(m[0])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = replicator(fitness_landscape)
    edges = incentive_process.multivariate_transitions(N, incentive, num_types=num_types, mu=mu)
    s = stationary_distribution(edges)
    ternary.heatmap(s, scale=N, scientific=True)
    d = expected_divergence(edges, q_d=0)
    ternary.heatmap(d, scale=N, scientific=True)
    pyplot.show()
def compute_entropy_rate(N=30, n=2, m=None, incentive_func=None, beta=1.,
                         mu=None, exact=False, lim=1e-13, logspace=False):
    if not m:
        m = np.ones((n, n))
    if not incentive_func:
        incentive_func = incentives.fermi
    if not mu:
        # mu = (n-1.)/n * 1./(N+1)
        mu = 1. / N

    fitness_landscape = incentives.linear_fitness_landscape(m)
    incentive = incentive_func(fitness_landscape, beta=beta, q=1)
    edges = incentive_process.multivariate_transitions(
        N, incentive, num_types=n, mu=mu)
    s = stationary.stationary_distribution(edges, exact=exact, lim=lim,
                                           logspace=logspace)
    e = stationary.entropy_rate(edges, s)
    return e, s
Пример #15
0
def rps_figures(N=60, q=1, beta=1.):
    """
    Three rock-paper-scissors examples.
    """

    m = [[0, -1, 1], [1, 0, -1], [-1, 1, 0]]
    num_types = len(m[0])
    fitness_landscape = linear_fitness_landscape(m)
    for i, mu in enumerate([1./math.sqrt(N), 1./N, 1./N**(3./2)]):
        # Approximate calculation
        mu = 3/2. * mu
        incentive = fermi(fitness_landscape, beta=beta, q=q)
        edges = incentive_process.multivariate_transitions(N, incentive, num_types=num_types, mu=mu)
        d = stationary_distribution(edges, lim=1e-10)

        figure, tax = ternary.figure()
        tax.heatmap(d, scale=N)
        tax.savefig(filename="rsp_mu_" + str(i) + ".eps", dpi=600)
Пример #16
0
def moran(N, game_matrix=None, mu=None, incentive_func=replicator,
          exact=False, logspace=False):
    """
    A convenience function for the Moran process with mutation. Computes the
    transition probabilities and the stationary distribution.

    The number of types is determined from the dimensions of the game_matrix.

    Parameters
    ----------
    N: int
        The population size
    game_matrix: list of lists or numpy matrix, None
        The game matrix of the process, e.g. [[1, 2], [2, 1]] for the two-type
        Hawk-Dove game. If not specified, the 2-type neutral landscape is used.
    mu: float, None
        The mutation rate, if None then `mu` is set to 1 / N
    incentive_func: function, replicator
        A function defining the process, e.g. the Moran process, logit, Fermi, etc.
        Incentives functions are in stationary.processes.incentives
    exact: bool, False
        Use the approximate or exact calculation function
    logspace: bool, False
        Compute in log-space or not

    Returns
    -------
    edges, s, er: the list of transitions, the stationary distribution, and the
    entropy rate.
    """

    if not game_matrix:
        game_matrix = [[1, 1], [1, 1]]
    if not mu:
        mu = 1. / N
    num_types = len(game_matrix[0])

    fitness_landscape = linear_fitness_landscape(game_matrix)
    incentive = incentive_func(fitness_landscape)
    edges = incentive_process.multivariate_transitions(
        N, incentive, num_types=num_types, mu=mu)
    s = stationary_distribution(edges, exact=exact, logspace=logspace)
    er = entropy_rate(edges, s)
    return edges, s, er
Пример #17
0
def tournament_stationary_3(N, mu=None):
    """
    Example for a tournament selection matrix.
    """

    if not mu:
        mu = 3. / 2 * 1. / N
    m = [[1, 1, 1], [0, 1, 1], [0, 0, 1]]
    num_types = len(m[0])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = replicator(fitness_landscape)
    edges = incentive_process.multivariate_transitions(N,
                                                       incentive,
                                                       num_types=num_types,
                                                       mu=mu)
    s = stationary_distribution(edges)
    ternary.heatmap(s, scale=N, scientific=True)
    d = expected_divergence(edges, q_d=0)
    ternary.heatmap(d, scale=N, scientific=True)
    pyplot.show()
Пример #18
0
def test_extrema_moran_4(lim=1e-16):
    """
    Test for extrema of the stationary distribution.
    """
    n = 3
    N = 60
    mu = 3./ (2 * N)
    m = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
    maxes = set([(20,20,20)])
    mins = set([(0, 0, 60), (0, 60, 0), (60, 0, 0)])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = logit(fitness_landscape, beta=0.1)
    edges = incentive_process.multivariate_transitions(N, incentive, num_types=n, mu=mu)
    s = stationary_distribution(edges, lim=lim)
    s2 = expected_divergence(edges, q_d=0)

    assert_equal(find_local_maxima(s), set(maxes))
    assert_equal(find_local_minima(s), set(mins))
    assert_equal(find_local_minima(s2), set(maxes))
    assert_equal(find_local_maxima(s2), set(mins))
Пример #19
0
def rps_figures(N=60, q=1, beta=1.):
    """
    Three rock-paper-scissors examples.
    """

    m = [[0, -1, 1], [1, 0, -1], [-1, 1, 0]]
    num_types = len(m[0])
    fitness_landscape = linear_fitness_landscape(m)
    for i, mu in enumerate([1. / math.sqrt(N), 1. / N, 1. / N**(3. / 2)]):
        # Approximate calculation
        mu = 3 / 2. * mu
        incentive = fermi(fitness_landscape, beta=beta, q=q)
        edges = incentive_process.multivariate_transitions(N,
                                                           incentive,
                                                           num_types=num_types,
                                                           mu=mu)
        d = stationary_distribution(edges, lim=1e-10)

        figure, tax = ternary.figure()
        tax.heatmap(d, scale=N)
        tax.savefig(filename="rsp_mu_" + str(i) + ".eps", dpi=600)
Пример #20
0
def test_extrema_moran_3(lim=1e-12):
    """
    Test for extrema of the stationary distribution.
    """
    n = 2
    N = 100
    mu = 6./ 25
    m = [[1, 0], [0, 1]]
    maxes = set([(38, 62), (62, 38)])
    mins = set([(50, 50), (100, 0), (0, 100)])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = replicator(fitness_landscape)
    edges = incentive_process.multivariate_transitions(N, incentive, num_types=n, mu=mu)
    s = stationary_distribution(edges, lim=lim)
    flow = inflow_outflow(edges)

    for q_d in [0, 1]:
        s2 = expected_divergence(edges, q_d=1)
        assert_equal(find_local_maxima(s), set(maxes))
        assert_equal(find_local_minima(s), set(mins))
        assert_equal(find_local_minima(s2), set([(50,50), (40, 60), (60, 40)]))
        assert_equal(find_local_maxima(flow), set(mins))
Пример #21
0
def two_dim_transitions_figure(N, m, mu=0.01, incentive_func=replicator):
    """
    Plot transition entropies and stationary distributions.
    """

    n = len(m[0])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = incentive_func(fitness_landscape)
    if not mu:
        mu = 1. / N
    edges = incentive_process.multivariate_transitions(N,
                                                       incentive,
                                                       num_types=n,
                                                       mu=mu)

    s = stationary_distribution(edges, exact=True)
    d = edges_to_edge_dict(edges)

    # Set up plots
    gs = gridspec.GridSpec(3, 1)
    ax1 = pyplot.subplot(gs[0, 0])
    ax1.set_title("Transition Probabilities")
    ups, downs, _ = two_dim_transitions(edges)
    xs = range(0, N + 1)
    ax1.plot(xs, ups)
    ax1.plot(xs, downs)

    ax2 = pyplot.subplot(gs[1, 0])
    ax2.set_title("Relative Entropy")
    divs1 = expected_divergence(edges)
    divs2 = expected_divergence(edges, q_d=0)
    plot_dictionary(divs1, ax=ax2)
    plot_dictionary(divs2, ax=ax2)

    ax3 = pyplot.subplot(gs[2, 0])
    ax3.set_title("Stationary Distribution")
    plot_dictionary(s, ax=ax3)
    ax3.set_xlabel("Number of A individuals (i)")
Пример #22
0
def four_dim_figures(N=30, beta=1., q=1.):
    """
    Four dimensional example. Three dimensional slices are plotted
    for illustation.
    """

    m = [[0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [0, 0, 0, 1]]
    num_types = len(m[0])
    fitness_landscape = linear_fitness_landscape(m)
    mu = 4. / 3 * 1. / N

    incentive = fermi(fitness_landscape, beta=beta, q=q)
    edges = incentive_process.multivariate_transitions(N, incentive, num_types=num_types, mu=mu)

    d1 = expected_divergence(edges, q_d=0, boundary=True)
    d2 = stationary_distribution(edges)

    # We need to slice the 4dim dictionary into three-dim slices for plotting.
    for slice_index in range(4):
        for d in [d1, d2]:
            slice_dict = slice_dictionary(d, N, slice_index=3)
            figure, tax = ternary.figure(scale=N)
            tax.heatmap(slice_dict, style="d")
    pyplot.show()