4

I'm trying to maximize Sharpe's ratio using scipy.minimize

1

I do this for finding CAPM's Security Market Line

2

So I have an equation:

3

4

Optional (if short positions is not allowed):

5

So I'm trying solve this:

def target_func(x, cov_matix, mean_vector, virtual_mean):
    f = float(-(x.dot(mean_vector) - virtual_mean) / np.sqrt(x.dot(cov_matix).dot(x.T)))
    return f


def optimal_portfolio_with_virtual_mean(profits, virtual_mean, allow_short=False):
    x = np.zeros(len(profits))
    mean_vector = np.mean(profits, axis=1)
    cov_matrix = np.cov(profits)
    cons = ({'type': 'eq',
             'fun': lambda x: np.sum(x) - 1})
    if not allow_short:
        bounds = [(0, None,) for i in range(len(x))]
    else:
        bounds = None
    minimize = optimize.minimize(target_func, x, args=(cov_matrix, mean_vector, virtual_mean,), bounds=bounds,
                                 constraints=cons)
    return minimize

But I always get Success: False (iteration limit exceeded). I tried to set maxiter = 10000 option, but it didn't help.

I will be greatful for any help

P.S. I use python 2.7

Community
  • 1
  • 1
Gcinbax
  • 187
  • 3
  • 12
  • As a first step for debugging: add ```options={'disp': True}``` to the ```optimize.minimize``` function and post the whole output. By the way: Did you set maxiter variable as dict-entry like the disp-example above? This is the way to do! – sascha Oct 17 '16 at 16:24
  • @sascha Yes I did. My options dict is like options={'maxiter': 10000}. And scipy.optimize output approve that maxiter was set correctly – Gcinbax Oct 17 '16 at 16:31
  • Then show the output, incl. disp. – sascha Oct 17 '16 at 16:33
  • Ok. It's obvious that this is broken! You don't want to observe Nans. Because we don't know the data/whole code you have to check for the reasons of the nans for yourself i think. – sascha Oct 17 '16 at 16:39
  • 2
    The initial point 0 is really bad: the objective can not be evaluated there. Note that often problems involving the Sharpe ratio can be reformulated into convex QPs [link](http://yetanothermathprogrammingconsultant.blogspot.com/2016/08/portfolio-optimization-maximize-sharpe.html). – Erwin Kalvelagen Oct 17 '16 at 19:28

1 Answers1

1

I dont know why, but it works perfectly, when I replacing

x = np.zeros(len(profits))

With

x = np.ones(len(profits))
Gcinbax
  • 187
  • 3
  • 12
  • 1
    Because only local-convergence is guaranteed and a vector of ones seem to be a much better start-point. – sascha Oct 17 '16 at 16:38