Example #1
0
def two_dim_wright_fisher_figure(N, m, mu=0.01, incentive_func=replicator):
    """
    Plot relative entropies and stationary distribution for the Wright-Fisher
    process.
    """

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

    edge_func = wright_fisher.multivariate_transitions(N, incentive, mu=mu, num_types=n)
    states = list(simplex_generator(N, d=n-1))
    s = stationary_distribution(edge_func, states=states, iterations=4*N)
    s0 = expected_divergence(edge_func, states=states, q_d=0)
    s1 = expected_divergence(edge_func, states=states, q_d=1)

    # Set up plots
    gs = gridspec.GridSpec(2, 1)

    ax2 = pyplot.subplot(gs[0, 0])
    ax2.set_title("Relative Entropy")
    plot_dictionary(s0, ax=ax2)
    plot_dictionary(s1, ax=ax2)

    ax3 = pyplot.subplot(gs[1, 0])
    ax3.set_title("Stationary Distribution")
    plot_dictionary(s, ax=ax3)
    ax3.set_xlabel("Number of A individuals (i)")
Example #2
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)
Example #3
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)
Example #4
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)")
Example #5
0
def two_dim_wright_fisher_figure(N, m, mu=0.01, incentive_func=replicator):
    """
    Plot relative entropies and stationary distribution for the Wright-Fisher
    process.
    """

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

    edge_func = wright_fisher.multivariate_transitions(N,
                                                       incentive,
                                                       mu=mu,
                                                       num_types=n)
    states = list(simplex_generator(N, d=n - 1))
    s = stationary_distribution(edge_func, states=states, iterations=4 * N)
    s0 = expected_divergence(edge_func, states=states, q_d=0)
    s1 = expected_divergence(edge_func, states=states, q_d=1)

    # Set up plots
    gs = gridspec.GridSpec(2, 1)

    ax2 = pyplot.subplot(gs[0, 0])
    ax2.set_title("Relative Entropy")
    plot_dictionary(s0, ax=ax2)
    plot_dictionary(s1, ax=ax2)

    ax3 = pyplot.subplot(gs[1, 0])
    ax3.set_title("Stationary Distribution")
    plot_dictionary(s, ax=ax3)
    ax3.set_xlabel("Number of A individuals (i)")
Example #6
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)
Example #7
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()
def test_extrema_wf(lim=1e-10):
    """
    For small mu, the Wright-Fisher process is minimal in the center.
    Test that this happens.
    """

    for n, N, mins in [(2, 40, [(20, 20)]), (3, 30, [(10, 10, 10)])]:
        mu = 1. / N**3
        m = numpy.ones((n, n))  # neutral landscape
        fitness_landscape = linear_fitness_landscape(m)
        incentive = replicator(fitness_landscape)

        edge_func = wright_fisher.multivariate_transitions(N,
                                                           incentive,
                                                           mu=mu,
                                                           num_types=n)
        states = list(simplex_generator(N, d=n - 1))
        s = stationary_distribution(edge_func,
                                    states=states,
                                    iterations=4 * N,
                                    lim=lim)
        s2 = expected_divergence(edge_func, states=states, q_d=0)

        assert_equal(find_local_minima(s), set(mins))

        er = entropy_rate(edge_func, s, states=states)
        assert_greater_equal(er, 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)]))
Example #10
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)]))
Example #11
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)
Example #12
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)")
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))
Example #14
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()
Example #15
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 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))
def test_extrema_wf(lim=1e-10):
    """
    For small mu, the Wright-Fisher process is minimal in the center.
    Test that this happens.
    """

    for n, N, mins in [(2, 40, [(20, 20)]), (3, 30, [(10, 10, 10)])]:
        mu = 1. / N ** 3
        m = numpy.ones((n, n)) # neutral landscape
        fitness_landscape = linear_fitness_landscape(m)
        incentive = replicator(fitness_landscape)

        edge_func = wright_fisher.multivariate_transitions(N, incentive, mu=mu, num_types=n)
        states = list(simplex_generator(N, d=n-1))
        s = stationary_distribution(edge_func, states=states, iterations=4*N, lim=lim)
        s2 = expected_divergence(edge_func, states=states, q_d=0)

        assert_equal(find_local_minima(s), set(mins))

        er = entropy_rate(edge_func, s, states=states)
        assert_greater_equal(er, 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))
Example #19
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()
if __name__ == '__main__':
    N = 40
    mu = 3./2. * 1./N
    m = [[1, 2], [2, 1]]
    fitness_landscape = linear_fitness_landscape(m, normalize=False)
    incentive = replicator(fitness_landscape)
    death_probabilities = even_death(N)

    edges = variable_population_transitions(
        N, fitness_landscape, death_probabilities, incentive=incentive, mu=mu)
    s = stationary_distribution(edges, iterations=10000)

    # Print out the states with the highest stationary probabilities
    vs = [(v, k) for (k, v) in s.items()]
    vs.sort(reverse=True)
    print(vs[:10])

    # Plot the stationary distribution and expected divergence
    figure, tax = ternary.figure(scale=N)
    tax.heatmap(s)

    d2 = expected_divergence(edges, q_d=0)
    d = dict()
    for k, v in d2.items():
        d[k] = math.sqrt(v)

    figure, tax = ternary.figure(scale=N)
    tax.heatmap(d)

    pyplot.show()
if __name__ == '__main__':
    N = 40
    mu = 3./2. * 1./N
    m = [[1, 2], [2, 1]]
    fitness_landscape = linear_fitness_landscape(m, normalize=False)
    incentive = replicator(fitness_landscape)
    death_probabilities = even_death(N)

    edges = variable_population_transitions(N, fitness_landscape, death_probabilities, incentive=incentive, mu=mu)
    s = stationary_distribution(edges, iterations=10000)

    # Print out the states with the highest stationary probabilities
    vs = [(v,k) for (k,v) in s.items()]
    vs.sort(reverse=True)
    print vs[:10]

    # Plot the stationary distributoin and expected divergence

    figure, tax = ternary.figure(scale=N)
    tax.heatmap(s)

    d2 = expected_divergence(edges, q_d=0)
    d = dict()
    for k, v in d2.items():
        d[k] = math.sqrt(v)

    figure, tax = ternary.figure(scale=N)
    tax.heatmap(d)

    pyplot.show()