mfe.utils¶
mfe.utils ¶
lag_matrix ¶
Construct a lag matrix from a 1-D or 2-D array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array of shape (T,) or (T, K)
|
|
required |
lags
|
int or list[int]
|
If int, lags 1..lags are included. If list, exactly those lags are included. |
required |
trim
|
bool
|
If True (default), drop the leading NaN rows. |
True
|
Returns:
| Type | Description |
|---|---|
array of shape (T - max_lag, len(lags) * K) if trim else (T, ...)
|
|
Source code in src/mfe/utils/lags.py
har_lag_matrix ¶
har_lag_matrix(rv: FloatArray, horizons: tuple[int, int, int] = (1, 5, 22), trim: bool = True) -> FloatArray
Build the HAR regressor matrix [RV_d, RV_w, RV_m] from daily RV.
Uses rolling averages, not raw lags, matching the Corsi (2009) definition: RV_{t|t-h} = (1/h) * sum_{k=0}^{h-1} RV_{t-k}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rv
|
(T,) array of daily realized variances
|
|
required |
horizons
|
tuple of 3 ints (daily, weekly, monthly)
|
|
(1, 5, 22)
|
trim
|
bool — drop leading NaNs
|
|
True
|
Returns:
| Type | Description |
|---|---|
(T - max_h, 3) array: columns are [RV_d_lag1, RV_w, RV_m]
|
|
Source code in src/mfe/utils/lags.py
sandwich ¶
Sandwich (robust) covariance: H^{-1} B H^{-1} where B = scores.T @ scores / T and H is the (negative) Hessian / T.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
FloatArray
|
|
required |
hessian
|
(P, P) negative Hessian evaluated at MLE
|
|
required |
Returns:
| Type | Description |
|---|---|
(P, P) robust covariance matrix
|
|
Source code in src/mfe/utils/vcv.py
newey_west ¶
newey_west(scores: FloatArray, bandwidth: int | None = None, hessian: FloatArray | None = None) -> FloatArray
Newey-West (HAC) covariance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
FloatArray
|
|
required |
bandwidth
|
number of lags; if None uses Andrews (1991) automatic selector
|
|
None
|
hessian
|
FloatArray | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
(P, P) matrix: B_hat (HAC meat) if hessian is None, else full sandwich
|
|
Source code in src/mfe/utils/vcv.py
lags ¶
Vectorized lag-matrix utilities.
All functions operate on (T,) or (T, K) arrays and return views or stride-tricks arrays where possible — no unnecessary copies.
lag_matrix ¶
Construct a lag matrix from a 1-D or 2-D array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array of shape (T,) or (T, K)
|
|
required |
lags
|
int or list[int]
|
If int, lags 1..lags are included. If list, exactly those lags are included. |
required |
trim
|
bool
|
If True (default), drop the leading NaN rows. |
True
|
Returns:
| Type | Description |
|---|---|
array of shape (T - max_lag, len(lags) * K) if trim else (T, ...)
|
|
Source code in src/mfe/utils/lags.py
har_lag_matrix ¶
har_lag_matrix(rv: FloatArray, horizons: tuple[int, int, int] = (1, 5, 22), trim: bool = True) -> FloatArray
Build the HAR regressor matrix [RV_d, RV_w, RV_m] from daily RV.
Uses rolling averages, not raw lags, matching the Corsi (2009) definition: RV_{t|t-h} = (1/h) * sum_{k=0}^{h-1} RV_{t-k}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rv
|
(T,) array of daily realized variances
|
|
required |
horizons
|
tuple of 3 ints (daily, weekly, monthly)
|
|
(1, 5, 22)
|
trim
|
bool — drop leading NaNs
|
|
True
|
Returns:
| Type | Description |
|---|---|
(T - max_h, 3) array: columns are [RV_d_lag1, RV_w, RV_m]
|
|
Source code in src/mfe/utils/lags.py
typing ¶
Shared type aliases for the mfe package.
vcv ¶
Robust covariance matrix estimators.
- sandwich (QMLE robust, a.k.a. Huber-White)
- newey_west (HAC)
- Both accept a (T, P) score matrix and optionally a (P, P) Hessian.
sandwich ¶
Sandwich (robust) covariance: H^{-1} B H^{-1} where B = scores.T @ scores / T and H is the (negative) Hessian / T.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
FloatArray
|
|
required |
hessian
|
(P, P) negative Hessian evaluated at MLE
|
|
required |
Returns:
| Type | Description |
|---|---|
(P, P) robust covariance matrix
|
|
Source code in src/mfe/utils/vcv.py
newey_west ¶
newey_west(scores: FloatArray, bandwidth: int | None = None, hessian: FloatArray | None = None) -> FloatArray
Newey-West (HAC) covariance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
FloatArray
|
|
required |
bandwidth
|
number of lags; if None uses Andrews (1991) automatic selector
|
|
None
|
hessian
|
FloatArray | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
(P, P) matrix: B_hat (HAC meat) if hessian is None, else full sandwich
|
|