# 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.

 Formulation(formula[, absorb, absorb_method]) Configuration for designing matrices and absorbing fixed effects. Integration(specification, size[, seed]) Configuration for building integration nodes and weights. Iteration(method[, method_options, …]) Configuration for solving fixed point problems. Optimization(method[, method_options, …]) Configuration for solving optimization problems.

## Data Construction Functions¶

There are also a number of convenience functions that can be used to construct common components of product and agent data.

 build_matrix(formulation, data) Construct a matrix according to a formulation. build_blp_instruments(formulation, product_data) Construct traditional excluded BLP instruments. Construct excluded differentiation instruments. build_id_data(T, J, F) Build a balanced panel of market and firm IDs. build_ownership(product_data[, …]) Build ownership matrices, $$O$$. build_integration(integration, dimensions) Build nodes and weights for integration over agent choice probabilities.

## Simulation Class¶

In addition to reading from data files, data can be simulated by initializing the following class.

 Simulation(product_formulations, beta, …) Simulation of synthetic data from BLP-type models.

Once initialized, the following method computes equilibrium prices and shares.

 Simulation.solve([firm_ids, ownership, …]) Compute synthetic prices and shares.

## Simulation Results Class¶

Solved simulations return the following results class.

 SimulationResults Results of a solved simulation of synthetic BLP data.

The simulation results can be converted into a Problem with the following method.

 Convert the solved simulation into a problem.

## Problem Class¶

Given real or simulated data and appropriate configurations, the BLP problem can be structured by initializing the following class.

 Problem(product_formulations, product_data) A BLP-type problem.

Once initialized, the following method solves the problem.

 Problem.solve([sigma, pi, rho, beta, gamma, …]) Solve the problem.

## Problem Results Class¶

Solved problems return the following results class.

 ProblemResults Results of a solved BLP problem.

In addition to class attributes, other post-estimation outputs can be estimated with the following methods, which each 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 diversion ratios, $$\mathscr{D}$$, with respect to a variable, $$x$$. Estimate matrices of long-run diversion ratios, $$\bar{\mathscr{D}}$$. 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 marginal costs, $$c$$. Approximate equilibrium prices after firm or cost changes, $$p^*$$, under the assumption that shares and their price derivatives are unaffected by such changes. ProblemResults.compute_prices([firm_ids, …]) Estimate equilibrium prices after firm or cost changes, $$p^*$$. ProblemResults.compute_shares([prices]) Estimate shares evaluated at specified prices. ProblemResults.compute_hhi([firm_ids, shares]) Estimate Herfindahl-Hirschman Indices, $$\text{HHI}$$. ProblemResults.compute_markups([prices, costs]) Estimate markups, $$\mathscr{M}$$. ProblemResults.compute_profits([prices, …]) 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 for the above post-estimation outputs. The following method returns a results class with all of the above methods, which returns a distribution of post-estimation outputs corresponding to different bootstrapped samples.

 ProblemResults.bootstrap([draws, seed, …]) 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^\textit{Opt}$$ and $$Z_S^\textit{Opt}$$.

## Boostrapped Problem Results Class¶

Parametric bootstrap computation returns the following class.

 BootstrappedResults Bootstrapped results of a solved problem.

This class has all of the same methods as ProblemResults, except for ProblemResults.bootstrap() and ProblemResults.compute_optimal_instruments().

## Optimal Instrument Results Class¶

Optimal instrument computation returns the following results class.

 OptimalInstrumentResults Results of optimal instrument computation.

The optimal instrument results can 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.

 OptimalInstrumentProblem A BLP problem updated with optimal excluded instruments.

## 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.

 Products Product data structured as a record array. Agents 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.

 parallel(processes) Context manager used for parallel processing in a with statement context.

## 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.

 options Global options. data 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.

 MultipleErrors Multiple errors that occurred around the same time. NonpositiveCostsError Encountered nonpositive marginal costs in a log-linear specification. InvalidParameterCovariancesError Failed to compute standard errors because of invalid estimated covariances of GMM parameters. InvalidMomentCovariancesError Failed to compute a weighting matrix because of invalid estimated covariances of GMM moments. DeltaFloatingPointError Encountered floating point issues when computing $$\delta$$. XiByThetaJacobianFloatingPointError Encountered floating point issues when computing the Jacobian of $$\xi$$ (equivalently, of $$\delta$$) with respect to $$\theta$$. CostsFloatingPointError Encountered floating point issues when computing marginal costs. OmegaByThetaJacobianFloatingPointError Encountered floating point issues when computing the Jacobian of $$\omega$$ (equivalently, of transformed marginal costs) with respect to $$\theta$$. SyntheticPricesFloatingPointError Encountered floating point issues when computing synthetic prices. SyntheticSharesFloatingPointError Encountered floating point issues when computing synthetic shares. EquilibriumPricesFloatingPointError Encountered floating point issues when computing equilibrium prices. EquilibriumSharesFloatingPointError Encountered floating point issues when computing equilibrium shares. AbsorptionConvergenceError An iterative de-meaning procedure failed to converge when absorbing fixed effects. ThetaConvergenceError The optimization routine failed to converge. DeltaConvergenceError The fixed point computation of $$\delta$$ failed to converge. SyntheticPricesConvergenceError The fixed point computation of synthetic prices failed to converge. EquilibriumPricesConvergenceError The fixed point computation of equilibrium prices failed to converge. ObjectiveReversionError Reverted a problematic GMM objective value. GradientReversionError Reverted problematic elements in the GMM objective gradient. DeltaReversionError Reverted problematic elements in $$\delta$$. CostsReversionError Reverted problematic marginal costs. XiByThetaJacobianReversionError Reverted problematic elements in the Jacobian of $$\xi$$ (equivalently, of $$\delta$$) with respect to $$\theta$$. OmegaByThetaJacobianReversionError Reverted problematic elements in the Jacobian of $$\omega$$ (equivalently, of transformed marginal costs) with respect to $$\theta$$. AbsorptionInversionError Failed to invert the $$A$$ matrix from Somaini and Wolak (2016) when absorbing two-way fixed effects. HessianEigenvaluesError Failed to compute eigenvalues for the GMM objective’s Hessian matrix. FittedValuesInversionError Failed to invert an estimated covariance when computing fitted values. SharesByXiJacobianInversionError Failed to invert a Jacobian of shares with respect to $$\xi$$ when computing the Jacobian of $$\xi$$ (equivalently, of $$\delta$$) with respect to $$\theta$$. IntraFirmJacobianInversionError Failed to invert an intra-firm Jacobian of shares with respect to prices when computing $$\eta$$. LinearParameterCovariancesInversionError Failed to invert an estimated covariance matrix of linear parameters. GMMParameterCovariancesInversionError Failed to invert an estimated covariance matrix of GMM parameters. GMMMomentCovariancesInversionError Failed to invert an estimated covariance matrix of GMM moments.