How to create this by a for loop?

조회 수: 1 (최근 30일)
Stian Haugen
Stian Haugen 2019년 4월 3일
편집: Agnish Dutta 2019년 4월 10일
I have a question creating this for loop. This is what i have tried:
steps = 1:22:length(data.MKT);
for j = 2:length(steps)
avg(j-1,1) = mean(data.MKT(steps(j-1):(steps(j)-1)));
end
for k = 1: length(avg)
fbar = avg(k);
for i = 2:length(steps)
RV((i-1),1) = sum((data.MKT(i-1) - fbar).^2);
end
end
This is incorrect.
I believe it is correct to run it from 1:22 instead 1/22 : 1.
All help appreciated.
  댓글 수: 2
Image Analyst
Image Analyst 2019년 4월 3일
What is t? And what does f t + d mean. Does it mean f(t+d)?
Stian Haugen
Stian Haugen 2019년 4월 3일
the data used is exess returns, and t is time periode. What they do is to calculate some form of variance for the 22 first observations it is based on the volatility managed portfolios by Moreira and Muir. If you have access to google scholar you find it here:
First result.
If not here is their description:
Skjermbilde 2019-04-03 kl. 15.21.07.png

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

답변 (1개)

Agnish Dutta
Agnish Dutta 2019년 4월 10일
편집: Agnish Dutta 2019년 4월 10일
Assuming that the f_t+d values are in a 1-D array in contiguous locations, you could use the following code to get the required results:
% The variable arr is a 1-D array containing the
% required values in contiguous locations
res = 0;
t = 1;
for i = 1:22
res = res + (arr(t + i) - mean(arr(t + i : t + i + 22))).^2;
end
You will have to do some pre-processing on the data and store the needed values from "data.MKT" to the "arr". Just make sure that f_t corresponds to "arr(t)".

카테고리

Help CenterFile Exchange에서 Agriculture에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by