piegy 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- piegy/__init__.py +96 -0
- piegy/__version__.py +17 -0
- piegy/analysis.py +222 -0
- piegy/data_tools.py +129 -0
- piegy/figures.py +503 -0
- piegy/model.py +1168 -0
- piegy/test_var.py +525 -0
- piegy/tools/__init__.py +15 -0
- piegy/tools/figure_tools.py +206 -0
- piegy/tools/file_tools.py +29 -0
- piegy/videos.py +322 -0
- piegy-1.0.0.dist-info/METADATA +104 -0
- piegy-1.0.0.dist-info/RECORD +16 -0
- piegy-1.0.0.dist-info/WHEEL +5 -0
- piegy-1.0.0.dist-info/licenses/LICENSE.txt +28 -0
- piegy-1.0.0.dist-info/top_level.txt +1 -0
piegy/figures.py
ADDED
@@ -0,0 +1,503 @@
|
|
1
|
+
'''
|
2
|
+
Contains all the major plot functions.
|
3
|
+
|
4
|
+
Plots for population:
|
5
|
+
- UV_heatmap: Used for 2D space (both N, M > 1), plot distribution of U, V in all patches within a specified time interval.
|
6
|
+
Average population over that interval is taken.
|
7
|
+
- UV_bar: Used for 1D space (N or M == 1), counterpart of UV_heatmap.
|
8
|
+
Plot average distribution of U, V in a specified time interval in a barplot.
|
9
|
+
- UV_dyna: Plot change of total U, V overtime.
|
10
|
+
- UV_hist: Make a histogram of U, V in a specified time interval.
|
11
|
+
- UV_std: Plot change of standard deviation of U, V over time.
|
12
|
+
- UV_expected_val: Calculate expected U, V distribution based on matrices, assuming no migration return values (np.array)
|
13
|
+
- UV_expected: Calculate expected distribution of U, V based on matrices, assuming no migration.
|
14
|
+
|
15
|
+
|
16
|
+
Plots for payoff:
|
17
|
+
- pi_heatmap: Used for 2D space, plot distribution of U_pi & V_pi within a specified time interval.
|
18
|
+
Average payoff over that interval is taken.
|
19
|
+
- pi_bar: Used for 1D space, counterpart of pi_heatmap.
|
20
|
+
Plot average distribution of U_pi & V_pi in a specified time interval in a bar plot.
|
21
|
+
- pi_dyna: Plot change of total U_pi, V_pi overtime.
|
22
|
+
- pi_hist: Make a histogram of U_pi, V_pi in a specified time interval.
|
23
|
+
- pi_std: Plot change of standard deviation of U_pi, V_pi over time.
|
24
|
+
|
25
|
+
|
26
|
+
Popu-payoff correlation:
|
27
|
+
- UV_pi: Make two scatter plots: x-axes are U, V, y-axes are U's and V's payoff, in a specified time interval.
|
28
|
+
Reveals relationship between population and payoff.
|
29
|
+
|
30
|
+
'''
|
31
|
+
|
32
|
+
|
33
|
+
from .tools import figure_tools as figure_t
|
34
|
+
from . import model as model
|
35
|
+
|
36
|
+
import matplotlib.pyplot as plt
|
37
|
+
import numpy as np
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
# curve type in plot
|
42
|
+
# used by UV_dyna, UV_std, and pi_dyna
|
43
|
+
CURVE_TYPE = '-'
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
def UV_heatmap(sim, U_color = 'Purples', V_color = 'Greens', start = 0.95, end = 1.0, annot = False, fmt = '.3g'):
|
48
|
+
'''
|
49
|
+
Makes two heatmaps for U, V average distribution over a time interval, respectively. Works best for 2D space.
|
50
|
+
1D works as well, but figures look bad.
|
51
|
+
|
52
|
+
Inputs:
|
53
|
+
sim: A model.simulation object.
|
54
|
+
U_color: Color for U's heatmap, uses matplotlib color maps.
|
55
|
+
V_color: Color for V's heatmap.
|
56
|
+
start: (0,1) float, where the interval should start from. Intended as a 'percentage'.
|
57
|
+
For example, start = 0.8 means the interval should start from the 80% point of sim.maxtime.
|
58
|
+
end: (0,1) float, where the interval ends.
|
59
|
+
annot: bool, whether to add annotations (show exact numbers for every patch).
|
60
|
+
fmt: Number format for annotations. How many digits you want to keep. Please set annot = True first and then use fmt.
|
61
|
+
|
62
|
+
Returns:
|
63
|
+
fig1, fig2: Two heatmaps of U, V distribution.
|
64
|
+
'''
|
65
|
+
|
66
|
+
start_index = int(start * sim.max_record)
|
67
|
+
end_index = int(end * sim.max_record)
|
68
|
+
|
69
|
+
# see ave_interval below
|
70
|
+
U_ave = figure_t.ave_interval(sim.U, start_index, end_index)
|
71
|
+
V_ave = figure_t.ave_interval(sim.V, start_index, end_index)
|
72
|
+
|
73
|
+
#### plot ####
|
74
|
+
|
75
|
+
U_title = figure_t.gen_title('U', start, end)
|
76
|
+
U_text = figure_t.gen_text(np.mean(U_ave), np.std(U_ave))
|
77
|
+
V_title = figure_t.gen_title('V', start, end)
|
78
|
+
V_text = figure_t.gen_text(np.mean(V_ave), np.std(V_ave))
|
79
|
+
|
80
|
+
fig1 = figure_t.heatmap(U_ave, U_color, annot, fmt, U_title, U_text)
|
81
|
+
fig2 = figure_t.heatmap(V_ave, V_color, annot, fmt, V_title, V_text)
|
82
|
+
|
83
|
+
return fig1, fig2
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
def UV_bar(sim, U_color = 'purple', V_color = 'green', start = 0.95, end = 1.0):
|
88
|
+
'''
|
89
|
+
Makes two barplots for U, V average distribution over a time interval. Works best for 1D space.
|
90
|
+
2D works as well, but figures look bad.
|
91
|
+
|
92
|
+
Inputs:
|
93
|
+
sim: A model.simulation object.
|
94
|
+
U_color: Color of U's barplot. Uses Matplotlib colors.
|
95
|
+
See available colors at: https://matplotlib.org/stable/gallery/color/named_colors.html
|
96
|
+
V_color: Color of V's barplot. Uses Matplotlib colors.
|
97
|
+
start: (0,1) float. How much proportion of sim.maxtime you want the interval to start from.
|
98
|
+
end: (0,1) float. Where you want the interval to end.
|
99
|
+
|
100
|
+
Returns:
|
101
|
+
fig1, fig2: Two Matplotlib bar plots, for U and V, respectively.
|
102
|
+
'''
|
103
|
+
|
104
|
+
start_index = int(start * sim.max_record)
|
105
|
+
end_index = int(end * sim.max_record)
|
106
|
+
|
107
|
+
U_ave = figure_t.ave_interval_1D(sim.U, start_index, end_index)
|
108
|
+
V_ave = figure_t.ave_interval_1D(sim.V, start_index, end_index)
|
109
|
+
|
110
|
+
#### plot ####
|
111
|
+
|
112
|
+
U_title = figure_t.gen_title('U', start, end)
|
113
|
+
U_text = figure_t.gen_text(np.mean(U_ave), np.std(U_ave))
|
114
|
+
V_title = figure_t.gen_title('V', start, end)
|
115
|
+
V_text = figure_t.gen_text(np.mean(V_ave), np.std(V_ave))
|
116
|
+
|
117
|
+
fig1 = figure_t.bar(U_ave, color = U_color, xlabel = 'patches', ylabel = 'U', title = U_title, text = U_text)
|
118
|
+
fig2 = figure_t.bar(V_ave, color = V_color, xlabel = 'patches', ylabel = 'V', title = V_title, text = V_text)
|
119
|
+
|
120
|
+
return fig1, fig2
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
def UV_dyna(sim, interval = 20, grid = True):
|
126
|
+
'''
|
127
|
+
Plots how total U, V change overtime.
|
128
|
+
The curves are not directly based on every single data point.
|
129
|
+
Rather, it takes the average over many intervals of points to smooth out local fluctuations.
|
130
|
+
For example, interval = 20 means the first point on the curves are based on the average value of data points 0~19.
|
131
|
+
So if there are 2000 data points in total, then there will be 2000 / 20 = 100 points on the curves.
|
132
|
+
|
133
|
+
Inputs:
|
134
|
+
sim: A model.simulation object.
|
135
|
+
interval: How many data points to take average over. Larger value makes curves smoother, but also loses local fluctuations.
|
136
|
+
NOTE: this interval doesn't overlap with sim.compress_itv.
|
137
|
+
e.g. you already took average over every 20 data points, then using interval <= 20 here has no smoothing effect.
|
138
|
+
grid: Whether to add grid lines to plot.
|
139
|
+
|
140
|
+
Returns:
|
141
|
+
fig: A Matplotlib figure, contains U's, V's, and U+V population.
|
142
|
+
'''
|
143
|
+
|
144
|
+
# store the average values in lists
|
145
|
+
U_curve = []
|
146
|
+
V_curve = []
|
147
|
+
total_curve = []
|
148
|
+
|
149
|
+
interval = figure_t.scale_interval(interval, sim.compress_itv)
|
150
|
+
interval_num = int(sim.max_record / interval)
|
151
|
+
|
152
|
+
for i in range(interval_num):
|
153
|
+
U_ave = figure_t.ave_interval(sim.U, i * interval, (i + 1) * interval)
|
154
|
+
V_ave = figure_t.ave_interval(sim.V, i * interval, (i + 1) * interval)
|
155
|
+
|
156
|
+
U_curve.append(np.sum(U_ave))
|
157
|
+
V_curve.append(np.sum(V_ave))
|
158
|
+
total_curve.append(U_curve[-1] + V_curve[-1])
|
159
|
+
|
160
|
+
#### plot ####
|
161
|
+
xaxis = np.linspace(0, sim.maxtime, len(U_curve))
|
162
|
+
|
163
|
+
fig, ax = plt.subplots()
|
164
|
+
ax.grid(grid)
|
165
|
+
ax.plot(xaxis, U_curve, CURVE_TYPE, label = 'U')
|
166
|
+
ax.plot(xaxis, V_curve, CURVE_TYPE, label = 'V')
|
167
|
+
ax.plot(xaxis, total_curve, CURVE_TYPE, label = 'total')
|
168
|
+
ax.title.set_text('U & V over time')
|
169
|
+
ax.legend()
|
170
|
+
|
171
|
+
return fig
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
def UV_hist(sim, U_color = 'purple', V_color = 'green', start = 0.95, end = 1.0):
|
177
|
+
'''
|
178
|
+
Makes density histograms for U, V's average distribution over an interval.
|
179
|
+
Sometimes it may not be shown in density plots due to matplotlib features.
|
180
|
+
|
181
|
+
Returns:
|
182
|
+
fig1, fig2: Two Matplotlib histograms, for U and V, respectively.
|
183
|
+
'''
|
184
|
+
|
185
|
+
start_index = int(start * sim.max_record)
|
186
|
+
end_index = int(end * sim.max_record)
|
187
|
+
|
188
|
+
U_ave = figure_t.ave_interval_1D(sim.U, start_index, end_index)
|
189
|
+
V_ave = figure_t.ave_interval_1D(sim.V, start_index, end_index)
|
190
|
+
|
191
|
+
#### plot ####
|
192
|
+
|
193
|
+
fig1, ax1 = plt.subplots()
|
194
|
+
ax1.set_xlabel('U')
|
195
|
+
ax1.set_ylabel('density')
|
196
|
+
ax1.hist(U_ave, color = U_color, density = True)
|
197
|
+
ax1.title.set_text(figure_t.gen_title('U hist', start, end))
|
198
|
+
|
199
|
+
fig2, ax2 = plt.subplots()
|
200
|
+
ax2.set_xlabel('V')
|
201
|
+
ax2.set_ylabel('density')
|
202
|
+
ax2.hist(V_ave, color = V_color, density = True)
|
203
|
+
ax2.title.set_text(figure_t.gen_title('V hist', start, end))
|
204
|
+
|
205
|
+
return fig1, fig2
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
def UV_std(sim, interval = 20, grid = True):
|
211
|
+
'''
|
212
|
+
Plots how standard deviation of U, V change over time.
|
213
|
+
Takes average over many small interval to smooth out local fluctuations.
|
214
|
+
|
215
|
+
Returns:
|
216
|
+
fig: A Matplotlib figure, contains U's and V's std curves.
|
217
|
+
'''
|
218
|
+
|
219
|
+
interval = figure_t.scale_interval(interval, sim.compress_itv)
|
220
|
+
interval_num = int(sim.max_record / interval)
|
221
|
+
|
222
|
+
U_std = []
|
223
|
+
V_std = []
|
224
|
+
|
225
|
+
for i in range(interval_num):
|
226
|
+
U_ave = figure_t.ave_interval(sim.U, i * interval, (i + 1) * interval)
|
227
|
+
V_ave = figure_t.ave_interval(sim.V, i * interval, (i + 1) * interval)
|
228
|
+
|
229
|
+
U_std.append(np.std(U_ave))
|
230
|
+
V_std.append(np.std(V_ave))
|
231
|
+
|
232
|
+
#### plot ####
|
233
|
+
xaxis = np.linspace(0, sim.maxtime, len(U_std))
|
234
|
+
|
235
|
+
fig, ax = plt.subplots()
|
236
|
+
ax.grid(grid)
|
237
|
+
ax.plot(xaxis, U_std, CURVE_TYPE, label = 'U std')
|
238
|
+
ax.plot(xaxis, V_std, CURVE_TYPE, label = 'V std')
|
239
|
+
ax.legend()
|
240
|
+
ax.set_xlabel('time (records)', fontsize = 11)
|
241
|
+
ax.title.set_text('std_dev over time')
|
242
|
+
|
243
|
+
return fig
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
def UV_expected_val(sim):
|
248
|
+
'''
|
249
|
+
Calculate expected U & V distribution based on matrices, assume no migration.
|
250
|
+
To differentiate from UV_expected in figures.py: this one return arrays (values).
|
251
|
+
'''
|
252
|
+
|
253
|
+
U_expected = np.zeros((sim.N, sim.M))
|
254
|
+
V_expected = np.zeros((sim.N, sim.M))
|
255
|
+
|
256
|
+
for i in range(sim.N):
|
257
|
+
for j in range(sim.M):
|
258
|
+
# say matrix = [a, b, c, d]
|
259
|
+
# U_proportion = (d - b) / (a - b - c + d)
|
260
|
+
U_prop = (sim.X[i][j][3] - sim.X[i][j][1]) / (sim.X[i][j][0] - sim.X[i][j][1] - sim.X[i][j][2] + sim.X[i][j][3])
|
261
|
+
# equilibrium payoff, U_payoff = V_payoff
|
262
|
+
eq_payoff = U_prop * sim.X[i][j][0] + (1 - U_prop) * sim.X[i][j][1]
|
263
|
+
|
264
|
+
# payoff / kappa * proportion
|
265
|
+
U_expected[i][j] = eq_payoff / sim.P[i][j][4] * U_prop
|
266
|
+
V_expected[i][j] = eq_payoff / sim.P[i][j][5] * (1 - U_prop)
|
267
|
+
|
268
|
+
return U_expected, V_expected
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
def UV_expected(sim, U_color = 'Purples', V_color = 'Greens', annot = False, fmt = '.3g'):
|
274
|
+
'''
|
275
|
+
Calculate expected population distribution based on matrices, assuming no migration.
|
276
|
+
For the formulas, see stochastic_mode.expected_UV
|
277
|
+
|
278
|
+
Some Inputs:
|
279
|
+
Note the colors are color maps.
|
280
|
+
|
281
|
+
Returns:
|
282
|
+
fig1, fig2: If 2D (N and M both > 1), then fig1 and fig2 are heatmaps.
|
283
|
+
If 1D (N or M == 1), then fig1 and fig2 are barplots.
|
284
|
+
'''
|
285
|
+
|
286
|
+
U_expected, V_expected = UV_expected_val(sim)
|
287
|
+
|
288
|
+
U_text = figure_t.gen_text(np.mean(U_expected), np.std(U_expected))
|
289
|
+
V_text = figure_t.gen_text(np.mean(V_expected), np.std(V_expected))
|
290
|
+
|
291
|
+
#### plot ####
|
292
|
+
|
293
|
+
if (sim.N != 1) and (sim.M != 1):
|
294
|
+
# 2D
|
295
|
+
fig1 = figure_t.heatmap(U_expected, U_color, annot, fmt, title = 'Expected U', text = U_text)
|
296
|
+
fig2 = figure_t.heatmap(V_expected, V_color, annot, fmt, title = 'Expected V', text = V_text)
|
297
|
+
|
298
|
+
else:
|
299
|
+
# 1D
|
300
|
+
fig1 = figure_t.bar(U_expected.flatten(), color = U_color, xlabel = 'patches', ylabel = 'popu', title = 'Expected U', text = U_text)
|
301
|
+
fig2 = figure_t.bar(V_expected.flatten(), color = V_color, xlabel = 'patches', ylabel = 'popu', title = 'Expected V', text = V_text)
|
302
|
+
|
303
|
+
return fig1, fig2
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
def pi_heatmap(sim, U_color = 'BuPu', V_color = 'YlGn', start = 0.95, end = 1.0, annot = False, fmt = '.3g'):
|
309
|
+
'''
|
310
|
+
Make heatmaps for payoff in a specified interval.
|
311
|
+
Works best for 2D. 1D works as well, but figures look bad.
|
312
|
+
|
313
|
+
Some Inputs:.
|
314
|
+
Note the colors are matplotlib color maps.
|
315
|
+
|
316
|
+
Returns:
|
317
|
+
fig1, fig2: Seaborn heatmaps, for U's & V's payoff distribution, respectively.
|
318
|
+
'''
|
319
|
+
|
320
|
+
start_index = int(sim.max_record * start)
|
321
|
+
end_index = int(sim.max_record * end)
|
322
|
+
|
323
|
+
U_pi_ave = figure_t.ave_interval(sim.U_pi, start_index, end_index)
|
324
|
+
V_pi_ave = figure_t.ave_interval(sim.V_pi, start_index, end_index)
|
325
|
+
|
326
|
+
U_title = figure_t.gen_title('U_pi', start, end)
|
327
|
+
U_text = figure_t.gen_text(np.mean(U_pi_ave), np.std(U_pi_ave))
|
328
|
+
V_title = figure_t.gen_title('V_pi', start, end)
|
329
|
+
V_text = figure_t.gen_text(np.mean(V_pi_ave), np.std(V_pi_ave))
|
330
|
+
|
331
|
+
fig1 = figure_t.heatmap(U_pi_ave, U_color, annot, fmt, U_title, U_text)
|
332
|
+
fig2 = figure_t.heatmap(V_pi_ave, V_color, annot, fmt, V_title, V_text)
|
333
|
+
|
334
|
+
return fig1, fig2
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
def pi_bar(sim, U_color = 'violet', V_color = 'yellowgreen', start = 0.95, end = 1.0):
|
340
|
+
'''
|
341
|
+
Make barplot for payoff in a specified interval.
|
342
|
+
Works best for 1D. 2D works as well, but figures look bad.
|
343
|
+
|
344
|
+
Returns:
|
345
|
+
fig1, fig2: Matplotlib barplots, for U's and V's payoff distribution, respectively.
|
346
|
+
'''
|
347
|
+
|
348
|
+
start_index = int(sim.max_record * start)
|
349
|
+
end_index = int(sim.max_record * end)
|
350
|
+
|
351
|
+
U_pi_ave = figure_t.ave_interval_1D(sim.U_pi, start_index, end_index)
|
352
|
+
V_pi_ave = figure_t.ave_interval_1D(sim.V_pi, start_index, end_index)
|
353
|
+
|
354
|
+
U_title = figure_t.gen_title('U_pi', start, end)
|
355
|
+
U_text = figure_t.gen_text(np.mean(U_pi_ave), np.std(U_pi_ave))
|
356
|
+
V_title = figure_t.gen_title('V_pi', start, end)
|
357
|
+
V_text = figure_t.gen_text(np.mean(V_pi_ave), np.std(V_pi_ave))
|
358
|
+
|
359
|
+
fig1 = figure_t.bar(U_pi_ave, U_color, 'patches', 'pi', U_title, U_text)
|
360
|
+
fig2 = figure_t.bar(V_pi_ave, V_color, 'patches', 'pi', V_title, V_text)
|
361
|
+
|
362
|
+
return fig1, fig2
|
363
|
+
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
def pi_dyna(sim, interval = 20, grid = True):
|
368
|
+
'''
|
369
|
+
Plot how payoffs change over time.
|
370
|
+
|
371
|
+
Returns:
|
372
|
+
fig: Matplotlib figure of U's, V's, and U+V payoff, either total or not.
|
373
|
+
'''
|
374
|
+
|
375
|
+
U_curve = []
|
376
|
+
V_curve = []
|
377
|
+
total_curve = []
|
378
|
+
|
379
|
+
interval = figure_t.scale_interval(interval, sim.compress_itv)
|
380
|
+
interval_num = int(sim.max_record / interval)
|
381
|
+
|
382
|
+
for i in range(interval_num):
|
383
|
+
U_ave = figure_t.ave_interval(sim.U_pi, i * interval, (i + 1) * interval)
|
384
|
+
V_ave = figure_t.ave_interval(sim.V_pi, i * interval, (i + 1) * interval)
|
385
|
+
|
386
|
+
U_curve.append(np.sum(U_ave))
|
387
|
+
V_curve.append(np.sum(V_ave))
|
388
|
+
total_curve.append(U_curve[-1] + V_curve[-1])
|
389
|
+
|
390
|
+
#### plot ####
|
391
|
+
xaxis = np.linspace(0, sim.maxtime, len(U_curve))
|
392
|
+
|
393
|
+
fig, ax = plt.subplots()
|
394
|
+
ax.grid(grid)
|
395
|
+
ax.plot(xaxis, U_curve, CURVE_TYPE, label = 'U_pi')
|
396
|
+
ax.plot(xaxis, V_curve, CURVE_TYPE, label = 'V_pi')
|
397
|
+
ax.plot(xaxis, total_curve, CURVE_TYPE, label = 'total')
|
398
|
+
ax.set_xlim(0, sim.maxtime)
|
399
|
+
ax.title.set_text('U&V _pi over time')
|
400
|
+
ax.legend()
|
401
|
+
|
402
|
+
return fig
|
403
|
+
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
def pi_hist(sim, U_color = 'violet', V_color = 'yellowgreen', start = 0.95, end = 1.0):
|
408
|
+
'''
|
409
|
+
Makes deensity histograms of U's and V's payoffs in a sepcified interval.
|
410
|
+
Sometimes it may not be shown in density plots due to matplotlib features.
|
411
|
+
|
412
|
+
Returns:
|
413
|
+
fig1, fig2: histogram of U's and V's payoff.
|
414
|
+
'''
|
415
|
+
|
416
|
+
start_index = int(start * sim.max_record)
|
417
|
+
end_index = int(end * sim.max_record)
|
418
|
+
|
419
|
+
U_pi_ave = figure_t.ave_interval_1D(sim.U_pi, start_index, end_index)
|
420
|
+
V_pi_ave = figure_t.ave_interval_1D(sim.V_pi, start_index, end_index)
|
421
|
+
|
422
|
+
#### plot ####
|
423
|
+
|
424
|
+
fig1, ax1 = plt.subplots()
|
425
|
+
ax1.set_xlabel('U_pi')
|
426
|
+
ax1.set_ylabel('density')
|
427
|
+
ax1.hist(U_pi_ave, color = U_color, density = True)
|
428
|
+
ax1.title.set_text(figure_t.gen_title('U_pi hist', start, end))
|
429
|
+
|
430
|
+
fig2, ax2 = plt.subplots()
|
431
|
+
ax2.set_xlabel('V_pi')
|
432
|
+
ax2.set_ylabel('density')
|
433
|
+
ax2.hist(V_pi_ave, color = V_color, density = True)
|
434
|
+
ax2.title.set_text(figure_t.gen_title('V_pi hist', start, end))
|
435
|
+
|
436
|
+
return fig1, fig2
|
437
|
+
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
def pi_std(sim, interval = 20, grid = True):
|
442
|
+
'''
|
443
|
+
Plots how standard deviation of payoff change over time.
|
444
|
+
|
445
|
+
Returns:
|
446
|
+
fig: Matplotlib figure of the std of payoffs.
|
447
|
+
'''
|
448
|
+
|
449
|
+
|
450
|
+
interval = figure_t.scale_interval(interval, sim.compress_itv)
|
451
|
+
interval_num = int(sim.max_record / interval)
|
452
|
+
|
453
|
+
U_pi_std = []
|
454
|
+
V_pi_std = []
|
455
|
+
|
456
|
+
for i in range(interval_num):
|
457
|
+
U_pi_ave = figure_t.ave_interval(sim.U_pi, i * interval, (i + 1) * interval)
|
458
|
+
V_pi_ave = figure_t.ave_interval(sim.V_pi, i * interval, (i + 1) * interval)
|
459
|
+
|
460
|
+
U_pi_std.append(np.std(U_pi_ave))
|
461
|
+
V_pi_std.append(np.std(V_pi_ave))
|
462
|
+
|
463
|
+
#### plot ####
|
464
|
+
xaxis = np.linspace(0, sim.maxtime, len(U_pi_std))
|
465
|
+
|
466
|
+
fig, ax = plt.subplots()
|
467
|
+
ax.grid(grid)
|
468
|
+
ax.plot(xaxis, U_pi_std, CURVE_TYPE, label = 'U_pi std')
|
469
|
+
ax.plot(xaxis, V_pi_std, CURVE_TYPE, label = 'V_pi std')
|
470
|
+
ax.legend()
|
471
|
+
ax.set_xlabel('time (records)', fontsize = 11)
|
472
|
+
ax.title.set_text('std over time')
|
473
|
+
|
474
|
+
return fig
|
475
|
+
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
def UV_pi(sim, U_color = 'violet', V_color = 'yellowgreen', alpha = 0.25, start = 0.95, end = 1.0):
|
480
|
+
'''
|
481
|
+
Make two scatter plots: x-axes are population and y-axes are payoff in a specified time interval.
|
482
|
+
Reveals relationship between population and payoff.
|
483
|
+
|
484
|
+
Returns:
|
485
|
+
fig1, fig2: U's and V's population-payoff scatter plots.
|
486
|
+
'''
|
487
|
+
|
488
|
+
start_index = int(start * sim.max_record)
|
489
|
+
end_index = int(end * sim.max_record)
|
490
|
+
|
491
|
+
U_ave = figure_t.ave_interval_1D(sim.U, start_index, end_index)
|
492
|
+
V_ave = figure_t.ave_interval_1D(sim.V, start_index, end_index)
|
493
|
+
|
494
|
+
U_pi_ave = figure_t.ave_interval(sim.U_pi, start_index, end_index)
|
495
|
+
V_pi_ave = figure_t.ave_interval(sim.V_pi, start_index, end_index)
|
496
|
+
|
497
|
+
|
498
|
+
fig1 = figure_t.scatter(U_ave, U_pi_ave, U_color, alpha, xlabel = 'U', ylabel = 'U_pi', title = 'U - U_pi')
|
499
|
+
fig2 = figure_t.scatter(V_ave, V_pi_ave, V_color, alpha, xlabel = 'V', ylabel = 'V_pi', title = 'V - V_pi')
|
500
|
+
|
501
|
+
return fig1, fig2
|
502
|
+
|
503
|
+
|