BLP-Python provides a Python implementation of random coefficient logit model of Berry, Levinsohn and Pakes (1995). The specific implementation follows the model described in Nevo (2000b).
This code uses tight tolerances for the contraction mapping (Dube et al. 2012). With BFGS method, it quickly converges to the optimum (See Nevo (2000b) Example below).
I would like to thank Prof. Nevo and others for making their MATLAB available, which this package is originally based on. Also, I would like to thank Wingware, who generously provided a free license of WingIDE for this non-commercial open source project.
- Use global states only for read-only variables
- Avoid inverting matrices whenever possible for numerical stability
- Use a tight tolerance for the contraction mapping
- Use greek unicode symbols whenever possible for readability
- μ and individual choice probability calculations are implemented in Cython, and it is parallelized across the simulation draws via openMP
- Use n-dimensional arrays (via xarray) to represent data more naturally
- Python 3.5 (for
@operator and unicode variable names). I recommend Anaconda Python Distribution, which comes with many of the scientific libraries, as well asconda, a convenient script to install many packages. numpyandscipyfor array operations and linear algebracythonfor parallelized market share integrationxarrayfor multidimensional labeled arrays (does not come with Anaconda, install withconda install xarray)pandasfor result printing
- With git:
git clone https://github.com/joonro/BLP-Python.git
- Or you can download the master branch as a zip archive
- I include the compiled Cython module (
_BLP.cp3X-win_amd64.pyd) for Python 3.5 and 3.6 64bit, so you should be able to run the code without compiling the module in Windows. You have to compile it if you want to change the Cython module (_BLP.pyx) or if you are on GNU/Linux or Mac OS. GNU/Linux distributions come withgccso it should be straightforward to compile the module. cdinto theBLP-Pythondirectory, and compile the cython module with the following command:python setup.py build_ext --inplace
- For Windows users, to compile the cython module with the openMP (parallelization) support with 64-bit Python, you have to install Microsoft Visual C++ compiler following instructions at https://wiki.python.org/moin/WindowsCompilers. For Python 3.5 and 3.6, you either install Microsoft Visual C++ 14.0 standalone, or you can install Visual Studio 2015 which contains Visual C++ 14.0 compiler.
examples/Nevo_2000b.py replicates the results from Nevo (2000b). In the examples folder, you can run the script as:
python ./Nevo_2000b.pyIt evaluates the objective function at the starting values and creates the following results table:
MeanSDIncomeIncome^2AgeChildConstant-1.8332940.3772003.0888000.0000001.1859000.000000.2578290.1294331.2126470.0000001.0123540.00000Price-32.4469221.84800016.598000-0.6590000.00000011.624507.7519131.078371172.7761108.9792570.0000005.20593Sugar0.142915-0.003500-0.1925000.0000000.0296000.000000.0128770.0122970.0455280.0000000.0365630.00000Mushy0.8016080.0810001.4684000.000000-1.5143000.000000.2034540.2060250.6978630.0000001.0983210.00000GMMobjective: 14.900789417017275Min-DistR-squared: 0.2718388379589566Min-DistweightedR-squared: 0.0946528053333926This code uses a tight tolerance for the contraction mapping, and it minimizes the GMM objective function to the correct minimum of 4.56. (With BFGS, it only needs 45 iterations).
After running the code, you can try the full estimation with:
BLP.estimate(θ20=θ20)For example, in an IPython console:
%runNevo_2000b.pyBLP.estimate(θ20=θ20)You should get the following results:
Optimizationterminatedsuccessfully. Currentfunctionvalue: 4.561515Iterations: 45Functionevaluations: 50Gradientevaluations: 50MeanSDIncomeIncome^2AgeChildConstant-2.0099190.5580942.2919720.0000001.2844320.0000000.3269970.1625331.2085690.0000000.6312150.000000Price-62.7299023.312489588.325237-30.1920210.00000011.05462714.8032151.340183270.44102114.1012300.0000004.122563Sugar0.116257-0.005784-0.3849540.0000000.0522340.0000000.0160360.0135050.1214580.0000000.0259850.000000Mushy0.4993730.0934140.7483720.000000-1.3533930.0000000.1985820.1854330.8021080.0000000.6671080.000000GMMobjective: 4.5615146550344186Min-DistR-squared: 0.4591043336106454Min-DistweightedR-squared: 0.10116438381046189You can check the gradient at the optimum:
>>>BLP._gradient_GMM(BLP.results['θ2']['x']) contractionmappingfinishedin0iterationsarray([ 1.23888940e-07, 1.15056001e-08, 1.58824491e-08, -4.45649242e-08, -9.61452074e-08, -1.75233503e-08, -9.94539619e-07, 9.60900497e-08, -3.30553299e-07, 1.24174991e-07, 4.17569410e-07, 1.33642515e-07, 1.94273594e-09])I verified that the optimum is achieved with Nelder-Mead (simplex), BFGS, TNC, and SLSQP=scipy.optimize= methods. BFGS and SLSQP were the fastest, and BFGS is the default.
I use pytest for unit testing. You can run them with:
python-mpytestBerry, S., Levinsohn, J., & Pakes, A. (1995). Automobile Prices In Market Equilibrium. Econometrica, 63(4), 841.
Dubé, J., Fox, J. T., & Su, C. (2012). Improving the Numerical Performance of BLP Static and Dynamic Discrete Choice Random Coefficients Demand Estimation. Econometrica, 1–34.
Nevo, A. (2000). A Practitioner’s Guide to Estimation of Random-Coefficients Logit Models of Demand. Journal of Economics & Management Strategy, 9(4), 513–548.
BLP-Python is released under the GPLv3.
- Change data structure to
xarray. - Major improvements on various aspects of the code.
- Fix
setup.pyfor the Cython module for non-windows operating systems (thanks to Cheng Nie)
- Use global state only for read-only variables; now gradient-based optimization (such as BFGS) works and it converges quickly
- Use pandas.DataFrame to show results cleanly
- Implement estimation of parameter means
- Implement standard error calculation
- Use greek letters whenever possible
- Add Nevo (2000b) example
- Add a unit test
- Improve README
- Implement GMM objective function and estimation of \( θ2 \)
- Initial release