In [4]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
In [710]:
import itertools
import numpy as np
import matplotlib.pyplot as plt
# Figure 2: illustration of the transition function
def prob_func(shares):
if sum(shares) != 1.0:
if 1.0 - sum(shares) < 0.000001:
shares[0] = 1-sum(shares[1:])
else:
raise AssertionError("Shares do not sum to unity but to {}".format(sum(shares)))
probs = []
for x_i in shares:
p_x_i = (12*x_i**2 - 5*x_i**3) / sum([12*x_j**2 - 5*x_j**3 for x_j in shares])
probs.append(p_x_i)
if sum(probs) != 1.0:
if 1.0 - sum(probs) < 0.000001:
probs[0] = 1-sum(probs[1:])
else:
raise AssertionError("Probabilities do not sum to unity but to {}".format(sum(probs)))
return probs
shares_x_i_print = np.linspace(0, 1, num=11)
shares_x_i = np.linspace(0, 1, num=110)
probs = []
probs_x_i_2 = [prob_func([x_i, 1-x_i])[0] for x_i in shares_x_i]
probs.append(probs_x_i_2)
probs_x_i_3 = [prob_func([x_i] + 2*[(1-x_i)/2])[0] for x_i in shares_x_i]
probs.append(probs_x_i_3)
probs_x_i_4 = [prob_func([x_i] + 3*[(1-x_i)/3])[0] for x_i in shares_x_i]
probs.append(probs_x_i_4)
probs_x_i_5 = [prob_func([x_i] + 4*[(1-x_i)/4])[0] for x_i in shares_x_i]
probs.append(probs_x_i_5)
locs = list(itertools.product(range(2), range(2)))
nbs_big = ["Two", "Three", "Four", "Five"]
nbs_small = ["two", "three", "four", "five"]
fixed_point = [int(x * 100) for x in [1/2, 1/3, 1/4, 1/5]]
plt.clf()
fig, ax = plt.subplots(2,2, figsize=(12, 9))
for i in range(len(locs)):
probs[i] = np.asarray(probs[i])
z = ax[locs[i]].plot(shares_x_i, probs[i],
color="#0080FF", alpha=0.5)
x = ax[locs[i]].plot(shares_x_i[1:fixed_point[i]+4],
probs[i][1:fixed_point[i]+4],
color="#0080FF", linestyle="None")[0]
y = ax[locs[i]].plot(shares_x_i[fixed_point[i]: ][::2],
probs[i][fixed_point[i]:][::2],
color="#0080FF", linestyle="None")[0]
ax[locs[i]].plot(fixed_point[i]/100, fixed_point[i]/100,
marker='o', markersize=8, color="#0080FF")
if i <= 1:
for j in shares_x_i[2+1:fixed_point[i]][::2]:
add_arrow(x, j,direction="left", size=20)
for j in shares_x_i[8+fixed_point[i]:-2][::2]:
add_arrow(y, j, direction="right", size=20)
else:
for j in shares_x_i[2+1:fixed_point[i]][::2]:
add_arrow(x, j,direction="left", size=20)
for j in shares_x_i[4+fixed_point[i]:-2][::2]:
add_arrow(y, j, direction="right", size=20)
ax[locs[i]].set_title(nbs_big[i] + " research programs", fontsize=10)
ax[locs[i]].plot(shares_x_i, shares_x_i, linestyle="--", color="silver")
ax[locs[i]].set_xlabel("x_i")
ax[locs[i]].set_ylabel("p(x_i)")
ax[locs[i]].set_xticks(shares_x_i_print)
ax[locs[i]].spines["top"].set_visible(False)
ax[locs[i]].spines["right"].set_visible(False)
ax[locs[i]].get_xaxis().tick_bottom()
ax[locs[i]].get_yaxis().tick_left()
plt.tight_layout()
In [673]:
shares_x_i[55]
Out[673]:
In [649]:
import itertools
import numpy as np
import matplotlib.pyplot as plt
# Figure 2: illustration of the transition function
def prob_func(shares):
if sum(shares) != 1.0:
if 1.0 - sum(shares) < 0.000001:
shares[0] = 1-sum(shares[1:])
else:
raise AssertionError("Shares do not sum to unity but to {}".format(sum(shares)))
probs = []
for x_i in shares:
p_x_i = (12*x_i**2 - 5*x_i**3) / sum([12*x_j**2 - 5*x_j**3 for x_j in shares])
probs.append(p_x_i)
if sum(probs) != 1.0:
if 1.0 - sum(probs) < 0.000001:
probs[0] = 1-sum(probs[1:])
else:
raise AssertionError("Probabilities do not sum to unity but to {}".format(sum(probs)))
return probs
shares_x_i_print = np.linspace(0, 1, num=11)
shares_x_i = np.linspace(0, 1, num=110)
probs = []
probs_x_i_2 = [prob_func([x_i, 1-x_i])[0] for x_i in shares_x_i]
probs.append(probs_x_i_2)
probs_x_i_3 = [prob_func([x_i] + 2*[(1-x_i)/2])[0] for x_i in shares_x_i]
probs.append(probs_x_i_3)
probs_x_i_4 = [prob_func([x_i] + 3*[(1-x_i)/3])[0] for x_i in shares_x_i]
probs.append(probs_x_i_4)
probs_x_i_5 = [prob_func([x_i] + 4*[(1-x_i)/4])[0] for x_i in shares_x_i]
probs.append(probs_x_i_5)
locs = list(itertools.product(range(2), range(2)))
nbs_big = ["Two", "Three", "Four", "Five"]
nbs_small = ["two", "three", "four", "five"]
fixed_point = [int(x * 100) for x in [1/2, 1/3, 1/4, 1/5]]
plt.clf()
fig, ax = plt.subplots(2,2, figsize=(12, 9))
for i in range(len(locs)):
probs[i] = np.asarray(probs[i])
z = ax[locs[0]].plot(shares_x_i, probs[0], color=plt.cm.viridis(2), alpha=0.5)
x = ax[locs[0]].plot(shares_x_i[1:fixed_point[0]][::2], probs[0][1:fixed_point[0]][::2],
color=plt.cm.viridis(1), linestyle="None")
y = ax[locs[0]].plot(shares_x_i[fixed_point[0]: ][::2], probs[0][fixed_point[0]:][::2],
color=plt.cm.viridis(1), linestyle="None")
for i in shares_x_i[2+1:fixed_point[0]]:
add_arrow(x[0], i, direction="left", size=20)
for i in shares_x_i[fixed_point[0]:-2][::2]:
add_arrow(y[0], i, direction="right", size=20)
plt.tight_layout()
In [ ]:
z = plt.plot(shares_x_i, probs[1], color=plt.cm.viridis(1), alpha=0.5, loc)[0]
x = plt.plot(shares_x_i[1:fixed_point[0]][::2], probs[1][1:fixed_point[0]][::2],
color=plt.cm.viridis(1), linestyle="None",)[0]
y = plt.plot(shares_x_i[fixed_point[0]: ][::2], probs[1][fixed_point[0]:][::2],
color=plt.cm.viridis(1), linestyle="None",)[0]
for i in shares_x_i[2+1:fixed_point[0]][::2]:
add_arrow(x, i, direction="left", size=20)
for i in shares_x_i[fixed_point[0]:-2][::2]:
add_arrow(y, i, direction="right", size=20)
In [616]:
fig, axes = plt.subplots(figsize=(12, 9))
probs[1] = np.asarray(probs[1])
fixed_point = [int(x * 100) for x in [1/2, 1/3, 1/4, 1/5]]
z = plt.plot(shares_x_i, probs[1], color=plt.cm.viridis(1), alpha=0.5)[0]
x = plt.plot(shares_x_i[1:fixed_point[0]][::2], probs[1][1:fixed_point[0]][::2],
color=plt.cm.viridis(1), linestyle="None",)[0]
y = plt.plot(shares_x_i[fixed_point[0]: ][::2], probs[1][fixed_point[0]:][::2],
color=plt.cm.viridis(1), linestyle="None",)[0]
for i in shares_x_i[2+1:fixed_point[0]][::2]:
add_arrow(x, i, direction="left", size=20)
for i in shares_x_i[fixed_point[0]:-2][::2]:
add_arrow(y, i, direction="right", size=20)
axes.plot(shares_x_i, shares_x_i, linestyle="--", color="silver")
Out[616]:
In [604]:
shares_x_i[20:-2][::2]
Out[604]:
In [391]:
[shares_x_i[10:31][::2]-.00]
Out[391]:
In [628]:
help(add_arrow)
In [554]:
def add_arrow(line, position=None, direction='right', size=15, color=None):
"""
add an arrow to a line.
line: Line2D object
position: x-position of the arrow. If None, mean of xdata is taken
direction: 'left' or 'right'
size: size of the arrow in fontsize points
color: if None, line color is taken.
"""
if color is None:
color = line.get_color()
xdata = line.get_xdata()
ydata = line.get_ydata()
if position is None:
position = xdata.mean()
# find closest index
start_ind = np.argmin(np.absolute(xdata - position))
if direction == 'right':
end_ind = start_ind + 1
else:
end_ind = start_ind - 1
line.axes.annotate('',
xytext=(xdata[start_ind], ydata[start_ind]),
xy=(xdata[end_ind], ydata[end_ind]),
arrowprops=dict(arrowstyle="->", color=color),
size=size
)
t = np.linspace(-2, 2, 100)
y = np.sin(t)
# return the handle of the line
line = plt.plot(t, y)[0]
add_arrow(line)
add_arrow(line,0.5)
add_arrow(line, 1)
add_arrow(line,1.5)
plt.show()
In [ ]:
import itertools
import numpy as np
import matplotlib.pyplot as plt
# Figure 2: illustration of the transition function
def prob_func(shares):
if sum(shares) != 1.0:
if 1.0 - sum(shares) < 0.000001:
shares[0] = 1-sum(shares[1:])
else:
raise AssertionError("Shares do not sum to unity but to {}".format(sum(shares)))
probs = []
for x_i in shares:
p_x_i = (12*x_i**2 - 5*x_i**3) / sum([12*x_j**2 - 5*x_j**3 for x_j in shares])
probs.append(p_x_i)
if sum(probs) != 1.0:
if 1.0 - sum(probs) < 0.000001:
probs[0] = 1-sum(probs[1:])
else:
raise AssertionError("Probabilities do not sum to unity but to {}".format(sum(probs)))
return probs
shares_x_i_print = np.linspace(0, 1, num=11)
shares_x_i = np.linspace(0, 1, num=110)
probs = []
probs_x_i_2 = [prob_func([x_i, 1-x_i])[0] for x_i in shares_x_i]
probs.append(probs_x_i_2)
probs_x_i_3 = [prob_func([x_i] + 2*[(1-x_i)/2])[0] for x_i in shares_x_i]
probs.append(probs_x_i_3)
probs_x_i_4 = [prob_func([x_i] + 3*[(1-x_i)/3])[0] for x_i in shares_x_i]
probs.append(probs_x_i_4)
probs_x_i_5 = [prob_func([x_i] + 4*[(1-x_i)/4])[0] for x_i in shares_x_i]
probs.append(probs_x_i_5)
locs = list(itertools.product(range(2), range(2)))
nbs_big = ["Two", "Three", "Four", "Five"]
nbs_small = ["two", "three", "four", "five"]
plt.clf()
fig, ax = plt.subplots(2,2, figsize=(12, 9))
for i in range(len(locs)):
probs[i] = np.asarray(probs[i])
ax[locs[i]].plot(shares_x_i, probs[i])
ax[locs[i]].set_title(nbs_big[i] + " research programs", fontsize=10)
ax[locs[i]].plot(shares_x_i, shares_x_i, linestyle="--", color="silver")
ax[locs[i]].set_xlabel("x_i")
ax[locs[i]].set_ylabel("p(x_i)")
ax[locs[i]].set_xticks(shares_x_i_print)
ax[locs[i]].spines["top"].set_visible(False)
ax[locs[i]].spines["right"].set_visible(False)
ax[locs[i]].get_xaxis().tick_bottom()
ax[locs[i]].get_yaxis().tick_left()
plt.tight_layout()