Nashpy Documentation

repository·main·Indexed 18 days ago

https://github.com/drvinceknight/nashpy

A Python library for analyzing 2-player games using game theoretic algorithms. Nashpy provides tools for finding Nash equilibria via support enumeration, vertex enumeration, and the Lemke-Howson algorithm, as well as simulating evolutionary dynamics such as replicator dynamics, fictitious play, and Moran processes. It supports both zero-sum and non-zero-sum (bi-matrix) games and is designed for ease of installation using the standard scientific Python stack (NumPy and SciPy).

Tokens
37.3K
Snippets
101
Records
155
Agent score
62%

What's inside Nashpy

  1. Explore Nashpy subpackages and modules

    main

    Nashpy is organized into several functional areas for game theory analysis:

    • nashpy.algorithms: Contains algorithms for finding Nash equilibria.
    • nashpy.learning: Contains implementations for learning algorithms in games.
    • nashpy.game: The core module for defining and interacting with game structures.
    • nashpy: The top-level package providing the primary entry points for the library.
  2. Available algorithms in the nash.algorithms package

    main

    The nash.algorithms package provides different algorithmic approaches for finding Nash equilibria in 2-player games. Depending on the game structure and size, you can choose from the following submodules:

    • nashpy.algorithms.support_enumeration: Used for finding equilibria by enumerating supports.
    • nashpy.algorithms.vertex_enumeration: Used for finding equilibria via vertex enumeration.
    • nashpy.algorithms.lemke_howson: Implements the Lemke-Howson algorithm, a pivoting algorithm for finding Nash equilibria in non-degenerate games.
  3. What is a Moran process?

    main

    A Moran process is a stochastic model used to describe evolutionary dynamics in a finite and constant population of $N$ individuals. Unlike replicator dynamics, which often assume infinite populations, the Moran process accounts for the discrete nature of individuals.

    At each step of the process:

    1. Fitness Calculation: Every individual's fitness is calculated based on their interactions with others in the population.
    2. Selection for Copying: An individual is chosen to be copied with a probability proportional to their fitness: $\frac{f_k(v)}{\sum f_h(v)}$.
    3. Selection for Removal: An individual is chosen to be removed uniformly at random with probability $1/N$.
    4. Replacement: The individual selected for copying is introduced, and the individual selected for removal is removed, maintaining a constant population size $N$.

    The process continues until the population reaches fixation (only one type of individual remains).

  4. What is a strategy in a normal form game?

    main

    In game theory, a strategy is a probability distribution over the set of available actions $\mathcal{A}$.

    For a player with an action set $\mathcal{A}$, a strategy $\sigma$ is represented as a vector in $[0, 1]^{|\mathcal{A}|}_{\mathbb{R}}$ such that the sum of all probabilities equals 1:

    $$\sum_{i=1}^{\mathcal{A}}\sigma_i = 1$$

    Example: Rock Paper Scissors In a Rock Paper Scissors game where actions are {Rock, Paper, Scissors}:

    • A strategy of always choosing Paper is $\sigma = (0, 1, 0)$.
    • A strategy of randomly choosing between Rock and Paper with equal probability is $\sigma = (1/2, 1/2, 0)$.
  5. What is the support of a strategy?

    main

    The support of a strategy $\sigma$, denoted as $\mathcal{S}(\sigma)$, is the set of all actions $i \in \mathcal{A}$ for which the probability of choosing that action is greater than zero ($\sigma_i > 0$).

    Examples:

    • If $\sigma = (1, 0, 0)$, the support is $\mathcal{S}(\sigma) = {1}$.
    • If $\sigma = (1/3, 1/3, 1/3)$, the support is $\mathcal{S}(\sigma) = {1, 2, 3}$.
    • If $\sigma = (2/5, 0, 3/5)$, the support is $\mathcal{S}(\sigma) = {1, 3}$.
  6. Understand the General Form of the Prisoners Dilemma

    main

    A game is considered a Prisoners Dilemma if its payoff matrices $A$ (for the row player) and $B$ (for the column player) follow this structure:

    $A = \begin{pmatrix} R & S \ T & P \end{pmatrix}, \quad B = \begin{pmatrix} R & T \ S & P \end{pmatrix}$

    Subject to these constraints:

    1. $T > R > P > S$ (Ensures 'Defect' dominates 'Cooperate')
    2. $2R > T + S$ (Ensures a social dilemma where mutual cooperation is collectively optimal)

    Where:

    • $R$: Reward for mutual cooperation
    • $S$: Sucker's payoff
    • $T$: Temptation to defect
    • $P$: Punishment for mutual defection
  7. How the Regret Minimization algorithm works

    main

    The Regret Minimization algorithm follows an iterative process to help players learn optimal strategies:

    1. Initialize: Set starting strategies for all players.
    2. Iterate: At each time step $t$:
      • Calculate Regret: Determine the regret for each player based on their current strategy compared to alternative strategies.
      • Update Strategies: Select the strategy that minimizes regret for each player.
    3. Repeat: Continue the process until a convergence criterion or a predefined stopping condition is met.
  8. How the vertex enumeration algorithm works in Nashpy

    main

    Nashpy implements a vertex enumeration algorithm (based on Nisan 2007) to find all Nash equilibria for a nondegenerate 2-player game.

    The Process

    1. Construct Best Response Polytopes: The algorithm first defines best response polytopes $P$ and $Q$ for the row and column players. These are derived from best response polyhedra by assuming a utility upper bound of 1 ($u=v=1$) to allow for scaling.
    2. Vertex Enumeration: The algorithm identifies the vertices of these polytopes. These vertices represent the intersections of the underlying halfspaces.
    3. Label Checking: The algorithm iterates through all pairs of vertices from $P$ and $Q$. A pair is considered a Nash equilibrium if the pair is "fully labeled" (meaning the labels cover the full set of integers required to satisfy the best response conditions).
    4. Normalization: Because the polytopes are scaled, the resulting probability vectors are normalized before being returned as the Nash equilibria.
  9. Understand Zero-Sum Games and Minimax Strategies

    main

    In a zero-sum game, one player's gain is exactly balanced by the other player's loss. For a game defined by a payoff matrix $A \in \mathbb{R}^{m \times n}$:

    • Max-min strategy ($x$): The row player seeks to maximize their minimum possible payoff. This is solved via a linear program to find $x$ such that $xA \geq \mathbb{1}u$, where $u$ is the max-min value.
    • Min-max strategy ($y$): The column player seeks to minimize the row player's maximum possible payoff. This is solved via a linear program to find $y$ such that $Ay^T \leq \mathbb{1}v$, where $v$ is the min-max value.

    The Minimax Theorem: For zero-sum games, the max-min value $u$ and the min-max value $v$ are equal ($u = v$). This ensures that both players can achieve the game's value through their respective optimal strategies.

  10. Available game theoretic algorithms in Nashpy

    main

    Nashpy provides several algorithms for solving or simulating games. While the README provides high-level links, the following features are implemented:

    • Equilibrium Solvers:
      • Support enumeration
      • Vertex enumeration
      • Lemke-Howson algorithm
    • Dynamics and Simulations:
      • Fictitious play
      • Stochastic fictitious play
      • Replicator dynamics
      • Replicator-mutation dynamics
      • Asymmetric replicator dynamics
      • Moran processes (including on interaction and replication graphs)
      • Introspection dynamics
    • Game Generation:
      • Generating games from repeated games
  11. Understand the concept of Regret Minimization

    main

    In game theory, Regret is the difference between a player's actual payoff and the payoff they would have received by playing a different strategy.

    Regret Minimization is a process where players iteratively update their strategies to minimize this difference. By minimizing regret over time, players adapt their strategies, which can lead to convergence towards a Nash Equilibrium (a state where no player has an incentive to deviate from their strategy unilaterally).