Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def test_stationary(t1=0.4, t2=0.6):
    """
    Test the stationary distribution computations a simple Markov process.
    """

    edges = [(0, 1, t1), (0, 0, 1. - t1), (1, 0, t2), (1, 1, 1. - t2)]
    s_0 = 1. / (1. + t1 / t2)
    exact_stationary = {0: s_0, 1: 1 - s_0}

    for logspace in [True, False]:
        s = stationary_distribution(edges, logspace=logspace)
        # Check that the stationary distribution satisfies balance conditions
        check_detailed_balance(edges, s)
        check_global_balance(edges, s)
        check_eigenvalue(edges, s)
        # Check that the approximation converged to the exact distribution
        for key in s.keys():
            assert_almost_equal(exact_stationary[key], s[key])
Exemplo n.º 6
0
def test_stationary(t1=0.4, t2=0.6):
    """
    Test the stationary distribution computations a simple Markov process.
    """

    edges = [(0, 1, t1), (0, 0, 1. - t1), (1, 0, t2), (1, 1, 1. - t2)]
    s_0 = 1./(1. + t1 / t2)
    exact_stationary = {0: s_0, 1: 1 - s_0}

    for logspace in [True, False]:
        s = stationary_distribution(edges, logspace=logspace)
        # Check that the stationary distribution satisfies balance conditions
        check_detailed_balance(edges, s)
        check_global_balance(edges, s)
        check_eigenvalue(edges, s)
        # Check that the approximation converged to the exact distribution
        for key in s.keys():
            assert_almost_equal(exact_stationary[key], s[key])
Exemplo n.º 7
0
def test_stationary_4():
    """
    Test the stationary distribution computations a simple Markov process.
    """

    edges = [(0, 0, 1. / 2), (0, 1, 1. / 2), (0, 2, 0), (0, 3, 0),
             (1, 0, 1. / 6), (1, 1, 1. / 2), (1, 2, 1. / 3), (1, 3, 0),
             (2, 0, 0), (2, 1, 1. / 3), (2, 2, 1. / 2), (2, 3, 1. / 6),
             (3, 0, 0), (3, 1, 0), (3, 2, 1. / 2), (3, 3, 1. / 2)]
    exact_stationary = {0: 1. / 8, 1: 3. / 8, 2: 3. / 8, 3: 1. / 8}

    for logspace in [True, False]:
        s = stationary_distribution(edges, logspace=logspace)
        # Check that the stationary distribution satisfies balance conditions
        check_detailed_balance(edges, s)
        check_global_balance(edges, s)
        check_eigenvalue(edges, s)
        # Check that the approximation converged to the exact distribution
        for key in s.keys():
            assert_almost_equal(exact_stationary[key], s[key])
Exemplo n.º 8
0
def test_stationary_4():
    """
    Test the stationary distribution computations a simple Markov process.
    """

    edges = [(0, 0, 1./2), (0, 1, 1./2), (0, 2, 0), (0, 3, 0),
             (1, 0, 1./6), (1, 1, 1./2), (1, 2, 1./3), (1, 3, 0),
             (2, 0, 0), (2, 1, 1./3), (2, 2, 1./2), (2, 3, 1./6),
             (3, 0, 0), (3, 1, 0), (3, 2, 1./2), (3, 3, 1./2)]
    exact_stationary = {0: 1./8, 1: 3./8, 2: 3./8, 3: 1./8}

    for logspace in [True, False]:
        s = stationary_distribution(edges, logspace=logspace)
        # Check that the stationary distribution satisfies balance conditions
        check_detailed_balance(edges, s)
        check_global_balance(edges, s)
        check_eigenvalue(edges, s)
        # Check that the approximation converged to the exact distribution
        for key in s.keys():
            assert_almost_equal(exact_stationary[key], s[key])