• Stars
    star
    104
  • Rank 319,903 (Top 7 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created about 7 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python toolbox for simulation of matching markets in economics

MatchingMarkets

A python toolbox for simulation of matching markets in economics.

The toolbox uses generic abstractions and python lambdas to produce simulations of general matching markets. Examples of matchings markets are organ exchanges, school/student assignment or matching goods and individuals in a barter economy. These markets can last one period, or can evolve over many time periods. Here is an example of a graph produced with the package of a market evolving over many periods:

alt tag

In this graph, nodes are people and node color is their "type" (think patient or donor blood type in organ transplants). The number on a node is periods of life left before death if not matched. An edge means they are compatible (it can be weighed if there's risk of failure). Edges get highlighted red before matches. In this simulation, only people of the same type can be compatible, and we naively match two compatible individuals at random. Our timing decision rule here is to naively try to match everyone each period.

The figure can be produced with this code:

import matchingmarkets as mm
import numpy.random as rng

newsim = mm.simulation(time_per_run=100, max_agents=5000,
                       arrival_rate=15, average_success_prob=lambda: 0.3,
                       typeGenerator=rng.randint,
                       compatFct=mm.stochastic_neighborSameType,
                       crit_input=3, numTypes=5)

# Make sure matplotlib is __not__ inline for this
newsim.graph(plot_time=0.8)

Dependencies

MatchingMarkets.py aims to be "batteries included". If you use the Anaconda distribution with python 3, you should have no problems using the package.

Formally, it requires Python 3.6+, Numpy/scipy, NetworkX, matplotlib with qt5agg backend (for interactive graph plotting) This backend can be changed manually in "Markets.py" A PuLP installation is included by default in matchingmarkets/algorithms/pulp for kidney solvers. This uses the COIN-OR cbc solver by default and can be changed to Gurobi, CPLEX or other compatible solvers manually

Included algorithms

Market Generating

It can be useful to try out a few settings on generators and visualize the output to see if the simuation is what you want. The current suite of generators is in this file. Currently we have:

  • Random or deterministic assignment of one or multiple abstract types

  • Blood types (for organ transplants)

  • It's easy to write a lambda and pass it in the typeGenerator attribute in a mm.simulation object. The lambda should respect the format of generator functions.

More important is the function defining match compatibility based on types of agents. This is in the same file as above. Using abstract types and cutoff values for the RNG, you can simulate many classic matching problems easily. It is also easy to write a lambda which simulates the compatibility you desire as long as it respects the form f(sourceAgent, receivingAgent, cutoff=1) -> float in [0,1] where the result is the match success probability, and cutoff is an optional parameter usually used in an RNG.

Use

Please refer to the tutorial notebook for more in depth instructions.

Download the package, change directory to the one containing it in your python console, and import matchingmarkets as mm.

Intended use is through the simulation object, as follows:

newsim = mm.simulation(
                      # Simulation parameters here
                      )
newsim.run()

#prints output of a simulation
newsim.stats()

out[5]:
Simulation Results
1  periods
50  runs
Stat      value  (std dev)
==================================
Welfare:   50.04 ( 6.7675 )
matches:   50.04  ( 6.7675 )
perished:  0.0  ( 0.0000 )
loss%:     0.0000  ( 0.0000 )

One way to get as much information about your simulation as possible is to run it with the verbose flag on, and plot a few periods:

newsim.verbose = True
newsim.graph(period=10)
# This will print all relevant information to console, and graph the output

The simulation class has many attributes to simulate static (single period) or dynamic (multi-period) matching markets. When creating the simulation class, you can pass the following parameters:

      runs: int
        number of trials when runs

      time_per_run: int
          number of time periods in a run

       max_agents: int
          maximum number of agents over a run overall

       logAllData: bool
          log every single period on every iteration
          Takes much longer, but outputs pretty plots in the stats() function
          if false, only logs final results on each run

        arrival_rate: int
            average number of new agents per period
            the lambda in a poisson distribution
            
        average_success_prob: f() -> float[0,1]
            cutoff value passed in neighborfct
            1 - pr(failure of match) for average of mrkt
            
        algorithm: f(list<agent>) -> dict{agent.name : agent.name}
            Matching Algorithm
            takes current agents in market as input
            returns a list of matches
            see algorithms.py for details
            
        arrival_fct: fct(float) -> int
            function that returns number of arrival this period
            poisson distribution draw by default
            
        crit_input:int
            input in time_to_crit, usually param in a rng fct
            
        discount: fct() -> [0,1]
            function generating agent discount rate
            
        matchUtilFct: fct(agent1, agent2, float) -> float
            returns utility for agent1 of matching to agent2
            
        metaAlgorithm: f(market, algorithm, **kwargs)
                            -> dict{agent.name : agent.name}
            Algorithm responsible for timing decisions
            Decides when to match, and who participates
            in the matching algorithm
            
        metaParams: dict{string: value}
            kwargs passed into the metaAlgorithm
            This can then be passed into the Algorithm
            
        neighborFct: fct(agent1, agent2, float) -> float
            rng function returning agents who are
            compatible matches based on input
            float parameter is a cutoff value for rng
            
        numTypes: int
            input in typeGenerator
            usually # of types

        utilityFctInput: float
            input for matchUtilFct (usually rng cutoff value)
            
        selfMatch: bool
            true if an agent can match himself
            ex: House market
            false if an agent has to match another
            ex: marriage market
            
        time_to_crit: fct() -> int
            function generating agent time to crit
            
        typeGenerator: fct(int) -> int
            function generating agent type
            
        verbose: bool
            print on relevant actions in update

TODO:

replace plotting animations with Celluloid

More Repositories

1

QuantEcon.py

A community based Python library for quantitative economics
Python
1,860
star
2

QuantEcon.jl

Julia implementation of QuantEcon routines
Julia
489
star
3

quantecon-notebooks-python

A Repository of Notebooks for the Python Lecture Site
Jupyter Notebook
263
star
4

lecture-python.notebooks

Notebooks for https://python.quantecon.org
Jupyter Notebook
225
star
5

lecture-source-py

Source files for "Lectures in Quantitative Economics" -- Python version
Jupyter Notebook
191
star
6

QuantEcon.cheatsheet

A cheatsheet for Python and Julia
TeX
155
star
7

lecture-source-jl

Source files for "Lectures in Quantitative Economics" -- Julia version
TeX
128
star
8

GameTheory.jl

Algorithms and data structures for game theory in Julia
Julia
122
star
9

QuantEcon.notebooks

Jupyter notebooks contributed by QuantEcon developers, users and the community
Jupyter Notebook
119
star
10

quantecon-notebooks-julia

Jupyter Notebook
110
star
11

book-dp1-public-companion

Dynamic Programming Volume 1
Julia
109
star
12

book-networks-public

Companion Site for Economic Networks: Theory and Computation
CSS
83
star
13

lecture-python.myst

Quantitative Economics with Python
Jupyter Notebook
77
star
14

sphinxcontrib-jupyter

A Sphinx Extension for Generating Jupyter Notebooks
Python
74
star
15

lecture-python

Source files for https://python.quantecon.org
Jupyter Notebook
60
star
16

Expectations.jl

Expectation operators for Distributions.jl objects
Julia
56
star
17

QuantEcon.lectures.code

Code Repository to Support QuantEcon Lecture Site
Python
52
star
18

lecture-python-programming.notebooks

Notebooks for https://python-programming.quantecon.org
Jupyter Notebook
51
star
19

rvlib

Distributions for Python
C
46
star
20

CompEcon.jl

Julia versions of the CompEcon routines by Miranda and Fackler.
Julia
46
star
21

lecture-julia.myst

Lecture source for "Quantitative Economics with Julia"
TeX
43
star
22

quantecon-notebooks-datascience

Jupyter Notebooks for https://datascience.quantecon.org
Jupyter Notebook
42
star
23

lecture-julia.notebooks

Download Notebooks for julia.quantecon.org
Jupyter Notebook
41
star
24

lecture-python-advanced

Source files for https://python-advanced.quantecon.org
Python
41
star
25

columbia_mini_course

Computational Economics with Python
Jupyter Notebook
40
star
26

lecture-python-programming.myst

Python Programming for Finance and Economics
JavaScript
40
star
27

dynamic_factor_models

Jupyter Notebook
35
star
28

lecture-python-intro

An Undergraduate Lecture Series for the Foundations of Computational Economics
Jupyter Notebook
33
star
29

BasisMatrices.jl

Routines for constructing BasisMatrices of different types (Chebyshev polynomials, B-Splines, piecewise linear, complete monomials, Smolyak...)
Julia
32
star
30

quantecon-book-theme

A Jupyter Book Theme for QuantEcon Book Style Projects
Jupyter Notebook
31
star
31

econometrics

Jupyter Notebook
29
star
32

krusell_smith_code

Code for the Krusell--Smith model
Jupyter Notebook
29
star
33

ShenzhenWinterCamp

Shenzhen Winter Camp 2018
Jupyter Notebook
28
star
34

lecture-jax

Lectures on Quantitative Economics Using JAX
TeX
27
star
35

lecture-datascience.myst

Source repository for QuantEcon Datascience
TeX
26
star
36

phd_workshops

2017 QuantEcon PhD Workshops on Computational Economics
Jupyter Notebook
25
star
37

RBA_RBNZ_Workshops

Code and teaching material for the workshops at the RBA and RBNZ
Jupyter Notebook
21
star
38

lecture-python-advanced.myst

Advanced Quantitative Economics with Python
TeX
21
star
39

jupinx

Jupyter + Sphinx Utilities and Tools
Python
20
star
40

ChicagoFed_workshop

Workshop on scientific computing for economists with Python and Julia
Jupyter Notebook
18
star
41

lecture-python-programming

Source files for https://python-programming.quantecon.org
Smarty
18
star
42

game-theory-notebooks

A collection of game theory notebooks
Jupyter Notebook
17
star
43

InstantiateFromURL.jl

Julia
17
star
44

lecture-python-advanced.notebooks

Notebooks that support https://python-advanced.quantecon.org
Jupyter Notebook
15
star
45

ContinuousDPs.jl

Continuous state dynamic programming
Julia
14
star
46

lecture-datascience.notebooks

Notebooks for datascience.quantecon.org
Jupyter Notebook
14
star
47

quantecon-notebooks-datascience-old

Jupyter Notebook
13
star
48

qe_lunch_workshops

Jupyter Notebook
13
star
49

cbc_workshops

Workshops for the Central Bank of Chile
Jupyter Notebook
12
star
50

SimpleDifferentialOperators.jl

Library for simple upwind finite differences
Julia
12
star
51

Copenhagen_workshop_2018

Copenhagen Summer School 2018 QuantEcon Workshop
Jupyter Notebook
11
star
52

sphinxcontrib-jupyter.minimal

A Minimal Example of the Jupyter Sphinx Extension
Python
9
star
53

summer_course_2019

QuantEcon-RSE Intensive Course on Computational Modeling
Jupyter Notebook
9
star
54

emet_summer_workshop

Repository for the North American Summer Meeting of the Econometric Society 2016 workshop
Jupyter Notebook
8
star
55

website

Website for QuantEcon Organisation
HTML
8
star
56

book-dp1-public

Website for Dynamic Programming TextBook
Julia
8
star
57

continuous_time_mcs

Continuous Time Markov Chains
Jupyter Notebook
7
star
58

jaxinterp

JAX compatible multilinear interpolation
7
star
59

NUS_workshop_2016

Computational Economics with Python and Julia
Jupyter Notebook
7
star
60

phd-macro-theory-book

Tom's PhD Macro Theory Book
TeX
7
star
61

quantecon-book-networks

Package for networks.quantecon.org (Economic Networks)
Python
6
star
62

QE_data_science_conf

QuantEcon discussion for the Berkeley Data Science x Economics conference
Jupyter Notebook
5
star
63

HCMC_workshop_2016

Modern Methods in Computational Economics in Ho Chi Minh City
Jupyter Notebook
5
star
64

Gensys.jl

A Julia version of Gensys (Sims 2000)
Jupyter Notebook
5
star
65

honours_workshop

2018 QuantEcon-RSE Honours Workshop
Jupyter Notebook
5
star
66

Keio_workshop

For the QuantEcon lecture at Keio in October 2016
Jupyter Notebook
5
star
67

DIIS_workshop

Dept of Industry, Innovation and Science QuantEcon-RSE Workshop
Jupyter Notebook
5
star
68

indian_summer_workshop

Online quantitative economics course for Indian students
5
star
69

imf_2024

Computational economics workshop at the IMF, March 2024
Jupyter Notebook
5
star
70

2021-workshop-rsit

RSIT Workshop (Uni Tรผbingen) 2021
Jupyter Notebook
4
star
71

dse_workshop

DSE Conference DP Workshop 2022
Jupyter Notebook
4
star
72

lecture-jax.notebooks

Notebooks for the lecture-jax lecture series
Jupyter Notebook
4
star
73

qe_libraries_article

Repo for a paper describing the QE libraries
TeX
4
star
74

docker

Docker Containers for QuantEcon
Dockerfile
4
star
75

WAMS-2017

Code and files for the QuantEcon workshop at WAMS 2017
Jupyter Notebook
4
star
76

lecture-econometrics-machine-learning

Python for Econometrics and Machine Learning
Jupyter Notebook
4
star
77

dynamic_programming_lecture

Fast dynamic programming with Python
3
star
78

rse_comp_econ_2023

RSE Computational Economics workshops Feb 2023
Jupyter Notebook
3
star
79

qeds

Python library to support the QuantEcon Data Science lecture series.
Python
3
star
80

rse_comp_econ_2022

Jupyter Notebook
3
star
81

parallelization_experiments

Jupyter Notebook
3
star
82

workshop.africa-july2023

Workshop Repository for Africa July 2023
Jupyter Notebook
3
star
83

project.lecture-mojo

Some Real World Examples using Mojo (Modular)
3
star
84

continuous_time_mcs.notebooks

Notebook Repository for Continuous Time Markov Chain Lecture Series
Jupyter Notebook
3
star
85

GC_CUNY_workshop_2019

Notebooks for introduction to using Python for economics
Jupyter Notebook
2
star
86

python_lecture_sandpit

A sandpit for developing lectures on the Python side
Smarty
2
star
87

ipynb_to_rst_tools

Tools, scripts and instructions for converting Jupyter notebooks to RST files
Jupyter Notebook
2
star
88

DFOLS.jl

Julia
2
star
89

short_python_workshop

A short introduction to Python for economists
Jupyter Notebook
2
star
90

imf_october_2023

IMF Presentation October 2023
Jupyter Notebook
2
star
91

quantecon-book-dp

Website for the Dynamic Programming textbook
2
star
92

quantecon-notebooks-julia-colab

Colab notebooks for the QuantEcon Julia lectures
Jupyter Notebook
2
star
93

reading-group-2024

A shared repository to support notes and discussion for the Reading Group 2024
Jupyter Notebook
2
star
94

python-lecture-sandpit.myst

A python lecture sandpit (for myst migration)
TeX
2
star
95

QuantEcon.py.benchmark

Contains benchmarks for the QuantEcon.py repository using the asv project
Python
1
star
96

QuantEcon.py-conda

For conda package of QuantEcon.py code library
Batchfile
1
star
97

TaxSmoothing

Notebooks on tax smoothing
Jupyter Notebook
1
star
98

sphinx-tojupyter

A Jupyter Notebook Writer for Sphinx
Jupyter Notebook
1
star
99

keio_comp_econ_2023

Computational economics with Python
Jupyter Notebook
1
star
100

sphinxcontrib-jupyter.testground

An Example and Test Case repository for the Sphinx Jupyter extension
Python
1
star