Code covered by the BSD License  

Highlights from
Efficient Frontier using different risk return measures

image thumbnail

Efficient Frontier using different risk return measures

by

 

27 Aug 2012 (Updated )

Code for finding optimal portfolios and plotting efficient frontier for diff. risk return measures

Efficient_frontiers.m
%---------Code for plotting efficient frontiers using different -----------
%---------------------measures of risk and return--------------------------


%This is the parent code that contains codes used in creating efficient ...
%frontier and finding optimal portfolio allocations. The code plots
%efficient frontier and finds optimal portfolio based on three frameworks;
%Mean-Variance, Mean-Semi Variance and Mean-VaR. In each framework 
%portfolio reuturn (Y-axis for efficient frontier graph) is a weighted
%average of individual asset's expected return. The risk for Mean-variance
%framework is measured by standard deviation of the portfolio returns, for
%Mean-Semi Variance framework it is measured by Semi Standard Deviation of
%Portfolio Returns and for Mean-VaR framework it is measured by VaR at a
%given confidence level.

%--------------------------------------------------------------------------
%--------------------------------------------------------------------------

%Declaration of variables
global VCV meanret corp ptile wght;

%Code for extracting data from Excel sheet
%The data format:1st column of Excel sheet should contain data for high
%yield bonds. The 2nd column is for investment grade bond or less risky 
%asset time series.
pathname=input('Please input the full pathname of the file: ','s');
sheet=input('Please enter the sheet number: ');
[data,text,raw]=xlsread(pathname,sheet);

%Code for determining the lenth of each time series and number of assets
dtasiz=size(data);

%Setting "hycorp" variable as high yield bond time series and "ivcorp" 
%as investment grade bond time series.
hycorp=data(:,2);
ivcorp=data(:,3);

%Code for vertical concatenation of two time series in "corp" variable
corp=[hycorp ivcorp];

%Creating a weight variable where 1st column represents weight of high
%yield bond and 2nd column represents weight of investment grade bonds in a
%portfolio. the weight for high yield bond increase from 0 to 1 and vice-
%versa for investment grade bonds.
w(:,1)=0:.0001:1;
w(:,2)=1:-.0001:0;

%creating a variable for initilizing weights and setting
%up the constraints for optimization.
weights=[.5;.5];
aeq=ones(1,2);
beq=1;
lb=zeros(2,1);
ub=ones(2,1);

%code to find mean, skewness and kurtosis of initial time series of returns
meanret=mean(corp);
skew=skewness(corp);
kurt=kurtosis(corp);





