python性能,循环速度提高

50few1ms  于 2021-05-29  发布在  Hadoop
关注(0)|答案(0)|浏览(221)

下面的函数应该每38天对股票价格进行一次核回归。

def kernelregressions(assetprices, windowperiod): #regresses prices on time
timeindex = np.linspace(1, assetprices.shape[0], assetprices.shape[0]) #time to start from 1 upto the length of the dataset
smoothprices = pd.DataFrame(index=timeindex, columns=assetprices.columns, dtype="float") # empty frame to store the smoothed prices
for symbol in assetprices.columns:
    for i in range(0,assetprices.shape[0]-windowperiod-1, windowperiod+1): #regressions are done for each 38 day period
        smoothprice = KernelReg(assetprices[symbol].iloc[i:i+windowperiod], 
                                timeindex[i:i+windowperiod], var_type="c", bw="cv_ls").fit()[0]
        smoothprices[symbol].iloc[i:i+windowperiod]= smoothprice
return smoothprices #returns smooth prices

“assetprices”输入是一个包含股票价格列的数据框,在我的例子中,窗口期是38。
当dataframe有100只股票的价格序列为500天时,上述函数将永远运行。
是否有python模块、包等(或者更好的方法做循环)我可以利用它来加速这个函数?是 Multiprocessing 或者 multithreading 适用于这里?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题