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)]))
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)]))
def full_example(N=60,
                 m=None,
                 mu=None,
                 pickle_filename="inv_enum.pickle",
                 beta=1.,
                 filename="enumerated_edges.csv"):
    """
    Full example of exporting the stationary calculation to C++.
    """

    print "Computing graph of the Markov process."
    if not mu:
        mu = 3. / 2 * 1. / N
    if m is None:
        m = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
    iterations = 5 * N

    num_types = len(m[0])
    fitness_landscape = linear_fitness_landscape(m, normalize=True)
    incentive = fermi(fitness_landscape, beta=beta)
    edges_gen = incentive_process.multivariate_transitions_gen(
        N, incentive, num_types=num_types, mu=mu)

    print "Outputting graph to %s" % filename
    inv_enum = output_enumerated_edges(N,
                                       num_types,
                                       edges_gen,
                                       filename=filename)
    print "Saving inverse enumeration to %s" % pickle_filename
    pickle_inv_enumeration(inv_enum, pickle_filename="inv_enum.pickle")

    print "Running C++ Calculation"
    cwd = os.getcwd()
    executable = os.path.join(cwd, "a.out")
    subprocess.call([executable, filename, str(iterations)])

    print "Loading stationary distribution"
    s = stationary_gen(filename="enumerated_stationary.txt",
                       pickle_filename="inv_enum.pickle")

    print "Rendering stationary to SVG"
    vmax, vmin = stationary_max_min()
    s = stationary_gen(filename="enumerated_stationary.txt",
                       pickle_filename="inv_enum.pickle")
    ternary.svg_heatmap(s,
                        N,
                        "stationary.svg",
                        vmax=vmax,
                        vmin=vmin,
                        style='h')
def full_example(N, m, mu, beta=1., pickle_filename="inv_enum.pickle",
                 filename="enumerated_edges.csv"):
    """
    Full example of exporting the stationary calculation to C++.
    """

    print("Computing graph of the Markov process.")
    if not mu:
        mu = 3. / 2 * 1. / N
    if m is None:
        m = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
    iterations = 200 * N

    num_types = len(m[0])
    fitness_landscape = linear_fitness_landscape(m)
    incentive = fermi(fitness_landscape, beta=beta)
    edges_gen = multivariate_transitions_gen(
        N, incentive, num_types=num_types, mu=mu)

    print("Outputting graph to %s" % filename)
    inv_enum = output_enumerated_edges(
        N, num_types, edges_gen, filename=filename)
    print("Saving inverse enumeration to %s" % pickle_filename)
    pickle_inv_enumeration(inv_enum, pickle_filename="inv_enum.pickle")

    print("Running C++ Calculation")
    cwd = os.getcwd()
    executable = os.path.join(cwd, "a.out")
    subprocess.call([executable, filename, str(iterations)])

    print("Rendering stationary to SVG")
    vmax, vmin = stationary_max_min()
    s = list(stationary_gen(
        filename="enumerated_stationary.txt",
        pickle_filename="inv_enum.pickle"))
    ternary.svg_heatmap(s, N, "stationary.svg", vmax=vmax, vmin=vmin, style='h')

    print("Rendering stationary")
    tax = render_stationary(s)
    tax.ticks(axis='lbr', linewidth=1, multiple=N//3, offset=0.015)
    tax.clear_matplotlib_ticks()
    plt.show()
def full_example(N=60, m=None, mu=None, pickle_filename="inv_enum.pickle",
                 beta=1., filename="enumerated_edges.csv"):
    """
    Full example of exporting the stationary calculation to C++.
    """

    print "Computing graph of the Markov process."
    if not mu:
        mu = 3. / 2 * 1. / N
    if m is None:
        m = [[0,1,1],[1,0,1],[1,1,0]]
    iterations = 5 * N


    num_types = len(m[0])
    fitness_landscape = linear_fitness_landscape(m, normalize=True)
    incentive = fermi(fitness_landscape, beta=beta)
    edges_gen = incentive_process.multivariate_transitions_gen(N, incentive, num_types=num_types, mu=mu)

    print "Outputting graph to %s" % filename
    inv_enum = output_enumerated_edges(N, num_types, edges_gen, filename=filename)
    print "Saving inverse enumeration to %s" % pickle_filename
    pickle_inv_enumeration(inv_enum, pickle_filename="inv_enum.pickle")

    print "Running C++ Calculation"
    cwd = os.getcwd()
    executable = os.path.join(cwd, "a.out")
    subprocess.call([executable, filename, str(iterations)])

    print "Loading stationary distribution"
    s = stationary_gen(filename="enumerated_stationary.txt",
                   pickle_filename="inv_enum.pickle")

    print "Rendering stationary to SVG"
    vmax, vmin = stationary_max_min()
    s = stationary_gen(filename="enumerated_stationary.txt",
                   pickle_filename="inv_enum.pickle")
    ternary.svg_heatmap(s, N, "stationary.svg", vmax=vmax, vmin=vmin, style='h')