如何计算投资组合的累计 return 和投资
How to compute cumulative return and investment on a portfolio
这是我的问题:
我有一个每月投资的 DataFrame :
df = pd.DataFrame({'Dates':['2018-07-31','2018-07-31','2018-07-31','2018-08-31','2018-08-31','2018-08-31',
'2018-09-30','2018-09-30','2018-09-30'],
"Name":["Apple",'Google','Facebook','JP Morgan','IBM','Netflix',"Apple","Tesla","Boeing"],
"Monthly Return":[-0.018988,-0.028009,0.111742,-0.034540,-0.025806,-0.043647,0.001045,
0.155379,0.011644],
"Total Weight":[0.7,0.2,0.1,0.5,0.3,0.2,0.6,0.2,0.2]})
我想计算累计投资,但我很难做到:
假设我们的初始投资为 1000 美元
如果我们考虑每月 Return 和每个资产的权重,
我们在 2018-07-31 有这个:
Dates Name Return Weight Investment Pofit/loss
2018-07-31 Apple -0.018988 0.7 700 -13.29
2018-07-31 Google -0.028009 0.2 200 -5.60
2018-07-31 Facebook 0.111742 0.1 100 11.17
所以对于 2018 年 7 月,我从 1000 美元开始,到月底我有 992.28 美元(1000 - 13.29 - 5.60 + 11.17)
这笔款项将在 2018 年 8 月进行再投资,到本月底我将拥有:992.28 美元 +/- 2018 年 8 月的总额 Profit/Loss。
我的目标是通过考虑每个月的 Profit/Loss 来获得最终金额,但我真的不知道该怎么做。
如果有人对此有想法,欢迎您!
如果您不是很清楚,请告诉我
这是一个解决方案,为清楚起见分为几个步骤:
df = pd.DataFrame({'Dates':['2018-07-31','2018-07-31','2018-07-31','2018-08-31','2018-08-31','2018-08-31',
'2018-09-30','2018-09-30','2018-09-30'],
"Name":["Apple",'Google','Facebook','JP Morgan','IBM','Netflix',"Apple","Tesla","Boeing"],
"Monthly Return":[-0.018988,-0.028009,0.111742,-0.034540,-0.025806,-0.043647,0.001045,
0.155379,0.011644],
"Total Weight":[0.7,0.2,0.1,0.5,0.3,0.2,0.6,0.2,0.2]})
df["weighted_return"] = df["Monthly Return"] * df["Total Weight"]
# df.groupby("Dates", freq="1M")
df["Dates"] = pd.to_datetime(df.Dates)
df.set_index("Dates", inplace=True)
t = df.groupby(pd.Grouper(freq="M")).sum()
此时,t
为:
Monthly Return Total Weight weighted_return eom_value
Dates
2018-07-31 0.064745 1.0 -0.007719 0.992281
2018-08-31 -0.103993 1.0 -0.033741 0.966259
2018-09-30 0.168068 1.0 0.034032 1.034032
现在,我们可以使用 cumprod
随时间计算 return:
t["eom_value"] = 1 + t.weighted_return
t.eom_value.cumprod()
结果:
Dates
2018-07-31 0.992281
2018-08-31 0.958800
2018-09-30 0.991430
这是我的问题:
我有一个每月投资的 DataFrame :
df = pd.DataFrame({'Dates':['2018-07-31','2018-07-31','2018-07-31','2018-08-31','2018-08-31','2018-08-31',
'2018-09-30','2018-09-30','2018-09-30'],
"Name":["Apple",'Google','Facebook','JP Morgan','IBM','Netflix',"Apple","Tesla","Boeing"],
"Monthly Return":[-0.018988,-0.028009,0.111742,-0.034540,-0.025806,-0.043647,0.001045,
0.155379,0.011644],
"Total Weight":[0.7,0.2,0.1,0.5,0.3,0.2,0.6,0.2,0.2]})
我想计算累计投资,但我很难做到: 假设我们的初始投资为 1000 美元
如果我们考虑每月 Return 和每个资产的权重, 我们在 2018-07-31 有这个:
Dates Name Return Weight Investment Pofit/loss
2018-07-31 Apple -0.018988 0.7 700 -13.29
2018-07-31 Google -0.028009 0.2 200 -5.60
2018-07-31 Facebook 0.111742 0.1 100 11.17
所以对于 2018 年 7 月,我从 1000 美元开始,到月底我有 992.28 美元(1000 - 13.29 - 5.60 + 11.17) 这笔款项将在 2018 年 8 月进行再投资,到本月底我将拥有:992.28 美元 +/- 2018 年 8 月的总额 Profit/Loss。
我的目标是通过考虑每个月的 Profit/Loss 来获得最终金额,但我真的不知道该怎么做。
如果有人对此有想法,欢迎您! 如果您不是很清楚,请告诉我
这是一个解决方案,为清楚起见分为几个步骤:
df = pd.DataFrame({'Dates':['2018-07-31','2018-07-31','2018-07-31','2018-08-31','2018-08-31','2018-08-31',
'2018-09-30','2018-09-30','2018-09-30'],
"Name":["Apple",'Google','Facebook','JP Morgan','IBM','Netflix',"Apple","Tesla","Boeing"],
"Monthly Return":[-0.018988,-0.028009,0.111742,-0.034540,-0.025806,-0.043647,0.001045,
0.155379,0.011644],
"Total Weight":[0.7,0.2,0.1,0.5,0.3,0.2,0.6,0.2,0.2]})
df["weighted_return"] = df["Monthly Return"] * df["Total Weight"]
# df.groupby("Dates", freq="1M")
df["Dates"] = pd.to_datetime(df.Dates)
df.set_index("Dates", inplace=True)
t = df.groupby(pd.Grouper(freq="M")).sum()
此时,t
为:
Monthly Return Total Weight weighted_return eom_value
Dates
2018-07-31 0.064745 1.0 -0.007719 0.992281
2018-08-31 -0.103993 1.0 -0.033741 0.966259
2018-09-30 0.168068 1.0 0.034032 1.034032
现在,我们可以使用 cumprod
随时间计算 return:
t["eom_value"] = 1 + t.weighted_return
t.eom_value.cumprod()
结果:
Dates
2018-07-31 0.992281
2018-08-31 0.958800
2018-09-30 0.991430