Skip to main content

get_factor_stats

get_factor_stats(
    X_train: numpy.ndarray,
    pca: sklearn.base.BaseEstimator,
    estimator: sklearn.base.BaseEstimator,
) ‑> systematica.preprocessing.factors.FactorParams
Calculate factor weights (n_components, symbols) based on PCA, intercepts and betas based on estimator.
It must be applied to the train set.
Parameters:
NameTypeDefaultDescription
retstp.Array2d--Array of asset returns (n_assets, n_times).
pcaBaseEstimator--PCA model that will be fitted to the standardized returns.
estimatorBaseEstimator--Regression model (e.g., LinearRegression) to fit the eigenportfolio returns and the asset returns to estimate residuals.
Returns:
TypeDescription
FactorParamsFactor weights (n_components, n_assets), intercept of the regression estimator and beta coefficient of the regression estimator. Parameters are calculated in the training set.

get_eigenportfolio_residuals

get_eigenportfolio_residuals(
    y_test: numpy.ndarray,
    params: systematica.preprocessing.factors.FactorParams,
) ‑> numpy.ndarray
Calculate residuals by regressing asset returns on eigenportfolio returns. Parameters:
NameTypeDefaultDescription
y_testtp.Array2d--Array of test asset returns (n_assets, n_test_times).
paramsFactorParams--Factor weights (n_components, n_assets), intercept of the regression estimator and beta coefficient of the regression estimator. Parameters are calculated in the training set.
Returns:
TypeDescription
tp.Array2dResidual returns (n_assets, n_test_times) for the test period.

fit_factor_model

fit_factor_model(
    X_train: numpy.ndarray,
    pca: sklearn.base.BaseEstimator,
    estimator: sklearn.base.BaseEstimator,
) ‑> systematica.preprocessing.factors.FactorParams
Fit model to asset returns by extracting risk-adjusted eigenvectors using PCA and applying to the returns data.
It must be applied to the train set.
Parameters:
NameTypeDefaultDescription
X_traintp.Array2d--Array of asset returns (n_assets, n_times).
pcaBaseEstimator--PCA model that will be fitted to the returns data.
estimatorBaseEstimator--Regression model (e.g., LinearRegression) to fit the eigenportfolio returns and the asset returns to estimate residuals.
Returns:
TypeDescription
FactorParamsFactor weights (n_components, n_assets), intercept of the regression estimator and beta coefficient of the regression estimator. Parameters are calculated in the training set.

predict_factor_model

predict_factor_model(
    y_test: numpy.ndarray,
    params: systematica.preprocessing.factors.FactorParams,
) ‑> numpy.ndarray
Predict residuals for the test set by applying the trained model on the returns and factor weights. Parameters:
NameTypeDefaultDescription
y_testtp.Array2d--Array of asset returns (n_assets, n_times) for the test set.
paramsFactorParams--Factor weights (n_components, n_assets), intercept of the regression estimator and beta coefficient of the regression estimator. Parameters are calculated in the training set.
Returns:
TypeDescription
tp.Array2dPredicted residuals (n_assets, n_times) for the test set.

residuals

residuals(
    rets: numpy.ndarray,
    lookback: int,
    pca: sklearn.base.BaseEstimator,
    estimator: sklearn.base.BaseEstimator,
) ‑> numpy.ndarray
Compute residual returns using PCA and regression, separating training and test data. Parameters:
NameTypeDefaultDescription
retstp.Array2d--Array of asset returns (n_assets, n_times).
lookbackint--Number of periods to look back for training.
pcaBaseEstimator--PCA model for factor weights.
estimatorBaseEstimator--Regression model (e.g., LinearRegression) to fit the eigenportfolio returns and the asset returns to estimate residuals.
Returns:
TypeDescription
tp.Array2dResidual returns (n_assets, n_times) for the test period.

residuals_cv

residuals_cv(
    rets: numpy.ndarray,
    index: numpy.ndarray,
    columns: Sequence[Hashable] = None,
    splitter: str = 'from_rolling',
    custom_splitter: str = None,
    custom_splitter_kwargs: Dict[str, Any] = None,
    training_window: int = 252,
    testing_window: int = 60,
    n_components: int | float = 15,
    estimator: sklearn.base.BaseEstimator = None,
    autotune: bool = False,
    to_numpy: bool = False,
    **split_kwargs,
) ‑> pandas.core.frame.DataFrame | numpy.ndarray
Cross-validation wrapper to apply residuals decomposition split-wise. Parameters:
NameTypeDefaultDescription
retstp.Array2d--Array of asset returns (n_assets, n_times).
indextp.Array1d--Datetime index for the results.
columnstp.LabelsNoneColumn labels for the results DataFrame.
splitterstrfrom_rollingMethod for splitting the data. Defaults to from_rolling.
custom_splitterstrNoneCustom splitter function, if provided.
custom_splitter_kwargstp.KwargsNoneAdditional arguments for the custom splitter.
training_windowint252Number of periods for the training window. Defaults to 252.
testing_windowint60Number of periods for the testing window. Defaults to 60.
n_componentsint15Number of PCA components to retain. Defaults to 15.
estimatorBaseEstimatorNoneRegression model to use for residual extraction. Defaults to LinearRegression.
autotuneboolFalseFlag to enable autotuning. Default is False.
to_numpyboolFalseIf True, return the result as a NumPy array. Defaults to False.
split_kwargstp.Kwargs--Additional keyword arguments for the splitter function.
Returns:
TypeDescription
pd.DataFrame or tp.Array2dResiduals after cross-validation.