jeffgortmaker / pyblp

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

Finding it difficult to understand micro moments syntax #46

Closed bshiller closed 4 years ago

bshiller commented 4 years ago

Hi Jeff,

I was wondering if you wouldn't mind helping me understand the syntax for adding micro moments. I tried this (as well as a number of other attempts at figuring out the syntax):

Micro_formulations = pyblp.FirstChoiceCovarianceMoment(1, 1, 0.45744) results1 = pr_problem.solve(...,micro_moments=(Micro_formulations))

and got the error:

File "", line 2, in results1 = pr_problem.solve(initial_sigma1,initial_pi1,sigma_bounds=sigma_bounds, pi_bounds = pi_bounds, method='1s',iteration=iteration,W_type='clustered',se_type='clustered',optimization=optimization,micro_moments=(Micro_formulations))

File "C:\Users\shiller.USERS\AppData\Local\Continuum\anaconda3\lib\site-packages\pyblp-0.8.1-py3.6.egg\pyblp\economies\problem.py", line 441, in solve moments = EconomyMoments(self, micro_moments)

File "C:\Users\shiller.USERS\AppData\Local\Continuum\anaconda3\lib\site-packages\pyblp-0.8.1-py3.6.egg\pyblp\moments.py", line 183, in init raise TypeError("micro_moments must be a sequence of micro moment instances.")

TypeError: micro_moments must be a sequence of micro moment instances.

Also, one other quick question. Is the demographics_index in the micro moments formulation the column of agent data loaded from a CSV (where the first column might identify market) or the index of the agent formulation?

Thanks!

chrisconlon commented 4 years ago

We need to do a better job with the tutorials for the micro-moments interface.

It should look something like this:

the key is that the micro-moments are stored in a tuple rather than a list.

mkt_ids=df_product.market_ids.unique() mm1=pyblp.FirstChoiceCovarianceMoment(1, 1, 0.4, mkt_ids) mm2=pyblp.FirstChoiceCovarianceMoment(0, 1, 0.3 ,mkt_ids) all_mm =(mm1,mm2)

problem.solve( ... micro_moments= all_mm, ... )

jeffgortmaker commented 4 years ago

Also: the issue with the original code is that (x) doesn't define a single-element tuple in Python -- you need (x,) (note the comma) or to get a single-element list, you can do [x].

And the demographics_index is the index in the agent formulation. If you print your problem after you initialize with micro moments, it should output some information where you can confirm that you've set it up right.

bshiller commented 4 years ago

Thanks Chris!

On Fri, Feb 7, 2020 at 12:46 PM Chris Conlon notifications@github.com wrote:

We need to do a better job with the tutorials for the micro-moments interface.

It should look something like this:

the key is that the micro-moments are stored in a tuple rather than a list.

mkt_ids=df_product.market_ids.unique() mm1=pyblp.FirstChoiceCovarianceMoment(1, 1, 0.4, mkt_ids) mm2=pyblp.FirstChoiceCovarianceMoment(0, 1, 0.3 ,mkt_ids) all_mm =(mm1,mm2)

problem.solve( sigma=init_sigma,pi=init_pi, micro_moments= our_mm, )

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jeffgortmaker/pyblp/issues/46?email_source=notifications&email_token=AEYDVKPXFJ2BQHKGK3LZVITRBWNAXA5CNFSM4KRSCN3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELD54OY#issuecomment-583523899, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEYDVKNYNE62WKDIBQWED3DRBWNAXANCNFSM4KRSCN3A .

-- Benjamin Shiller Assistant Professor Brandeis University

bshiller commented 4 years ago

Thanks Jeff!

On Fri, Feb 7, 2020 at 1:42 PM Jeff Gortmaker notifications@github.com wrote:

Also: the issue with the original code is that (x) doesn't define a single-element tuple in Python -- you need (x,) (note the comma) or to get a single-element list, you can do [x].

