Estimation of matching models

Alfred Galichon (NYU & Sciences Po)

'math+econ+code' masterclass on optimal transport and economic applications

With python code examples

© 2018-2022 by Alfred Galichon. Past and present support from NSF grant DMS-1716489, ERC grant CoG-866274 are acknowledged, as well as inputs from contributors listed here.

If you reuse material from this masterclass, please cite as:
Alfred Galichon, 'math+econ+code' masterclass on optimal transport and economic applications, January 2022. https://github.com/math-econ-code/mec_optim

Learning objectives

References

[B] Becker (1973). 'A Theory of Marriage: Part 1.' Journal of Political Economy.

[CS] Choo and Siow (2006). 'Who Marries Whom and Why'. Journal of Political Economy.

[MN] McCullagh and Nelder (1989). Generalized Linear Models, Second Edition. Chapman and Hall/CRC.

[COQ] Chiappori, Oreffice and Quintana-Domeque (2012). 'Fatter Attraction: Anthropometric and Socioeconomic Matching on the Marriage Market'. Journal of Political Economy.

[CSW] Chiappori, Salanié, and Weiss (2017). 'Partner Choice and the Marital College Premium'. American Economic Review.

[DG] Dupuy and Galichon (2014). 'Personality traits and the marriage market'. Journal of Political Economy.

[GS] Galichon and Salanié (2020). 'Cupid's Invisible Hand: Social Surplus and Identification in Matching Models'. Preprint (first version 2011).

Motivation: models of matching since Gary Becker

Loading our libraries

We start with loading the libraries we will need. They are rather standard.

A look at our data

The data used by Choo and Siow is census data on marriages between age categories, from age 16 (row/column 0) to age 75 (row/age 59). It is thus 60x60 tables:

The data also includes the number of single individuals per age category:

The following loads the data and rescales them appropriately:

Building the model

The analysis here follows [GS], who build on the logit model by [CS].

Optimal matching

The matching surplus between $i$ and $j$ is therefore $$\tilde{\Phi}_{ij}=\Phi_{x_{i}y_{j}}+\varepsilon_{iy_{j}}+\eta_{x_{i}j}$$ where $\Phi_{xy}=\alpha_{xy}+\gamma_{xy}$. The value of optimal matching is thus, under its dual form, \begin{align*}\min_{u_{i},v_{j}} & \sum_{i\in\mathcal{I}}u_{i}+\sum_{j\in\mathcal{J}} v_{j}\\ s.t.~ & u_{i}+v_{j}\geq\Phi_{x_{i}y_{j}}+\varepsilon_{iy_{j}}+\eta_{x_{i}j}\\ & u_{i}\geq\varepsilon_{i0}\\ & v_{j}\geq\eta_{j0} \end{align*}

Written like this, the lp has $\left\vert \mathcal{I}\right\vert +\left\vert \mathcal{J}\right\vert $ variables and $\left\vert \mathcal{I} \right\vert \times\left\vert \mathcal{J}\right\vert +\left\vert \mathcal{I} \right\vert +\left\vert \mathcal{J}\right\vert $ constraints. Assuming that there are $K$ individuals per type for each type, this is $K\left( \left\vert \mathcal{X}\right\vert +\left\vert \mathcal{Y}\right\vert \right) $ variables and $K^{2}\left( \left\vert \mathcal{X}\right\vert \times\left\vert \mathcal{Y}\right\vert \right) +K\left( \left\vert \mathcal{X}\right\vert +\left\vert \mathcal{Y}\right\vert \right) $ constraints.

The number of constraints is quadratic with respect to $K$. Fortunately, a little thinking about the implications of separability will help us reduce this complexity.

A property of equilibrium

We have:


Lemma. Consider the set $\mathcal{I}_{xy}$ of men of type $x$ matched to women of type $y$ at equilibrium. If $\mathcal{I}_{xy}$ is nonempty, then $u_{i}-\varepsilon_{iy}$ is a constant across $\mathcal{I}_{xy}$.


