首页 > 解决方案 > Python - 'for' 循环的更快替代方案

问题描述

我正在尝试在 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 循环。请告诉我。非常感谢 !!!

标签: pythonperformancefor-loopquantitative-financemathematical-lattices

解决方案


推荐阅读