I'm trying to use Gurobi to model an optimization max function whose objective function is $$f(x)=\frac{r^Tx}{x^TQx}.$$

Thus maximizing this function $f(x)$ is the same as maximizing $\log f(x)$ and I want to implement it in Gurobi. My understanding is Gurobi doesn't handle log objective functions so I've made a linear maximization function $z(x) = t$ as follows: \begin{align}\max&\quad z(x)=t\\\text{s.t.}&\quad\log(r^Tx) - \log(x^TQx) - t = 0\tag{C1}\\&\quad\vdots\quad\text{(other equality constraints)}.\end{align} Does anyone know how to implement constraint C1 in Gurobi?

  • 1
    $\begingroup$ The Sharpe ratio can be cast as a SOCP. We have a blog post about it at themosekblog.blogspot.com/2020/10/… $\endgroup$ Dec 10, 2020 at 6:13
  • $\begingroup$ Or even simpler a QP (for those not lucky enough to have an SOCP solver) $\endgroup$ Dec 10, 2020 at 8:22
  • $\begingroup$ ...but the important question is if you really wrote your objective correctly? Most likely you want to maximize the so called Sharpe ratio, and then you want to have the square root of the quadratic term. Otherwise the problem i most likely ill-posed $\endgroup$ Dec 10, 2020 at 9:06
  • $\begingroup$ Yes it'll be std deviation but using the log delta that would simply become a scalar $\endgroup$ Dec 10, 2020 at 17:38
  • $\begingroup$ Then you should keep the standard Sharpe model, as it allows you to use a very standard QP formulation of the objective (you don't want to unnecessarily solve it is as a nonlinear program as you try to do now) $\endgroup$ Dec 10, 2020 at 19:36

1 Answer 1


I will make the following assumptions:

  1. $Q$ is symmetric positive semidefinite
  2. $r^Tx \ge 0$

Note that maximizing $\frac{r^Tx}{X^TQx}$ is equivalent to minimizing $\frac{X^TQx}{r^Tx}$ .

Under the stated assumptions, this is a convex optimization problem, which can be submitted to and solved by Gurobi.

The easiest way of doing so is using a convex optimization tool such as CVX or CVXPY. The CVX code would be

cvx_solver gurobi
variable x(n)

which uses the CVX function quad_over_lin. chol(A) is the upper triangular Cholesky factor of A.

CVXPY also has the same quad_ovdr_lin function as CVX, but depending on which function is called to compute the Cholesky factor, you would need to apply a transpose to the Cholesky factor if a lower triangular Cholesky factor is generated

This could instead be done using a "native" Gurobi interface. In that case, you would need to minimize a new variable, t, subject to the rotated second order constraint:

norm(chol(A)*x) <= sqrt(t*r'*x), t >= 0, r'*x >= 0

using the appropriate syntax to specify this constraint for whatever interface you are using. As before, if chol(A) is lower triangular, you need to use its transpose.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.