# MPEC Code

## MATLAB Code for BLP’s GMM estimator of the Random Coefficients Logit using the MPEC algorithm

#### Updated February 2012

**If you use the code for your research, kindly use the reference: **Jean-Pierre Dubé, Jeremy Fox and Che-Lin Su (2012), "Improving the Numerical Performance of BLP Static and Dynamic Discrete Choice Random Coefficients Demand Estimation," *Econometrica*, 2012, 80(5).

**Zip files with all downloadable code**:

**MATLAB and TOMLAB:**MPEC_tomlab_Hessian.zip**MATLAB and ktrlink:**MPEC_ktrlink_Hessian.zip

## Overview of Code

The script file, main_MPEC_hessian.m, runs the following steps:

- 1. Creation of a synthetic data set
- 2. Estimation of Homogenous Logit via two-stage least squares (which we use to formulate starting values for MPEC)
- 3. Estimation of Random Coefficients Logit via GMM using the MPEC algorithm
- 4. Calculation of the standard errors for the MPEC estimates
- 5. To increase the number of simulation draws use to evaluate the integrals in the market shares, please edit the variable "
*nn"*, in main_MPEC_hessian.m. The default settings in the code are too low for published work.

The code uses KNITRO. KNITRO can now be called directly from the MATLAB Optimization Toolbox using *ktrlink*. Alternatively, KNITRO can be called in MATLAB using the TOMLAB interface, a 3rd-party optimization toolbox that enables calling state-of-the-art solvers such as KNITRO and SNOPT. To use KNITRO with MATLAB’s Optimization Toolbox, you must purchase a KNITRO license directly from Ziena or Artelys. If you use the TOMLAB interface, then a KNITRO license (along with a TOMLAB license) must be purchased directly from TOMLAB Optimization.

## Implementing MPEC

To see the exact syntax used to call KNITRO through TOMLAB, please refer to the function runGMMMPECTOMLAB.m. The code is able to handle a reasonably large number of products and markets, and hence constraints. This fact is due primarily to the fact that the MPEC optimization problem (for BLP) is sparse. An important step in using the constrained optimization algorithm is to specify the sparsity pattern of the constraints. The use of sparse matrix representation reduces memory concerns considerably.

In addition, we supply the __analytic gradients__ of the MPEC objective function, GMMMPEC_grad.m, and of the constraints, GMMMPEC_dc.m, as well as the __Hessian__ of the Lagrangian, GMMMPEC_hess.m, and of the constraints, GMMMPEC_d2c.m. The performance of the optimization routine is substantially improved when this additional information is supplied. Unless otherwise specified, we used analytic gradients and Hessians to produce all the results reported in the paper.

Please note that the results in the paper were produced using the KNITRO solver, version 6. KNITRO recently released version 7.0.0. In our experience, the new add-in linear equation solvers in Knitro 7.0.0 do not perform as well for BLP demand estimation as the linear equation solver from version 6. Therefore, we recommend using the following options when using KNITRO (7.0 or above):

**blasoption**** 0**

**linsolver**** 4**

**bar_directinterval**** 100000**

Please refer to the Knitro manuals for a more detailed explanation of these settings.

## Very Large-Scale Problems

For models with more than 5,000 markets share equations, users need to supply the constraint Jacobian, Hessian and their respective sparsity patterns in sparse matrix format. Sparse matrices are critical to economize on memory usage. We have successfully solved instances of BLP models with 30,000 market share equations (150 markets and 20 products) using 500 MB RAM and 50,000 market share equations (250 markets and 20 products) using 1.5 GB RAM. Please contact the authors if you would like to request the MATLAB code for solving large-scale BLP models.

Materials provided are for **Educational Use Only**. All articles are the sole Copyright of their respective publishers.