Returns and standard deviation groupwise

I have a time series of daily stock prices for several firms. Now I want to calculate the daily returns and the rolling standard deviation of 250 days for each firm. I have no clue how to implement that in Matlab. Whereas in Stata I could only use the "by" command, it didnt find a similar function in Matlab.
I did group my firm ID and tried to apply splitapply. It works but what I get is a cell array. If I do not specify "{}" (cell array) in my function, this function wont work.
func =@(p){log(p(2:end)./p(1:end-1))} ;
r = splitapply(func,p,Entity);
The goal is to calculate daily returns (in an array) and annualized std. dev to further calculate the distance to default for each company.
Has someone a hint, how this might work? Thanks for any hint!

 採用された回答

John D'Errico
John D'Errico 2017 年 11 月 5 日

0 投票

I'm not sure what you are asking by "daily returns". Perhaps you want a moving mean, on a 250 day time window. That is simple.
mean250 = conv(dailydata,ones(250)/250,'valid');
But a moving window standard deviation is also easy enough. Download my movingstd tool from the File Exchange. It uses convolution with a trick to get the std in a window, and do it efficiently.

1 件のコメント

Leo
Leo 2017 年 11 月 5 日
thanks a lot! My main issue doing this calculation for each firm, such that if I have a time series of firms and their stock prices that the return in t1 is zero and t+1: r+1 = log(pt+1/pt)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFinancial Toolbox についてさらに検索

質問済み:

Leo
2017 年 11 月 5 日

コメント済み:

Leo
2017 年 11 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by