0

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
maria saz
  • 191
  • 1
  • 10

1 Answers1

3

I believe this is not convex. From what I understand there are several ways to attack this problem

  1. Use a general purpose NLP solver (this is the method I used)
  2. Trace the efficient frontier to find the point on this frontier with the best Sharpe Ratio
  3. Under some conditions, this problem can be transformed into a convex QP (see e.g. Gerard Cornuejols, Reha Tütüncü, Optimization Methods in Finance, 2007).
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39