MATLAB: How to calculate z-score by monthly

mjbefore

Hi,
I have many securities at each month and I'd like calculate z-score of among those securities monthly. How do I do that?
I managed to calculate mean and standard deviation using accumarray, but it doesn't work with z-score.
for example, I have monthly returns so I calculated total return of mean and std monthly.
tot_ret_mu = accumarray(subs, tot_ret, [], @nanmean);
tot_ret_sd=accumarray(subs, tot_ret, [], @nanstd);
Using this information, can someone help me how to calculate z-score, please? I have [years,~,subs]=unique(dates(:,1)) for the date from the data set. Thanks, Madeline
The data looks like this. There are more factors, but I'm showing only one.

Best Answer

The only approach I can come up with is to create your own function:
nanzscore = @(x) (x - nanmean(x))./nanstd(x);
tot_ret_sd=accumarray(subs, tot_ret, [], nanzscore);
NOTE This is UNTESTED CODE, although the function itself works.
Related Question