When you invest in stocks you want to get a high return, but a low risk. There's also going to be a few constraints. That sounds like a job for cvxpy.
The new code, with constraints, now looks like this;
x = cp.Variable(len(mean_stock)) stock_return = mean_stock * x stock_risk = cp.quad_form(x, cov_stock) p = 1 objective = cp.Maximize(stock_return - p * stock_risk) constraints = [x >= 0, cp.sum(x) == 1] prob = cp.Problem(objective=objective, constraints=constraints) prob.solve(), x.value
Feedback? See an issue? Something unclear? Feel free to mention it here.
If you want to be kept up to date, consider getting the newsletter.