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’sProducts
(with \(J_t\) rows), andagents
is the market’sAgents
(with \(I_t\) rows), unlesspyblp.options.micro_computation_chunks
is larger than its default of1
, in which caseagents
is a chunk of the market’sAgents
. The returnedvalues
should be an array of the same shape as theweights
returned bycompute_weights
ofdataset
.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])
whered
andc
are indices that are changing in the outer loop should instead belambda t, p, a, d=d, c=c: np.outer(a.demographics[:, d], p.X2[:, c])
; otherwise, the values ofd
andc
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