Skip to main content# Python Packages for Economics: Model and Simulation

**About**

**Table of Contents**

**I. Nashpy**

**[****Documentation****]**

**1. Introduction**

**2. License**

**3. Required Citation**

**4. Example**

**II. QuantEcon**

**[****Documentation****][****Website****]**

**1. Introduction**

**2. License**

**3. Required Citation**

**4. Example**

**III. Game Theory Explorer**

**[****Documentation****][****Website****][****Apps in the Cloud****]**

**1. Introduction:**

**2. License**

**3. Required Citation:**

**4. Examples:**

**IV.Gambit**

**[****Website****]**

**1. Introduction**

**2. License**

**3. Required Citation**

**4. Example**

# V. Mesa

# [Documentation] [Github]

**1. Introduction**

**2. License**

**3. Required Citation**

**4. Example**

# VI. Case Studies

**Normal Form Game [Part I]**

**Extensive Form Game [Part II]**

**Evolutionary Game [Part III]**

Econ 211 Autumn 2021 Project 1

Published onMay 04, 2022

Python Packages for Economics: Model and Simulation

In these instructions, we introduce five Python Packages for Economics: Nashpy, QuantEcon, Game Theory Explorer, Gambit, and Mesa. All five packages are useful for modeling and simulations. Both Nashpy and QuantEcon can solve simple game theory problems. To be specific, Nashpy is a Python library used for the computation of equilibria in 2 player strategic form games (Nashpy, 2017) while QuantEcon offers additional computational tools for economics, econometrics, and decision making (QuantEcon, n.d.). Game Theory Explorer (GTE) and Gambit are used for simulating and solving extensive games. GTE is an online graphical user interface for small and medium-size games while Gambit provides more complete functions. Mesa is a framework provided by Python that aims to do agent-based modeling.

In the first four sections, we introduce each software by providing the references, license information, and required citations. In the last section, we present a case study for each software.

*Resources:*

Github Repository: [URL]

Illustrator in Whimsical Folder: [URL]

PubPub Page: [URL]

[Documentation][Website][Apps in the Cloud]

In these instructions, we introduce five Python Packages for Economics: Nashpy, QuantEcon, Game Theory Explorer, Gambit, and Mesa. All five packages are useful for modeling and simulations.

Both Nashpy and QuantEcon can solve simple game theory problems. To be specific, Nashpy is a Python library used for the computation of equilibria in 2 player strategic form games (Nashpy, 2017) while QuantEcon offers additional computational tools for economics, econometrics, and decision making (QuantEcon, n.d.). Game Theory Explorer (GTE) and Gambit are used for simulating and solving extensive games. GTE is an online graphical user interface for small and medium-size games while Gambit provides more complete functions. Mesa is a framework provided by Python that aims to do agent-based modeling.

In the first four sections, we introduce each software by providing the references, license information, and required citations. In the last section, we present a case study for each software.

Whimsical Folder for Illustrators: [URL]

[Nashpy] is a Python library for the computation of equilibria in 2 player strategic form games (Nashpy, 2017). Check [This document] for instructions.

Figure 1: The functionings of Nashpy

[Source: Created by whimsical]

Nashpy is Free/Open Source software, released under the terms of the MIT License.

Vincent Knight (2021). *Nashpy: a python library for the computation of equilibria of 2 player strategic games*, Version 0.0.28. https://github.com/drvinceknight/Nashpy

In this [Colab Notebook 1: NashPy], we demonstrate how to solve the Nash Equilibrium of three classic games using Nashpy: the prisoner’s dilemma, matching pennies, and the battle of sex in Nashpy.

[QuantEcon] is a project dedicated to the development of open-source computational tools for economics, econometrics, and decision making (QuantEcon, n.d.). Check [this document] to learn how to access QuantEcon via Python.

Figure 2.1:QuantEcon overview

[Source: Created by whimsical]

QuantEcon is Free/Open Source software, released under the terms of the BSD-3-Clause License.

Thomas J. Sargent and John Stachurski (2021), *Quantitative Economics (Python),* Version 0.5.1. https://quantecon.org/quantecon-py/

Chart 2.2. Represents the pipeline to solve for Nash Equilibrium (NE) of Battle of Sexes in QuantEcon. In this [Colab Notebook 2: Nashpy vs QuantEcon], we compare Nashpy and QuantEcon packages for solving matrix games in a concrete example.

