# How can I find the portfolio with maximum Sharpe Ratio - Using Lagrange Multipliers

In Markowitz' portfolio theory we can construct portfolios with the minimum variance for a given expected return (or vice versa). Across expected risks, this traces out the well-known efficient frontier.

To find the so-called tangency portfolio, we look to solve:

$$\max_x \frac{\mu^T x}{\sqrt{x^T Q x}}$$

Following Tütüncü (section 5.2), this can be reformulated under a change of variables to a simpler quadratic optimisation problem:

$$\min_{y,\kappa} y^T Q y \qquad \text{where} \quad (\mu-r_f)^T y = 1,\; \kappa > 0$$

I've solved the problem and got values for $y$. However.. $\kappa$ is defined in terms of $x$... So, whilst I'm sure this is a stupid question, how do we actually translate the $y$ vector to recover the true portfolio weights $x$??

The only thing I can think of is that I did not include a constraint for $\kappa$. This is for the same reason as above (that it is defined in terms of $x$, and so not available), and because the KKT conditions suggested in this answer also ignore the $\kappa >0$ term.

• The $\kappa$ must be related to the other variables in the problem, otherwise your formulation does not make much sense. Commented Apr 5, 2018 at 20:45
• I agree; it doesn't make sense to me. $\kappa$ is introduced on p62 of the book linked in the question, and the formulation is at the bottom of the same page, so perhaps I'm missing a nuance in the mathematical derivation, and how the feasible set $\chi$ is defined?
– Zac
Commented Apr 6, 2018 at 0:59

The trick is in the transformation of the constraints used to solve the optimisation problem. This can be seen in the definition of the set $\chi^+$ in the two lines following equation 5.4 of Tütüncü. So, for example, the usual budget constraint ($e^Tx = 1$) would be replaced by ($e^Tx - \kappa = 0$). After the addition of that constraint, the solution with the maximum Sharpe ratio is $x^* = \frac{\hat{x}}{\hat{\kappa}}$, where $(\hat{x},\hat{\kappa})$ is the solution to the quadratic programming problem (see bottom of page 62).

• The formulation allows for a variable $\kappa$ that can take any positive value. In your code, you are using $\kappa$ as a constraint in your Lagrangean, when you should be augmenting your solution vector with a $\kappa$. In other words, you should be solving for $(x, \kappa)$ rather than $x$. Commented Apr 6, 2018 at 14:44
• I think that’s the wrong approach. Your Lagrangean is $L(x, \kappa, \lambda_1, \lambda_2)$, and you should be solving for $(x, \kappa, \lambda_1, \lambda_2)$. Hence, your $A$ matrix should be $(N+3) \times (N+3)$. There should be an extra row for $\frac{\partial L}{\partial \kappa} = \lambda_1$. Your $b$ values should be all 0, apart from the $\lambda_2$ entry, which should be 1. Commented Apr 6, 2018 at 17:44

H is the hessian matrix

f = [0;0;0;0;0;0;0;0;0;0;0];
n = 10;
rf = 0.0082;
ExpReturns =  -0.00591 + 0.002 * (1:10)';

% Optimization problem data
lb = zeros(n+1,1);
ub = inf*ones(n+1,1);
F = ones(n,1);
Aeq = [( AvrReturn- rf)' 0;ones(1,n) -1];
beq = [1; 0];
A = [eye(n),-1*ones(n,1)];
b = zeros(n,1);

As you follow through the derivation, you'll note that $$y = \lambda \cdot x$$. This transformation was created to remove the numerator from the main optimization problem, and reframe it as a boundary condition $$(\mu^T - \mathbb{1}^T) \cdot y = 1$$.
After you've finished the optimization problem and obtained your y vector, you now have to tackle $$x = \frac{y}{\lambda}$$. You don't need to keep track of this transformation constant during the problem, because we know that by the nature of portfolio weights, $$\mathbb{1}^T\cdot x = 1$$, or rather the sum of all weights should equal 1. So all you have to do now, is normalize your y vector - that is, calculate the sum of your values in the y vector, and use that value as the scalar divisor for y to obtain x. $$x = \frac{y}{\sum^{len(y)}_{i=1}y_i}$$.