上下文
在这个问题之后,我为可变收益率创建了三个新列,如下所示。
Regular Contribution Return/Loss Balance Monthly returns Variable Return/Loss Variable Return Balance
Date
2021-04-30 100 1.250000e+01 1.012500e+03 0.000000 0.000000e+00 1.009000e+05
2021-05-31 100 1.390625e+01 1.126406e+03 0.010016 1.865492e+06 1.511990e+08
2021-06-30 100 1.533008e+01 1.241736e+03 0.013418 2.499015e+06 2.025121e+08
2021-07-31 100 1.677170e+01 1.358508e+03 -0.009173 -1.708369e+06 -1.382708e+08
2021-08-31 100 1.823135e+01 1.476739e+03 -0.013527 -2.519416e+06 -2.039627e+08
... ... ... ... ... ... ...
2104-03-31 100 2.625473e+07 2.126633e+09 0.000164 3.045373e+04 2.567542e+06
2104-04-30 100 2.658292e+07 2.153216e+09 0.002226 4.146561e+05 3.368655e+07
2104-05-31 100 2.691521e+07 2.180132e+09 0.004802 8.942742e+05 7.253388e+07
2104-06-30 100 2.725165e+07 2.207383e+09 -0.000907 -1.688402e+05 -1.357455e+
**可变收益是通过计算一组资产的年收益率除以12得出的,如下代码所示。
但问题是,我无法在for循环中两次使用同一个变量(大写\u insert):
for date in date_range:
# fixed return
current_return_loss = (interest_rate/12) * capital_insert
return_losses.append(current_return_loss)
balances.append(capital_insert + current_return_loss)
capital_insert += (current_return_loss + Additional_contribution)
fixed_r = capital_insert
# variable return
current_var_return_loss = (var_rtns/12) * capital_insert
Var_rtn_balance = current_var_return_loss+var_capital_insert
capital_insert += (current_var_return_loss + Additional_contribution)
var_capital_insert = capital_insert
这将返回:
pd.concat([fixed_r, var_capital_insert], axis=1)
0 0
0 8.49966762e+30 8.49966762e+30
1 1.02794206e+32 1.02794206e+32
2 2.39554936e+32 2.39554936e+32
3 8.65418098e+29 8.65418098e+29
4 2.92365668e+29 2.92365668e+29
... ... ...
994 1.82899073e+31 1.82899073e+31
995 8.85282386e+30 8.85282386e+30
996 1.47948140e+31 1.47948140e+31
997 2.80848428e+31 2.80848428e+31
998 6.78230893e+30 6.78230893e+30
我通常只是改变变量名,但我目前正在使用它来构建一个web应用程序,我正在使用这个输入变量来创建图表(在下面的图表中,这是 Initial value
.
创建一个新的变量是可能的,并且可以在一个新的数据输入框中使用它,但是我想在走这条路线之前看看是否有办法做到这一点。
问题
是否可以使用相同的变量 capital_insert
在for循环中两次为固定收益率余额和可变收益率余额的最终余额创建列?
可运行示例
# Portfolio
portfolio = pd.DataFrame(columns=['Date', 'Regular Contribution', 'Return/Loss', 'Balance', 'Monthly returns', 'Variable Return/Loss', 'Variable Return Balance'])
interest_rate = 0.15
Additional_contribution = 100
# start data
today = datetime.date.today()
date_range = pd.date_range(start=today, periods=1000, freq='m') # date
investment = [Additional_contribution]*len(date_range)
return_losses = []
balances = []
var_return_losses = []
var_return_balance = []
capital_insert = 1000
var_capital_insert = 1000
var_rtns = Portfolio.iloc[0:999]
for date in date_range:
# fixed return
current_return_loss = (interest_rate/12) * capital_insert
return_losses.append(current_return_loss)
balances.append(capital_insert + current_return_loss)
capital_insert += (current_return_loss + Additional_contribution) # balance for fixed rate of return
# fixed_r = capital_insert
# variable return
current_var_return_loss = (var_rtns/12) * capital_insert
Var_rtn_balance = current_var_return_loss+var_capital_insert
var_capital_insert += (current_var_return_loss + Additional_contribution) # balance for variable rate of return
# var_capital_insert = capital_insert
portfolio['Date'] = pd.to_datetime(date_range).date
portfolio['Regular Contribution'] = investment
portfolio['Return/Loss'] = return_losses
portfolio['Balance'] = balances
portfolio['Monthly returns'] = var_rtns
portfolio['Variable Return/Loss'] = current_var_return_loss
portfolio['Variable Return Balance'] = Var_rtn_balance
portfolio.set_index('Date', inplace=True)
balance_at_end = balances[-1]
var_balance_at_end = Var_rtn_balance.iloc[-1]
暂无答案!
目前还没有任何答案,快来回答吧!