필터 지우기
필터 지우기

Sorting return by dates

조회 수: 2 (최근 30일)
King To Leung
King To Leung 2022년 7월 29일
댓글: King To Leung 2022년 7월 30일
I would like to sort the average return of each year. I have a data base of stock returns from 1992-2021. I would like to know the average stock return of each year.
How should I code this? Should i set a date range? I know there is a sort function to place the years in ascending orders. However, it is unable to help me to calculate the return of a specific year.
Also, the dates are in matlab form, like ,727594, which requires datevec function to look at the dates. Does it affect the coding part?
or this task can be think as adding up the return and find the average return of stocks in the same year.
I have totally no idea how to do this, please help me with this. Thank you very much

답변 (2개)

Walter Roberson
Walter Roberson 2022년 7월 29일
[Y, ~] = datevec(TheDateNumbers) ;
G = findgroups(Y) ;
results = grpstats(YourData, G, "mean");
If you want the return per stock then stock identification should be also be passed to findgroups.
  댓글 수: 1
King To Leung
King To Leung 2022년 7월 29일
Hi, Thank you for your answer.
I'm sorry that I don't really understand how the codes work. How can I get the mean return of each year from 1992-2021.
What should I enter in "TheDateNumbers"?As there are a lot of dates.

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


Dyuman Joshi
Dyuman Joshi 2022년 7월 29일
편집: Dyuman Joshi 2022년 7월 29일
%to get the year of a date
[year,~,~] = datevec(727594)
year = 1992
%storing the years in y (months and dates are not necessary)
%dates are stored in the 2nd column
[y,~,~] = datevec(data_crsp(:,2));
%calculating mean stock return for each year, return is in the 7th column
for k=1992:2021
stockreturn(k-1991) = mean(data_crsp(y==k,7));
%1st element will correspond to 1992, 2nd - 1993 and so on
end
  댓글 수: 12
Dyuman Joshi
Dyuman Joshi 2022년 7월 30일
Use the attach symbol in the dashboard menu.
King To Leung
King To Leung 2022년 7월 30일
I tried to attach the matlab data, but the system says it's over 5MB

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

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by