In [1]:
%reset
import numpy as np
import sys
sys.path.insert(0,'../')
import pyevodyn.utils as utils
import pyevodyn.games as games
import pyevodyn.numerical as numerical
from pyevodyn.simulation import MoranProcess
In [2]:
def convergence_test(array, size):
return np.any([i==size for i in array])
In [3]:
a= [2,2,2]
%timeit convergence_test(a,3)
In [4]:
def convergence_test_with_index(array, size):
if np.any([i==size for i in array]):
return array.argmax()
return None
In [5]:
a = np.array([0,0,3])
convergence_test_with_index(a,3)
Out[5]:
In [3]:
def weighted_values(probabilities):
values = xrange(0, len(probabilities))
bins = np.add.accumulate(probabilities)
return values[np.digitize([np.random.rand()], bins)]
probabilities = np.array([0.5, 0.5, 0,0,0,0])
%timeit weighted_values(probabilities)
%timeit utils.simulate_discrete_distribution(probabilities)
In [8]:
def funcioncita(population_array, population_size):
current_distribution = np.array(population_array, dtype=float)
current_distribution /= float(population_size)
return current_distribution
In [15]:
%timeit funcioncita(np.array([1,2,3]), 6)
In [16]:
def funcioncita_fast(population_array, population_size):
current_distribution = population_array.copy()
current_distribution /= float(population_size)
return current_distribution
In [17]:
%timeit funcioncita_fast(np.array([1,2,3]), 6)
In [20]:
def weighted_pick(weights):
"""
Weighted random selection
returns n_picks random indexes.
the chance to pick the index i
is give by the weight weights[i].
"""
t = np.cumsum(weights)
s = np.sum(weights)
return np.searchsorted(t,np.random.rand(1)*s)[0]
In [25]:
weighted_pick([0.1, 0.2, 0.5, 0.5, 1.0, 1.1, 2.0])
Out[25]:
In [26]:
a = [0.1, 0.2, 0.5, 0.5, 1.0, 1.1, 2.0]
a/=np.sum(a)
In [29]:
%timeit weighted_pick(a)
In [30]:
%timeit utils.simulate_discrete_distribution(a)
In [2]:
np.array
Out[2]:
In [2]:
mp = MoranProcess(population_size=10, intensity_of_selection=0.0, game_matrix=games.allc_tft_alld() , number_of_strategies=3, fitness_mapping='exp', mutation_probability=0.1)
In [3]:
mp.payoff_function(population_array=[0,7,3])
Out[3]:
In [4]:
games.allc_tft_alld()
Out[4]:
In [1]:
A = np.array([[1,2,3], [4,5,6], [7,8,9]])
In [3]:
x = np.array([0.5,0.5,0.5])
In [8]:
np.dot(A,x)
Out[8]:
In [9]:
np.dot(x,np.dot(A,x))
Out[9]:
In [10]:
1.5+7.5/2.0+6
Out[10]:
In [7]:
np.array([2,2,2])*np.array([1,2,3])
Out[7]:
In [13]:
(np.dot(A,x)-np.dot(x,np.dot(A,x)))*x
Out[13]:
In [12]:
3-11.25
Out[12]:
In [ ]: