Calculating standard deviation for each year

5 ビュー (過去 30 日間)
Luca
Luca 2021 年 4 月 11 日
編集済み: Luca 2021 年 4 月 11 日
Hi,
I need to calculate for each stock the standard deviation for each year of observation. I created a subset with to stocks which is attached.
I was able to calculate the standard deviation for the two stocks with the splitapply() command.
% Creating volatility for two stocks
stocks.years = year(stocks.dates);
[group,years,ID]= findgroups(stocks.years, stocks.ID);
vola2 = splitapply(@std, stocks.RET, group);
voltb2 = table(ID, years, vola2);
voltb2 = sortrows(voltb2, 'ID');
My problem is that by the way stockreturns are create the first observation is always a NaN. Therefore for the standard deviation for the first year is NaN.
Is there a way to omit the NaN line in the calculation for standard deviation?
Thanks in advance

採用された回答

Jan
Jan 2021 年 4 月 11 日
Do you want:
vola2 = splitapply(@(x) std(x, 'omitnan'), stocks.RET, group);
  1 件のコメント
Luca
Luca 2021 年 4 月 11 日
Hi Jan,
Thank you very much it worked perfectly.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by