I'm trying to use CVXPY to maximise the Sharpe Ratio of a stock portfolio.
The variable w is a portfolio weight vector, Sigma is an nxn correlation matrix, mu - is the average return of each portfolio stock, and rf - the risk-free rate (a scalar value).
At first, I tried to construct the problem as: Maximise((ret-rf)/(sqrt(risk))), which raised a TypeError: Can only divide by a scalar constant. I tried bypassing this issue by taking the log of the value I'm trying to maximise, however now I am getting an "invalid syntax" raised by 'prob.solve()'. I'm pretty sure that the issue arising from the maximisation formula, but I'm not sure what it is.
(I've tried both CVXPY log formulas, namely log_det() and log_sum_exp())
Here's the code below:
from cvxpy import *
def portfolio(mu, Sigma, rf):
n = len(mu)
w = Variable(n)
ret = mu.T*w
risk = quad_form(w, Sigma)
prob = Problem(Maximize(log_det(ret-rf)-log_det(sqrt(risk)),
[sum_entries(w) == 1])
prob.solve()
return w.value