求和函数的问题

Problems with sum functions

使用其他编程语言来加速 R 是一项重大创新,但似乎也很危险。任何统计函数都必须稳定使用,给定相同条目的相同结果,例如 sumsdcor... 我遇到的问题是使用大量数据(10^8 样本)。我使用 RCPP 和 RCPPPARALLEL 版本的 sum 来演示大数据的错误。

#include <Rcpp.h>
using namespace Rcpp;
/********************************************************
 *           Rcpp inner function :sum
 *******************************************************/
// [[Rcpp::export]]
double RCPPSUM(NumericVector x) {
  return sum(x);
}

/*** R


library("Rcpp")
library("RcppParallel")
  options(digits=22)# showing more details in number  
  sourceCpp(system.file("tests/cpp/sum.cpp", package = "RcppParallel"))
##  Given two function parallelVectorSum  (RcppParallel) and vectorSum (C++STL)
##  We manipulated a very big vector
  x<-rnorm(100000000)
  (RSUM<-sum(x))
  (RCPP<-RCPPSUM(x))
  (RCPPPARALLEL<-parallelVectorSum(x))
  (STD<-vectorSum(x))
  identical(RSUM,RCPP)
  identical(STD,RCPP)
  identical(RSUM,RCPPPARALLEL)
  identical(RCPP,RCPPPARALLEL)
## Simple checking of stability
  v<-0
for(i in 1:100)v[i]<-sum(x)
  (mean(v))
  (sd(v))
####stable sd =0
  for(i in 1:100)v[i]<-vectorSum(x)
    (mean(v))
    (sd(v))
###stable sd =0
  for(i in 1:100)v[i]<-RCPPSUM(x)
  (mean(v))
  (sd(v))
###stable sd =0

  for(i in 1:100)v[i]<-parallelVectorSum(x)
  (mean(v))
  (sd(v))
##instable sd!=0
      */

我们可以将并行版本定性为不稳定,因此将其排除在外。 RCPP 和 RSUM 哪个结果才是真的?

简述:

  1. 推测警报:似乎也很危险。请备份或删除它。
  2. 请注意大小写正确。我们分别将其写为 RcppRcppParallel。虽然我猜你只是指标签,所以这并不重要。
  3. 使用 options(digits=22) 很可爱但没用。双精度给你大约 16 位数字。只希望得到更多并不能给你更多。