我需要根据等式计算出值S(n)
s(n) = (1 + r(1))(1+r(2))...(1+r(n))
的值
S(after N days) = (1+r(after 1 day))*(1+r(after 2 days))...(1+r(after N days))
r来自:
r = return_daily + sig_daily
但是计算了D次(参见下面的代码,D每天计算一次并且是随机值)
我只是不太确定如何实现包含变化变量的等式(r每天变化)。
感谢您的帮助
相关守则:
#import all modules required
import numpy as np # using different notation for easier writting
import scipy as sp
import matplotlib.pyplot as plt
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#collect variables provided by the user
period = 63 # can be edited to an input() command for variable periods.
Return_annual = np.arange(0.00,0.15,0.05) # creates an array ??? not sure if helpful
sig_annual = np.arange(0.01,0.31,0.01) #use .31 as python doesnt include the upper range value.
#functions for variables of daily return and risk.
Return_daily = (1/252)*R_annual
sig_daily = (1/(np.sqrt(252)))*sig_annual
D=np.random.normal(size=period)
r_i=Return_daily + sig_daily # must be calculated D times
答案 0 :(得分:0)
您如何收到投入?
您可以逐个接收它们并将它们附加到元列表中,然后您可以使用一个函数来传递您的r(n)列表并输出您想要的任何内容。
例如,如果您想要几何关联的返回:
rors=[0.4, 0.5, 0.8]
#calculate compound return
def calc_ror(ror_list):
final_value=0
for i in ror_list:
final_value=(1+i)*(1+final_value)-1
return final_value
calc_ror(rors)
Out[35]: 2.7799999999999994
另一个很酷的技巧是使用变量输入参数,在这种情况下,函数中的每个参数都是不同的返回值。
#calculate compound return
def calc_ror(*args):
final_value=0
for i in args:
final_value=(1+i)*(1+final_value)-1
return final_value
calc_ror(0.4, 0.5, 0.8)
Out[37]: 2.7799999999999994
或者最后使用pandas
import pandas as pd
rors=[0.4, 0.5, 0.8]
df = pd.DataFrame(rors, columns=['ror'])
df_cum=pd.DataFrame((1 + df['ror']).cumprod()-1)
df_cum.tail(1)
Out[42]:
ror
2 2.78
我希望其中一个替代方案适用于您的案例