skfolio.prior.TimeSeriesFactorModel#
- class skfolio.prior.TimeSeriesFactorModel(loading_matrix_estimator=None, factor_prior_estimator=None, residual_variance=True, higham=False, max_iteration=100)[source]#
Time-series factor model estimator.
The purpose of factor models is to impose a structure on financial variables and their covariance matrix by explaining them through a small number of common factors. This reduces the number of free parameters in the estimation problem, making portfolio optimization more robust against noise. Factor models also provide a decomposition of risk into systematic and idiosyncratic components.
This estimator implements a time-series regression approach: for each asset \(i\), the return is regressed on a common set of factor return series:
\[r_i(t) = \alpha_i + B_i \, f(t) + \epsilon_i(t)\]where \(B_i\) is the factor loadings (exposures), \(f(t)\) is the vector of factor returns, \(\alpha_i\) is the intercept, and \(\epsilon_i(t)\) is the idiosyncratic return (residual).
The expected return vector is:
\[\mu = B \, \mathbb{E}[f] + \alpha\]and the covariance matrix is:
\[\Sigma = B \, F \, B^\top + D\]where \(F\) is the factor covariance matrix and \(D\) is the diagonal matrix of idiosyncratic variances.
Note
This formulation assumes that the factors are tradable assets or portfolios (e.g. long-short equity factors or ETF returns), so that the factor sample mean is a valid estimate of the factor risk premium. When factors are non-tradable variables (e.g. macroeconomic series), sometimes called a macroeconomic factor model in the literature, the sample mean no longer equals the risk premium and a two-pass procedure such as Fama-MacBeth (1973) is required to estimate the cross-sectional price of risk \(\lambda\). That procedure also requires a large estimation universe in order to reliably identify the factor risk premia.
- Parameters:
- loading_matrix_estimatorLoadingMatrixEstimator, optional
Estimator of the loading matrix (betas) of the factors. The default (
None) is to useLoadingMatrixRegressionwhich fits the factors usingLassoCVon each asset separately.- factor_prior_estimatorBasePrior, optional
Estimator of the factor return distribution. It is used to estimate the
ReturnDistributioncontaining the factor expected returns and covariance matrix. The default (None) is to useEmpiricalPrior.- residual_variancebool, default=True
Deprecated since version The:
residual_varianceparameter is deprecated and will be removed in a future version. Residual variance is always added.If this is set to True, the diagonal term of the residuals covariance (residuals variance) is added to the factor model covariance.
- highambool, default=False
If this is set to True, the Higham (2002) algorithm is used to find the nearest positive semi-definite covariance matrix. It is more accurate but slower than the default clipping method. For more information see
cov_nearest.- max_iterationint, default=100
Only used when
highamis set to True. Maximum number of iterations of the Higham (2002) algorithm.
- Attributes:
- return_distribution_ReturnDistribution
Fitted
ReturnDistributioncontaining the asset distribution and moments estimation based on the factor model.- factor_prior_estimator_BasePrior
Fitted
factor_prior_estimator.- loading_matrix_estimator_BaseLoadingMatrix
Fitted
loading_matrix_estimator.- n_features_in_int
Number of assets seen during
fit.- feature_names_in_ndarray of shape (
n_features_in_,) Names of features seen during
fit. Defined only whenXhas feature names that are all strings.
Methods
fit(X, y[, factors])Fit the Time-series factor model estimator.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
set_fit_request(*[, factors])Configure whether metadata should be requested to be passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
- fit(X, y, factors=None, **fit_params)[source]#
Fit the Time-series factor model estimator.
- Parameters:
- Xarray-like of shape (n_observations, n_assets)
Price returns of the assets.
- yarray-like of shape (n_observations, n_factors)
Factors’ returns.
- factorsarray-like of shape (n_observations, n_factors), optional
Factors’ returns. If provided, it will override
y.- **fit_paramsdict
Parameters to pass to the underlying estimators. Only available if
enable_metadata_routing=True, which can be set by usingsklearn.set_config(enable_metadata_routing=True). See Metadata Routing User Guide for more details.
- Returns:
- selfTimeSeriesFactorModel
Fitted estimator.
- get_metadata_routing()[source]#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating 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_fit_request(*, factors='$UNCHANGED$')#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
- factorsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
factorsparameter infit.
- Returns:
- selfobject
The updated object.
- 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.