{ "cells": [ { "cell_type": "markdown", "id": "98eb65d8", "metadata": {}, "source": [ "# 3) Stripped-down poker\n", "\n", "In this tutorial, we'll create an extensive-form representation of a one-card poker game from [Reiley et al (2008)](#references), a classroom game under the name \"stripped-down poker\".\n", "This is perhaps the simplest interesting game with imperfect information.\n", "\n", "We'll use \"stripped-down poker\" to demonstrate and explain the following with Gambit:\n", "\n", "1. Setting up an extensive-form game with imperfect information using [information sets](#information-sets)\n", "2. [Computing and interpreting Nash equilibria](#computing-and-interpreting-nash-equilibria) and understanding mixed behaviour and mixed strategy profiles\n", "3. [Acceptance criteria for Nash equilibria](#acceptance-criteria-for-nash-equilibria)\n", "\n", "In our version of the game, there are two players, **Alice** and **Bob**, and a deck of cards, with equal numbers of **King** and **Queen** cards.\n", "\n", "- The game begins with each player putting \\$1 in the pot.\n", " - A card is dealt at random to Alice.\n", " - Alice observes her card.\n", " - Bob does not observe the card.\n", "- Alice then chooses either to **Bet** or to **Fold**.\n", " - If she chooses to Fold, Bob wins the pot and the game ends.\n", " - If she chooses to Bet, she adds another \\$1 to the pot.\n", "- Bob then chooses either to **Call** or **Fold**.\n", " - If he chooses to Fold, Alice wins the pot and the game ends.\n", " - If he chooses to Call, he adds another $1 to the pot.\n", "- There is then a showdown, in which Alice reveals her card.\n", " - If she has a King, then she wins the pot.\n", " - If she has a Queen, then Bob wins the pot.\n", "\n", "In addition to `pygambit`, this tutorial introduces the `gtdraw` package, which can be used to draw extensive form games in Python.\n", "`gtdraw` is optional here; if it isn't available, the drawing cells below print a short note.\n", "To install `gtdraw`, see [gtdraw](https://www.gambit-project.org/gtdraw/).\n", "Another option for visualising extensive form games is to install the Gambit GUI and use it to load a saved EFG file." ] }, { "cell_type": "code", "execution_count": null, "id": "69cbfe81", "metadata": {}, "outputs": [], "source": [ "try:\n", " from gtdraw import draw\n", "except ImportError:\n", " def draw(*args, **kwargs):\n", " print(\n", " \"gtdraw is not installed, so the game tree cannot be drawn here; \"\n", " \"see https://www.gambit-project.org/gtdraw/ to install it.\"\n", " )\n", "\n", "import pygambit as gbt" ] }, { "cell_type": "markdown", "id": "70819881", "metadata": {}, "source": [ "Create the game with two players:" ] }, { "cell_type": "code", "execution_count": null, "id": "ad6a1119", "metadata": {}, "outputs": [], "source": [ "g = gbt.Game.new_tree(\n", " players=[\"Alice\", \"Bob\"],\n", " title=\"Stripped-Down Poker: a simple game of one-card poker from Reiley et al (2008).\"\n", ")" ] }, { "cell_type": "markdown", "id": "d9796238", "metadata": {}, "source": "In addition to the two named players, Gambit also instantiates a chance player internally to represent moves of chance. It isn't exposed as a value you can access directly -- `Game.players` lists only the personal players; chance moves are identified via `Node.event` instead (see below)." }, { "cell_type": "code", "execution_count": null, "id": "841f9f74", "metadata": {}, "outputs": [], "source": "list(g.players)" }, { "cell_type": "markdown", "id": "0d4c7f5b", "metadata": {}, "source": [ "A move belonging to the chance player is called an **event**, and is created with `append_event` rather than `append_move`, since it requires the probability distribution over its actions to be specified explicitly. Like `append_move`, `append_event` takes an `H`-built selector identifying the node(s) to add the event at.\n", "\n", "The first step in this game is that Alice is dealt a card which could be a King or Queen, each with probability 1/2.\n", "\n", "To simulate this in Gambit, we create a chance event at the root node of the game, using `gbt.H.path()` to select it:" ] }, { "cell_type": "code", "execution_count": null, "id": "fe80c64c", "metadata": {}, "outputs": [], "source": "g.append_event(\n gbt.H.path(),\n actions={\"King\": gbt.Rational(1, 2), \"Queen\": gbt.Rational(1, 2)}\n)" }, { "cell_type": "code", "execution_count": null, "id": "867cb1d8-7a5d-45d1-9349-9bbc2a4e2344", "metadata": {}, "outputs": [], "source": [ "draw(g, color_scheme=\"gambit\")" ] }, { "cell_type": "markdown", "id": "5cf73f0a", "metadata": {}, "source": [ "## Information sets\n", "\n", "In this game, information structure is important.\n", "Alice knows her card, so the two nodes at which she has the move are part of different **information sets**.\n", "\n", "We'll therefore need to append Alice's move separately for each possible card, i.e. the scenarios where she has a King or a Queen.\n", "`append_move` takes an `H`-built selector describing which node(s) to add the move at; `gbt.H.path(label)` describes the node reached by taking the action labeled `label` from the root:" ] }, { "cell_type": "code", "execution_count": null, "id": "0e3bb5ef", "metadata": {}, "outputs": [], "source": [ "for card in [\"King\", \"Queen\"]:\n", " g.append_move(gbt.H.path(card), player=\"Alice\", actions=[\"Bet\", \"Fold\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "0c522c2d-992e-48b6-a1f8-0696d33cdbe0", "metadata": {}, "outputs": [], "source": [ "draw(g, color_scheme=\"gambit\")" ] }, { "cell_type": "markdown", "id": "4c8d0343", "metadata": {}, "source": [ "The loop above causes each of the newly-appended moves to be in new information sets, reflecting the fact that Alice's decision depends on the knowledge of which card she holds.\n", "\n", "In contrast, Bob does not know Alice’s card, and therefore cannot distinguish between the two nodes at which he has to make his decision:\n", "\n", " - Chance player chooses King, then Alice Bets: `gbt.H.path(\"King\", \"Bet\")`\n", " - Chance player chooses Queen, then Alice Bets: `gbt.H.path(\"Queen\", \"Bet\")`\n", "\n", "In other words, Bob's decision when Alice Bets with a Queen should be part of the same information set as Bob's decision when Alice Bets with a King.\n", "\n", "To set this scenario up in Gambit, we'll need to add both possible moves as part of the same information set.\n", "This can be done with a single selector: `gbt.H.path(..., \"Bet\")` describes the node reached by *any* single action from the root (either card), followed by \"Bet\" -- so it matches both of Bob's decision nodes at once, joining them into one information set:" ] }, { "cell_type": "code", "execution_count": null, "id": "dbfa7035", "metadata": {}, "outputs": [], "source": [ "g.append_move(\n", " gbt.H.path(..., \"Bet\"),\n", " player=\"Bob\",\n", " actions=[\"Call\", \"Fold\"]\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "e85b3346-2fea-4a73-aa72-9efb436c68c1", "metadata": {}, "outputs": [], "source": [ "draw(g, color_scheme=\"gambit\")" ] }, { "cell_type": "markdown", "id": "c4eeb65f", "metadata": {}, "source": "In game theory terms, this creates \"imperfect information\".\nBob cannot distinguish between these two nodes in the game tree, so he must use the same same probabilities for Call vs. Fold in both situations.\n\nThis is crucial in games where players must make decisions without full knowledge of the state of the game.\n\nLet's now set up the four possible payoff outcomes for the game, assigning each directly to the terminal node(s) it results at.\nWe'll label them according to player 1 (Alice):" }, { "cell_type": "code", "execution_count": null, "id": "29aa60a0", "metadata": {}, "outputs": [], "source": [ "# Alice folds, Bob wins small\n", "g.make_outcome(\n", " gbt.H.path(..., \"Fold\"),\n", " {\"Alice\": -1, \"Bob\": 1},\n", " \"Lose\"\n", ")\n", "\n", "# Bob sees Alice Bet and calls, correctly believing she is bluffing, Bob wins big\n", "g.make_outcome(\n", " gbt.H.path(\"Queen\", \"Bet\", \"Call\"),\n", " {\"Alice\": -2, \"Bob\": 2},\n", " \"Lose Big\"\n", ")\n", "\n", "# Bob sees Alice Bet and calls, incorrectly believing she is bluffing, Alice wins big\n", "g.make_outcome(\n", " gbt.H.path(\"King\", \"Bet\", \"Call\"),\n", " {\"Alice\": 2, \"Bob\": -2},\n", " \"Win Big\"\n", ")\n", "\n", "# Bob does not call Alice's Bet, Alice wins small\n", "g.make_outcome(\n", " gbt.H.path(..., \"Bet\", \"Fold\"),\n", " {\"Alice\": 1, \"Bob\": -1},\n", " \"Win\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "fdee7b53-7820-44df-9d17-d15d0b9667aa", "metadata": {}, "outputs": [], "source": [ "draw(g, color_scheme=\"gambit\")" ] }, { "cell_type": "markdown", "id": "17eb6af5", "metadata": {}, "source": [ "## Computing and interpreting Nash equilibria\n", "\n", "\n", "Since our one-card poker game has two players, we can use the `lcp_solve` algorithm in Gambit to compute a Nash equilibrium:" ] }, { "cell_type": "code", "execution_count": null, "id": "4d92c8d9", "metadata": {}, "outputs": [], "source": [ "result = gbt.nash.lcp_solve(g)\n", "result" ] }, { "cell_type": "markdown", "id": "e5946077", "metadata": {}, "source": [ "The result of the calculation is returned as an `LcpBehaviorResult` object.\n", "\n", "The equilibrium found is reported in `LcpBehaviorResult.equilibrium`; in this case, this is a single `MixedBehaviorProfile`.\n", "\n", "For one-card poker, we expect to find a single equilibrium (one `MixedBehaviorProfile`):" ] }, { "cell_type": "code", "execution_count": null, "id": "9967d6f7", "metadata": {}, "outputs": [], "source": [ "print(\"Equilibrium found:\", result.equilibrium is not None)\n", "eqm = result.equilibrium" ] }, { "cell_type": "markdown", "id": "1b1cd21e", "metadata": {}, "source": [ "If we inspect the object type, we can see it's a `MixedBehaviorProfileRational` which is a subclass of `MixedBehaviorProfile` that uses rational numbers for probabilities:" ] }, { "cell_type": "code", "execution_count": null, "id": "3293e818", "metadata": {}, "outputs": [], "source": [ "type(eqm)" ] }, { "cell_type": "markdown", "id": "69f67b5b", "metadata": {}, "source": [ "A mixed behavior profile specifies, for each information set, the probability distribution over actions at that information set.\n", "\n", "Indexing a mixed behaviour profile by a player gives a `MixedBehavior`, which specifies probability distributions at each of the player's information sets:" ] }, { "cell_type": "code", "execution_count": null, "id": "4cf38264", "metadata": {}, "outputs": [], "source": [ "type(eqm[\"Alice\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "85e7fdda", "metadata": {}, "outputs": [], "source": [ "eqm[\"Alice\"]" ] }, { "cell_type": "markdown", "id": "6615115d", "metadata": {}, "source": "In this case, at Alice's first information set, the one at which she has the King, she always Bets.\n\nAt her second information set, where she has the Queen, she sometimes bluffs, raising with probability one-third.\n\nThe probability distribution at an information set is represented by a `MixedAction`.\n\nA `MixedBehavior` is a collection of these, one per information set belonging to the player; iterating over it does exactly that:" }, { "cell_type": "code", "execution_count": null, "id": "f45a82b6", "metadata": {}, "outputs": [], "source": "for number, (_infoset, mixed_action) in enumerate(eqm[\"Alice\"], start=1):\n print(\n f\"At information set {number}, \"\n f\"Alice plays Bet with probability: {mixed_action['Bet']}\"\n f\" and Fold with probability: {mixed_action['Fold']}\"\n )" }, { "cell_type": "markdown", "id": "9eeae046", "metadata": {}, "source": [ "We can alternatively iterate through each of a player's actions like so:" ] }, { "cell_type": "markdown", "id": "9c1f6a2e", "metadata": {}, "source": "`Game.get_infosets` returns each information set's canonical member as a `History`. Indexing the profile directly by it requires turning it into a `Selector` first, since `MixedBehaviorProfile.__getitem__` accepts only a player label or a `Selector`; `History.actions` projects it down to the plain tuple of action labels `H.path` expects:" }, { "cell_type": "code", "execution_count": null, "id": "83bbd3e5", "metadata": {}, "outputs": [], "source": "for number, node in enumerate(g.get_infosets(\"Alice\"), start=1):\n for action in g.get_actions(gbt.H.path(*node.actions)):\n print(\n f\"At information set {number}, \"\n f\"Alice plays {action} with probability: {eqm[gbt.H.path(*node.actions)][action]}\"\n )" }, { "cell_type": "markdown", "id": "1f121d48", "metadata": {}, "source": [ "Now let's look at Bob’s strategy:" ] }, { "cell_type": "code", "execution_count": null, "id": "6bf51b38", "metadata": {}, "outputs": [], "source": [ "eqm[\"Bob\"]" ] }, { "cell_type": "markdown", "id": "e906c4c4", "metadata": {}, "source": [ "Bob Calls Alice’s Bet two-thirds of the time.\n", "\n", "Since Bob has just one information set, we can get its representative node and index\n", "the profile directly by it to read off a single action's probability:" ] }, { "cell_type": "code", "execution_count": null, "id": "2966e700", "metadata": {}, "outputs": [], "source": "(bob_node,) = g.get_infosets(\"Bob\")\neqm[gbt.H.path(*bob_node.actions)][\"Call\"]" }, { "cell_type": "markdown", "id": "db19411b", "metadata": {}, "source": [ "Because this is an equilibrium, Bob is indifferent between the two actions at his information set, meaning he has no reason to prefer one action over the other, in expectation, given Alice's expected strategy.\n", "\n", "`MixedBehaviorProfile.action_values` returns the expected payoff of taking each action, conditional on reaching its information set, grouped by information set:" ] }, { "cell_type": "code", "execution_count": null, "id": "a7d3816d", "metadata": {}, "outputs": [], "source": "bob_action_values = eqm.action_values[gbt.H.path(*bob_node.actions)]\nfor action in g.get_actions(gbt.H.path(*bob_node.actions)):\n print(\n f\"When Bob plays {action} his expected payoff is {bob_action_values[action]}\"\n )" }, { "cell_type": "markdown", "id": "6491fdda", "metadata": {}, "source": "Bob's indifference between his actions arises because of his beliefs given Alice's strategy.\n\n`MixedBehaviorProfile.beliefs` returns the probability of reaching each node, conditional on its information set being reached.\n\nRecall that the two nodes in Bob's only information set are identified by the action-label sequences `(\"King\", \"Bet\")` and `(\"Queen\", \"Bet\")`:" }, { "cell_type": "code", "execution_count": null, "id": "4a54b20c", "metadata": {}, "outputs": [], "source": "for node in g.get_members(gbt.H.path(*bob_node.actions)):\n print(\n f\"Bob's belief in reaching the {node[-2].action} -> \"\n f\"{node[-1].action} node is: {eqm.beliefs[node]}\"\n )" }, { "cell_type": "markdown", "id": "351bb3ce", "metadata": {}, "source": [ "Bob believes that, conditional on Alice raising, there's a 3/4 chance that she has the King; therefore, the expected payoff to Calling is in fact -1 as computed.\n", "\n", "`MixedBehaviorProfile.infoset_probs` returns the probability with which each information set is reached:" ] }, { "cell_type": "code", "execution_count": null, "id": "b250c1cd", "metadata": {}, "outputs": [], "source": "eqm.infoset_probs[gbt.H.path(*bob_node.actions)]" }, { "cell_type": "markdown", "id": "9216ea34", "metadata": {}, "source": [ "The corresponding probability that a node is reached in the play of the game is given by `MixedBehaviorProfile.realiz_probs`, and the expected payoff to a player conditional on reaching a node is given by `MixedBehaviorProfile.history_values`:" ] }, { "cell_type": "code", "execution_count": null, "id": "6f01846b", "metadata": {}, "outputs": [], "source": "bob_history_values = eqm.history_values[\"Bob\"]\nfor node in g.get_members(gbt.H.path(*bob_node.actions)):\n print(\n f\"The probability that the node {node[-2].action} -> \"\n f\"{node[-1].action} is reached is: {eqm.realiz_probs[node]}. \",\n f\"Bob's expected payoff conditional on reaching this node is {bob_history_values[node]}\"\n )" }, { "cell_type": "markdown", "id": "5ba0c241", "metadata": {}, "source": [ "The overall expected payoff to a player given the behavior profile is returned by `MixedBehaviorProfile.payoffs`:" ] }, { "cell_type": "code", "execution_count": null, "id": "5079d231", "metadata": {}, "outputs": [], "source": [ "eqm.payoffs[\"Alice\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "c55f2c7a", "metadata": {}, "outputs": [], "source": [ "eqm.payoffs[\"Bob\"]" ] }, { "cell_type": "markdown", "id": "26d5e8ff", "metadata": {}, "source": [ "The equilibrium computed expresses probabilities in rational numbers.\n", "\n", "Because the numerical data of games in Gambit [are represented exactly](#representation-of-numerical-data-of-a-game), methods which are specialized to two-player games, `lp_solve`, `lcp_solve`, and `enummixed_solve`, can report exact probabilities for equilibrium strategy profiles.\n", "\n", "This is enabled by default for these methods.\n", "\n", "When a game has an extensive representation, equilibrium finding methods default to computing on that representation.\n", "It is also possible to compute using the strategic representation.\n", "`pygambit` transparently computes the reduced strategic form representation of an extensive game:" ] }, { "cell_type": "code", "execution_count": null, "id": "d4ecff88", "metadata": {}, "outputs": [], "source": "g.get_strategies(\"Alice\")" }, { "cell_type": "markdown", "id": "a9bf9b73", "metadata": {}, "source": [ "In the strategic form of this game, Alice has four strategies.\n", "\n", "The generated strategy labels list the action numbers taken at each information set.\n", "For example, label '11' refers to the strategy gets dealt the King, then Bets.\n", "\n", "We can therefore apply a method which operates on a strategic game to any game with an extensive representation." ] }, { "cell_type": "code", "execution_count": null, "id": "24e4b6e8", "metadata": {}, "outputs": [], "source": [ "gnm_result = gbt.nash.gnm_solve(g)\n", "gnm_result" ] }, { "cell_type": "markdown", "id": "d88b736b", "metadata": {}, "source": [ "`gnm_solve` can be applied to any game with any number of players, and uses a path-following process in floating-point arithmetic, so it returns profiles with probabilities expressed as floating-point numbers.\n", "\n", "This method operates on the strategic representation of the game, so the returned results are of type `MixedStrategyProfile` (specifically `MixedStrategyProfileDouble`):" ] }, { "cell_type": "code", "execution_count": null, "id": "d9ffb4b8", "metadata": {}, "outputs": [], "source": [ "gnm_eqm = gnm_result.equilibria[0]\n", "type(gnm_eqm)" ] }, { "cell_type": "markdown", "id": "102d22c2", "metadata": {}, "source": "Indexing a `MixedStrategyProfile` by a player gives the probability distribution over that player's strategies only.\n\nThe expected payoff to each strategy is given by `MixedStrategyProfile.strategy_values`, grouped by player, and the overall expected payoff to each player is given by `MixedStrategyProfile.payoffs`:" }, { "cell_type": "code", "execution_count": null, "id": "56e2f847", "metadata": {}, "outputs": [], "source": "gnm_payoffs = gnm_eqm.payoffs\ngnm_strategy_values = gnm_eqm.strategy_values\nfor player in g.players:\n print(\n f\"{player}'s expected payoffs playing:\"\n )\n for strategy in g.get_strategies(player):\n print(\n f\"Strategy {strategy}: {gnm_strategy_values[player][strategy]:.4f}\"\n )\n print(\n f\"{player}'s overall expected payoff: {gnm_payoffs[player]:.4f}\"\n )\n print()" }, { "cell_type": "markdown", "id": "874be231", "metadata": {}, "source": [ "When a game has an extensive representation, we can convert freely between a mixed strategy profile and the corresponding mixed behaviour profile representation of the same strategies using `MixedStrategyProfile.as_behavior` and `MixedBehaviorProfile.as_strategy`.\n", "\n", "- A mixed **strategy** profile maps each strategy in a game to the corresponding probability with which that strategy is played.\n", "- A mixed **behaviour** profile maps each action at each information set in a game to the corresponding probability with which the action is played, conditional on that information set being reached.\n", "\n", "Let's convert the equilibrium we found using `gnm_solve` to a mixed behaviour profile and iterate through the players actions to show their expected payoffs, comparing as we go with the payoffs found by `lcp_solve`:" ] }, { "cell_type": "code", "execution_count": null, "id": "d18a91f0", "metadata": {}, "outputs": [], "source": "for player in g.players:\n print(\n f\"{player}'s expected payoffs:\"\n )\n gnm_action_values = gnm_eqm.as_behavior().action_values\n lcp_action_values = eqm.action_values\n for number, node in enumerate(g.get_infosets(player), start=1):\n for action in g.get_actions(gbt.H.path(*node.actions)):\n print(\n f\"At information set {number}, \"\n f\"when playing {action} - \"\n f\"gnm: {gnm_action_values[gbt.H.path(*node.actions)][action]:.4f}\"\n f\", lcp: {str(lcp_action_values[gbt.H.path(*node.actions)][action])}\"\n )\n print()" }, { "cell_type": "markdown", "id": "b2867dca", "metadata": {}, "source": "Acceptance criteria for Nash equilibria\n---------------------------------------\n\nSome methods for computing Nash equilibria operate using floating-point arithmetic and/or generate candidate equilibrium profiles using methods which involve some form of successive approximations, and so return only approximate equilibria.\nGambit expresses how close an approximation must be, before it is reported as an equilibrium, via a `maxregret` parameter, interpreted *proportionally* to the range of payoffs in the game.\nSee [Acceptance criteria for approximate Nash equilibria](../algorithms.html#pygambit-nash-maxregret) in the algorithms documentation for the full explanation of `maxregret` and the guarantees it provides.\n\nAs an example, consider solving our one-card poker game using `logit_solve`. The range of the payoffs in this game is 4 (from +2 to -2):\n" }, { "cell_type": "code", "execution_count": null, "id": "0c55f745", "metadata": {}, "outputs": [], "source": [ "g.max_payoff, g.min_payoff" ] }, { "cell_type": "markdown", "id": "6263ad6e", "metadata": {}, "source": [ "`logit_solve` is a globally-convergent method, in that it computes a sequence of profiles which is guaranteed to have a subsequence that converges to a\n", "Nash equilibrium. \n", "\n", "The default value of `maxregret` for this method is set at `1e-8`:" ] }, { "cell_type": "code", "execution_count": null, "id": "101598c6", "metadata": {}, "outputs": [], "source": [ "logit_solve_result = gbt.nash.logit_solve(g, maxregret=1e-8)\n", "logit_solve_result.success" ] }, { "cell_type": "code", "execution_count": null, "id": "9b142728", "metadata": {}, "outputs": [], "source": [ "ls_eqm = logit_solve_result.equilibrium\n", "ls_eqm.max_regret()" ] }, { "cell_type": "markdown", "id": "a2ba06c4", "metadata": {}, "source": [ "The value of `MixedBehaviorProfile.max_regret` of the computed profile exceeds `1e-8` measured in terms of payoffs of the game.\n", "However, when considered relative to the scale of the game's payoffs, we see it is less than `1e-8` of the payoff range, as requested:" ] }, { "cell_type": "code", "execution_count": null, "id": "ff405409", "metadata": {}, "outputs": [], "source": [ "ls_eqm.max_regret() / (g.max_payoff - g.min_payoff)" ] }, { "cell_type": "markdown", "id": "54635455", "metadata": {}, "source": [ "In general, for globally-convergent methods especially, there is a tradeoff between precision and running time.\n", "\n", "We could instead ask only for an $\\varepsilon$-equilibrium with a (scaled) $\\varepsilon$ of no more than `1e-4`:" ] }, { "cell_type": "code", "execution_count": null, "id": "31b0143c", "metadata": {}, "outputs": [], "source": [ "(\n", " gbt.nash.logit_solve(g, maxregret=1e-4).equilibrium\n", " .max_regret() / (g.max_payoff - g.min_payoff)\n", ")" ] }, { "cell_type": "markdown", "id": "dc8c8509", "metadata": {}, "source": [ "The tradeoff comes from some methods being slow to converge on some games, making it useful instead to get a more coarse approximation to an equilibrium (higher `maxregret` value) which is faster to calculate:" ] }, { "cell_type": "code", "execution_count": null, "id": "7cfba34a", "metadata": {}, "outputs": [], "source": [ "%%time\n", "gbt.nash.logit_solve(g, maxregret=1e-4)" ] }, { "cell_type": "code", "execution_count": null, "id": "6f1809a7", "metadata": {}, "outputs": [], "source": [ "%%time\n", "gbt.nash.logit_solve(g, maxregret=1e-8)" ] }, { "cell_type": "markdown", "id": "76461069", "metadata": {}, "source": [ "The convention of expressing `maxregret` scaled by the game's payoffs standardises the behavior of methods across games.\n", "\n", "For example, consider solving the poker game instead using `liap_solve()`." ] }, { "cell_type": "code", "execution_count": null, "id": "414b6f65", "metadata": {}, "outputs": [], "source": [ "(\n", " gbt.nash.liap_solve(g.mixed_strategy_profile(), maxregret=1e-1)\n", " .equilibrium.max_regret() / (g.max_payoff - g.min_payoff)\n", ")" ] }, { "cell_type": "markdown", "id": "c6853432", "metadata": {}, "source": [ "If, instead, we double all payoffs, the output of the method is unchanged:" ] }, { "cell_type": "code", "execution_count": null, "id": "a892dc2b", "metadata": {}, "outputs": [], "source": "for label in g.get_outcomes():\n payoffs = g.get_outcome_payoffs(label)\n g.set_outcome_payoffs(label, {player: value * 2 for player, value in payoffs})\n\n(\n gbt.nash.liap_solve(g.mixed_strategy_profile(), maxregret=1e-1)\n .equilibrium.max_regret() / (g.max_payoff - g.min_payoff)\n)" }, { "cell_type": "markdown", "id": "5f1f66e0", "metadata": {}, "source": "## Representation of numerical data of a game\n\nPayoffs to players and probabilities of actions at events are specified as numbers.\nGambit represents the numerical values in a game in exact precision, using either decimal or rational representations.\n\nTo illustrate, consider a trivial game which just has one move for the chance player:" }, { "cell_type": "code", "execution_count": null, "id": "2f79695a", "metadata": {}, "outputs": [], "source": [ "small_game = gbt.Game.new_tree()\n", "small_game.append_event(gbt.H.path(), dict.fromkeys([\"a\", \"b\", \"c\"], gbt.Rational(1, 3)))\n", "list(small_game.get_action_probs(gbt.H.path()).values())" ] }, { "cell_type": "markdown", "id": "dc4522b5", "metadata": {}, "source": "Here we specified an equal-probability distribution over the three actions, using `pygambit`'s `Rational` class, which is derived from Python's `fractions.Fraction`, to represent the probabilities exactly.\n\nNumerical data can be set as rational numbers.\n`make_event` forms a collection of nodes into an event with a given distribution; applied to a single node which is already an event, it resets the probabilities.\nLet's use it to change the distribution, again with `Rational` numbers:" }, { "cell_type": "code", "execution_count": null, "id": "5de6acb2", "metadata": {}, "outputs": [], "source": [ "small_game.make_event(\n", " gbt.H.path(),\n", " {\"a\": gbt.Rational(1, 4), \"b\": gbt.Rational(1, 2), \"c\": gbt.Rational(1, 4)}\n", ")\n", "list(small_game.get_action_probs(gbt.H.path()).values())" ] }, { "cell_type": "markdown", "id": "23263b21", "metadata": {}, "source": [ "Numerical data can also be explicitly specified as decimal numbers:" ] }, { "cell_type": "code", "execution_count": null, "id": "c47d2ab6", "metadata": {}, "outputs": [], "source": [ "small_game.make_event(\n", " gbt.H.path(),\n", " {\"a\": gbt.Decimal(\".25\"), \"b\": gbt.Decimal(\".50\"), \"c\": gbt.Decimal(\".25\")}\n", ")\n", "list(small_game.get_action_probs(gbt.H.path()).values())" ] }, { "cell_type": "markdown", "id": "bffda303", "metadata": {}, "source": [ "Although the two representations above are mathematically equivalent, `pygambit` remembers the format in which the values were specified.\n", "\n", "Expressing rational or decimal numbers as above is verbose and tedious.\n", "`pygambit` offers a more concise way to express numerical data in games: when setting numerical game data, `pygambit` will attempt to convert text strings to their rational or decimal representation.\n", "The above can therefore be written more compactly using string representations:" ] }, { "cell_type": "code", "execution_count": null, "id": "04329084", "metadata": {}, "outputs": [], "source": [ "small_game.make_event(gbt.H.path(), {\"a\": \"1/4\", \"b\": \"1/2\", \"c\": \"1/4\"})\n", "list(small_game.get_action_probs(gbt.H.path()).values())" ] }, { "cell_type": "code", "execution_count": null, "id": "9015e129", "metadata": {}, "outputs": [], "source": [ "small_game.make_event(gbt.H.path(), {\"a\": \".25\", \"b\": \".50\", \"c\": \".25\"})\n", "list(small_game.get_action_probs(gbt.H.path()).values())" ] }, { "cell_type": "markdown", "id": "9f22d40d", "metadata": {}, "source": [ "As a further convenience, `pygambit` will accept Python `int` and `float` values.\n", "`int` values are always interpreted as `Rational` values.\n", "\n", "`pygambit` attempts to render `float` values in an appropriate `Decimal` equivalent.\n", "In the majority of cases, this creates no problems.\n", "For example:" ] }, { "cell_type": "code", "execution_count": null, "id": "0a019aa5", "metadata": {}, "outputs": [], "source": [ "small_game.make_event(gbt.H.path(), {\"a\": .25, \"b\": .50, \"c\": .25})\n", "list(small_game.get_action_probs(gbt.H.path()).values())" ] }, { "cell_type": "markdown", "id": "d53adcd4", "metadata": {}, "source": [ "However, rounding can cause difficulties when attempting to use `float` values to represent values which do not have an exact decimal representation:" ] }, { "cell_type": "code", "execution_count": null, "id": "1991d288", "metadata": {}, "outputs": [], "source": [ "try:\n", " small_game.make_event(gbt.H.path(), {\"a\": 1/3, \"b\": 1/3, \"c\": 1/3})\n", "except ValueError as e:\n", " print(\"ValueError:\", e)" ] }, { "cell_type": "markdown", "id": "89fefd34", "metadata": {}, "source": [ "This behavior can be slightly surprising, especially in light of the fact that\n", "in Python:" ] }, { "cell_type": "code", "execution_count": null, "id": "b1dc37fd", "metadata": {}, "outputs": [], "source": [ "1/3 + 1/3 + 1/3" ] }, { "cell_type": "markdown", "id": "a06699af", "metadata": {}, "source": [ "In checking whether these probabilities sum to one, `pygambit` first converts each of the probabilities to a `Decimal` representation, via the following method:" ] }, { "cell_type": "code", "execution_count": null, "id": "dc1edea2", "metadata": {}, "outputs": [], "source": [ "gbt.Decimal(str(1/3))" ] }, { "cell_type": "markdown", "id": "4bfff415", "metadata": {}, "source": [ "...and the sum-to-one check then fails because:" ] }, { "cell_type": "code", "execution_count": null, "id": "1edd90d6", "metadata": {}, "outputs": [], "source": [ "gbt.Decimal(str(1/3)) + gbt.Decimal(str(1/3)) + gbt.Decimal(str(1/3))" ] }, { "cell_type": "markdown", "id": "5208b7a4", "metadata": {}, "source": [ "Setting payoffs for players also follows the same rules.\n", "Representing probabilities and payoffs exactly is essential, because `pygambit` offers (in particular for two-player games) the possibility of computation of equilibria exactly, because the Nash equilibria of any two-player game with rational payoffs and chance probabilities can be expressed exactly in terms of rational numbers.\n", "\n", "It is therefore advisable always to specify the numerical data of games either in terms of `Decimal` or `Rational` values, or their string equivalents.\n", "It is safe to use `int` values, but `float` values should be used with some care to ensure the values are recorded as intended." ] }, { "cell_type": "markdown", "id": "65def67e", "metadata": {}, "source": [ "#### References\n", "\n", "Reiley, David H., Michael B. Urbancic and Mark Walker. (2008) \"Stripped-down poker: A classroom game with signaling and bluffing.\" *The Journal of Economic Education* 39(4): 323-341." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }