Python-替代“ for”循环的更快方法

时间:2018-09-12 17:21:04

标签: python performance for-loop quantitative-finance mathematical-lattices

我正在尝试在Python中构造一个二项式晶格模型。想法是存在多个二项式晶格,并基于特定晶格中的值,在其他晶格中执行一系列操作。 这些操作类似于“期权定价模型”(参考Black Scholes模型),其计算方式从晶格的最后一列开始,然后一次又一次迭代到上一列。 例如, 如果我有n列的二项式晶格, 1.我为单个或多个晶格计算第n列中的值。 2.根据这些值,以相同或其他二项式网格更新第(n-1)列中的值 3.这个过程一直持续到我到达第一列为止。

简而言之,我无法同时处理所有晶格的计算,因为每一列中的值取决于下一列中的值,依此类推。

从编码角度来看, 我编写了一个函数,可以对网格中的特定列进行计算,并输出用作过程中下一列输入的数字。

def column_calc(StockPrices_col, ConvertProb_col, y_col, ContinuationValue_col, ConversionValue_col, coupon_dates_index, convert_dates_index ,
                call_dates_index, put_dates_index, ConvertProb_col_new, ContinuationValue_col_new, y_col_new,tau, r, cs, dt,call_trigger,
                putPrice,callPrice):

for k in range(1, n+1-tau):

     ConvertProb_col_new[n-k] = 0.5*(ConvertProb_col[n-1-k] + ConvertProb_col[n-k])

     y_col_new[n-k] =  ConvertProb_col_new[n-k]*r + (1- ConvertProb_col_new[n-k]) *(r + cs) 

     # Calculate the holding value
     ContinuationValue_col_new[n-k] = 0.5*(ContinuationValue_col[n-1-k]/(1+y_col[n-1-k]*dt) +  ContinuationValue_col[n-k]/(1+y_col[n-k]*dt))

     # Coupon payment date
     if np.isin(n-1-tau, coupon_dates_index) == True:

         ContinuationValue_col_new[n-k] = ContinuationValue_col_new[n-k] + Principal*(1/2*c);

     # check put/call schedule
     callflag = (np.isin(n-1-tau, call_dates_index)) & (StockPrices_col[n-k] >= call_trigger)
     putflag = np.isin(n-1-tau, put_dates_index)
     convertflag = np.isin(n-1-tau, convert_dates_index)

     # if t is in call date
     if (np.isin(n-1-tau, call_dates_index) == True) & (StockPrices_col[n-k] >= call_trigger):

         node_val = max([putPrice * putflag, ConversionValue_col[n-k] * convertflag, min(callPrice, ContinuationValue_col_new[n-k])] )
     # if t is not call date    
     else:

         node_val = max([putPrice * putflag, ConversionValue_col[n-k] * convertflag, ContinuationValue_col_new[n-k]] )


     # 1. if Conversion happens
     if node_val == ConversionValue_col[n-k]*convertflag:
         ContinuationValue_col_new[n-k] = node_val
         ConvertProb_col_new[n-k] = 1

     # 2. if put happens
     elif node_val == putPrice*putflag:
         ContinuationValue_col_new[n-k] = node_val
         ConvertProb_col_new[n-k] = 0

     # 3. if call happens
     elif node_val == callPrice*callflag:
         ContinuationValue_col_new[n-k] = node_val
         ConvertProb_col_new[n-k] = 0

     else:
         ContinuationValue_col_new[n-k] = node_val


return ConvertProb_col_new, ContinuationValue_col_new, y_col_new

我正在通过for循环为晶格中的每一列调用此函数。 因此,基本上,我正在为所有计算运行一个嵌套的for循环。

我的问题是-这非常慢。 该功能不需要太多时间。但是我通过for循环调用该函数的第二次迭代非常耗时(平均时间,该函数将在下面的for循环中迭代,接近1000或1500)运行整个模型大约需要2.5分钟从标准建模的角度来看非常慢。 如上所述,大部分时间都由嵌套的for循环占用,如下所示:

temp_mat = np.empty((n,3))*(np.nan)
temp_mat[:,0] = ConvertProb[:, n-1]
temp_mat[:,1] = ContinuationValue[:, n-1]
temp_mat[:,2] = y[:, n-1]

ConvertProb_col_new = np.empty((n,1))*(np.nan)
ContinuationValue_col_new = np.empty((n,1))*(np.nan)
y_col_new = np.empty((n,1))*(np.nan)


for tau in range(1,n):    

    ConvertProb_col = temp_mat[:,0]
    ContinuationValue_col = temp_mat[:,1]
    y_col = temp_mat[:,2]

    ConversionValue_col = ConversionValue[:, n-tau-1]
    StockPrices_col = StockPrices[:, n-tau-1]



    out = column_calc(StockPrices_col, ConvertProb_col, y_col, ContinuationValue_col, ConversionValue_col, coupon_dates_index, convert_dates_index ,call_dates_index, put_dates_index, ConvertProb_col_new, ContinuationValue_col_new, y_col_new, tau, r, cs, dt,call_trigger,putPrice,callPrice)

    temp_mat[:,0] = out[0].reshape(np.shape(out[0])[0],)
    temp_mat[:,1] = out[1].reshape(np.shape(out[1])[0],)
    temp_mat[:,2] = out[2].reshape(np.shape(out[2])[0],)

#Final value
print(temp_mat[-1][1])

有什么办法可以减少嵌套for循环所消耗的时间?还是有什么我可以代替嵌套for循环使用的替代方法。 请告诉我。非常感谢!!!

0 个答案:

没有答案