[英]Python-Apply Asset value and volatility calculation function for each row in csv file
我導入了一個 csv 文件並計算了股票的資產價值和波動率。
df = pd.read_csv (r'test.csv')
df['ret_col'] = np.log(df.price) - np.log(df.price.shift(1))
df['sigma_e'] = np.std(df.ret_col)
T = 1
我的 function 是:
def equation(x):
d1 = (np.log(x[0]/df.face_val_debt) + (df.r_f+x[1]**2/2)*T)/(x[1] * np.sqrt(T))
d2 = d1 - x[1] * np.sqrt(T)
res1 = x[0] * norm.cdf(d1) - np.exp(-df.r_f*T) * df.face_val_debt * norm.cdf(d2) - df.v_e
res2 = x[0] * norm.cdf(d1) * x[1] - df.v_e * df.sigma_e
return(res1**2+res2**2)
for i, row in df.iterrows():
x0 = [row["v_e"], row["sigma_e"]]
result = minimize(equation, x0)
result
df.at[i, "v_a"] = list(result['x'])[0]
df.at[i, "sigma_a"] = list(result['x'])[1]
錯誤是ValueError: can only convert an array of size 1 to a Python scalar
when running minimize(equation, x0)。 誰能解釋這種情況下的問題是什么? 謝謝。
我將 function 放在循環之后,所以它工作正常。 謝謝你的提示。
對於 i,df.iterrows() 中的行:
def equation(x):
d1 = (np.log(x[0]/row["default_point"]) +
(row["arf_rate"]+x[1]**2/2)*T)/(x[1] * np.sqrt(T))
d2 = d1 - x[1] * np.sqrt(T)
res1 = x[0] * norm.cdf(d1) - np.exp(-row["arf_rate"]*T) *
row["default_point"] * norm.cdf(d2) - row["V_equity"]
res2 = (x[0] * norm.cdf(d1) * x[1])/row["asigma_equity"] -
row["V_equity"]
return(res1**2+res2**2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.