Source files¶
Subpackages¶
Submodules¶
nashpy.game module¶
A class for a normal form game
- class nashpy.game.Game(*args: Any)[source]¶
Bases:
object
A class for a normal form game.
- Parameters
A (-) – non zero sum games.
B (2 dimensional list/arrays representing the payoff matrices for) – non zero sum games.
A – zero sum game.
- asymmetric_replicator_dynamics(x0=None, y0=None, timepoints=None)[source]¶
Returns two arrays, corresponding to the two players, showing the probability of each strategy being played over time using the asymmetric replicator dynamics algorithm.
- Parameters
x0 (array) – The initial population distribution of the row player.
y0 (array) – The initial population distribution of the column player.
timepoints (array) – The iterable of timepoints.
- Returns
The 2 population distributions over time.
- Return type
Tuple
- fictitious_play(iterations, play_counts=None)[source]¶
Return a given sequence of actions through fictitious play. The implementation corresponds to the description of chapter 2 of [Fudenberg1998].
1. Players have a belief of the strategy of the other player: a vector representing the number of times the player has chosen a given strategy. 2. Players choose a best response to the belief. 3. Players update their belief based on the latest choice of the opponent.
- Parameters
iterations (int) – The number of iterations of the algorithm.
play_counts (array) – The play counts.
- Returns
The play counts
- Return type
Generator
- fixation_probabilities(initial_population, repetitions)[source]¶
Return the fixation probabilities for all types of individuals.
The returned array will have the same dimension as the number of rows or columns as the payoff matrix A. The ith element of the returned array corresponds to the probability that the ith strategy becomes fixed given the initial population.
This is a stochastic algorithm and the probabilities are estimated over a number of repetitions.
- Parameters
initial_population (array) – the initial population
repetitions (int) – The number of iterations of the algorithm.
- Returns
The fixation probability of each type.
- Return type
array
- is_best_response(sigma_r, sigma_c)[source]¶
Checks if sigma_r is a best response to sigma_c and vice versa.
- Parameters
sigma_r (array) – The row player strategy
sigma_c (array) – The column player strategy
- Returns
A pair of booleans, the first indicates if sigma_r is a best response to sigma_c. The second indicates if sigma_c is a best response to sigma_r.
- Return type
tuple
- lemke_howson(initial_dropped_label)[source]¶
Obtain the Nash equilibria using the Lemke Howson algorithm implemented using integer pivoting.
Algorithm implemented here is Algorithm 3.6 of [Nisan2007].
Start at the artificial equilibrium (which is fully labeled)
Choose an initial label to drop and move in the polytope for which the vertex has that label to the edge that does not share that label. (This is implemented using integer pivoting)
A label will now be duplicated in the other polytope, drop it in a similar way.
Repeat steps 2 and 3 until have Nash Equilibrium.
- Parameters
initial_dropped_label (int) – The initial dropped label.
- Returns
An equilibria
- Return type
Tuple
- lemke_howson_enumeration()[source]¶
Obtain Nash equilibria for all possible starting dropped labels using the lemke howson algorithm. See Game.lemke_howson for more information.
Note: this is not guaranteed to find all equilibria.
- Yields
Tuple – An equilibria
- moran_process(initial_population, mutation_probability=0)[source]¶
Return a generator of population across the Moran process. The last population is when only a single type of individual is present in the population.
- Parameters
initial_population (array) – the initial population
mutation_probability (float) – the probability of an individual selected to be copied mutates to another individual from the original set of strategies (even if they are no longer present in the population).
- Returns
The generations.
- Return type
Generator
- replicator_dynamics(y0=None, timepoints=None, mutation_matrix=None)[source]¶
Implement replicator dynamics Return an array showing probability of each strategy being played over time. The total population is constant. Strategies can either stay constant if equilibria is achieved, replicate or die.
- Parameters
y0 (array) – The initial population distribution.
timepoints (array) – The iterable of timepoints.
mutation_matrix (array) – The mutation rate matrix. Element [i, j] gives the probability of an individual of type i mutating to an individual of type j. Default behaviour is to be the identity matrix which corresponds to no mutation.
- Returns
The population distributions over time.
- Return type
array
- stochastic_fictitious_play(iterations, play_counts=None, etha=0.1, epsilon_bar=0.01)[source]¶
Return a given sequence of actions and mixed strategies through stochastic fictitious play. The implementation corresponds to the description given in [Hofbauer2002].
- Parameters
iterations (int) – The number of iterations of the algorithm.
play_counts (array) – The play counts.
etha (float) – The noise parameter for the logit choice function.
epsilon_bar (float) – The maximum stochastic perturbation.
- Returns
The play counts
- Return type
Generator
- support_enumeration(non_degenerate=False, tol=1e-16)[source]¶
Obtain the Nash equilibria using support enumeration.
Algorithm implemented here is Algorithm 3.4 of [Nisan2007].
For each k in 1…min(size of strategy sets)
For each I,J supports of size k
Solve indifference conditions
Check that have Nash Equilibrium.
- Parameters
non_degenerate (bool) – Whether or not to consider supports of equal size. By default (False) only considers supports of equal size.
tol (float) – A tolerance parameter for equality.
- Returns
The equilibria.
- Return type
generator
- vertex_enumeration()[source]¶
Obtain the Nash equilibria using enumeration of the vertices of the best response polytopes.
Algorithm implemented here is Algorithm 3.5 of [Nisan2007].
Build best responses polytopes of both players
For each vertex pair of both polytopes
Check if pair is fully labelled
Return the normalised pair
- Returns
The equilibria.
- Return type
generator
Module contents¶
A library with algorithms on 2 player games.