Proof. For $i\in\mathcal{I}$ such that $x_{i}=x$, \begin{align*} u_{i} & =\max_{j\in\mathcal{J}}\left\{ \tilde{\Phi}_{ij}-v_{j}% ,\varepsilon_{i0}\right\} \\ & =\max_{y\in\mathcal{Y}}\left\{ U_{xy}+\varepsilon_{iy},\varepsilon _{i0}\right\} \end{align*} where $U_{xy}=\max_{j:y_{j}=y}\left\{ \Phi_{xy}+\eta_{x_{i}j}-v_{j}\right\} $, thus $u_{i}\geq U_{xy}+\varepsilon_{iy}$ with equality on $\mathcal{I}% _{xy}$. With similar notations, $v_{j}\geq V_{xy}+\eta_{xj}$ with equality on $\mathcal{J}_{xy}$. As a result, if $\mathcal{I}_{xy}$ is nonempty, then $U_{xy}+V_{xy}=\Phi_{xy}$ and $\forall i\in\mathcal{I}_{xy},$ $u_{i}% =U_{xy}+\varepsilon_{iy}$.

A simplification

In the sequel, we shall see that adding an auxiliary variable to the previous lp will lead to decreasing the computational complexity of the problem.

Observe that the first set of constraints is reexpressed by saying that, for every $x\in\mathcal{X}$, $y\in\mathcal{Y}$, $$ \min_{i:x_{i}=x}\left\{ u_{i}-\varepsilon_{iy}\right\} +\min_{j:y_{j} =y}\left\{ v_{j}-\eta_{xj}\right\} \geq\Phi_{xy}. $$

Hence, letting $U_{xy}=\min_{i:x_{i}=x}\left\{ u_{i}-\varepsilon _{iy}\right\} $ and $V_{xy}=\min_{j:y_{j}=y}\left\{ v_{j}-\eta_{xj}\right\} $, a solution of the previous lp should satisfy $$ u_{i}=\max_{y\in\mathcal{Y}}\left\{ U_{xy}+\varepsilon_{iy},\varepsilon _{i0}\right\} \text{ and }v_{j}=\max_{x\in\mathcal{X}}\left\{ V_{xy} +\varepsilon_{xj},\varepsilon_{0j}\right\} . $$

The problem rewrites as \begin{align} \min_{u_{i},v_{j},U_{xy},V_{xy}} & \sum_{i\in\mathcal{I}}u_{i}+\sum _{j\in\mathcal{J}}v_{j}\label{simplifiedDual}\\ s.t.~ & U_{xy}+V_{xy}\geq\Phi_{xy}~\left[ \mu_{xy}\geq0\right] \nonumber\\ & u_{i}\geq U_{x_{i}y}+\varepsilon_{iy_{j}}~\left[ \mu_{iy}\right] \nonumber\\ & v_{j}\geq V_{xy_{j}}+\eta_{x_{i}j}~\left[ \mu_{xi}\right] \nonumber\\ & u_{i}\geq\varepsilon_{i0}~\left[ \mu_{i0}\right] \nonumber\\ & v_{j}\geq\eta_{j0}~\left[ \mu_{0x}\right] \nonumber \end{align}

This problem has $K\left( \left\vert \mathcal{X}\right\vert +\left\vert \mathcal{Y}\right\vert \right) +\left\vert \mathcal{X}\right\vert \times\left\vert \mathcal{Y}\right\vert $ variables and $\left( \left\vert \mathcal{X}\right\vert \times\left\vert \mathcal{Y}\right\vert \right) +K\left( 2\left\vert \mathcal{X}\right\vert \times\left\vert \mathcal{Y} \right\vert +\left\vert \mathcal{X}\right\vert +\left\vert \mathcal{Y} \right\vert \right) $ constraints.

The number of constraint is now linear with respect to $K$.

Consequences

1. Lagrange multipliers:

2. Utilities:

Large market limit

Now look at the limit of previous markets when the number of market participants gets large, holding fixed the frequency of each types.

In the large population limit $n_{x}$ and $m_{y}$ are now interpreted as the mass distribution of respective types $x$ and $y$.

We shall from now on assume that $\mathbf{P}_{x}$ and $\mathbf{Q}_{y}$, the distributions of random utility vectors $\left( \varepsilon_{y}\right) $ and $\left( \eta_{x}\right) $, have a density with full support. This will ensure that the Emax operators associated with the choice problems of the men and the women respectively \begin{align*} & G_x(U_{x.}) = \mathbb{E}_\mathbf{P} \left[\max_{y\in\mathcal{Y} }\left\{ U_{xy}+\varepsilon_{iy},\varepsilon_{i0}\right\} \right]\text{, and }\\ & H_y(V_{.y}) = \mathbb{E}_\mathbf{Q} \left[\max_{x\in\mathcal{X} }\left\{ V_{xy}+\eta_{xj},\eta_{0j}\right\} \right],\end{align*}as well as the corresponding entropies of choice $G_x^{\ast}$ and $H_y^{\ast}$ are continuously differentiable.

