Skip to content

Negative gamma in L2_reg doens't work (OptimizationError)  #101

Closed

Description

@almirb

Hello @robertmartin8 ! How are you?

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!

Activity

  1. robertmartin8 commented on Apr 23, 2020

    @robertmartin8
    Collaborator

    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().

    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).

    Best,
    Robert

  2. added
    bug
    on Apr 23, 2020
  3. almirb commented on Apr 23, 2020

    @almirb
    Author

    Thank you @robertmartin8 ! Have a nice day!

  4. almirb commented on Apr 28, 2020

    @almirb
    Author

    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_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?

    Thanks again!

  5. reopened this on Apr 28, 2020
  6. robertmartin8 commented on Apr 28, 2020

    @robertmartin8
    Collaborator

    Hello again,

    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 gamma shouldn'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.

    Hope this helps!

    Robert

  7. added
    question
    and removed
    bug
    on Apr 28, 2020
  8. almirb commented on Apr 28, 2020

    @almirb
    Author

    Thanks @robertmartin8 ! You are "the guy"! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions