skfolio.prior.TimeSeriesFactorModel#
- class skfolio.prior.TimeSeriesFactorModel(loading_matrix_estimator=None, factor_prior_estimator=None, factor_families=None, 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) = a_i + B_i \, f(t) + \epsilon_i(t)\]where \(B_i\) is the factor loadings (exposures), \(f(t)\) is the vector of factor returns, \(a_i\) is the intercept of asset \(i\)’s time-series regression, and \(\epsilon_i(t)\) is the idiosyncratic return, obtained as the regression residual.
The expected return vector is:
\[\mu = B \, \mathbb{E}[f] + a\]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 expected factor returns and the factor covariance matrix. The default (None) is to useEmpiricalPrior.- factor_familiesarray-like of shape (n_factors,), optional
Family label for each factor. When provided, the labels are stored in the
FactorModeland can be used by downstream diagnostics, plots and optimization constraints referencing factor families. The default (None) means that no family labels are attached to the factors.- 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])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=None, *, factors, **fit_params)[source]#
Fit the Time-series factor model estimator.
- Parameters:
- Xarray-like of shape (n_observations, n_assets)
Price returns of the assets.
- yNone
Not used, present for scikit-learn compatibility.
- factorsarray-like of shape (n_observations, n_factors)
Factors’ returns.
- **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.