程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在 R 中优化此功能?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在 R 中优化此功能??

开发过程中遇到如何在 R 中优化此功能?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在 R 中优化此功能?的解决方法建议,希望对你解决如何在 R 中优化此功能?有所启发或帮助;

我在 @H_616_3@top500Stocks 中有大约 977 个 obs,其中包含 977 只股票的名称。

@H_616_3@head(top500Stocks,10)
    ï..Symbol
1    REliANCE
2         TCS
3    HdfcBANK
4        INFY
5  HINDUNILVR
6        Hdfc
7   ICICIBANK
8   KOTAKBANK
9        SBIN
10 BAJFINANCE

并且我在stocksRetData中有top500Stocks中每只股票的Date,OHLC和Adj.Close,Vol和Ret

@H_616_3@  head(stocksRetData[[1]],3)
          Date     Open     High      Low    Close Adj.Close    Volume   Ret
    1 20000103 28.18423 29.86935 28.18423 38.94457  29.86935  28802010 0.000
    2 20000104 30.66445 32.26056 29.82188 42.06230  32.26056  61320457 0.080
    3 20000105 30.45677 34.16522 30.45677 43.71014  33.52440 173426953 0.039

现在对于给定的lookBACkPeriod 和holdPeriod,我正在尝试运行以下函数,但大约需要1 分钟。我怎样才能让它更快?因为我必须运行多个lookBACkPeriod 和holdPeriod,所以它需要很长时间才能完成。

@H_616_3@CalC.MOD_Mscore.Ret.High <- function(lookBACkPeriod,holdPeriod,fnoStocks,stocksRetData,@R_402_10586@lTestPeriod) {
  
  #We go through each stock and calculate ModifIEd mscores where we give more importance to recent data
  
  WeeklyData <- List()
  wmean <- function(x,k) mean(seq(k)/k * X)
  
  for (i in 1:nrow(fnoStocks)){
    
    out <- stocksRetData[[i]]
    out <- tail(out,@R_402_10586@lTestPeriod)
    
    if (nrow(out)==@R_402_10586@lTestPeriod){
      
      tempdf <- transform(out,wtMean = rollapply(Ret,lookBACkPeriod,wmean,k = lookBACkPeriod,align = "right",fill = NA))
      
      tempdf <- transform(tempdf,ExitVal = rollapply(lead(High,holdPeriod),max,fill = NA))
      
      tempdf$NWeekRet <- (tempdf$ExitVal - tempdf$Adj.Close ) / tempdf$Adj.Close
      
      tempdf <- tempdf[!is.na(tempdf$wtMean),]
      tempdf <- tempdf[!is.na(tempdf$ExitVal),]
      tempdf$Stockname = fnoStocks[i,1]
      tempdf$WeekNum = c((lookBACkPeriod):(nrow(tempdf)+lookBACkPeriod-1))
      
      WeeklyData[[i]] <- data.frame(
        Stockname = tempdf$Stockname,WeekNum = tempdf$WeekNum,M_score = tempdf$wtMean,NWeekRet = tempdf$NWeekRet,StringsAsFactors =  falSE
      )
      
    }
  }# i ends here
  
  return(bind_rows(WeeklyData))
}

这需要一分钟多的时间才能完成。

@H_616_3@ a <- CalC.MOD_Mscore.Ret.High(4,14,fnoStocks = top500Stocks,stocksRetData = stocksRetData,2000)

解决方法

首先,我不建议在 @H_616_3@for 中使用 @H_616_3@R 循环。我会用 @H_616_3@lapply like

重写你的循环
@H_616_3@CalC.MOD_MScore.Ret.High <- function(lookBACkPeriod,holdPeriod,fnoStocks,stocksRetData,@R_402_10586@lTestPeriod) {
  
  #We go through each stock and calculate Modified mscores where we give more importance to recent data
  
  wmean <- function(x,k) mean(seq(k)/k * X)
  
  WeeklyData <- lapply(1:nrow(fnoStocks),function(i) {
    out <- stocksRetData[[i]]
    out <- tail(out,@R_402_10586@lTestPeriod)
    if(nrow(out)!=@R_402_10586@lTestPeriod) return(NULL)
    
    tempDF <- transform(out,wtMean = rollapply(Ret,lookBACkPeriod,wmean,k = lookBACkPeriod,align = "right",fill = NA))
    
    tempDF <- transform(tempDF,ExitVal = rollapply(lead(High,holdPeriod),max,fill = NA))
    
    tempDF$NWeekRet <- (tempDF$ExitVal - tempDF$Adj.Close ) / tempDF$Adj.Close
    
    tempDF <- tempDF[!is.na(tempDF$wtMean),]
    tempDF <- tempDF[!is.na(tempDF$ExitVal),]
    tempDF$StockName = fnoStocks[i,1]
    tempDF$WeekNum = c((lookBACkPeriod):(nrow(tempDF)+lookBACkPeriod-1))
    
    data.frame(
      StockName = tempDF$StockName,WeekNum = tempDF$WeekNum,M_Score = tempDF$wtMean,NWeekRet = tempDF$NWeekRet,StringsAsFactors =  falSE
    )
  })
  
  return(bind_rows(WeeklyData))
}

拥有 @H_616_3@lapply 可以更轻松地在其上投放一些并行化工具。 您可以查看包 @H_616_3@parallel。使用此软件包,您可以并行化并利用您机器上的多个内核。因此,您需要设置一个集群,这会产生一些开销,但我认为它会在您的情况下得到回报。要使用它,请通过 @H_616_3@cl <- parallel::makeCluster(parallel::detectCores()) 设置集群。 @H_616_3@detectCores 方法获取机器上可用内核的数量。然后,您可以将 @H_616_3@lapply 编辑为

@H_616_3@WeeklyData <- parallel::parLapply(cl = cl,1:nrow(fnoStocks),function(i) {
  ...
})

完成所有计算后,调用 @H_616_3@parallel::stopCluster(cl) 停止集群。

大佬总结

以上是大佬教程为你收集整理的如何在 R 中优化此功能?全部内容,希望文章能够帮你解决如何在 R 中优化此功能?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。