Clear Filters
Clear Filters

The inverse of covariance matrix in Markowitz optimization

1 view (last 30 days)
I’m doing a portfolio optimization problem but the covariance matrix is not of full rank. However, the portopt function does figure out the efficient frontier, while the code I made following mean-variance equation can’t get that. The warning is that “Matrix is close to singular or badly scaled.” Could anyone tell me how to improve the code? Thanks a lot.
%%%code using portopt function
load('SP500.mat')
SPT=SP500';
SP=SPT(~any(isnan(SPT),2),:);
SP=SP';
Ret=price2ret(SP);
Stocks=Ret(:,2:end);
Index=Ret(:,1);
R=mean(Stocks);
Cov=cov(Stocks);
Std=std(Stocks);
portopt(R,Cov,30)
hold on
plot(Std,R,'.r')
plot(std(Index),mean(Index),'*k')
legend('Efficient Frontier','Individual Stocks','S&P 500')
%%%code using mean-variance equation
load('SP500.mat')
SPT=SP500';
SP=SPT(~any(isnan(SPT),2),:);
SP=SP';
Ret=price2ret(SP);
Stocks=Ret(:,2:end);
Index=Ret(:,1);
R=mean(Stocks);
Cov=cov(Stocks);
Std=std(Stocks);
Inv=inv(Cov);
One=ones(size(R,2),1);
a=R*Inv*R';
b=R*Inv*One;
c=One'*Inv*One;
x=0:0.005:0.25;
y=sqrt((c*x.^2-2*b*x+a)/(a*c-b^2));
plot(y,x)

Answers (0)

Categories

Find more on Portfolio Optimization and Asset Allocation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!