Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Min/Man number assets nor working well #399

Closed
amos-peter opened this issue Dec 5, 2021 · 4 comments
Closed

Min/Man number assets nor working well #399

amos-peter opened this issue Dec 5, 2021 · 4 comments
Labels
question Further information is requested

Comments

@amos-peter
Copy link

amos-peter commented Dec 5, 2021

Please guide me on how to get the right min/max value for the number of assets. I am working to maximize a set of score objectives with a min/max number of assets constraints.

# Construct efficient frontier
from pypfopt.risk_models import CovarianceShrinkage
import math

# Construct efficient frontier
mu = expected_returns.mean_historical_return(prices)
S = risk_models.sample_cov(prices)

#['CVXOPT', 'ECOS', 'ECOS_BB', 'GLPK', 'GLPK_MI', 'OSQP', 'SCIPY', 'SCS']
ef = EfficientFrontier(mu, S,weight_bounds=(0, 1),solver='ECOS_BB')

#min num assets
b = cp.Variable(len(ef.tickers), boolean=True)
ef.add_constraint(lambda x: x <= booleans)
ef.add_constraint(lambda x: cp.sum(booleans) == 6)

#min 0
ef.add_constraint(lambda x: x >= min_zero)
score_rank = mu.rank(pct = True)

#Max 0.4 
for i in range(len(prices.columns)):
    ef.add_constraint(lambda w: w[i] <= 0.4)

#wgt = ef.min_volatility()
wgt = ef.convex_objective(comp_score_obj, score=score_rank)
w_list = list(wgt.values())
clean_wgt = dict(ef.clean_weights())

#clean manually 
round_wgt = [round(num,3) for num in w_list]
round_wgt
@amos-peter amos-peter added the question Further information is requested label Dec 5, 2021
@phschiele
Copy link
Contributor

@amos-peter Can you specify what you mean by "right" min/max value for the number of assets?

@amos-peter
Copy link
Author

@phschiele It is working no problem using default objectives such min_vol or max_sharpe. However it is not working when i try using convex objective such as maximize a set of scores.

@amos-peter
Copy link
Author

@phschiele
i added this code:

booleans = cp.Variable(len(ef.tickers), boolean=True)
ef.add_constraint(lambda x: x <= booleans)
ef.add_constraint(lambda x: cp.sum(booleans) >= 3)

However it is not working if using convex objectives.

@phschiele
Copy link
Contributor

@amos-peter Happy to take a look if you can provide a full MWE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants