Skip to content

artificial_regressors module

Helpers to construct Salanié-Wolak artificial regressors.

make_K(X, shares)

Build second-order Salanié-Wolak artificial regressors.

Parameters:

Name Type Description Default
X ndarray

Product characteristics of shape (n_products, n_x).

required
shares ndarray

Product-level market shares of shape (n_products,).

required

Returns:

Type Description
ndarray

np.ndarray: Matrix K with shape (n_products, n_x).

Source code in frac_blp/artificial_regressors.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def make_K(X: np.ndarray, shares: np.ndarray) -> np.ndarray:
    """
    Build second-order Salanié-Wolak artificial regressors.

    Args:
        X (np.ndarray): Product characteristics of shape ``(n_products, n_x)``.
        shares (np.ndarray): Product-level market shares of shape ``(n_products,)``.

    Returns:
        np.ndarray: Matrix ``K`` with shape ``(n_products, n_x)``.
    """
    eS_X = X.T @ shares
    djm = eS_X - X / 2.0
    return cast(np.ndarray, -djm * X)

make_K_and_y(X2, shares, J)

Construct second-order regressors and the log-share LHS by market.

Parameters:

Name Type Description Default
X2 ndarray

Regressors with random coefficients.

required
shares ndarray

Observed market shares.

required
J int

Number of products per market.

required

Returns:

Name Type Description
TwoArrays TwoArrays

(K, y) where K are artificial regressors and y is the

TwoArrays

stacked log share ratios.

Source code in frac_blp/artificial_regressors.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def make_K_and_y(X2: np.ndarray, shares: np.ndarray, J: int) -> TwoArrays:
    """
    Construct second-order regressors and the log-share LHS by market.

    Args:
        X2 (np.ndarray): Regressors with random coefficients.
        shares (np.ndarray): Observed market shares.
        J (int): Number of products per market.

    Returns:
        TwoArrays: ``(K, y)`` where ``K`` are artificial regressors and ``y`` is the
        stacked log share ratios.
    """
    n_obs = X2.shape[0]
    n_x2 = X2.shape[1]
    K = np.zeros((n_obs, n_x2))  # the artificial Salanie-Wolak regressors
    y = np.zeros(n_obs)  # and the regression LHS

    for t in range(n_obs // J):
        this_market = slice(t * J, (t + 1) * J)
        these_shares = shares[this_market]
        sum_shares = these_shares.sum()
        this_market_zero_share = 1.0 - sum_shares
        this_X2 = X2[this_market, :]

        # artificial regressors and LHS for Salanie-Wolak
        K[this_market, :] = make_K(this_X2, these_shares)
        y[this_market] = np.log(these_shares / this_market_zero_share)
    return K, y