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!

 Réponse acceptée

John D'Errico
John D'Errico le 5 Nov 2017
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 commentaire

Leo
Leo le 5 Nov 2017
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)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Financial Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by