MATLAB Answers

How to a create for loop for this ?

5 views (last 30 days)
Hannes Arnar
Hannes Arnar on 28 Jan 2020
Commented: Hannes Arnar on 28 Jan 2020
data2 = data1(1:25,:);
Cc = cov(data2);
data3 = data1(26:51,:);
Cc1 = cov(data3);
data4 = data1(52:77,:);
Cc2 = cov(data4);

  1 Comment

Guillaume
Guillaume on 28 Jan 2020
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.

Sign in to comment.

Answers (3)

Hannes Arnar
Hannes Arnar on 28 Jan 2020
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);

  0 Comments

Sign in to comment.


Hannes Arnar
Hannes Arnar on 28 Jan 2020
the size of it is 1247 x 20

  0 Comments

Sign in to comment.


Paresh yeole
Paresh yeole on 28 Jan 2020
Edited: Paresh yeole on 28 Jan 2020
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

  1 Comment

Sign in to comment.

Sign in to answer this question.

Tags