MATLAB: Returns and standard deviation groupwise

distance to defaultgroupwisereturnsstandard deviation

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!

Best Answer

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.
Related Question