Under these assumptions, the problem becomes \begin{align*} \min_{U,V} ~& G\left( U\right) +H\left( V\right) \\ s.t.~ & U_{xy}+V_{xy}\geq\Phi_{xy}~\left[ \mu_{xy}\right] \end{align*} where \begin{align*} G\left( U\right) & =\sum_{x\in\mathcal{X}}n_{x}\mathbb{E}_{\mathbf{P}% }\left[ \max_{y\in\mathcal{Y}}\left\{ U_{xy}+\varepsilon_{iy},\varepsilon _{i0}\right\} \right] \\ H\left( V\right) & =\sum_{y\in\mathcal{Y}}m_{y}\mathbb{E}_{\mathbf{Q}% }\left[ \max_{x\in\mathcal{X}}\left\{ V_{xy}+\eta_{xj},\eta_{0j}\right\} \right] \end{align*}

By first order conditions, $$ \frac{\partial G\left( U\right) }{\partial U_{xy}}=\mu_{xy}=\frac{\partial H\left( V\right) }{\partial V_{xy}}. $$ and $\mu_{xy}>0$ for every $x\in\mathcal{X}$ and $y\in\mathcal{Y}$.

Social planner's problem

The primal problem corresponding the problem above is $$ \max_{\mu_{xy}\geq0}\sum_{\substack{x\in\mathcal{X}\\y\in\mathcal{Y}}}\mu _{xy}\Phi_{xy}-\mathcal{E}\left( \mu\right) $$ where $$ \mathcal{E}\left( \mu\right) =G^{\ast}\left( \mu\right) +H^{\ast}\left( \mu\right) $$

Recall $G^{\ast}\left( \mu\right) =\max\left\{ \sum_{xy}\mu _{xy}U_{xy}-G\left( U\right) \right\} $ is the Legendre transform of $G$, and similarly for $H^{\ast}$.

Constructing the surplus:

Denoting $a$ a generic element of

$\mathcal{A}=\mathcal{X}\times \mathcal{Y}\cup \mathcal{X}\times \left\{ 0\right\} \cup \left\{ 0\right\} \times \mathcal{Y}$ the set of matches, and
$\mathcal{K}=\left\{ 1,...,K\right\} $ the index set of the parameter,
we may view

and we define $d_{xy}=2$, $d_{x0}=d_{0y}=1$.

Let's create a class TUlogit that handles this:

Identification of the matching surplus


Theorem. By first order conditions, we get the identifcation formula of $\Phi$: $$ \Phi_{xy}=\frac{\partial G^{\ast}\left( \mu\right) }{\partial\mu_{xy}} +\frac{\partial H^{\ast}\left( \mu\right) }{\partial\mu_{xy}}. $$


This means that the surplus function is identified nonparametrically given the matching patterns $\mu$ and assuming a fixed distribution of unobserved heterogeneity.

