jeffgortmaker / pyblp

BLP Demand Estimation with Python
https://pyblp.readthedocs.io
MIT License
240 stars 83 forks source link

Error estimating nested logit (and RCNL) with group-specific rho #76

Closed kevindanray closed 3 years ago

kevindanray commented 3 years ago

I am attempting to run Nested Logit and RCNL on data with groups of 3 or more. I am able to successfully execute the group-rho code for the fake cereal data with problem.solve(rho = [0.7, 0.7]). But with my data when I include the required number of elements in rho it returns an IndexError. Here is the last bit of the traceback.

~\Anaconda3\lib\site-packages\pyblp\parameters.py in get_group_associations(self, groups) 101 """Get an indicator for the group associated with the parameter.""" 102 group_associations = np.zeros((groups.group_count, 1), options.dtype) --> 103 group_associations[self.location] = 1 104 return group_associations 105

IndexError: index 2 is out of bounds for axis 0 with size 2

kevindanray commented 3 years ago

It does not appear to be explained by the number of elements in the group...I was able to run it for the fake cereal data nested by sugar content as a way to test it with more nests. It seems to be specific to my data/problem.

df3 = product_data.copy() df3['nesting_ids'] = df3['sugar'] groups = df3.groupby(['market_ids', 'nesting_ids']) df3['demand_instruments21'] = groups['shares'].transform(np.size) nl_formulation = pyblp.Formulation('0 + prices') problem = pyblp.Problem(nl_formulation, df3) nl_results3 = problem.solve(rho = [0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9])

jeffgortmaker commented 3 years ago

Thanks for this! I managed to replicate the bug in a unit test. The issue shows up when there are fewer groups in a market than there are across all markets. I'm going to work on a fix.

kevindanray commented 3 years ago

Thank you!

jeffgortmaker commented 3 years ago

For sure! I pushed a commit that should solve the problem. Can you try again with the dev version of the code?

You'll need to clone this repo and import pyblp from there. If you installed the current version with pip, you'll probably have to run pip uninstall pyblp.

kevindanray commented 3 years ago

Sure. But I'll need instructions on cloning and importing the dev version.

jeffgortmaker commented 3 years ago

Three steps (hopefully):

  1. pip uninstall pyblp.
  2. git clone https://github.com/jeffgortmaker/pyblp in some local directory, assuming you have git installed. For example, I have a repos directory, and after running the above command in that directory, I have a pyblp subdirectory.
  3. Add that pyblp subdirectory to your PYTHONPATH environment variable. For example, mine looks like PYTHONPATH=.../repos/pyblp where ... is replaced with the parent directories of the repos directory.

The last step should allow you to import pyblp anywhere, including in notebooks. You'll have to restart any running notebooks/python processes for these changes to take effect.

kevindanray commented 3 years ago

It is indeed working now. Thank you for a speedy fix.