我想將每個公司在各自收益日期的回報和預期回報相乘,例如 Return(30-Jan-2017)*Return(31-Jan-2017)*Return(01-Feb-2017) [對于下一個收益日期等等]

我的代碼如下:
firms = unique(T.Ticker(:,:))
for i = 1:length(firms)
idx = firms(i);
Dates = T.EarningsDate(T.Ticker == idx,:);
ERDates=unique(Dates);
for n = 1:length(ERDates)
x = strcmpi(Dates,ERDates(n));
T.ret(x) = prod(T.Return(x))
end
end
但是,我得到了 APPL(前 60 行)的所有正確計算值,但隨后回圈以某種方式停止,我只收到 0。

有沒有人有一些提示,我能做什么?
uj5u.com熱心網友回復:
幀更改:如果您使用findgroupsTicker 和 EarningsDate 來識別唯一分組,則不需要使用嵌套回圈,然后只需對這些進行單個回圈。
首先,一個最小的示例資料集:
% Set up some example data
T = table();
rng(0);
T.Ticker = repelem( ["AAPL"; "AMGN"], 6, 1 );
T.EarningsDate = repelem( datetime(2022, randi(12,4,1), randi(28,4,1)), 3, 1 );
T.Date = T.EarningsDate repmat( [-1;0;1], 4, 1 );
T.Return = rand(height(T),1)-0.5;
T.ExpectedReturn = T.Return .* rand(height(T),1)*2;
然后是一個簡單的回圈,詳情見評論:
% Get groups for each unique combination of Ticker and EarningsDate
grp = findgroups( T.Ticker, T.EarningsDate );
% Loop over the groups and get the product for each one
T.ret(:) = NaN;
for igrp = 1:max(grp)
idx = (grp == igrp); % Rows which match this group
% Assign all group rows a "ret" value equal to their "Return" product
T.ret( idx ) = prod( T.Return(idx) );
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/527945.html
上一篇:如何在字串中查找重復項
下一篇:如何在“FOR”回圈中“迭代”?
