Matching Market

Table of Contents

One-to-One Matching
Many-to-One Matching
Modeling Simulation

Import a class, Matching() from matching.py.


In [1]:
# coding: UTF-8
%matplotlib inline
from matching import * 
from matching_simu import *

One-to-One Matching

1) Deffered Acceptance

Based on this wiki.


In [2]:
prop_prefs = [[1, 0, 2],
                         [0, 1, 2],
                         [0, 1, 2]]
resp_prefs = [[0, 2, 1],
                         [1, 0, 2],
                         [1, 0, 2]]

In [3]:
M1 = Matching(prop_prefs, resp_prefs)

In [4]:
prop_matched, resp_matched = M1.DA()

In [5]:
prop_matched


Out[5]:
array([0, 1, 2])

In [6]:
resp_matched


Out[6]:
array([0, 1, 2])

In [7]:
M1.summary()


-----Initial Setting-----
The number of proposers : 3
The number of respondants : 3
Preference of proposers : (unmatched = 3)
[[1 0 2]
 [0 1 2]
 [0 1 2]]
Preference of respondants : (unmatched = 3)
[[0 2 1]
 [1 0 2]
 [1 0 2]]
-----Matching Type-----
One-to-One Matching
Deffered Acceptance
-----Result-----
Stable matching pairs
Proposers side : 
[0 1 2]
Respondants side : 
[0 1 2]

In [8]:
M1.graph()


2) Top Trading Cycle

Initial settings are the same as the above.


In [9]:
prop_matched, resp_matched = M1.TTC()

In [10]:
prop_matched


Out[10]:
array([1, 0, 2])

In [11]:
resp_matched


Out[11]:
array([1, 0, 2])

In [12]:
M1.summary()


-----Initial Setting-----
The number of proposers : 3
The number of respondants : 3
Preference of proposers : (unmatched = 3)
[[1 0 2]
 [0 1 2]
 [0 1 2]]
Preference of respondants : (unmatched = 3)
[[0 2 1]
 [1 0 2]
 [1 0 2]]
-----Matching Type-----
One-to-One Matching
Top Trading Cycle
-----Result-----
Stable matching pairs
Proposers side : 
[1 0 2]
Respondants side : 
[1 0 2]

In [13]:
M1.graph()


3) The Boston Public Schools System

Initial settings are the same as the above.


In [14]:
prop_matched, resp_matched = M1.BS()

In [15]:
prop_matched


Out[15]:
array([1, 2, 0])

In [16]:
resp_matched


Out[16]:
array([2, 0, 1])

In [17]:
M1.summary()


-----Initial Setting-----
The number of proposers : 3
The number of respondants : 3
Preference of proposers : (unmatched = 3)
[[1 0 2]
 [0 1 2]
 [0 1 2]]
Preference of respondants : (unmatched = 3)
[[0 2 1]
 [1 0 2]
 [1 0 2]]
-----Matching Type-----
One-to-One Matching
Boston Algorithm
-----Result-----
Stable matching pairs
Proposers side : 
[1 2 0]
Respondants side : 
[2 0 1]

In [18]:
M1.graph()


Many-to-One Matching

Assume that the number of students is 4, that of laboratories is 3 and each laboratory has a capacity.


In [19]:
prop_prefs = [[1, 2, 0],
                         [2, 0, 1],
                         [2, 1, 0],
                         [2, 0, 1]]
resp_prefs = [[3, 2, 1, 0],
                         [2, 1, 3, 0],
                         [0, 1, 3, 2]]
caps = [1, 2, 2]

In [20]:
M2 = Matching(prop_prefs, resp_prefs, caps)

1) Deffered Acceptance


In [21]:
prop_matched, resp_matched, indptr = M2.DA()

In [22]:
prop_matched


Out[22]:
array([1, 2, 1, 2])

In [23]:
resp_matched


Out[23]:
array([4, 2, 0, 1, 3])

In [24]:
indptr


Out[24]:
array([0, 1, 3, 5])

In [25]:
M2.summary()


