[英]How to calculate future value of investment with Pandas when interest rates and payments vary over time?
Let's assume that I would like to know how the value of my invested money changes over time.假设我想知道我投资的资金的价值是如何随时间变化的。 I have the following data in Pandas DataFrame:
我在 Pandas DataFrame 中有以下数据:
contribution monthly_return
2020-01-01 91.91 np.Nan
2020-02-01 102.18 0.037026
2020-03-01 95.90 -0.012792
2020-04-01 117.89 -0.009188
2020-05-01 100.44 0.011203
2020-06-01 98.89 0.053917
2020-07-01 106.10 -0.049397
2020-08-01 112.55 0.062375
2020-09-01 103.16 -0.063198
...and so on. ...等等。 Every month I invest additional sum of money to my "fund" (contribution).
每个月我都会向我的“基金”(捐款)投入额外的资金。 Monthly return shows how the value of my money changed during last month.
每月回报显示上个月我的钱的价值如何变化。
I would like to add additional column, where I could find information on current value of my investment in every month (so I can plot it on a graph).我想添加额外的列,我可以在其中找到有关我每个月投资当前价值的信息(以便我可以将其绘制在图表上)。 As far as I know, I cannot use any of numpy financial functions (such as np.fv()) because contributions and rates changes over time.
据我所知,我不能使用任何 numpy 财务函数(例如 np.fv()),因为贡献和费率会随着时间的推移而变化。 I can cumulative sum contributions, but I don't know how to add profit and loss on investment.
我可以累积捐款,但我不知道如何增加投资的损益。
This may be a trivial question, but I am completely stuck and I have wasted more hours on this problem than I could ever admit.这可能是一个微不足道的问题,但我完全被困住了,我在这个问题上浪费的时间比我承认的要多。 Any help would be appreciated!
任何帮助,将不胜感激!
Suppose you have a df
:假设你有一个
df
:
print(df)
contribution monthly_return
2020-01-01 91.91 NaN
2020-02-01 102.18 0.037026
2020-03-01 95.90 -0.012792
2020-04-01 117.89 -0.009188
2020-05-01 100.44 0.011203
2020-06-01 98.89 0.053917
2020-07-01 106.10 -0.049397
2020-08-01 112.55 0.062375
2020-09-01 103.16 -0.063198
Then let's find a multiplier your money grows monthly:然后让我们找到您的钱每月增长的乘数:
df['monthly_multiplier'] = 1 + df['monthly_return'].shift(-1)
print(df)
contribution monthly_return monthly_multiplier
2020-01-01 91.91 NaN 1.037026
2020-02-01 102.18 0.037026 0.987208
2020-03-01 95.90 -0.012792 0.990812
2020-04-01 117.89 -0.009188 1.011203
2020-05-01 100.44 0.011203 1.053917
2020-06-01 98.89 0.053917 0.950603
2020-07-01 106.10 -0.049397 1.062375
2020-08-01 112.55 0.062375 0.936802
2020-09-01 103.16 -0.063198 NaN
Finally we can iterate over rows and see how your wealth grows:最后,我们可以遍历行,看看你的财富是如何增长的:
df['fv'] = 0
fv = 0
for index, row in df.iterrows():
fv = (fv+row['contribution'])*row['monthly_multiplier']
df.loc[index,'fv']=fv
print(df)
contribution monthly_return monthly_multiplier fv
2020-01-01 91.91 NaN 1.037026 95.313060
2020-02-01 102.18 0.037026 0.987208 194.966728
2020-03-01 95.90 -0.012792 0.990812 288.194245
2020-04-01 117.89 -0.009188 1.011203 410.633607
2020-05-01 100.44 0.011203 1.053917 538.629162
2020-06-01 98.89 0.053917 0.950603 606.027628
2020-07-01 106.10 -0.049397 1.062375 756.546589
2020-08-01 112.55 0.062375 0.936802 814.171423
2020-09-01 103.16 -0.063198 NaN NaN
df['fv']
is your wealth at the end of the stated month, or just prior your next contribution. df['fv']
是您在指定月末或下一次捐款之前的财富。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.