Portfolio Models¶
Some Theory¶
Riskfolio-Lib allows to calculate optimum portfolios that result from optimize one of the following 4 objective functions:
Maximum Return Portfolio:
Minimum Risk Portfolio:
Maximum Risk Adjusted Return Ratio Portfolio:
Maximum Utility Portfolio:
Where:
\(\mu\): is the vector of expected returns.
\(w\): is the vector of weights of the optimum portfolio.
\(Aw \geq B\): is a set of linear constraints.
\(\phi_{i}(w)\): are 10 available risk measures. The available risk measures are:
Standard Deviation [A1].
Mean Absolute Deviation [A2].
Semi Standard Deviation [A3].
First Lower Partial Moment (Omega Ratio) [A4].
Second Lower Partial Moment (Sortino Ratio) [A4].
Conditional Value at Risk [A5].
Worst Realization (Minimax) [A6]
Maximum Drawdown of uncompounded returns (Calmar Ratio) [A7].
Average Drawdown of uncompounded returns [A7].
Conditional Drawdown at Risk of uncompounded returns [A7].
\(c_{i}\): are maximum values on each risk measure.
\(r_{f}\): is the risk free rate. When the risk measure is the first or second lower partial moment, \(r_{f}\) is the minimum acceptable return \(\text{MAR}\).
\(\lambda\): is the risk aversion coefficient of the investor.
Module Methods¶
-
class
Portfolio.
Portfolio
(returns=None, sht=False, uppersht=0.2, upperlng=1, factors=None, alpha=0.01, kindbench=True, allowTO=False, turnover=0.05, allowTE=False, TE=0.05, benchindex=None, benchweights=None, ainequality=None, binequality=None, upperdev=None, uppermad=None, uppersdev=None, upperflpm=None, upperslpm=None, upperCVaR=None, upperwr=None, uppermdd=None, upperadd=None, upperCDaR=None)[source]¶ Class that creates a portfolio object with all properties needed to calculate optimum portfolios.
- Parameters
returns (DataFrame, optional) – A dataframe that containts the returns of the assets. The default is None.
sht (bool, optional) – Indicate if the portfolio consider short positions (negative weights). The default is False.
uppersht (float, optional) – Indicate the maximum value of the sum of short positions. The default is 0.2.
upperlng (float, optional) – Indicate the maximum value of the sum of long positions (positive weights). The default is 1.
factors (DataFrame, optional) – A dataframe that containts the returns of the factors. The default is None.
alpha (float, optional) – Significance level of CVaR and CDaR. The default is 0.01.
kindbench (bool, optional) – True if the benchmark is a portfolio with detailed weights and False if the benchmark is an index. The default is True.
allowTO (bool, optional) – Indicate if there is turnover constraints. The default is False.
turnover (float, optional) – The maximum limit of turnover deviatons. The default is 0.05.
allowTE (bool, optional) – Indicate if there is tracking error constraints.. The default is False.
TE (float, optional) – The maximum limit of tracking error deviatons. The default is 0.05.
benchindex (DataFrame, optional) – A dataframe that containts the returns of an index. If kindbench is the tracking error constraints are calculated respect to this index. The default is None.
benchweights (DataFrame, optional) – A dataframe that containts the weights of an index. The default is the equally weighted portfolio 1/N.
ainequality (nd-array, optional) – The matrix \(A\) of the linear constraint \(A \geq B\). The default is None.
binequality (1d-array, optional) – The matrix \(B\) of the linear constraint \(A \geq B\). The default is None.
upperdev (float, optional) – Constraint on max level of standard deviation. The default is None.
uppermad (float, optional) – Constraint on max level of MAD. The default is None.
uppersdev (float, optional) – Constraint on max level of semi standard deviation. The default is None.
upperflpm (float, optional) – Constraint on max level of first lower partial moment. The default is None.
upperslpm (float, optional) – Constraint on max level of second lower partial moment. The default is None.
upperCVaR (float, optional) – Constraint on max level of CVaR. The default is None.
upperwr (float, optional) – Constraint on max level of worst realization. The default is None.
uppermdd (float, optional) – Constraint on max level of maximum drawdown of uncompounded cumulated returns. The default is None.
upperadd (float, optional) – Constraint on max level of average drawdown of uncompounded cumulated returns. The default is None.
upperCDaR (float, optional) – Constraint on max level of conditional drawdown at risk of uncompounded cumulated returns. The default is None.
-
assets_stats
(method_mu='hist', method_cov='hist', **kwargs)[source]¶ Calculate the inputs that will be use by the optimization method when we select the input model=’Classic’.
- Parameters
**kwargs (dict) – All aditional parameters of mean_vector and covar_matrix functions.
See also
riskfolio.ParamsEstimation.mean_vector()
,riskfolio.ParamsEstimation.covar_matrix()
-
blacklitterman_stats
(P, Q, rf=0, w=None, delta=None, eq=True, method_mu='hist', method_cov='hist', **kwargs)[source]¶ Calculate the inputs that will be use by the optimization method when we select the input model=’BL’.
- Parameters
P (DataFrame of shape (n_views, n_assets)) – Analyst’s views matrix, can be relative or absolute.
Q (DataFrame of shape (n_views, 1)) – Expected returns of analyst’s views.
delta (float) – Risk aversion factor. The default value is 1.
rf (scalar, optional) – Risk free rate. The default is 0.
w (DataFrame of shape (n_assets, 1)) – Weights matrix, where n_assets is the number of assets. The default is None.
eq (bool, optional) – Indicates if use equilibrum or historical excess returns. The default is True.
**kwargs (dict) – Other variables related to the mean and covariance estimation.
See also
riskfolio.ParamsEstimation.black_litterman()
-
factors_stats
(method_mu='hist', method_cov='hist', **kwargs)[source]¶ Calculate the inputs that will be use by the optimization method when we select the input model=’FM’.
- Parameters
**kwargs (dict) – All aditional parameters of risk_factors function.
See also
riskfolio.ParamsEstimation.forward_regression()
,riskfolio.ParamsEstimation.backward_regression()
,riskfolio.ParamsEstimation.loadings_matrix()
,riskfolio.ParamsEstimation.risk_factors()
-
optimization
(model='Classic', rm='MV', obj='Sharpe', rf=0, l=2, hist=True)[source]¶ This method that calculates the optimum portfolio according to the optimization model selected by the user. The general problem that solves is:
\[\begin{split}\begin{align} &\underset{x}{\text{optimize}} & & F(w)\\ &\text{s. t.} & & Aw \geq B\\ & & & \phi_{i}(w) \leq c_{i}\\ \end{align}\end{split}\]Where:
\(F(w)\) is the objective function.
\(Aw \geq B\) is a set of linear constraints.
\(\phi_{i}(w) \leq c_{i}\) are constraints on maximum values of several risk measures.
- Parameters
model (str can be 'Classic', 'BL' or 'FM') –
The model used for optimize the portfolio. The default is ‘Classic’. Posible values are:
’Classic’: use estimates of expected return vector and covariance matrix that depends on historical data.
’BL’: use estimates of expected return vector and covariance matrix based on the Black Litterman model.
’FM’: use estimates of expected return vector and covariance matrix based on a Risk Factor model specified by the user.
rm (str, optional) –
The risk measure used to optimze the portfolio. The default is ‘MV’. Posible values are:
’MV’: Standard Deviation.
’MAD’: Mean Absolute Deviation.
’MSV’: Semi Standard Deviation.
’FLPM’: First Lower Partial Moment (Omega Ratio).
’SLPM’: Second Lower Partial Moment (Sortino Ratio).
’CVaR’: Conditional Value at Risk.
’WR’: Worst Realization (Minimax)
’MDD’: Maximum Drawdown of uncompounded returns (Calmar Ratio).
’ADD’: Average Drawdown of uncompounded returns.
’CDaR’: Conditional Drawdown at Risk of uncompounded returns.
obj (str can be {'MinRisk', 'Utility', 'Sharpe' or 'MaxRet'.) –
Objective function of the optimization model. The default is ‘Sharpe’. Posible values are:
’MinRisk’: Minimize the selected risk measure.
’Utility’: Maximize the Utility function \(mu w - l \phi_{i}(w)\).
’Sharpe’: Maximize the risk adjusted return ratio based on the selected risk measure.
’MaxRet’: Maximize the expected return of the portfolio.
rf (float, optional) – Risk free rate, must be in the same period of assets returns. The default is 0.
l (scalar, optional) – Risk aversion factor of the ‘Utility’ objective function. The default is 2.
hist (bool, optional) – Indicate if uses historical or factor estimation of returns to calculate risk measures that depends on scenarios (All except ‘MV’ risk measure). The default is True.
- Returns
w – The weights of optimum portfolio.
- Return type
DataFrame
-
frontier_limits
(model='Classic', rm='MV', rf=0, hist=True)[source]¶ Method that calculates the minimum risk and maximum return portfolios available with current assets and constraints.
- Parameters
model (str, optional) – Methodology used to estimate input parameters. The default is ‘Classic’.
rm (str, optional) – Risk measure used by the optimization model. The default is ‘MV’.
rf (scalar, optional) – Risk free rate. The default is 0.
hist (bool, optional) – Indicate if uses historical or factor estimation of returns to calculate risk measures that depends on scenarios (All except ‘MV’ risk measure). The default is True.
- Returns
limits – A dataframe that containts the weights of the portfolios.
- Return type
DataFrame
Notes
This method is preferable (faster) to use instead of efficient_frontier method to know the range of expected return and expected risk.
-
efficient_frontier
(model='Classic', rm='MV', points=20, rf=0, hist=True)[source]¶ Method that calculates several portfolios in the efficient frontier of the selected risk measure, available with current assets and constraints.
- Parameters
model (str, optional) – Methodology used to estimate input parameters. The default is ‘Classic’.
rm (str, optional) – Risk measure used by the optimization model. The default is ‘MV’.
points (scalar, optional) – Number of point calculated from the efficient frontier. The default is 50.
rf (scalar, optional) – Risk free rate. The default is 0.
hist (bool, optional) – Indicate if uses historical or factor estimation of returns to calculate risk measures that depends on scenarios (All except ‘MV’ risk measure). The default is True.
- Returns
frontier – A dataframe that containts the weights of the portfolios.
- Return type
DataFrame
Notes
It’s recommendable that don’t use this method when there are too many assets (more than 100) and you are using a scenario based risk measure (all except standard deviation). It’s preferable to use frontier_limits method (faster) to know the range of expected return and expected risk.
Bibliography¶
- A1
Harry Markowitz. Portfolio selection. The Journal of Finance, 7(1):77–91, 1952. URL: http://www.jstor.org/stable/2975974.
- A2
Hiroshi Konno and Hiroaki Yamazaki. Mean-absolute deviation portfolio optimization model and its applications to tokyo stock market. Management Science, 37(5):519–531, 1991. URL: https://EconPapers.repec.org/RePEc:inm:ormnsc:v:37:y:1991:i:5:p:519-531.
- A3
Renata Mansini, W. Ogryczak, and M.Grazia Speranza. Twenty years of linear programming based portfolio optimization. European Journal of Operational Research, 234:518–535, 04 2014. doi:10.1016/j.ejor.2013.08.035.
- A4(1,2)
Renata Mansini, W. Ogryczak, and M.Grazia Speranza. Linear Models for Portfolio Optimization, pages 19–45. Springer, 01 2015. doi:10.1007/978-3-319-18482-1_2.
- A5
R. Tyrrell Rockafellar and Stanislav Uryasev. Optimization of conditional value-at-risk. Journal of Risk, 2:21–41, 2000.
- A6
Renata Mansini, W. Ogryczak, and M.Grazia Speranza. On lp solvable models for portfolio selection. Informatica, 14:37–62, 01 2003.
- A7(1,2,3)
A. Chekhlov, S. Uryasev, and M. Zabarankin. Portfolio Optimization With Drawdown Constraints. In Panos M Pardalos, Athanasios Migdalas, and George Baourakis, editors, Supply Chain And Finance, volume of World Scientific Book Chapters, chapter 13, pages 209–228. World Scientific Publishing Co. Pte. Ltd., edition, November 2004. URL: https://ideas.repec.org/h/wsi/wschap/9789812562586_0013.html, doi:.