CPLEX solver in MATLAB: maximizing Sharpe Ratio

342 views
Skip to first unread message

Tommy Lavertes

unread,
Feb 5, 2017, 11:55:43 AM2/5/17
to YALMIP
Hello, I was wondering if you can help me implement a Sharpe ratio optimization problem in CPLEX for matlab.
I read your post for YALMIP portfolio optimization but I can't seem to reproduce the same results when I try to change my Sharpe ratio optimization problem into the convex version you utilized.

This is my code:
-------------------------------------------------------------------------------------------------------------------
function [weights_new] = max_sharpe_ratio(asset_init, exp_ret, Q, risk_free)
% Computes the maximum Sharpe Ratio using CPLEX solver

% Set variable upper and lower bounds
n = length(asset_init);
k = 1;
lb = k*ones(n,1);
ub = lb;

% Set linear aspect of objective function
c = zeros(n,1);

% Set linear constraints
A1 = exp_ret - risk_free;
A2 = ones(1,n);
lhs = 1;
rhs = lhs;

% Compute optimal portfolio weights
cplex1.Cplex('Sharpe');
cplex1.Model.sense = 'minimize';
cplex1.addcols(c, [], lb, ub);
cplex1.addrows(k, A2, k);
cplex1.addrows(lhs, A1', rhs);
cplex1.Model.Q = 2*Q;
cplex1.solve();

% New optimal weights
weights_new = cplex1.Solution.x / sum(cplex1.Solution.x);
-------------------------------------------------------------------------------------------------------------------

My function tries to solve the optimization problem for:

min        (y^T)*Q*y
s.t.         sum((exp_ret - risk_free)*y) = 1
              sum(y) = k
              k*lower bound <= y <= k*upper bound
              k >= 0
whereby my optimal weights = y/k

My CPLEX solver always gives me an error string of "Infeasible" and so I cannot compute properly

Johan Löfberg

unread,
Feb 5, 2017, 12:43:53 PM2/5/17
to YALMIP
No idea, There's a reason we use modeling layers

Tommy Lavertes

unread,
Feb 5, 2017, 2:16:59 PM2/5/17
to YALMIP
Then is there a way to implement your portfolio example into a form CPLEX understands?
So CPLEX takes in optimization problems in the following form:

minimize      (c^T)*x + 0.5*(x^T)*Q*x       % Objective quadratic/linear function 
s.t               bl <= A*x <= bu                  % Linear constraints (equalities has bl = bu)
                   (d^T)*x + (x^T)*Q*x <= r      % Quadratic constraints
                   l <= x <= u                         % Variable bounds

Johan Löfberg

unread,
Feb 6, 2017, 1:42:12 AM2/6/17
to YALMIP
Not sure what you mean. 

Via YALMIP, you can export the model to cplex format if you want (using the command export)
Reply all
Reply to author
Forward
0 new messages