You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Negative gamma in L2_reg doens't work (OptimizationError) #101
In comment #13 (comment), you said we could go for asset centralization using the L2_reg objective with negative gamma, but in all my tests, I always got an "OptimizationError" when I tried this.
Do you know whats may be happening? It's a normal behavior?
risk_free_rate = 0.035
mu = mean_historical_return(sec_data)
S = CovarianceShrinkage(sec_data).ledoit_wolf()
ef = EfficientFrontier(mu, S)
ef.add_objective(objective_functions.L2_reg, gamma=-0.1)
weights = ef.max_sharpe(risk_free_rate)
cleaned_weights = ef.clean_weights()
The error:
~\Anaconda3\envs\pyfinance\lib\site-packages\pypfopt\base_optimizer.py in _solve_cvxpy_opt_problem(self)
196 opt.solve()
197 except (TypeError, cp.DCPError):
--> 198 raise exceptions.OptimizationError
199 if opt.status != "optimal":
200 raise exceptions.OptimizationError
OptimizationError: Please check your objectives/constraints or use a different solver.
This error also occurs when the number of dataset entries is smaller than 250 days.
Thanks for reaching out! PyPortfolioOpt recently migrated its backend to use proper convex programming. The difference of two convex functions is not necessarily convex, so pypfopt is correct in returning an optimisation error.
My current recommendation is to perform a standard optimisation with no L2 reg. Then remove the k smallest weights. The resulting portfolio may not sum to 1, but this is fixed by w = w / w.sum().
This error also occurs when the number of dataset entries is smaller than 250 days.
How many stocks are you trying to optimise for? If you have many stocks but not much historical data, the sample covariance matrix may not be positive semidefinite (in which case optimisation may fail).
And what about the opposite? Now I want to go for max sharpe on diversification.
Is It better to add an objective_functions.L2_reg and increase gamma or decrease the high weight_bounds on EfficientFrontier?
On my tests, I'm also experiencing some OptimizationErrors depending on what values of gamma, weight_bounds and time window I'm using (on 50 assets portfolio).
How to track what is causing those OptimizationErrors? Should I check the cov matrix?
And what about the opposite? Now I want to go for max sharpe on diversification.
Is It better to add an objective_functions.L2_reg and increase gamma or decrease the high weight_bounds on EfficientFrontier
I can't really say a priori – you have to try both. I would lean towards max_sharpe with the gamma parameter, because it results in more "natural" portfolios. Using a weight bound is quite crude, and will result in lots of assets hitting that upper bound.
On my tests, I'm also experiencing some OptimizationErrors depending on what values of gamma, weight_bounds and time window I'm using (on 50 assets portfolio).
I realised that PyPortfolioOpt was hiding a lot of information, so I recently released an update which should provide more detailed error messages, telling you exactly which part is failing.
Please pip install -U PyPortfolioOpt for the latest version, and check that pypfopt.__version__ gives 1.2.0.
In any case, my guesses for what's causing the errors are as follows:
The weight_bounds error is typically raised when there is no feasible portfolio, e.g you are trying to build a portfolio of 50 assets with lower weight bound of 0.05 (this would sum up to more than 1).
As for gamma, if you are using min_volatility, the value of gammashouldn't affect the convex optimiser.
For the time-window, I believe the problem will be something to do with the positive-definiteness of the covariance matrix. You could try risk_models._is_positive_semidefinite(S) to check.
Activity
robertmartin8 commentedon Apr 23, 2020
Hi @almirb,
Thanks for reaching out! PyPortfolioOpt recently migrated its backend to use proper convex programming. The difference of two convex functions is not necessarily convex, so pypfopt is correct in returning an optimisation error.
My current recommendation is to perform a standard optimisation with no L2 reg. Then remove the k smallest weights. The resulting portfolio may not sum to 1, but this is fixed by
w = w / w.sum().How many stocks are you trying to optimise for? If you have many stocks but not much historical data, the sample covariance matrix may not be positive semidefinite (in which case optimisation may fail).
Best,
Robert
almirb commentedon Apr 23, 2020
Thank you @robertmartin8 ! Have a nice day!
almirb commentedon Apr 28, 2020
Hello @robertmartin8 !
And what about the opposite? Now I want to go for max sharpe on diversification.
Is It better to add an
objective_functions.L2_regand increase gamma or decrease the highweight_boundsonEfficientFrontier?On my tests, I'm also experiencing some
OptimizationErrorsdepending on what values ofgamma,weight_boundsand time window I'm using (on 50 assets portfolio).How to track what is causing those
OptimizationErrors? Should I check the cov matrix?Thanks again!
robertmartin8 commentedon Apr 28, 2020
Hello again,
I can't really say a priori – you have to try both. I would lean towards
max_sharpewith the gamma parameter, because it results in more "natural" portfolios. Using a weight bound is quite crude, and will result in lots of assets hitting that upper bound.I realised that PyPortfolioOpt was hiding a lot of information, so I recently released an update which should provide more detailed error messages, telling you exactly which part is failing.
Please
pip install -U PyPortfolioOptfor the latest version, and check thatpypfopt.__version__gives 1.2.0.In any case, my guesses for what's causing the errors are as follows:
weight_boundserror is typically raised when there is no feasible portfolio, e.g you are trying to build a portfolio of 50 assets with lower weight bound of 0.05 (this would sum up to more than 1).gamma, if you are usingmin_volatility, the value ofgammashouldn't affect the convex optimiser.risk_models._is_positive_semidefinite(S)to check.Hope this helps!
Robert
almirb commentedon Apr 28, 2020
Thanks @robertmartin8 ! You are "the guy"! :)