mfe.crosssection¶
mfe.crosssection ¶
mfe.crosssection — Cross-sectional econometrics.
ols OLS with White heteroskedastic SEs olsnw OLS with Newey-West HAC SEs fama_macbeth Two-pass FM regression with Shanken correction rolling_betas Rolling time-series betas for FM pass 1 pca Principal component analysis with factor interpretation
FMResult
dataclass
¶
FMResult(lambda_mean: FloatArray, lambda_std: FloatArray, t_stats: FloatArray, p_values: FloatArray, t_stats_shanken: FloatArray, p_values_shanken: FloatArray, lambda_series: FloatArray, r_squared_mean: float, n_periods: int, n_assets: int, factor_names: list[str])
Fama-MacBeth estimation result.
PCAResult
dataclass
¶
PCAResult(eigenvalues: FloatArray, eigenvectors: FloatArray, factors: FloatArray, loadings: FloatArray, explained_variance: FloatArray, cumulative_variance: FloatArray, n_components: int, n_obs: int, n_vars: int, mean: FloatArray)
Principal component analysis result.
reconstruct ¶
Reconstruct data from the first k_c PCA components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k_c
|
number of components to use; if None uses all n_components
|
|
None
|
Returns:
| Type | Description |
|---|---|
(T, K) reconstructed data matrix (in original scale, mean added back)
|
|
Source code in src/mfe/crosssection/pca.py
olsnw ¶
olsnw(y: FloatArray, X: FloatArray, include_const: bool = True, nw_lags: int | None = None) -> OLSResult
OLS regression with Newey-West HAC standard errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
FloatArray
|
|
required |
X
|
FloatArray
|
|
required |
include_const
|
prepend a constant (default True)
|
|
True
|
nw_lags
|
int | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
OLSResult with .vcv_robust = Newey-West VCV, .std_errors = NW standard errors.
|
|
Source code in src/mfe/crosssection/ols.py
fama_macbeth ¶
fama_macbeth(returns: FloatArray, betas: FloatArray, include_intercept: bool = True, nw_lags: int = 0, shanken_correction: bool = True) -> FMResult
Two-pass Fama-MacBeth regression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
betas
|
FloatArray
|
|
required |
include_intercept
|
bool
|
|
True
|
nw_lags
|
int
|
|
0
|
shanken_correction
|
bool
|
|
True
|
Returns:
| Type | Description |
|---|---|
FMResult
|
|
Source code in src/mfe/crosssection/fm.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 135 136 137 138 | |
rolling_betas ¶
Rolling time-series OLS betas: for each asset n, regress returns on factors using a trailing window.
Returns (N, K) array of end-of-sample betas. Useful for the first pass of Fama-MacBeth.
Source code in src/mfe/crosssection/fm.py
fm ¶
Fama-MacBeth two-pass cross-sectional regression.
Fama, E.F. & MacBeth, J.D. (1973): "Risk, Return, and Equilibrium: Empirical Tests", Journal of Political Economy.
Two passes: Pass 1: For each time period t, regress cross-sectional returns on factor loadings (betas) to get factor risk premia lambda_t. Pass 2: Average lambda_t across time and compute t-statistics with Shanken (1992) correction for errors-in-variables.
Also implements rolling-window beta estimation (first step of pass 1).
FMResult
dataclass
¶
FMResult(lambda_mean: FloatArray, lambda_std: FloatArray, t_stats: FloatArray, p_values: FloatArray, t_stats_shanken: FloatArray, p_values_shanken: FloatArray, lambda_series: FloatArray, r_squared_mean: float, n_periods: int, n_assets: int, factor_names: list[str])
Fama-MacBeth estimation result.
fama_macbeth ¶
fama_macbeth(returns: FloatArray, betas: FloatArray, include_intercept: bool = True, nw_lags: int = 0, shanken_correction: bool = True) -> FMResult
Two-pass Fama-MacBeth regression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
betas
|
FloatArray
|
|
required |
include_intercept
|
bool
|
|
True
|
nw_lags
|
int
|
|
0
|
shanken_correction
|
bool
|
|
True
|
Returns:
| Type | Description |
|---|---|
FMResult
|
|
Source code in src/mfe/crosssection/fm.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 135 136 137 138 | |
rolling_betas ¶
Rolling time-series OLS betas: for each asset n, regress returns on factors using a trailing window.
Returns (N, K) array of end-of-sample betas. Useful for the first pass of Fama-MacBeth.
Source code in src/mfe/crosssection/fm.py
ols ¶
OLS and OLSNW regression — MFE toolbox ols.m / olsnw.m equivalents.
Design rationale: statsmodels OLS exists but requires a DataFrame/array and returns a result object with a non-trivial API. These functions are thin, fast wrappers that match the MFE toolbox calling convention exactly and fit naturally into pipelines that already use mfe.utils.vcv.
ols(Y, X) — OLS with White heteroskedastic SEs olsnw(Y, X) — OLS with Newey-West HAC SEs
ols ¶
OLS regression with White heteroskedasticity-robust standard errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
FloatArray
|
|
required |
X
|
FloatArray
|
|
required |
include_const
|
prepend a column of ones (default True)
|
|
True
|
Returns:
| Type | Description |
|---|---|
OLSResult
|
.params — coefficient vector [const?, beta_1..beta_K] .t_stats — White-robust t-statistics .std_errors — White-robust standard errors .vcv — classical (homoskedastic) VCV .vcv_robust — White heteroskedasticity-robust VCV |
Source code in src/mfe/crosssection/ols.py
olsnw ¶
olsnw(y: FloatArray, X: FloatArray, include_const: bool = True, nw_lags: int | None = None) -> OLSResult
OLS regression with Newey-West HAC standard errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
FloatArray
|
|
required |
X
|
FloatArray
|
|
required |
include_const
|
prepend a constant (default True)
|
|
True
|
nw_lags
|
int | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
OLSResult with .vcv_robust = Newey-West VCV, .std_errors = NW standard errors.
|
|
Source code in src/mfe/crosssection/ols.py
pca ¶
Principal Component Analysis for financial returns.
Standalone PCA module with the interface matching the MFE MATLAB pca.m: - eigendecomposition of the sample covariance - proportion of variance explained per component - factor scores (principal components) - loadings matrix - reconstruction of the original data from K_c components
This is a clean public API wrapping the internals already used in mfe.multivariate.gogarch. The MFE MATLAB pca.m is documented but rarely exposed directly — we make it first-class here.
Distinct from sklearn.decomposition.PCA in that: - we expose the covariance structure (not correlation by default) - we match financial conventions: K_c components from covariance, not correlation, and we return eigenvalues in variance units (not explained variance ratio) alongside the standard stats
PCAResult
dataclass
¶
PCAResult(eigenvalues: FloatArray, eigenvectors: FloatArray, factors: FloatArray, loadings: FloatArray, explained_variance: FloatArray, cumulative_variance: FloatArray, n_components: int, n_obs: int, n_vars: int, mean: FloatArray)
Principal component analysis result.
reconstruct ¶
Reconstruct data from the first k_c PCA components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k_c
|
number of components to use; if None uses all n_components
|
|
None
|
Returns:
| Type | Description |
|---|---|
(T, K) reconstructed data matrix (in original scale, mean added back)
|
|
Source code in src/mfe/crosssection/pca.py
pca ¶
pca(data: FloatArray, n_components: int | None = None, demean: bool = True, standardize: bool = False) -> PCAResult
Principal component analysis of a (T, K) data matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
FloatArray
|
|
required |
n_components
|
number of components to retain; if None, keeps all K
|
|
None
|
demean
|
bool
|
|
True
|
standardize
|
bool
|
|
False
|
Returns:
| Type | Description |
|---|---|
PCAResult
|
.eigenvalues — (K,) eigenvalues of sample covariance/correlation matrix .eigenvectors — (K, K) eigenvector matrix (columns sorted descending) .factors — (T, K_c) factor scores = demeaned_data @ eigenvectors[:, :K_c] .loadings — (K, K_c) factor loadings scaled by sqrt(eigenvalue) .explained_variance — proportion of total variance per component |