-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples.py
273 lines (220 loc) · 8.26 KB
/
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import math
import os
import matplotlib
from matplotlib import pyplot
import matplotlib.gridspec as gridspec
from stationary import stationary_distribution
from stationary.processes.incentives import *
from stationary.processes import incentive_process, wright_fisher
from stationary.utils.math_helpers import simplex_generator, slice_dictionary
from stationary.utils.edges import edges_to_edge_dict
from stationary.utils.plotting import plot_dictionary
from stationary.utils import expected_divergence
from stationary.utils.bomze import bomze_matrices
from stationary.utils.files import ensure_directory
import ternary
# Font config for plots
font = {'size': 20}
matplotlib.rc('font', **font)
def bomze_figures(N=60, beta=1, process="incentive", directory=None):
"""
Makes plots of the stationary distribution and expected divergence for each
of the plots in Bomze's classification.
"""
if not directory:
directory = "bomze_paper_figures_%s" % process
ensure_directory(directory)
for i, m in enumerate(bomze_matrices()):
mu = 3./2 * 1./N
fitness_landscape = linear_fitness_landscape(m)
incentive = fermi(fitness_landscape, beta=beta)
edges = incentive_process.multivariate_transitions(N, incentive,
num_types=3, mu=mu)
d1 = stationary_distribution(edges)
filename = os.path.join(directory, "%s_%s_stationary.eps" % (i, N))
figure, tax = ternary.figure(scale = N)
tax.heatmap(d1)
tax.savefig(filename=filename)
pyplot.close(figure)
for q_d in [0., 1.]:
d2 = expected_divergence(edges, q_d=q_d)
filename = os.path.join(directory, "%s_%s_%s.eps" % (i, N, q_d))
figure, tax = ternary.figure(scale = N)
tax.heatmap(d2)
tax.savefig(filename=filename)
pyplot.close(figure)
def four_dim_figures(N=30, beta=1., q=1.):
"""
Four dimensional example. Three dimensional slices are plotted
for illustation.
"""
m = [[0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [0, 0, 0, 1]]
num_types = len(m[0])
fitness_landscape = linear_fitness_landscape(m)
mu = 4. / 3 * 1. / N
incentive = fermi(fitness_landscape, beta=beta, q=q)
edges = incentive_process.multivariate_transitions(N, incentive, num_types=num_types, mu=mu)
d1 = expected_divergence(edges, q_d=0, boundary=True)
d2 = stationary_distribution(edges)
# We need to slice the 4dim dictionary into three-dim slices for plotting.
for slice_index in range(4):
for d in [d1, d2]:
slice_dict = slice_dictionary(d, N, slice_index=3)
figure, tax = ternary.figure(scale=N)
tax.heatmap(slice_dict, style="d")
pyplot.show()
def graphical_abstract_figures(N=60, q=1, beta=0.1):
"""
Three dimensional process examples.
"""
a = 0
b = 1
m = [[a, b, b], [b, a, b], [b, b, a]]
mu = (3. / 2 ) * 1. / N
fitness_landscape = linear_fitness_landscape(m)
incentive = fermi(fitness_landscape, beta=beta, q=q)
edges = incentive_process.multivariate_transitions(N, incentive, num_types=3, mu=mu)
d = stationary_distribution(edges, iterations=None)
figure, tax = ternary.figure(scale=N)
tax.heatmap(d, scale=N)
tax.savefig(filename="ga_stationary.eps", dpi=600)
d = expected_divergence(edges, q_d=0)
figure, tax = ternary.figure(scale=N)
tax.heatmap(d, scale=N)
tax.savefig(filename="ga_d_0.eps", dpi=600)
d = expected_divergence(edges, q_d=1)
figure, tax = ternary.figure(scale=N)
tax.heatmap(d, scale=N)
tax.savefig(filename="ga_d_1.eps", dpi=600)
def rps_figures(N=60, q=1, beta=1.):
"""
Three rock-paper-scissors examples.
"""
m = [[0, -1, 1], [1, 0, -1], [-1, 1, 0]]
num_types = len(m[0])
fitness_landscape = linear_fitness_landscape(m)
for i, mu in enumerate([1./math.sqrt(N), 1./N, 1./N**(3./2)]):
# Approximate calculation
mu = 3/2. * mu
incentive = fermi(fitness_landscape, beta=beta, q=q)
edges = incentive_process.multivariate_transitions(N, incentive, num_types=num_types, mu=mu)
d = stationary_distribution(edges, lim=1e-10)
figure, tax = ternary.figure()
tax.heatmap(d, scale=N)
tax.savefig(filename="rsp_mu_" + str(i) + ".eps", dpi=600)
def tournament_stationary_3(N, mu=None):
"""
Example for a tournament selection matrix.
"""
if not mu:
mu = 3./2 * 1./N
m = [[1,1,1], [0,1,1], [0,0,1]]
num_types = len(m[0])
fitness_landscape = linear_fitness_landscape(m)
incentive = replicator(fitness_landscape)
edges = incentive_process.multivariate_transitions(N, incentive, num_types=num_types, mu=mu)
s = stationary_distribution(edges)
ternary.heatmap(s, scale=N, scientific=True)
d = expected_divergence(edges, q_d=0)
ternary.heatmap(d, scale=N, scientific=True)
pyplot.show()
def two_dim_transitions(edges):
"""
Compute the + and - transitions for plotting in two_dim_transitions_figure
"""
d = edges_to_edge_dict(edges)
N = sum(edges[0][0])
ups = []
downs = []
stays = []
for i in range(0, N+1):
try:
up = d[((i, N-i), (i+1, N-i-1))]
except KeyError:
up = 0
try:
down = d[((i, N-i), (i-1, N-i+1))]
except KeyError:
down = 0
ups.append(up)
downs.append(down)
stays.append(1 - up - down)
return ups, downs, stays
def two_dim_transitions_figure(N, m, mu=0.01, incentive_func=replicator):
"""
Plot transition entropies and stationary distributions.
"""
n = len(m[0])
fitness_landscape = linear_fitness_landscape(m)
incentive = incentive_func(fitness_landscape)
if not mu:
mu = 1./ N
edges = incentive_process.multivariate_transitions(N, incentive, num_types=n, mu=mu)
s = stationary_distribution(edges, exact=True)
d = edges_to_edge_dict(edges)
# Set up plots
gs = gridspec.GridSpec(3, 1)
ax1 = pyplot.subplot(gs[0, 0])
ax1.set_title("Transition Probabilities")
ups, downs, _ = two_dim_transitions(edges)
xs = range(0, N+1)
ax1.plot(xs, ups)
ax1.plot(xs, downs)
ax2 = pyplot.subplot(gs[1, 0])
ax2.set_title("Relative Entropy")
divs1 = expected_divergence(edges)
divs2 = expected_divergence(edges, q_d=0)
plot_dictionary(divs1, ax=ax2)
plot_dictionary(divs2, ax=ax2)
ax3 = pyplot.subplot(gs[2, 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 two_player_example(N=50):
"""
Two player examples plots.
"""
m = [[1, 2], [2, 1]]
#m = [[1, 1], [0, 1]]
mu = 1. / N
incentive_func = replicator
figure, ax = pyplot.subplots()
two_dim_transitions_figure(N, m, mu=mu, incentive_func=incentive_func)
figure, ax = pyplot.subplots()
two_dim_wright_fisher_figure(N, m, mu=mu, incentive_func=replicator)
pyplot.show()
if __name__ == '__main__':
# Two-type example
two_player_example()
## Three dimensional examples
graphical_abstract_figures()
rps_figures()
tournament_stationary_3(N=60, mu=1./3)
bomze_figures()
## Four dimensional examples
four_dim_figures()