%Efficient frontier using Returns and Standard Deviation as measures
%code for creating variance covariance martix
meanvar.stdev=std(corp);
meanvar.correl=corrcoef(corp);
meanvar.VCV=corr2cov(meanvar.stdev,meanvar.correl);
VCV=meanvar.VCV;
%creating a vector of portfolio returns for differnt weight allocations.
meanvar.portret=w*meanret';
%calculating vector of risk (standard deviation) for different weight
%allocations.
meanvar.risk1=w*meanvar.VCV;
meanvar.risk2=meanvar.risk1.*w;
meanvar.portrisk=sqrt(sum(meanvar.risk2,2));
%Code for computing optimal portfolio weights, risk and return based on
%maximizing Sharpe's ratio.
[meanvar.opmvwgts,meanvar.sharp]=fmincon(@Sharpe,weights,[],[],aeq,beq,lb,ub);
meanvar.sharp=-meanvar.sharp;
meanvar.opmvret=meanret*meanvar.opmvwgts;
meanvar.opmvrisk=sqrt(meanvar.opmvwgts'*meanvar.VCV*meanvar.opmvwgts);
%Code for calculating total return of optimal portfolio
meanvar.dailyret=corp*meanvar.opmvwgts;
meanvar.dailyret_1=meanvar.dailyret+1;
meanvar.totret=cumprod(meanvar.dailyret_1);

%Plotting Mean-Variance efficient frontier
plot(meanvar.portrisk*100,meanvar.portret*100,'b');
ylabel('Returns (%)');
xlabel('Standard Deviation (%)');
hold on;
plot(meanvar.opmvrisk*100,meanvar.opmvret*100,'ms');
grid on;
hold off;




%Efficient frontier using Semi Variance
%Code for asking the user whether he wants to use mean returns or user
%input returns for calculating semi standard deviation.
meansvar.bretcoic=input('Do you want to use average returns as benchmark returns Y/N: ','s');
if meansvar.bretcoic== 'Y'
    meansvar.bnchret=meanret;
else
    meansvar.bnchret(1,1)=input('Please enter Benchmark returns for High Yield Bonds: ');
    meansvar.bnchret(1,2)=input('Please enter Benchmark returns for Investable grade Bonds: ');
end
%code for formatting time series for calculating semi standard deviations.
%Semi standard deviation is deviation of returns below a given threshold
%level.
meansvar.bnchcorp=bsxfun(@minus,corp,meansvar.bnchret);
meansvar.bnchcorpmin(:,1)=min(meansvar.bnchcorp(:,1),0);
meansvar.bnchcorpmin(:,2)=min(meansvar.bnchcorp(:,2),0);
meansvar.semistd=std(meansvar.bnchcorpmin);
meansvar.semicorrel=corrcoef(meansvar.bnchcorpmin);
meansvar.semiVCV=corr2cov(meansvar.semistd,meansvar.semicorrel);
VCV=meansvar.semiVCV;
%creating a vector of portfolio returns for differnt weight allocations.
meansvar.semiportret=w*meanret';
%calculating vector of risk (Semi standard deviation) for different weight
%allocations.
meansvar.semirisk1=w*meansvar.semiVCV;
meansvar.semirisk2=meansvar.semirisk1.*w;
meansvar.semiportrisk=sqrt(sum(meansvar.semirisk2,2));
%Code for computing optimal portfolio weights, risk and return based on
%maximizing return-Semideviation ratio.
[meansvar.opmvwgts,meansvar.sharp]=fmincon(@Sharpe,weights,[],[],aeq,beq,lb,ub);
meansvar.sharp=-meansvar.sharp;
meansvar.opmvret=meanret*meansvar.opmvwgts; %Return of optimal portfolio
meansvar.opmvrisk=sqrt(meansvar.opmvwgts'*meansvar.semiVCV*meansvar.opmvwgts);
%Code for calculating total return of optimal portfolio
meansvar.dailyret=corp*meansvar.opmvwgts;
meansvar.dailyret_1=meansvar.dailyret+1;
meansvar.totret=cumprod(meansvar.dailyret_1);


%Plotting Mean-Semivariance efficient frontier
plot(meansvar.semiportrisk*100,meansvar.semiportret*100,'r');
ylabel('Returns (%)');
xlabel('Semi Standard Deviation (%)');
hold on;
plot(meansvar.opmvrisk*100,meansvar.opmvret*100,'ms');
grid on;
hold off;






% Efficient frontier using VaR
%Code for asking the user the percentile to use for calculating VaR
ptile=input('Please input the percentile you would like to use for calculating VaR: ');
wsiz=size(w);
%Loop for calulating VaR of portfolios with differnt asset allocations
for i=1:wsiz(1)
    var.varret=[];
    var.varportret=[];
    var.varret=bsxfun(@times,corp,w(i,:));
    var.varportret=sum(var.varret,2);
    var.VaR(i,1)=prctile(var.varportret,ptile);
end
%%Code for computing optimal portfolio weights, risk and return based on
%maximizing return-VaR ratio.
[var.opmvwgts,var.sharp]=fmincon(@mnvarratio,weights,[],[],aeq,beq,lb,ub);
var.sharp=-var.sharp;%Ratio of Optimal Portfolio
var.opmvret=meanret*var.opmvwgts;%Return of optimal portfolio
var.opmvrisk=prctile(corp*var.opmvwgts,ptile);%VaR of optimal portfolio
%%Code for calculating total return of optimal portfolio
var.dailyret=corp*var.opmvwgts;
var.dailyret_1=var.dailyret+1;
var.totret=cumprod(var.dailyret_1);


%Plotting Mean-VaR efficient frontier
plot(var.VaR*100,meansvar.semiportret*100,'g');
ylabel('Returns (%)');
xlabel('VaR (%)');
hold on;
plot(var.opmvrisk*100,var.opmvret*100,'ms');
grid on;
hold off;





% %Efficient frontier using Higher moments
% disp(sprintf('The following code will plot efficient frontier where risk is measured by \ncombination of second and third moments of portfolio return series.\nThe moments will be weighted according to the weights entered by the user.\nThe sum of weights should add upto 1'));
% wght(1)=input('Please enter the weight for variance');
% wght(2)=1-wght(1);
% for i=1:wsiz(1)
%     hm.ret=[];
%     hm.portret=[];
%     hm.ret=bsxfun(@times,corp,w(i,:));
%     hm.portret=sum(hm.ret,2);
%     hm.var(i,1)=std(hm.portret);
%     hm.var(i,2)=-skewness(hm.portret);
% end
% hm.risk=hm.var*wght';
% [hm.opwgts,hm.sharp]=fmincon(@highermom,weights,[],[],aeq,beq,lb,ub);
% hm.opportret=corp*hm.opwgts;
% hm.opvar(1)=std(hm.opportret);
% hm.opvar(2)=-skewness(hm.opportret);
% hm.oprisk=hm.opvar*wght';
% hm.opmvret=meanret*hm.opwgts;
% 
% 
% %Plotting Mean-Higher moment efficient frontier
% 
% plot(hm.risk*100,meansvar.semiportret*100,'g');
% ylabel('Returns (%)');
% xlabel('Risk (%)');
% hold on;
% plot(hm.oprisk*100,hm.opmvret*100,'ms');
% grid on;
% hold off;





%plotting the graphs
plot(meanvar.portrisk*100,meanvar.portret*100,'b');
xlabel('Risk (%)');
ylabel('Return (%)');
hold on;
plot(meanvar.opmvrisk*100,meanvar.opmvret*100,'ms');
hold on;
plot(meansvar.semiportrisk*100,meansvar.semiportret*100,'r');
hold on;
plot(meansvar.opmvrisk*100,meansvar.opmvret*100,'ms');
hold on;
plot(var.VaR*100,meansvar.semiportret*100,'g');
hold on;
plot(var.opmvrisk*100,var.opmvret*100,'ms');
grid on;


Contact us