[英]Is there a faster method to calculate implied volatility using mibian module for millions of rows in a csv/xl file?
我的情況:
CSV文件已轉換為數據幀df5
並且下面的for循環中使用的所有列都是float
型的,此代碼有效,但要花費30,000
個小時才能完成30,000
行。
我要從我的情況中得到什么:
我需要對數百萬行執行相同的操作,並且正在尋找可以使其速度大大提高的修復程序/替代解決方案。
以下是我當前使用的代碼:
for row in np.arange(0,len(df5)):
underlyingPrice = df5.iloc[row]['CLOSE_y']
strikePrice = df5.iloc[row]['STRIKE_PR']
interestRate = 10
dayss = df5.iloc[row]['Days']
optPrice = df5.iloc[row]['CLOSE_x']
result = BS([underlyingPrice,strikePrice,interestRate,dayss], callPrice= optPrice)
df5.iloc[row,df5.columns.get_loc('IV')]= result.impliedVolatility
您的循環似乎從每一行中獲取值來構建另一列IV
。
使用apply方法可以更快地完成此操作,該方法允許在每行/列上使用一個函數來計算結果。
像這樣:
def useBS(row):
underlyingPrice = row['CLOSE_y']
strikePrice = row['STRIKE_PR']
interestRate = 10
dayss = row['Days']
optPrice = row['CLOSE_x']
result = BS([underlyingPrice,strikePrice,interestRate,dayss], callPrice= optPrice)
return result.impliedVolatility
df5['IV'] = df5.apply(useBS, axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.