skfolio.moments.EmpiricalVariance#

class skfolio.moments.EmpiricalVariance(window_size=None, ddof=1, assume_centered=False)[source]#

Empirical Variance estimator.

This is the variance-only counterpart of EmpiricalCovariance, computing only the diagonal elements (variances) and assuming zero correlation. This is appropriate when:

  • Estimating idiosyncratic (specific) risk in factor models, where residual returns are uncorrelated by construction

  • Working with orthogonalized or uncorrelated return series

  • The full covariance structure is not needed or is constructed separately

Parameters:
window_sizeint, optional

Window size. The model is fitted on the last window_size observations. The default (None) is to use all the data.

ddofint, default=1

Normalization is by (n_observations - ddof). Note that ddof=1 will return the unbiased estimate, and ddof=0 will return the simple average. The default value is 1.

assume_centeredbool, default=False

If False (default), the data are mean-centered before computing the variance. This is the standard behavior when working with raw returns where the mean is not guaranteed to be zero. If True, the estimator assumes the input data are already centered. Use this when you know the returns have zero mean, such as pre-demeaned data or regression residuals.

Attributes:
variance_ndarray of shape (n_assets,)

Estimated variance vector.

n_features_in_int

Number of assets seen during fit.

feature_names_in_ndarray of shape (n_features_in_,)

Names of assets seen during fit. Defined only when X has asset names that are all strings.

Methods

fit(X[, y])

Fit the empirical variance estimator.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

set_params(**params)

Set the parameters of this estimator.

Examples

>>> from skfolio.datasets import load_sp500_dataset
>>> from skfolio.moments import EmpiricalVariance
>>> from skfolio.preprocessing import prices_to_returns
>>>
>>> prices = load_sp500_dataset()
>>> X = prices_to_returns(prices)
>>> model = EmpiricalVariance()
>>> model.fit(X)
>>> print(model.variance_[:5])
fit(X, y=None)[source]#

Fit the empirical variance estimator.

Parameters:
Xarray-like of shape (n_observations, n_assets)

Price returns of the assets.

yIgnored

Not used, present for API consistency by convention.

Returns:
selfEmpiricalVariance

Fitted estimator.

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.

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.