robertmartin8 / PyPortfolioOpt

Financial portfolio optimisation in python, including classical efficient frontier, Black-Litterman, Hierarchical Risk Parity
https://pyportfolioopt.readthedocs.io/
MIT License
4.38k stars 940 forks source link

ArpackNoConvergence: ARPACK error -1: No convergence (1511 iterations, 0/1 eigenvectors converged) #375

Closed didataai closed 2 years ago

didataai commented 2 years ago

Hi, I´m getting errors when I try to get to optimize my portfolio using Bitcoin and Stocks; The error occurs when I put bitcoin together only.I have checked the DATA (date), price, etc . Seems to be ok; Total 326 assets;

ef.max_sharpe(rfrate) ArpackNoConvergence: ARPACK error -1: No convergence (3251 iterations, 0/1 eigenvectors converged)

Likewise, a possible workaround would be to increase the number of interactions.

here

I have not figured out how, and where, So, wondering if you have any idea?

Thanks in advance!

didataai commented 2 years ago

btw - using semi-covariance, instead - it works.

robertmartin8 commented 2 years ago

Hi @didataai,

I haven’t seen that error before - could you paste a code sample here?

didataai commented 2 years ago

Hi @robertmartin8,

I have updated my data-quotes for bitcoin and stocks, thereby, I could reproduce the error; it is sort of weird. Anyway, now I´m getting error related to Solver . It happens when I´m using bitcoin with another class of asset, like US Stocks for example, if I run with Bitcoin only, it works.

---------------------------------------------------------------------------
DCPError                                  Traceback (most recent call last)
~\AppData\Roaming\Python\Python38\site-packages\pypfopt\base_optimizer.py in _solve_cvxpy_opt_problem(self)
    238             else:
--> 239                 self._opt.solve(verbose=self._verbose, **self._solver_options)
    240         except (TypeError, cp.DCPError) as e:

~\anaconda3\lib\site-packages\cvxpy\problems\problem.py in solve(self, *args, **kwargs)
    461             solve_func = Problem._solve
--> 462         return solve_func(self, *args, **kwargs)
    463 

~\anaconda3\lib\site-packages\cvxpy\problems\problem.py in _solve(self, solver, warm_start, verbose, gp, qcp, requires_grad, enforce_dpp, **kwargs)
    948 
--> 949         data, solving_chain, inverse_data = self.get_problem_data(
    950             solver, gp, enforce_dpp, verbose)

~\anaconda3\lib\site-packages\cvxpy\problems\problem.py in get_problem_data(self, solver, gp, enforce_dpp, verbose)
    569             self._cache.invalidate()
--> 570             solving_chain = self._construct_chain(
    571                 solver=solver, gp=gp, enforce_dpp=enforce_dpp)

~\anaconda3\lib\site-packages\cvxpy\problems\problem.py in _construct_chain(self, solver, gp, enforce_dpp)
    797         self._sort_candidate_solvers(candidate_solvers)
--> 798         return construct_solving_chain(self, candidate_solvers, gp=gp,
    799                                        enforce_dpp=enforce_dpp)

~\anaconda3\lib\site-packages\cvxpy\reductions\solvers\solving_chain.py in construct_solving_chain(problem, candidates, gp, enforce_dpp)
    154         return SolvingChain(reductions=[ConstantSolver()])
--> 155     reductions = _reductions_for_problem_class(problem, candidates, gp)
    156 

~\anaconda3\lib\site-packages\cvxpy\reductions\solvers\solving_chain.py in _reductions_for_problem_class(problem, candidates, gp)
     90                        "Consider calling solve() with `qcp=True`.")
---> 91         raise DCPError(
     92             "Problem does not follow DCP rules. Specifically:\n" + append)

DCPError: Problem does not follow DCP rules. Specifically:
The following constraints are not DCP:
Sum(var935, None, False) == var945 @ var1012 , because the following subexpressions are not:
|--  var945 @ var1012
@ Promote(var945, (101,)) @ Promote(var1012, (101,))

The above exception was the direct cause of the following exception:

OptimizationError                         Traceback (most recent call last)
<ipython-input-55-6600e60796fe> in <module>
      5 ef = EfficientFrontier(mu, S)
      6 ef.max_sharpe(rfrate)
----> 7 cleaned_weights = ef.max_sharpe(rfrate)
      8 perf = ef.portfolio_performance(verbose=True)

~\AppData\Roaming\Python\Python38\site-packages\pypfopt\efficient_frontier\efficient_frontier.py in max_sharpe(self, risk_free_rate)
    281         ] + new_constraints
    282 
--> 283         self._solve_cvxpy_opt_problem()
    284         # Inverse-transform
    285         self.weights = (self._w.value / k.value).round(16) + 0.0

~\AppData\Roaming\Python\Python38\site-packages\pypfopt\base_optimizer.py in _solve_cvxpy_opt_problem(self)
    239                 self._opt.solve(verbose=self._verbose, **self._solver_options)
    240         except (TypeError, cp.DCPError) as e:
--> 241             raise exceptions.OptimizationError from e
    242 
    243         if self._opt.status not in {"optimal", "optimal_inaccurate"}:

OptimizationError: Please check your objectives/constraints or use a different solver.
didataai commented 2 years ago
rfrate = 0.09

mu = expected_returns.mean_historical_return(df)
S = risk_models.sample_cov(df) 
ef = EfficientFrontier(mu, S)
ef.max_sharpe(rfrate)
cleaned_weights = ef.max_sharpe(rfrate) 
perf = ef.portfolio_performance(verbose=True)
robertmartin8 commented 2 years ago

Hi @didataai, based on your code snipper the problem is that you are calling ef.max_sharpe twice – each call adds new constraints and transforms variables so calling it twice leads to infeasible optimisation problems.

Cheers, Robert

didataai commented 2 years ago

Hi @robertmartin8,
Thank you, initially for sharing a great lib - also, to support it .! []´s