Figure 2.2:QuantEcon Examples - Battle of Sexes

[Source: Created by whimsical]

Game theory Explorer (GTE) software allows researchers to create games in extensive form and to compute equilibria accordingly, and provides an intuitive representation of the Game Tree (Savani et al., 2014). Check [this document] to learn more about GTE.

GTE is Free/Open Source software, released under the GNU General Public Licence.

Rahul Savani and Bernhard von Stengel (2015), Game Theory Explorer – Software for the Applied Game Theorist. *Computational Management Science* 12, 5-33.

Figure 3 demonstrate how we create the game tree successfully and solve for the equilibrium

Figure 3: Chart for Game Theory Explorer example

[Source: Created by whimsical]

Gambit is an open-source collection of tools for doing computation in game theory. It provides a graphical interface to help get an intuition about simple games and command-line tools and Python scripting API to support practical applications (Gambit, 2017). However, the Python API is sometimes not stable and the graphical interface is more recommended. Check [official website] to get started with Gambit.

Figure 4: Gambit overview

[Source: Created by whimsical]

Gambit is Free/Open Source software, released under the terms of the GNU General Public License.

McKelvey, Richard D., McLennan, Andrew M., and Turocy, Theodore L. (2014). *Gambit: Software Tools for Game Theory*, Version 16.0.1. http://www.gambit-project.org.

Here we provide an example of solving an imperfect information game with Gambit. The game is constructed as shown in **Figure 5.** Notice that in the game tree we constructed below is asymmetric and has an information set, which means it is an imperfect information game. By using GTE, we are not able to solve this kind of imperfect information and asymmetric game, while Gambit allows us to achieve that. You can download the Gambit graphical interface via [this link]. Check [this document] to learn how to use the interface.

Figure 5: Construct and solve imperfect information game with Gambit

Mesa is a modular framework for building, analyzing, and visualizing agent-based models. Agent-based models are computer simulations which involve multiple entities acting and interacting with one another based on their programmed behavior (Mesa, 2021). Check [this document] to learn more about Mesa.

Figure 6: Mesa Overview

[Source: Created by whimsical]

Mesa is Free/Open Source software, released under the terms of Apache2 license.

Kazil, Jackie, Masad, David, Crooks and Andrew (2020). *Utilizing Python for Agent-Based Modeling: The Mesa Framework*. https://github.com/projectmesa/mesa

In the [Colab Notebook 3: Mesa], we used Mesa to simulate an evolutionary game in the paper entitled “Evolutionary Game for Mining Pool Selection in Blockchain Networks.” This paper studies “the dynamics of mining pool selection in a blockchain network, where mining pools may choose arbitrary block mining strategies” (Liu et. al, 2018). We simulate the two pool cases and visualize the miners’ dynamic switching between these two pools. Figure 7 presents the pseudo-code.

Figure 7.1: Mesa Example 1: Pseudo Code

Figure 7.2: Mesa example 2: the Pipeline

[Source: Created by whimsical]

In this section, we provide a case study for each software mentioned in the first four sections by presenting sample code assignments that utilize the four software packages for solving game theory problems. The assignments consist of three parts: Part I: Normal Form Game; Part II: Extensive Form Game; Part II: Evolutionary Game. We provide sample questions and answers in two separate Colab notebooks （[Colab Notebook 4.1: Case Study Questions],[Colab Notebook 4.2: Case Study Answer]).

In this part, we provide problem sets for solving two normal form games, one classical game Battle of sexes (Noam Nisan et. al, 2007, p.7 ), one related to Blockchain application The miners’ dilemma (Ittay, 2015). Students are asked to use NashPy and QuantEcon to get pure equilibrium strategies and mixed equilibrium strategies.

In this part, we provide problem sets for solving two extensive form games, one classical Game of moving (Noam Nisan et. al, 2007, pp. 67-68), one related to the Blockchain Finite Blockchain game (Ewerhart et al. 2020). Students are asked to use Game Theory Explorer (GTE) to solve the extensive form game with perfect information and Gambit to solve the extensive form game with imperfect information.

In this part, we provide problem sets for solving two evolutionary games, one classical The evolutionary equilibrium of block withholding attack, (Cheng et. al, 2021), and the other related to blockchain Mining Pool Selection (Liu et. al, 2018). Students are asked to use Mesa to do agent-based modeling to simulate these two evolutionary games.