# How to find tangency portfolio (maximize sharpe ratio) using quadprog

15 views (last 30 days)
Hi,
We are performing a mean-var portfolio optimazation including serveral constraints. Using Mean-Variance Stochastic goal programming we have found the sub -optimal frontier accountin g for desierable weights in a subset of sustainable assets. Using the code below we were able to find the sub-optimal frontier changing the desired return level (r) . However, we now want to obtain the tangency portfolio as our selected optimal portfolio strategy, but we can´t figure out how. Given a risk free rate of 0.000412 does anyone know how we can find the tangency portfolio (i.e maximizing the sharp ratio) ?
Best regards ,
Agnethe and Guro
Covariance = table2array(C)
Mean_return = table2array(M)
Mean_return_h = table2array(M1)
nAsset = numel(Mean_return); r = 0.03
e_0 = 0.027900578
%Optimization Problem
portprob = optimproblem;
x = optimvar('x',nAsset,'LowerBound',0,'UpperBound',1);
objective = x'*Covariance*x;
portprob.Objective = objective;
sumcons = sum(x) == 1;
portprob.Constraints.sumcons = sumcons;
averagereturn = dot(Mean_return, x) >= r;
portprob.Constraints.averagereturn = averagereturn;
averagereturn2 = dot(Mean_return_h, x) >= e_0;
portprob.Constraints.averagereturn2 = averagereturn2;
%Run Optimization Problem
tic
[x1,fval1] = solve(portprob,'Options',options);
toc
Covariance2 = table2array(C2) %Covariance Matrix All Assets
ReturnStrong = value'*Mean_return
VarStrong = value'*Covariance2*value
RiskStrong = sqrt(VarStrong)
##### 2 CommentsShowHide 1 older comment
John D'Errico on 15 Mar 2019
Of course, now you just removed that line completely from your code. But now I note that the code you show never even assigns value, but you use it afterwards. If you just change your code randomly, nobody will ever be able to help you.

Alejandra Pena-Ordieres on 3 Dec 2021
Hi Guro,
I understand that you are trying to find the maximum Sharpe ratio portfolio using quadprog. Theoretically, the max Sharpe ratio portfolio is a nonlinear nonconvex problem that cannot be solved using quadprog (quadprog only solves quadratic problems). The natural formulation of the max Sharpe ratio problem looks as follows:
That being said, there is way to rewrite the max Sharpe ratio problem as a quadratic convex optimization problem given some mild assumtpions. The quadratic version of the max Sharpe ratio problem looks like:
and the weights will be given by .
Thus, the solution to the max Sharpe ratio problem can be obtained with the following code:
% Excess returns
mu = muGross - riskFreeRate;
% Portfolio problem
prob = optimproblem('ObjectiveSense','minimize');
% Variables
% Surrogate portfolio weights
y = optimvar('y',nAssets,1);
% Auxiliary variable
tau = optimvar('tau',1,1,'LowerBound',0); % tau >= 0
% Objective
% min y'*Sigma*y
prob.Objective = y'*Sigma*y;
% Constraints
% Sum of means equal to 1
prob.Constraints.sigmaSumToOne = mu'*y == 1;
% Linear equality constraints
prob.Constraints.equalities = A*y == b*tau;
% Linear inequality constraints
prob.Constraints.inequalities = D*y <= d*tau;
% Solve problem
options = optimoptions('quadprog','Display','iter'); % Any other options
x = solve(prob,'options',options);
w = x.y/x.tau;
Notice that to add the risk-free rate, the mean μ should represent the vector of expected excess returns, i.e., the difference between the gross return and the risk-free rate.
An easier way to solve this problem is to use the Portfolio object in MATLAB:
% Create Portfolio object
p = Portfolio('AssetCovar',Sigma,'AssetMean',muGross);
p = setDefaultConstraints(p); % Long-only fully invested
% Set equalities -- Ax = b
p = setEquality(p,A,b);
% Set inequalities -- Dx <= d
p = setInequality(p,D,d);
% Set risk free rate
p.RiskFreeRate = riskFreeRate;
% Find max Sharpe Ratio portfolio
w2 = estimateMaxSharpeRatio(p);
Hope this helps.
Alejandra