-
Notifications
You must be signed in to change notification settings - Fork 2
/
fixation_examples.py
77 lines (59 loc) · 1.88 KB
/
fixation_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import math
import matplotlib
from matplotlib import pyplot
import matplotlib.gridspec as gridspec
from numpy import arange
from stationary import stationary_distribution, convenience
from stationary.utils.math_helpers import normalize
# Font config for plots
font = {'size': 20}
matplotlib.rc('font', **font)
def fixation_probabilities(N, r):
"""
The fixation probabilities of the classical Moran process.
Parameters
----------
N: int
The population size
r: float
Relative fitness.
"""
def phi(N, r, i=1.):
return (1. - math.pow(r, -i)) / (1. - math.pow(r, -N))
if r == 0:
return (0., 1.)
if r == 1:
return (1. / N, 1. / N)
return (phi(N, r), phi(N, 1. / r))
def fixation_comparison(N=20, r=1.2, mu=1e-24):
"""
Plot the fixation probabilities and the stationary limit.
"""
fs = []
ss = []
diffs = []
domain = list(arange(0.5, 1.5, 0.01))
for r in domain:
game_matrix = [[1, 1], [r, r]]
edges, s, er = convenience.moran(N, game_matrix, mu, exact=True,
logspace=True)
fix_1, fix_2 = fixation_probabilities(N, r)
s_1, s_2 = s[(0, N)], s[(N, 0)]
f = normalize([fix_1, fix_2])
fs.append(f[0])
ss.append(s_1)
diffs.append(fs[-1] - ss[-1])
gs = gridspec.GridSpec(2, 1)
ax1 = pyplot.subplot(gs[0, 0])
ax2 = pyplot.subplot(gs[1, 0])
ax1.plot(domain, fs)
ax1.plot(domain, ss)
ax1.set_xlabel("Relative fitness $r$")
ax1.set_ylabel("Fixation Probability $\\rho_A / (\\rho_A + \\rho_B)$")
ax1.set_title("Fixation Probabilities and Stationary Distribution")
ax2.plot(domain, diffs)
ax2.set_xlabel("Relative fitness $r$")
ax2.set_ylabel("$\\rho_A / (\\rho_A + \\rho_B) - s_{(O, N)}$")
pyplot.show()
if __name__ == '__main__':
fixation_comparison(16)