Hence only the joint surplus $\Phi _{xy}=\alpha_{xy}+\gamma_{xy}$ is identified. However, if the transfers $\hat{w}_{xy}$ are observed too (e.g. wages in labour market), then $U_{xy}=\alpha_{xy}+w_{xy}$ and $V_{xy}=\gamma_{xy}-w_{xy}$, so that $\alpha$ and $\gamma$ are separately identified by $$ \left\{ \begin{array} [c]{c}% \hat{\alpha}_{xy}=\frac{\partial G^{\ast}\left( \mu\right) }{\partial\mu_{xy}}-\hat{w}_{xy}\\ \hat{\gamma}_{xy}=\frac{\partial H^{\ast}\left( \mu\right) }{\partial\mu_{xy}}+\hat{w}_{xy}% \end{array} \right. $$

Choo and Siow's logit model

In Choo and Siow's model [CS], the heterogeneities in tastes are Gubmel, we have $$ \mathcal{E}\left( \mu\right) =2\sum_{\substack{x\in\mathcal{X}% \\y\in\mathcal{Y}}}\mu_{xy}\log\mu_{xy}+\sum_{x\in\mathcal{X}}\mu_{x0}\log \mu_{x0}+\sum_{y\in\mathcal{Y}}\mu_{0y}\log\mu_{0y}. $$ Note that $\mathcal{E}\left( \mu\right) < + \infty$ if and only if $\mu \in\mathcal{M}\left( n,m\right) $.

By first order conditions above, Choo-Siow's TU-logit model implies the following matching function: $$ \mu_{xy}=M_{xy}\left( \mu_{x0},\mu_{0y}\right) :=\sqrt{\mu_{x0}}\sqrt {\mu_{0y}}\exp\left( \frac{\Phi_{xy}}{2}\right) $$

This is a gravity equation of sorts. The full link with gravity equations is explored in the next lecture.

As a result, $\partial\mathcal{E}\left( \mu\right) /\partial\mu _{xy}=2\log\mu_{xy}-\log\mu_{x0}-\log\mu_{0y}$, which implies that $\Phi_{xy}$ is estimated by Choo and Siow's identification formula $$ \hat{\Phi}_{xy}=\log\frac{\hat{\mu}_{xy}^{2}}{\hat{\mu}_{x0}\hat{\mu}_{0y}}. $$

Solving equilibrium in the Choo-Siow model

Write down the equilibrium equations in the TU-logit model: $$ \left\{ \begin{array} [c]{c} \sum_{y\in\mathcal{Y}}\sqrt{\mu_{x0}}\sqrt{\mu_{0y}}\exp\left( \frac {\Phi_{xy}}{2}\right) +\mu_{x0}=n_{x}\\ \sum_{x\in\mathcal{X}}\sqrt{\mu_{x0}}\sqrt{\mu_{0y}}\exp\left( \frac {\Phi_{xy}}{2}\right) +\mu_{0y}=m_{y}% \end{array} \right. $$

Setting $a_{x}=\sqrt{\mu_{x0}}$, $b_{y}=\sqrt{\mu_{0y}}$, and $K_{xy}=\exp\left( \Phi_{xy}/2\right) $, this rewrites as $$ \left\{ \begin{array} [c]{c} \sum_{y\in\mathcal{Y}}K_{xy}a_{x}b_{y}+a_{x}^{2}=n_{x}\\ \sum_{x\in\mathcal{X}}K_{xy}a_{x}b_{y}+b_{y}^{2}=m_{y}% \end{array} \right.$$

which is a variant of the equations previously seen to accomodate unmatched agents.

We can easily adapt the IPFP to this setting. The IPFP will consists in iteratively solving quadratic equations: $$ \left\{ \begin{array} [c]{l} a_{x}^{2t+1}=\sqrt{n_{x}+\left( \sum_{y\in\mathcal{Y}}b_{y}^{2t}% K_{xy}/2\right) ^{2}}-\sum_{y\in\mathcal{Y}}b_{y}^{2t}K_{xy}/2\\ b_{y}^{2t+2}=\sqrt{m_{y}+\left( \sum_{x\in\mathcal{X}}a_{x}^{2t+1}% K_{xy}/2\right) ^{2}}-\sum_{x\in\mathcal{X}}a_{x}^{2t+1}K_{xy}/2 \end{array} \right. $$

Dual problem

The dual problem is given by $$ \min_{u,v}\left\{ \begin{array} [c]{c}% \sum_{x}n_{x}u_{x}+\sum_{y}m_{y}v_{y}\\ +2\sum_{xy}\sqrt{n_{x}m_{y}}\exp\left( \frac{\Phi_{xy}-u_{x}-v_{y}}{2}\right) \\ +\sum_{x}n_{x}\exp\left( -u_{x}\right) +\sum_{y}m_{y}\exp\left( -v_{y}\right) \end{array} \right\} $$

Remarks.

DIY approach

We first try the "do-it-yourself" approach, following [GS]. Construct the objective function:

Construct its gradient:

Optimize using scipy.optimize:

GLM computation

We can reformulate the model as a generalized linear model, see [MN] for a reference on the latter.

Setting
$B= \begin{pmatrix} I_{X}\otimes 1_{Y} & 1_{X}\otimes I_{Y} \\ I_{X} & 0_{X\times Y} \\ 0_{Y\times X} & I_{Y}% \end{pmatrix}% $
and
$C=\left( -B, \Phi \right) ,$
which we code into:

Letting $\theta =\left( u,v,\lambda \right) $,
one has that the estimation problem above reformulates as

$\min_{\theta }\left\{ 1^{\top }\delta \exp \left( \delta ^{-1}\left( C\theta \right) \right) -\left( \delta \hat{\mu}\right) ^{\top }\delta ^{-1}C\theta \right\} $

and thus it results from the Poisson regression of
$Y=\hat{\mu}$ on $X=\delta ^{-1}C$ with weights vector $d$.

Poisson coerciveness

Coercivity detector:

$\max_{\mu \geq 0,u\geq 0}u$

s.t. $X^{\top }\mu +0u=b$

$I\mu -1u\geq 0$

We use the Poisson regression included in the scikit-learn library, see
https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.PoissonRegressor.html