How to find tangency portfolio (maximize sharpe ratio) using quadprog
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
John D'Errico
2019-3-15
编辑:John D'Errico
2019-3-15
Note that just as you have learned to assign a field in a structure, you can access it too using the dot, without using getfield.
value = x1.x
John D'Errico
2019-3-15
编辑:John D'Errico
2019-3-15
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
2021-12-3
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/822775/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/822780/image.png)
and the weights will be given by
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/822785/image.png)
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Portfolio Optimization and Asset Allocation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!