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)")
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)")
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_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_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_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)