And the demographics_index is the index in the agent formulation. If you print your problem after you initialize with micro moments, it should output some information where you can confirm that you've set it up right.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jeffgortmaker/pyblp/issues/46?email_source=notifications&email_token=AEYDVKMLPG6T5M64RJEZ6HLRBWTRPA5CNFSM4KRSCN3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELED4QI#issuecomment-583548481, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEYDVKLQVL422PTY2KEW5I3RBWTRPANCNFSM4KRSCN3A .

-- Benjamin Shiller Assistant Professor Brandeis University

clukewatson commented 4 years ago

Posting here given the topic, but I can move it to its own if best.

I've not gotten too much into the weeds on micro-moments (in theory or in PyBLP), but how would I use PyBLP to implement something akin to Petrin's conditional moments? It does not seem obvious to me that one could. There is no equation number but page 714 of the JPE article has his moments; e.g., E[ agent i buys new vehicle | y_i < y1 ] or E[ f_i | i buys minivan ].

Maybe I am just not understanding the micro moments? One issue is that pyblp.DemographicExpectationMoment seems to be product-by-product and pyblp.DemographicCovarianceMoment seems like it is a different moment type.

chrisconlon commented 4 years ago

Without getting into too much detail:

  1. Jeff and I are actively working on expanding micro-moment coverage.
  2. We've looked at Petrin (2002) and microBLP(2004) pretty carefully and we're trying to implement those kinds of moments.
  3. DemographicExpectationMoment seems to be what you want for like E[kids_i | i buys minivan].
  4. We don't have inequality restrictions in the moments right now -- to do that correctly would require an additional layer of numerical integration (Over the income distribution for example).
  5. Our early simulation results suggest that DemographicCovarianceMoments are usually what you're going to want assuming you can calculate them.
  6. There used to be a way to do unconditional on purchase probability to get inside share vs. outside good for the covariance -- but it looks like that option was eliminated...? 6a. You can do DemographicExpectationMoment(None,demog_id) and you will get say the expected income of all new car purchasers.
  7. The documentation for micro-moments is very much a work in progress. There is a reason they didn't make it into the RAND paper(!).

On Wed, Jul 15, 2020 at 8:34 PM C.Luke Watson notifications@github.com wrote:

Posting here given the topic, but I can move it to its own if best.

I've not gotten too much into the weeds on micro-moments (in theory or in PyBLP), but how would I use PyBLP to implement something akin to Petrin's conditional moments? It does not seem obvious to me that one could. There is no equation number but page 714 of the JPE article has his moments; e.g., E[ agent i buys new vehicle | y_i < y1 ] or E[ f_i | i buys minivan ].

Maybe I am just not understanding the micro moments? One issue is that pyblp.DemographicExpectationMoment seems to be product-by-product and pyblp.DemographicCovarianceMoment seems like it is a different moment type.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jeffgortmaker/pyblp/issues/46#issuecomment-659085798, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7IOWPQTKYIPXWIHYWE5ULR3ZDHVANCNFSM4KRSCN3A .

jeffgortmaker commented 4 years ago

I'll add a couple things to what Chris said:

  1. You should be able to encode most types of "product-agent" information into DemographicCovarianceMoments by adding extra variables to your agent_formulation and to your formulation for X2. (Note that including such extra variables does not require extra parameters in sigma or pi -- you can set these to zero.) For example, you can think of "agent i buys new vehicle" as a constant column in X2, and "y_i < y1" as a dummy variable in your matrix of demographics. Covariances between such characteristics can provide many types of micro moments.

  2. I think in points 6 and 6a, Chris is referring to an older API, e.g. the classes mentioned at the top of this thread are outdated. As Chris mentioned we're still working on this stuff, so the API is subject to change again (and any comments on it are really appreciated). With the current API, setting product_id=None for DemographicExpectationMoment means that you're matching a demographic for purchasers of the outside good, not of all inside goods. In my mind this is the primary purpose (other than convenience for some moments) of using DemographicExpectationMoments instead of DemographicCovarianceMoments, the latter of which is always computed conditional on the purchase of an inside good.