-----Initial Setting-----
The number of proposers : 4
The number of respondants : 3
Preference of proposers : (unmatched = 3)
[[1 2 0]
 [2 0 1]
 [2 1 0]
 [2 0 1]]
Preference of respondants : (unmatched = 4)
[[3 2 1 0]
 [2 1 3 0]
 [0 1 3 2]]
Capacity of respondants : 
[1, 2, 2]
-----Matching Type-----
Many-to-One Matching
Deffered Acceptance
-----Result-----
Stable matching pairs
Proposers side : 
[1 2 1 2]
Respondants side : 
[4 2 0 1 3]
indptr : 
[0 1 3 5]

In [26]:
M2.graph()


2) Top Trading Cycle

Initial settings are the same as the above.


In [27]:
prop_matched, resp_matched, indptr = M2.TTC()

In [28]:
prop_matched


Out[28]:
array([1, 2, 2, 0])

In [29]:
resp_matched


Out[29]:
array([3, 4, 0, 1, 2])

In [30]:
indptr


Out[30]:
array([0, 1, 3, 5])

In [31]:
M2.summary()


-----Initial Setting-----
The number of proposers : 4
The number of respondants : 3
Preference of proposers : (unmatched = 3)
[[1 2 0]
 [2 0 1]
 [2 1 0]
 [2 0 1]]
Preference of respondants : (unmatched = 4)
[[3 2 1 0]
 [2 1 3 0]
 [0 1 3 2]]
Capacity of respondants : 
[1, 2, 2]
-----Matching Type-----
Many-to-One Matching
Top Trading Cycle
-----Result-----
Stable matching pairs
Proposers side : 
[1 2 2 0]
Respondants side : 
[3 4 0 1 2]
indptr : 
[0 1 3 5]

In [32]:
M2.graph()


3) The Boston Public Schools System

Initial settings are the same as the above.


In [33]:
prop_matched, resp_matched, indptr = M2.BS()

In [34]:
prop_matched


Out[34]:
array([1, 2, 1, 2])

In [35]:
resp_matched


Out[35]:
array([4, 2, 0, 3, 1])

In [36]:
indptr


Out[36]:
array([0, 1, 3, 5])

In [37]:
M2.graph()


Modeling Simulation

Utility Function for the i-th student is given as follows.

$U_{i} = \alpha CV_{j} + (1-\alpha) PV_{ij}$

$CV_{j}$ means a common value to the j-th laboratory.
$PV_{ij}$ means the i-th student's private value to the j-th laboratory.
These are uniformly distributed over [0, 1).
$\alpha$ is a parameter, default set is 0.8.

Suppose that the preferences of the students are decided by this utility function,
and the number of students is 100, that of laboratories is 20.

Simulate 3000 times at each capacity
and return an average rank.


In [38]:
caps_list = [5, 7, 9, 11, 13, 15]
aves_DA = simulate(caps_list, 'DA')
aves_TTC = simulate(caps_list, 'TTC')
aves_BS = simulate(caps_list, 'BS')

In [39]:
aves_DA


Out[39]:
[4.6918033333333335,
 2.28063333333334,
 1.8561500000000173,
 1.5031266666666707,
 1.4060633333333263,
 1.3812533333333377]

In [40]:
aves_TTC


Out[40]:
[2.7613533333333318,
 2.0869066666666587,
 1.666336666666665,
 1.4666366666666673,
 1.3929833333333324,
 1.230013333333341]

In [41]:
aves_BS


Out[41]:
[3.6134166666666703,
 2.0507066666666631,
 1.7239300000000197,
 1.4885100000000013,
 1.4230666666666771,
 1.3206033333333462]

In [42]:
plt.plot(caps_list, aves_DA, label="DA")
plt.plot(caps_list, aves_TTC, label="TTC")
plt.plot(caps_list, aves_BS, label="BS")
plt.title('Compare the mechanisms')
plt.xlim(5, 15)
plt.ylim(1, 5)
plt.xlabel('Capacity')
plt.ylabel('Average Rank')
plt.legend(loc=1)
plt.show()