frac_utils module¶
Utility helpers for building instruments and projections in FRAC.
make_X(X_exo, X_endo)
¶
Combine exogenous and endogenous regressors into a single regressor matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_exo
|
ndarray | None
|
Exogenous regressors. |
required |
X_endo
|
ndarray | None
|
Endogenous regressors. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The concatenated regressor matrix. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
Source code in frac_blp/frac_utils.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
make_Z_full(Z, X1_exo=None, degree_Z=2, degree_X1=2)
¶
Build a full set of polynomial instruments for FRAC without demographics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Z
|
ndarray
|
Baseline instruments. |
required |
X1_exo
|
ndarray | None
|
Exogenous regressors without random coefficients. |
None
|
degree_Z
|
int
|
Maximum degree applied to columns of |
2
|
degree_X1
|
int
|
Maximum degree applied to columns of |
2
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Instrument matrix whose columns enumerate every admissible |
ndarray
|
combination of polynomial terms, preceded by a column of ones. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in frac_blp/frac_utils.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
make_centered_covariance(X)
¶
Compute the centered covariance matrix of a given matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input matrix with shape (n_samples, n_features). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Centered covariance matrix with shape (n_features, n_features). |
Source code in frac_blp/frac_utils.py
164 165 166 167 168 169 170 171 172 173 174 175 176 | |
make_y(shares, J)
¶
Create the dependent variable vector for FRAC estimation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shares
|
ndarray
|
Market shares, a (T*J,) vector stacked by market. |
required |
J
|
int
|
Number of products in each market. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The log of each share divided by the outside share |
ndarray
|
of its market, with the same shape as |
Source code in frac_blp/frac_utils.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
mnl_probabilities(u, outside=False)
¶
Return the MNL probabilities for utilities u.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
ndarray
|
Utilities of the J alternatives, a 1D array. |
required |
outside
|
bool
|
If True, include an outside option with utility 0. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The J probabilities |
ndarray
|
|
Source code in frac_blp/frac_utils.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
proj_on_Z(X, Z)
¶
Project each column of X onto Z and report fitted values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Variables to project, an (n, K) matrix. |
required |
Z
|
ndarray
|
Instruments used for the projections, an (n, L) matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
tuple[np.ndarray, np.ndarray, np.ndarray]: |
ndarray
|
of projected columns; |
ndarray
|
coefficients; and |
Source code in frac_blp/frac_utils.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |