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

Testing if the population has converged...


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)


10000 loops, best of 3: 21.9 us per loop

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]:
2

testing simulate discrete distribution in utils module


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)


100000 loops, best of 3: 14.4 us per loop
100000 loops, best of 3: 4.05 us per loop

Snippets


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)


100000 loops, best of 3: 17 us per loop

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)


100000 loops, best of 3: 19.4 us per loop

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]:
2

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)


100000 loops, best of 3: 16.8 us per loop

In [30]:
%timeit utils.simulate_discrete_distribution(a)


100000 loops, best of 3: 10.5 us per loop

In [2]:
np.array


Out[2]:
<function numpy.core.multiarray.array>

Tests for java compliance


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]:
array([ 2.        ,  2.31666667,  1.11666667])

In [4]:
games.allc_tft_alld()


Out[4]:
array([[ 3.  ,  3.  ,  0.  ],
       [ 3.  ,  3.  ,  0.95],
       [ 4.  ,  1.15,  1.  ]])

Test for replicator


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]:
array([  3. ,   7.5,  12. ])

In [9]:
np.dot(x,np.dot(A,x))


Out[9]:
11.25

In [10]:
1.5+7.5/2.0+6


Out[10]:
11.25

In [7]:
np.array([2,2,2])*np.array([1,2,3])


Out[7]:
array([2, 4, 6])

In [13]:
(np.dot(A,x)-np.dot(x,np.dot(A,x)))*x


Out[13]:
array([-4.125, -1.875,  0.375])

In [12]:
3-11.25


Out[12]:
-8.25

In [ ]: