pyblp.MicroPart

class pyblp.MicroPart(name, dataset, compute_values)

Configuration for a micro moment part \(p\).

Each micro moment part \(p\) is defined by its dataset \(d_p\) and micro values \(v_{pijt}\), which are used in (35) and (36). For example, a micro moment part \(p\) with \(v_{pijt} = y_{it} x_{jt}\) yields the mean \(\bar{v}_p\) or expectation \(v_p\) of an interaction between some demographic \(y_{it}\) and product characteristic \(x_{jt}\).

See Conlon and Gortmaker (2023) for a more in-depth discussion of the standardized framework used by PyBLP for incorporating micro data into BLP-style estimation.

Parameters
  • name (str) – The unique name of the micro moment part, which will be used for outputting information about micro moments.

  • dataset (MicroDataset) – The MicroDataset \(d_p\) on which the micro part is computed.

  • compute_values (callable) –

    Function for computing micro values \(v_{pijt}\) (or \(v_{pijkt}\) if the dataset \(d_p\) contains second choice data) in a market of the following form:

    compute_values(t, products, agents) --> values
    

    where t is the market in which to compute values, products is the market’s Products (with \(J_t\) rows), and agents is the market’s Agents (with \(I_t\) rows), unless pyblp.options.micro_computation_chunks is larger than its default of 1, in which case agents is a chunk of the market’s Agents. The returned values should be an array of the same shape as the weights returned by compute_weights of dataset.

    Warning

    If using different lambda functions to define different compute_values functions in a loop, any variables that are changing within the loop should be passed as extra arguments to the function to preserve their scope. For example, lambda t, p, a: np.outer(a.demographics[:, d], p.X2[:, c]) where d and c are indices that are changing in the outer loop should instead be lambda t, p, a, d=d, c=c: np.outer(a.demographics[:, d], p.X2[:, c]); otherwise, the values of d and c in the current loop’s iteration will be lost.

    Warning

    If using product-specific demographics, agents.demographics will be a \(I_t \times D \times J_t\) array, instead of a \(I_t \times D\) array like usual. Non-product specific demographics will be repeated \(J_t\) times.

    Note

    Particularly when using product-specific demographics or second choices, it may be convenient to use numpy.einsum, which handles many multiplying multi-dimensional arrays with common dimensions in an elegant way.

Examples

Methods