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 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
def test_wright_fisher(N=20, lim=1e-10, n=2):
    """Test 2 dimensional Wright-Fisher process."""
    for n in [2, 3]:
        mu = (n - 1.) / n * 1. / (N + 1)
        m = numpy.ones((n, n))  # neutral landscape
        fitness_landscape = linear_fitness_landscape(m)
        incentive = replicator(fitness_landscape)

        # Wright-Fisher
        for low_memory in [True, False]:
            edge_func = wright_fisher.multivariate_transitions(
                N, incentive, mu=mu, num_types=n, low_memory=low_memory)
            states = list(simplex_generator(N, d=n - 1))
            for logspace in [False, True]:
                s = stationary_distribution(edge_func,
                                            states=states,
                                            iterations=200,
                                            lim=lim,
                                            logspace=logspace)
                wf_edges = edge_func_to_edges(edge_func, states)

                er = entropy_rate(wf_edges, s)
                assert_greater_equal(er, 0)

                # Check that the stationary distribution satistifies balance conditions
                check_detailed_balance(wf_edges, s, places=2)
                check_global_balance(wf_edges, s, places=4)
                check_eigenvalue(wf_edges, s, places=2)
Example #4
0
def test_wright_fisher(N=20, lim=1e-10, n=2):
    """Test 2 dimensional Wright-Fisher process."""
    for n in [2, 3]:
        mu = (n - 1.) / n * 1. / (N + 1)
        m = numpy.ones((n, n)) # neutral landscape
        fitness_landscape = linear_fitness_landscape(m)
        incentive = replicator(fitness_landscape)

        # Wright-Fisher
        for low_memory in [True, False]:
            edge_func = wright_fisher.multivariate_transitions(
                N, incentive, mu=mu, num_types=n, low_memory=low_memory)
            states = list(simplex_generator(N, d=n-1))
            for logspace in [False, True]:
                s = stationary_distribution(
                    edge_func, states=states, iterations=200, lim=lim,
                    logspace=logspace)
                wf_edges = edge_func_to_edges(edge_func, states)

                er = entropy_rate(wf_edges, s)
                assert_greater_equal(er, 0)

                # Check that the stationary distribution satistifies balance
                # conditions
                check_detailed_balance(wf_edges, s, places=2)
                check_global_balance(wf_edges, s, places=4)
                check_eigenvalue(wf_edges, s, places=2)
def test_incentive_process(lim=1e-14):
    """
    Compare stationary distribution computations to known analytic form for
    neutral landscape for the Moran process.
    """

    for n, N in [(2, 10), (2, 40), (3, 10), (3, 20), (4, 10)]:
        mu = (n - 1.) / n * 1./ (N + 1)
        alpha = N * mu / (n - 1. - n * mu)

        # Neutral landscape is the default
        edges = incentive_process.compute_edges(N, num_types=n,
                                                incentive_func=replicator, mu=mu)
        for logspace in [False, True]:
            stationary_1 = incentive_process.neutral_stationary(
                N, alpha, n, logspace=logspace)
            for exact in [False, True]:
                stationary_2 = stationary_distribution(
                    edges, lim=lim, logspace=logspace, exact=exact)
                for key in stationary_1.keys():
                    assert_almost_equal(
                        stationary_1[key], stationary_2[key], places=4)

        # 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)

        # Test Entropy Rate bounds
        er = entropy_rate(edges, stationary_1)
        h = (2. * n - 1) / n * numpy.log(n)
        assert_less_equal(er, h)
        assert_greater_equal(er, 0)
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
Example #7
0
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)
        assert_equal(find_local_minima(s), set(mins))
        er = entropy_rate(edge_func, s, states=states)
        assert_greater_equal(er, 0)