skfolio.descriptor.EWMarketBeta#

class skfolio.descriptor.EWMarketBeta(half_life=60.0, aggregation_period=1, min_periods=None, shrinkage_group=None, min_group_size=5, shrinkage_bounds=(0.0, 1.0), eps=None)[source]#

Exponentially weighted market beta descriptor.

Measures each asset’s sensitivity to the market portfolio using exponentially weighted covariance and variance estimates [1]:

\[\beta_i = \frac{\text{Cov}(r_i, r_m)}{\text{Var}(r_m)}\]

where \(r_i\) is the return of asset \(i\), \(r_m\) is the cap-weighted market return computed on the estimation universe and the EWMA uses decay \(\lambda = \exp(-\ln(2) / \text{half\_life})\).

Parameters:
half_lifefloat, default=60.0

EWMA half-life in observations after aggregation. For example, with aggregation_period=5 and half_life=60, the EWMA decays over 60 aggregated periods, equivalent to 300 raw observations.

aggregation_periodint, default=1

Number of consecutive observations to aggregate before updating EWMA statistics. Aggregation can reduce desynchronization effects. Asset and market returns are averaged over the same observations, as described in Notes.

min_periodsint, optional

Minimum number of observations required before computing each asset’s market beta. With aggregation enabled, each average counts as one observation. Until this count is reached, the asset’s output is NaN. If None, defaults to \(\lceil\text{half\_life}\rceil\), with a minimum of 1.

shrinkage_groupstr, optional

Name of a categorical field containing group labels (e.g., "industry") for Bayesian shrinkage. When provided, raw betas are shrunk toward the cap-weighted group mean using an empirical Bayes approach:

\[\beta_i^{\text{shrunk}} = w_i \cdot \beta_i^{\text{raw}} + (1 - w_i) \cdot \mu_g\]

where \(w_i = \tau_g^2 / (\tau_g^2 + \sigma_i^2)\), \(\mu_g\) is the cap-weighted group mean, \(\tau_g^2\) is the prior variance (cross-sectional variance minus noise) and \(\sigma_i^2\) is the estimation error variance.

Assets with missing category codes or non-positive market capitalization are excluded from shrinkage. If None (default), no shrinkage is applied.

min_group_sizeint, default=5

Minimum number of assets in a group to compute group-specific statistics. Groups with fewer assets fall back to global (cross-sectional) statistics. Only used when shrinkage_group is provided.

shrinkage_boundstuple of float, default=(0.0, 1.0)

Lower and upper bounds (w_min, w_max) for the raw-beta weight \(w_i\). Lower values apply more shrinkage toward the group mean, while higher values keep more of the raw beta. The coefficient is clipped to this range after estimation.

For example, (0.1, 0.9) keeps at least 10% weight on the raw beta and at least 10% weight on the group mean. Only used when shrinkage_group is provided.

epsfloat, optional

Deprecated and ignored. It will be removed in version 2.0. Supplying a positive value emits a FutureWarning.

Attributes:
n_assets_int

Number of assets seen during fitting.

asset_names_ndarray of shape (n_assets,)

Asset names seen during fitting.

market_beta_ndarray of shape (n_assets,)

Last fitted market beta value for each asset.

Methods

fit_transform(X[, y])

Compute exponentially weighted market betas.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

partial_fit_transform(X[, y])

Update EWMA state on X and return betas for this batch.

set_params(**params)

Set the parameters of this estimator.

Notes

NaNs are allowed as missing observations. Non-missing returns values must be finite. Covariance and market variance use the same observations for each asset, with X.active_mask=True and non-missing returns. This is equivalent to regression with an intercept. Missing returns do not reset estimates. Setting X.active_mask=False resets estimates and sets output to NaN. Too little variation in market returns also produces NaN.

Shrinkage assumes independent errors with constant variance. It requires at least three observations and an estimable beta variance. Otherwise, assets subject to shrinkage return NaN.

Market returns are the X["market_cap"]-weighted average of X["returns"] where X.estimation_mask=True and both fields are finite. A ValueError is raised if these assets have no positive total market capitalization at any observation.

References

[1]

“Capital asset prices: A theory of market equilibrium under conditions of risk”. The Journal of Finance. Sharpe, W. F. (1964).

fit_transform(X, y=None, **fit_params)[source]#

Compute exponentially weighted market betas.

Parameters:
XAssetPanel

Input panel containing returns and market_cap, and when shrinkage_group is set, that group characteristic.

yNone

Ignored. Present for compatibility with scikit-learn’s API.

**fit_paramsdict

Additional fit parameters. Ignored.

Returns:
betasndarray of shape (n_observations, n_assets)

Market beta for each observation and asset.

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

partial_fit_transform(X, y=None, **fit_params)[source]#

Update EWMA state on X and return betas for this batch.

This method supports online updates by continuing from the current fitted state. Use fit_transform to start from a clean state.

Parameters:
XAssetPanel

Input panel containing at least "returns" and "market_cap". If shrinkage is enabled, it must also contain the field specified by shrinkage_group.

yNone

Ignored. Present for compatibility with scikit-learn’s API.

**fit_paramsdict

Additional fit parameters. Ignored.

Returns:
betasndarray of shape (n_observations, n_assets)

Market beta for each observation and asset.

set_params(**params)#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.