Table of Contents


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()


[50, 33, 25, 20]
<matplotlib.figure.Figure at 0x13b288908>

In [673]:
shares_x_i[55]


Out[673]:
0.50458715596330284

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()


<matplotlib.figure.Figure at 0x13aecd588>

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]:
[<matplotlib.lines.Line2D at 0x13ce8fd30>]

In [604]:
shares_x_i[20:-2][::2]


Out[604]:
array([ 0.18348624,  0.20183486,  0.22018349,  0.23853211,  0.25688073,
        0.27522936,  0.29357798,  0.31192661,  0.33027523,  0.34862385,
        0.36697248,  0.3853211 ,  0.40366972,  0.42201835,  0.44036697,
        0.4587156 ,  0.47706422,  0.49541284,  0.51376147,  0.53211009,
        0.55045872,  0.56880734,  0.58715596,  0.60550459,  0.62385321,
        0.64220183,  0.66055046,  0.67889908,  0.69724771,  0.71559633,
        0.73394495,  0.75229358,  0.7706422 ,  0.78899083,  0.80733945,
        0.82568807,  0.8440367 ,  0.86238532,  0.88073394,  0.89908257,
        0.91743119,  0.93577982,  0.95412844,  0.97247706])

In [391]:
[shares_x_i[10:31][::2]-.00]


Out[391]:
[array([ 0.09174312,  0.11009174,  0.12844037,  0.14678899,  0.16513761,
         0.18348624,  0.20183486,  0.22018349,  0.23853211,  0.25688073,
         0.27522936])]

In [628]:
help(add_arrow)


Help on function add_arrow in module __main__:

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.


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()