Mean Variance optimisation (self-financing strategy)

15 views (last 30 days)
Hi,
I want to change my weights in order to find the optimal weights which will maximise a sharpe ratio. I am looking at the past 12 data points to find a mean return and a variance covariance matrix and I want to calculate the optimal weights for the maximum sharpe ratio on a rolling one month basis. I have tried using the MATLAB functions, however, they all specify that the sum of weights must equal 1. In my case, as it is a self-financing strategy, the sum of all my weights must equal 0 and vary between -1 and 1.
I have calculated my variance covariance matrices and mean returns with the following:
RETURNS = xlsread('RETS.xlsx');
[N K] = size(RETURNS)
%N=168 - 168 months worth of data (monthly)
%K=9- different currencies/stocks
bin = 12; %How many returns to look back At
V = zeros(K,K);
RET = zeros(bin,K);
rf = 0; % Risk free
for i = 1:N-(bin-1)
RET = RETURNS(i:i+bin-1,:);
R = mean(RET);
R = R';
V = cov(RET);
W = ones(1,K);
SharpeRatio= (W*R-rf)./(W*V*W')
%I need to vary the weights,W, in order to find the optimal weights which
%would maximise the SharpeRatio defined above.
end
I really do not know how to vary the weights in order to maximise the sharpe ratio. Do i need to make a function/function handle, how does one do that?
Any help would be greatly appreciated,
Warm regards,
Niv

Answers (1)

Matt J
Matt J on 2 Jun 2013
FMINCON will do it, if you have the Optimization Toolbox.
  2 Comments
Nirvair Vig
Nirvair Vig on 2 Jun 2013
Edited: Matt J on 2 Jun 2013
Hi Matt, many thanks for your quick response. I have modified my code to the following:
CT.m
clc
clear all
RETURNS = xlsread('AFR_CarryTrade.xlsx');
[N K] = size(RETURNS)
bin = 12; %How many returns to look back At
V = zeros(K,K);
RET = zeros(bin,K);
rf = 0; % Risk free
for i = 1:N-(bin-1)
RET = RETURNS(i:i+bin-1,:);
R = mean(RET);
R = R';
V = cov(RET);
k = 0;
W = ones(1,K);
SharpeRatio= (W*R-rf)./(W*V*W');
% TargetReturn=0.15;
X0=zeros(K,N);
Aeq = [R;ones(N,K)];
Beq = [0];
LB = ones(K,N)*-1;
UB = ones(K,N);
[OptimalWeights, OptimalVariance] = fmincon(@Weights,X0,[],[],Aeq,Beq,LB,UB);
end
%With
function [Weights] = f(W,V, R)
Weights = @(W)(W*R)./(W'*V*W)
%Being saved under optimalweight.m
I am getting an error, could you please advise where I am going wrong. Apparently its the Aeq line error: Dimensions of matrices being concatenated are not consistent.
Matt J
Matt J on 2 Jun 2013
Edited: Matt J on 2 Jun 2013
R does not have K columns and therefore cannot be concatenated to ones(N,K).
Also, Beq needs to have the same number of rows as Aeq.
Also, you have an error later since fmincon has no way of passing R to f(). Do this instead
[OptimalWeights, OptimalVariance] = fmincon(@(W)(W*R)./(W'*V*W),X0,[],[],Aeq,Beq,LB,UB);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!