我有一个数据框,其累积股票回报从 1 到 5 天:
1dReturn 2dReturn 3dReturn 4dReturn 5dReturn
Ticker
LUNA -3.077 -3.077 -6.923 -6.915 -6.615
YTEN -2.139 -2.139 -18.182 -16.043 -16.578
我想计算每日回报。有没有功能?
下面的代码创建了上面的表格:
df = pd.DataFrame({'1dReturn': [-3.077, -2.139],
'2dReturn': [-3.077, -2.139],
'3dReturn': [-6.923, -18.182],
'4dReturn': [-6.915, -16.043],
'5dReturn': [-6.615, -16.578],},
index=['LUNA', 'YTEN'])
得出每日回报的公式如下:
daily returns day 2: cD2/d1
daily returns day 3: cD4/(d1*d2)
daily returns day 4: cD5/(d1*d2*d3)
daily returns day 5: cD5/(d1*d2*d3*d4)
其中 cD1 是第 1 天的暨回报,d1 是 d1 等的每日回报。
np.exp(np.log(cumReturn + 1.0).diff()) - 1
暨回归是熊猫中的累积回报系列。
R_cum_i = (1 + R_daily_i(* (1 + R_daily_i-1( ... - 1
R_cum_i-1 = (1 + R_daily_i-1(* (1 + R_daily_i-2( ... - 1
所以
R_cum_i =(R_cum_i-1 + 1( * (1 + R_daily_i-1( - 1
1 + R_daily_i-1 = (R_cum_i + 1(/(R_cum_i-1 + 1(
1 + R_daily_i-1 = exp(log(R_cum_i + 1(/(R_cum_i-1 + 1(((
1 + R_daily_i-1 = exp(log(R_cum_i + 1( - log(R_cum_i-1 + 1((
1 + R_daily_i-1 = exp(log(R_cum_i + 1(.diff(((
然后
R_daily_i-1 = exp(log(R_cum_i + 1(.diff((( - 1