【发布时间】:2019-04-17 14:55:09
【问题描述】:
对于我的论文,我需要期权的隐含波动率,我已经为它创建了以下函数:
#Implied volatility solver
def Implied_Vol_Solver(s_t,K,t,r_f,option,step_size):
#s_t=Current stock price, K=Strike price, t=time until maturity, r_f=risk-free rate and option=option price,stepsize=is precision in stepsizes
#sigma set equal to steps to make a step siz equal to the starting point
sigma=step_size
while sigma < 1:
#Regualar BlackScholes formula (current only call option, will also be used to calculate put options)
d_1=(np.log(s_t/K)+(r_f+(sigma**2)/2)*t)/(sigma*(np.sqrt(t)))
d_2=d_1-np.square(t)*sigma
P_implied=s_t*norm.cdf(d_1)-K*np.exp(-r_f*t)*norm.cdf(d_2)
if option-(P_implied)<step_size:
#convert stepts to a string to find the decimal point (couldn't be done with a float)
step_size=str(step_size)
#rounds sigma equal to the stepsize
return round(sigma,step_size[::-1].find('.'))
sigma+=step_size
return "Could not find the right volatility"
我需要的变量位于 Pandas DataFrame 中,并且我已经为它创建了一个循环,以测试它是否有效(当它正常工作时我将添加其他变量):
for x in df_option_data['Settlement_Price']:
df_option_data['Implied_Volatility']=Implied_Vol_Solver(100,100,1,0.01,x,0.001)
但是,当我运行这个循环时,我会得到整个 Implied_Voltality 列的 0.539,而且这些数字需要不同,我哪里错了?或者有没有更简单的解决方案?
我还尝试了以下方法:
df_option_data['Implied_Volatility']=Implied_Vol_Solver(100,100,1,0.01,np.array(df_option_data['Settlement_Price']),0.001)
但是我得到以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
基本上我需要的是以下内容:一个包含 5 列输入变量和 1 列输出变量(隐含波动率)的数据框,由函数计算。
【问题讨论】:
标签: python pandas function loops