qis.EwmLinearModel¶
- class qis.EwmLinearModel(x, y, loadings=None, x_covars=None, residual_vars=None)[source]¶
Bases:
LinearModellinear factor model with exponentially weighted time-varying loadings.
Implements
LinearModelwith EWM estimation: the betas are re-estimated at every date from an exponentially weighted covariance, so an exposure that changes is tracked rather than averaged away over the sample. Construct with the factor and asset panels, callfit, then read the loadings through the LinearModel interface.- Variables:
x (pandas.DataFrame | pandas.Series) – factor returns, shape (T, K), one column per factor
y (pandas.DataFrame | pandas.Series) – asset returns, shape (T, n), one column per asset
loadings (Dict[str, pandas.DataFrame]) – factor name to a (T, n) dates-by-assets frame of time-varying betas, populated by
fit; row t of the frames of all factors is the assets-by-factors matrix B_t
- Parameters:
- __init__(x, y, loadings=None, x_covars=None, residual_vars=None)¶
Methods
__init__(x, y[, loadings, x_covars, ...])compute_active_factor_risk(...)Compute factor-only active risk, excluding idiosyncratic risk.
compute_agg_factor_exposures(weights)Compute portfolio factor exposures
X[t, q] = sum_i B[t, i, q] * w[t, i].compute_factor_risk_contribution(weights[, ...])Compute factor risk contributions and their ratios for portfolio weights over time.
fit([span, ewm_lambda, is_x_correlated, ...])Estimate time series EWM betas using exponential weighting.
get_asset_factor_attribution([asset, add_total])Attribute an asset's return to factors with the loadings of the previous date.
get_asset_factor_betas([time_period, asset])Get the loadings of one asset, dates by factors, over an optional time period.
get_factor1_loadings()Get the dates-by-assets loadings of the first factor.
get_factor_alpha([x, y, lag, span])Compute the model residual
y[t] - sum_q B[t - lag, q] x[t, q]and the explained part.get_factor_loadings(factor)Get the dates-by-assets loadings of a specific factor.
get_loadings_at_date(date)Return the loadings in force at
date, factors by assets.get_model_ewm_r2([span, lag])Compute the exponentially weighted, uncentred R² of each asset.
get_model_residuals_corrs([span])Compute the EWM correlation matrix of the in-sample residuals at the last date.
plot_factor_loadings(factor[, var_format, ...])Plot factor loadings time series.
print()Print model components for debugging.
Attributes
loadingsresidual_varsx_covarsxy- fit(span=31, ewm_lambda=0.94, is_x_correlated=True, mean_adj_type=MeanAdjType.NONE, init_type=InitType.X0, warmup_period=20)[source]¶
Estimate time series EWM betas using exponential weighting.
The loadings dated t solve the EWM least-squares problem on the returns up to and including t.
xandyare left unchanged: any mean adjustment is applied to copies used for estimation only, soget_factor_alphaandget_model_ewm_r2work on the returns as supplied.- Parameters:
span (int | None) – Span for EWM calculation; overrides
ewm_lambdawhen given.ewm_lambda (float) – Decay parameter for EWM, used when
spanis None.is_x_correlated (bool) – If True, invert the full factor cross-moment matrix; if False, use its diagonal only, which treats the factors as uncorrelated.
mean_adj_type (MeanAdjType) – Mean removed from both panels before the moments are formed.
NONEregresses through the origin;EWMAandEXPANDINGare point in time;INSAMPLEsubtracts the full-sample mean and is forward-looking.init_type (InitType) – Seed of the EWMA mean when
mean_adj_typeisEWMA; no effect otherwise. The defaultInitType.X0seeds with the first observation, which is point in time.InitType.MEANseeds with the full-sample mean, a look-ahead whose weight in the mean at position t is lambda^(t+1) (about 0.24 at the first reported beta for span 31); it was the default before this release.warmup_period (int) – Last position whose betas are left missing: positions 0 to
warmup_period, that iswarmup_period + 1rows (21 by default), are NaN.
- Raises:
ValueError – If the factor and asset return index labels or order do not match exactly.
- Return type:
None