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

52 views (last 30 days)

Show older comments

Guro Larsgaard
on 15 Mar 2019

Answered: Alejandra Pena-Ordieres
on 3 Dec 2021

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

C = readtable('V strong.xlsx')

Covariance = table2array(C)

M = readtable('m.xlsx')

Mean_return = table2array(M)

M1 = readtable('h.xlsx')

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

options = optimoptions('quadprog','Display','iter','TolFun',1e-10);

tic

[x1,fval1] = solve(portprob,'Options',options);

toc

C2 = readtable('V1.xlsx')

Covariance2 = table2array(C2) %Covariance Matrix All Assets

ReturnStrong = value'*Mean_return

VarStrong = value'*Covariance2*value

RiskStrong = sqrt(VarStrong)

##### 2 Comments

John D'Errico
on 15 Mar 2019

Edited: John D'Errico
on 15 Mar 2019

### Accepted Answer

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

##### 0 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!