robertmartin8 / PyPortfolioOpt

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

OptimizationError: Please check your objectives/constraints or use a different solver. #591

Open indilyone opened 6 months ago

indilyone commented 6 months ago

What are you trying to do? Clear description of the problem you are trying to solve with PyPortfolioOpt

What have you tried?

What data are you using? What asset class, how many assets, how many data points. Preferably provide a sample of the dataset as a csv attachment.

In this excel, the returns are in sheet "sheet1" and covariance matrix is in sheet "cov".

I am trying to do a basic mean variance optimization using ef.min_volatility() but errors

OptimizationError: Please check your objectives/constraints or use a different solver.

I also tried using non-convex optimization in the cookbook example, but the result ended up being the 1/n weights.

target_risk = 0.05 ef = EfficientFrontier(mu, S, weight_bounds=(0, 0.5))

Weights sum to zero

weight_constr = {"type": "eq", "fun": lambda w: np.sum(w)-1}

Portfolio vol less than target vol

risk_constr = { "type": "eq", "fun": lambda w: target_risk ** 2 - np.dot(w.T, np.dot(ef.cov_matrix, w)), } constraints = [weight_constr, risk_constr]

ef.nonconvex_objective( lambda w, mu: -w.T.dot(mu), # min negative return i.e max return objective_args=(ef.expected_returns,), weights_sum_to_one=True, constraints=constraints,

)

weights = ef.clean_weights() weights

COV.xlsx