API Documentation¶
The majority of the package consists of classes, which compartmentalize different aspects of the BLP model. There are some convenience functions as well.
Configuration Classes¶
Various components of the package require configurations for how to approximate integrals, solve fixed point problems, and solve optimimzation problems. Such configurations are specified with the following classes.
|
Configuration for designing matrices and absorbing fixed effects. |
|
Configuration for building integration nodes and weights. |
|
Configuration for solving fixed point problems. |
|
Configuration for solving optimization problems. |
Custom optimization configurations can be used to help debug optimization, to define non-standard optimization routines, or to add ad-hoc moments to configured problems. They can use various information about optimization progress so far.
Information about the current progress of optimization. |
Data Manipulation Functions¶
There are also a number of convenience functions that can be used to construct common components of product and agent data, or manipulate other PyBLP objects.
|
Construct a matrix according to a formulation. |
|
Construct “sums of characteristics” excluded BLP instruments. |
Construct excluded differentiation instruments. |
|
|
Build a balanced panel of market and firm IDs. |
|
Build ownership matrices, \(O\). |
|
Build nodes and weights for integration over agent choice probabilities. |
|
Convert a NumPy record array into a dictionary. |
|
Save an object as a pickle file. |
|
Load a pickled object into memory. |
Problem Class¶
Given data and appropriate configurations, a BLP-type problem can be structured by initializing the following class.
|
A BLP-type problem. |
Once initialized, the following method solves the problem.
|
Solve the problem. |
Micro Moment Classes¶
Micro dataset configurations are passed to micro part configurations, which are passed to micro moment configurations, which in turn can be passed to Problem.solve()
.
|
Configuration for a micro dataset \(d\) on which micro moments are computed. |
|
Configuration for a micro moment part \(p\). |
|
Configuration for a micro moment \(m\). |
Problem Results Class¶
Solved problems return the following results class.
Results of a solved BLP problem. |
The results can be pickled or converted into a dictionary.
|
Save these results as a pickle file. |
|
Convert these results into a dictionary that maps attribute names to values. |
The following methods test the validity of overidentifying and model restrictions.
Test the validity of overidentifying restrictions with the Hansen \(J\) test. |
|
|
Test the validity of model restrictions with the distance test. |
Test the validity of model restrictions with the Lagrange multiplier test. |
|
|
Test the validity of model restrictions with the Wald test. |
In addition to class attributes, other post-estimation outputs can be estimated market-by-market with the following methods, each of which return an array.
Estimate aggregate elasticities of demand, \(\mathscr{E}\), with respect to a variable, \(x\). |
|
|
Estimate matrices of elasticities of demand, \(\varepsilon\), with respect to a variable, \(x\). |
Estimate matrices of derivatives of demand with respect to a variable, \(x\). |
|
Estimate arrays of second derivatives of demand with respect to a variable, \(x\). |
|
Estimate arrays of second derivatives of profits with respect to a prices. |
|
Estimate matrices of diversion ratios, \(\mathscr{D}\), with respect to a variable, \(x\). |
|
Estimate matrices of long-run diversion ratios, \(\bar{\mathscr{D}}\). |
|
Estimate matrices of choice probabilities. |
|
|
Extract diagonals from stacked \(J_t \times J_t\) matrices for each market \(t\). |
|
Extract means of diagonals from stacked \(J_t \times J_t\) matrices for each market \(t\). |
|
Estimate mean utilities, \(\delta\). |
|
Estimate marginal costs, \(c\). |
Estimate matrices of passthrough of marginal costs to equilibrium prices, \(\Upsilon\). |
|
Approximate equilibrium prices after firm or cost changes, \(p^*\), under the assumption that shares and their price derivatives are unaffected by such changes. |
|
|
Estimate equilibrium prices after firm or cost changes, \(p^*\). |
|
Estimate shares. |
|
Estimate Herfindahl-Hirschman Indices, \(\text{HHI}\). |
|
Estimate markups, \(\mathscr{M}\). |
|
Estimate population-normalized gross expected profits, \(\pi\). |
Estimate population-normalized consumer surpluses, \(\text{CS}\). |
A parametric bootstrap can be used, for example, to compute standard errors forpost-estimation outputs. The following method returns a results class with the same methods in the list directly above, which returns a distribution of post-estimation outputs corresponding to different bootstrapped samples.
|
Use a parametric bootstrap to create an empirical distribution of results. |
Optimal instruments, which also return a results class instead of an array, can be estimated with the following method.
Estimate feasible optimal or efficient instruments, \(Z_D^\text{opt}\) and \(Z_S^\text{opt}\). |
Importance sampling can be used to create new integration nodes and weights. Its method also returns a results class.
|
Use importance sampling to construct nodes and weights for integration. |
The following methods can compute micro moment values, compute scores from micro data, or simulate such data.
Estimate micro moment values, \(f_m(v)\). |
|
|
Compute scores for observations \(n \in N_d\) from a micro dataset \(d\). |
|
Compute scores for all agent-choices, treated as observations \(n \in N_d\) from a micro dataset \(d\). |
|
Simulate observations \(n \in N_d\) from a micro dataset \(d\). |
Bootstrapped Problem Results Class¶
Parametric bootstrap computation returns the following class.
Bootstrapped results of a solved problem. |
This class has many of the same methods as ProblemResults()
. It can also be pickled or converted into a dictionary.
Save these results as a pickle file. |
|
|
Convert these results into a dictionary that maps attribute names to values. |
Optimal Instrument Results Class¶
Optimal instrument computation returns the following results class.
Results of optimal instrument computation. |
The results can be pickled or converted into a dictionary.
Save these results as a pickle file. |
|
|
Convert these results into a dictionary that maps attribute names to values. |
They can also be converted into a Problem
with the following method.
Re-create the problem with estimated feasible optimal instruments. |
This method returns the following class, which behaves exactly like a Problem
.
A BLP problem updated with optimal excluded instruments. |
Importance Sampling Results Class¶
Importance sampling returns the following results class:
Results of importance sampling. |
The results can be pickled or converted into a dictionary.
Save these results as a pickle file. |
|
|
Convert these results into a dictionary that maps attribute names to values. |
They can also be converted into a Problem
with the following method.
Re-create the problem with the agent data constructed from importance sampling. |
This method returns the following class, which behaves exactly like a Problem
.
A BLP problem updated after importance sampling. |
Simulation Class¶
The following class allows for evaluation of more complicated counterfactuals than is possible with ProblemResults
methods, or for simulation of synthetic data from scratch.
|
Simulation of data in BLP-type models. |
Once initialized, the following method replaces prices and shares with equilibrium values that are consistent with true parameters.
|
Replace simulated prices and market shares with equilibrium values that are consistent with true parameters. |
A less common way to solve the simulation is to assume simulated prices and shares represent and equilibrium and to replace exogenous variables instead.
|
Replace exogenous product characteristics with values that are consistent with true parameters. |
Simulation Results Class¶
Solved simulations return the following results class.
Results of a solved simulation of synthetic BLP data. |
This class has many of the same methods as ProblemResults
. It can also be pickled or converted into a dictionary.
Save these results as a pickle file. |
|
|
Convert these results into a dictionary that maps attribute names to values. |
It can also be converted into a Problem
with the following method.
Convert the solved simulation into a problem. |
Structured Data Classes¶
Product and agent data that are passed or constructed by Problem
and Simulation
are structured internally into classes with field names that more closely resemble BLP notation. Although these structured data classes are not directly constructable, they can be accessed with Problem
and Simulation
class attributes. It can be helpful to compare these structured data classes with the data or configurations used to create them.
Product data structured as a record array. |
|
Agent data structured as a record array. |
Multiprocessing¶
A context manager can be used to enable parallel processing for methods that perform market-by-market computation.
|
Context manager used for parallel processing in a |
Options and Example Data¶
In addition to classes and functions, there are also two modules that can be used to configure global package options and locate example data that comes with the package.
Global options. |
|
Locations of example data that are included in the package for convenience. |
Exceptions¶
When errors occur, they will either be displayed as warnings or raised as exceptions.
Multiple errors that occurred around the same time. |
|
Encountered nonpositive marginal costs in a log-linear specification. |
|
Encountered nonpositive synthetic marginal costs in a log-linear specification. |
|
Failed to compute standard errors because of invalid estimated covariances of GMM parameters. |
|
Failed to compute a weighting matrix because of invalid estimated covariances of GMM moments. |
|
Encountered a numerical error. |
|
Encountered a numerical error when computing \(\delta\). |
|
Encountered a numerical error when computing marginal costs. |
|
Encountered a numerical error when computing micro moments. |
|
Encountered a numerical error when computing the Jacobian (holding \(\beta\) fixed) of \(\xi\) (equivalently, of \(\delta\)) with respect to \(\theta\). |
|
Encountered a numerical error when computing the Jacobian (holding \(\gamma\) fixed) of \(\omega\) (equivalently, of transformed marginal costs) with respect to \(\theta\). |
|
Encountered a numerical error when computing the Jacobian of micro moments with respect to \(\theta\). |
|
Encountered a numerical error when computing micro moment covariances. |
|
Encountered a numerical error when computing synthetic prices. |
|
Encountered a numerical error when computing synthetic shares. |
|
Encountered a numerical error when computing the synthetic \(\delta\). |
|
Encountered a numerical error when computing synthetic marginal costs. |
|
Encountered a numerical error when computing synthetic micro data. |
|
Encountered a numerical error when computing synthetic micro moments. |
|
Encountered a numerical error when computing micro scores. |
|
Encountered a numerical error when solving for a realization of equilibrium prices and shares. |
|
Encountered a numerical error when computing a realization of the Jacobian (holding \(\beta\) fixed) of \(\xi\) (equivalently, of \(\delta\)) or \(\omega\) (equivalently, of transformed marginal costs) with respect to \(\theta\). |
|
Encountered a numerical error when computing a post-estimation output. |
|
A fixed effect absorption procedure failed to properly absorb fixed effects. |
|
Shares were clipped during the final iteration of the fixed point routine for computing \(\delta\). |
|
The optimization routine failed to converge. |
|
The fixed point computation of \(\delta\) failed to converge. |
|
The fixed point computation of synthetic prices failed to converge. |
|
The fixed point computation of the synthetic \(\delta\) failed to converge. |
|
The fixed point computation of equilibrium prices failed to converge. |
|
Reverted a problematic GMM objective value. |
|
Reverted problematic elements in the GMM objective gradient. |
|
Reverted problematic elements in \(\delta\). |
|
Reverted problematic marginal costs. |
|
Reverted problematic micro moments. |
|
Reverted problematic elements in the Jacobian (holding \(\beta\) fixed) of \(\xi\) (equivalently, of \(\delta\)) with respect to \(\theta\). |
|
Reverted problematic elements in the Jacobian (holding \(\gamma\) fixed) of \(\omega\) (equivalently, of transformed marginal costs) with respect to \(\theta\). |
|
Reverted problematic elements in the Jacobian of micro moments with respect to \(\theta\). |
|
Failed to compute eigenvalues for the GMM objective’s (reduced) Hessian matrix. |
|
Failed to compute eigenvalues for a firm’s profit Hessian. |
|
Failed to invert an estimated covariance when computing fitted values. |
|
Failed to invert a Jacobian of shares with respect to \(\xi\) when computing the Jacobian (holding \(\beta\) fixed) of \(\xi\) (equivalently, of \(\delta\)) with respect to \(\theta\). |
|
Failed to invert an intra-firm Jacobian of shares with respect to prices. |
|
Failed to invert the matrix to recover the passthrough matrix. |
|
Failed to invert an estimated covariance matrix of linear parameters. |
|
Failed to invert an estimated covariance matrix of GMM parameters. |
|
Failed to invert an estimated covariance matrix of GMM moments. |