Note
Go to the end to download the full example code. or to run this example in your browser via Binder
Factor Model#
This tutorial shows how to use the FactorModel
estimator in
the MeanRisk
optimization.
A prior estimator fits a PriorModel
containing
the distribution estimate of asset returns. It represents the investor’s prior beliefs
about the model used to estimate such distribution.
The PriorModel
is a dataclass containing:
mu
: Expected returns estimation
covariance
: Covariance matrix estimation
returns
: assets returns estimation
cholesky
: Lower-triangular Cholesky factor of the covariance estimation (optional)
The FactorModel
estimator estimates the PriorModel
using a factor model and a
prior estimator of the factor’s returns. 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 can help overcome estimation
error by reducing the number of parameters, i.e., the dimensionality of the estimation
problem, making portfolio optimization more robust against noise in the data. Factor
models also provide a decomposition of financial risk to systematic and security
specific components.
To be compatible with scikit-learn
, the fit
method takes X
as the assets returns
and y
as the factors returns. Note that y
is in lowercase even for a 2D array
(more than one factor). This is for consistency with the scikit-learn API.
In this tutorial we will build a Maximum Sharpe Ratio portfolio using the FactorModel
estimator.
Data#
We load the S&P 500 dataset composed of the daily prices of 20 assets from the SPX Index composition and the Factors dataset composed of the daily prices of 5 ETF representing common factors:
from plotly.io import show
from sklearn.linear_model import RidgeCV
from sklearn.model_selection import train_test_split
from skfolio import Population, RiskMeasure
from skfolio.datasets import load_factors_dataset, load_sp500_dataset
from skfolio.moments import GerberCovariance, ShrunkMu
from skfolio.optimization import MeanRisk, ObjectiveFunction
from skfolio.preprocessing import prices_to_returns
from skfolio.prior import EmpiricalPrior, FactorModel, LoadingMatrixRegression
prices = load_sp500_dataset()
factor_prices = load_factors_dataset()
prices = prices["2014":]
factor_prices = factor_prices["2014":]
X, y = prices_to_returns(prices, factor_prices)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, shuffle=False)
Factor Model#
We create a Maximum Sharpe Ratio model using the Factor Model that we fit on the training set:
model_factor_1 = MeanRisk(
risk_measure=RiskMeasure.VARIANCE,
objective_function=ObjectiveFunction.MAXIMIZE_RATIO,
prior_estimator=FactorModel(),
portfolio_params=dict(name="Factor Model 1"),
)
model_factor_1.fit(X_train, y_train)
model_factor_1.weights_
array([1.22188511e-09, 1.31737525e-03, 4.74253021e-10, 7.70440099e-10,
9.46436338e-10, 1.24959219e-09, 5.13001762e-02, 6.35652050e-02,
7.36336705e-10, 1.79104637e-01, 5.03133290e-02, 7.13696569e-02,
4.12895735e-02, 2.27967880e-01, 5.13346758e-02, 1.44128512e-01,
2.88142338e-10, 6.19657594e-02, 5.63432134e-02, 1.09849875e-09])
We can change the BaseLoadingMatrix
that estimates the loading
matrix (betas) of the factors.
The default is the LoadingMatrixRegression
, which fit the factors using a
LassoCV
on each asset separately.
For example, let’s change the LassoCV
into a RidgeCV
without intercept and use
parallelization:
model_factor_2 = MeanRisk(
risk_measure=RiskMeasure.VARIANCE,
objective_function=ObjectiveFunction.MAXIMIZE_RATIO,
prior_estimator=FactorModel(
loading_matrix_estimator=LoadingMatrixRegression(
linear_regressor=RidgeCV(fit_intercept=False), n_jobs=-1
)
),
portfolio_params=dict(name="Factor Model 2"),
)
model_factor_2.fit(X_train, y_train)
model_factor_2.weights_
array([3.97776252e-02, 6.57851861e-03, 2.18408031e-02, 8.98298183e-03,
3.16206284e-02, 1.42391438e-02, 8.00175085e-02, 8.32124541e-02,
4.74823262e-02, 8.59482204e-02, 4.59789920e-02, 5.91802980e-02,
8.42282444e-02, 1.05685439e-01, 6.43876113e-02, 7.94746608e-02,
4.31092783e-08, 5.23719260e-02, 4.35228602e-02, 4.54697151e-02])
We can also change the prior estimator of the factors.
It is used to estimate the PriorModel
containing the factors
expected returns and covariance matrix.
For example, let’s estimate the factors expected returns with James-Stein shrinkage and the factors covariance matrix with the Gerber covariance estimator:
model_factor_3 = MeanRisk(
risk_measure=RiskMeasure.VARIANCE,
objective_function=ObjectiveFunction.MAXIMIZE_RATIO,
prior_estimator=FactorModel(
factor_prior_estimator=EmpiricalPrior(
mu_estimator=ShrunkMu(), covariance_estimator=GerberCovariance()
)
),
portfolio_params=dict(name="Factor Model 3"),
)
model_factor_3.fit(X_train, y_train)
model_factor_3.weights_
array([4.48283750e-12, 4.49645166e-12, 3.48850261e-13, 5.59880563e-13,
4.22380709e-13, 5.12492676e-13, 1.68739416e-02, 2.26134346e-11,
4.33953561e-13, 6.45574521e-02, 6.24004576e-02, 9.61481439e-02,
3.68197345e-01, 2.44687699e-01, 5.21327461e-12, 8.72794158e-11,
1.77006663e-13, 1.47134961e-01, 2.66255502e-12, 4.78965839e-13])
Factor Analysis#
Each fitted estimator is saved with a trailing underscore. For example, we can access the fitted prior estimator with:
prior_estimator = model_factor_3.prior_estimator_
We can access the prior model with:
prior_model = prior_estimator.prior_model_
We can access the loading matrix with:
loading_matrix = prior_estimator.loading_matrix_estimator_.loading_matrix_
Empirical Model#
For comparison, we also create a Maximum Sharpe Ratio model using the default Empirical estimator:
model_empirical = MeanRisk(
risk_measure=RiskMeasure.VARIANCE,
objective_function=ObjectiveFunction.MAXIMIZE_RATIO,
portfolio_params=dict(name="Empirical"),
)
model_empirical.fit(X_train)
model_empirical.weights_
array([1.01562655e-01, 7.81176235e-02, 1.48598568e-11, 1.89116954e-02,
7.18538495e-12, 3.64582189e-12, 1.10584830e-01, 2.88210632e-11,
3.64220700e-11, 8.95142106e-11, 1.62632716e-01, 4.66785730e-11,
1.77784307e-01, 9.61513792e-02, 1.10083470e-11, 9.72248235e-03,
1.74361693e-12, 2.44532312e-01, 4.38087390e-11, 5.50514320e-12])
Prediction#
We predict all models on the test set:
ptf_factor_1_test = model_factor_1.predict(X_test)
ptf_factor_2_test = model_factor_2.predict(X_test)
ptf_factor_3_test = model_factor_3.predict(X_test)
ptf_empirical_test = model_empirical.predict(X_test)
population = Population(
[ptf_factor_1_test, ptf_factor_2_test, ptf_factor_3_test, ptf_empirical_test]
)
fig = population.plot_cumulative_returns()
show(fig)
Let’s plot the portfolios’ composition:
population.plot_composition()
Total running time of the script: (0 minutes 3.236 seconds)