How to a create for loop for this ?

조회 수: 1 (최근 30일)
Hannes Arnar
Hannes Arnar 2020년 1월 28일
댓글: Hannes Arnar 2020년 1월 28일
data2 = data1(1:25,:);
Cc = cov(data2);
data3 = data1(26:51,:);
Cc1 = cov(data3);
data4 = data1(52:77,:);
Cc2 = cov(data4);
  댓글 수: 1
Guillaume
Guillaume 2020년 1월 28일
편집: Guillaume 2020년 1월 28일
Shouldn't the indices be 26:50 and 51:75 to be consistent with the 25 rows of the 1st calculation? How many rows does your data1 matrix has?
While it's easy to write a loop, you can even do this without a loop in just one line, as long as the step is consistent and the height of the matrix is a multiple of the step.

댓글을 달려면 로그인하십시오.

답변 (3개)

Hannes Arnar
Hannes Arnar 2020년 1월 28일
close all; clear all; clc;
data1 = xlsread('dataCompanyprices','Sheet1','W4:AP1250');
S = std(data1)*sqrt(252); %Standard deviation for assets
C = cov(data1)*252; %Annual Covariance
r = mean(data1)*252; %Annual asset return
e = ones(1,20); %Unit vector
w = [1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20 1/20];
Avar = (w*C*w')*252; %annual varinance
minR = (e*inv(C)*r') / (e*inv(C)*e') %expected return of min-var portfolio
AminR = (e*inv(C)) / (e*inv(C)*e') %Asset allocation for min-var portfolio
varminR = 1 / (e*inv(C)*e') %Varinace of min-var portfolio
stdminR = 1 / (sqrt(e*inv(C)*e')) %standard deviation of min-var portfolii
rc = r'*w %the demand that the portfolio delivers the returns
[A, B, w, ER, sigP] = AssetAllWithTarget(e,r,C,minR:0.001:1); %Function for frontier
figure %Plots the frontier and the assets as a risk-return profile
plot(sigP,ER,'-')
title('Frontier for Icelandic stock market from year 2015 - 2020')
xlabel('Risk [std]')
ylabel('Expected return [EX]')
hold on
plot(stdminR,minR,'*')
legend({'Efficent Frontier','Min-var Portfolio'},'Location','northwest')
data2 = data1(1:25,:); %% vill búa til for-lykkju fyrir þetta
Cc = cov(data2);
data3 = data1(26:51,:);
Cc1 = cov(data3);
data4 = data1(52:77,:);
Cc2 = cov(data4);

Hannes Arnar
Hannes Arnar 2020년 1월 28일
the size of it is 1247 x 20

Paresh yeole
Paresh yeole 2020년 1월 28일
편집: Paresh yeole 2020년 1월 28일
There is inconsistency. The first covariance value is for 25 numbers and the other two are for 26 numbers. If you are looking for 25 no. sets then following loop would do.
for i =1:3
Cc(i) = cov(data(1+(i-1)*25 : 25*i,: ));
end

카테고리

Help CenterFile 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!

Translated by