I imported a csv file and calculate asset value and volatility of a stock.
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
My function is:
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]
The error is ValueError: can only convert an array of size 1 to a Python scalar
when running minimize(equation, x0). Could anyone explain what is the problem in this case? Thanks.