R有条件的累加和 [英] R cumulative sum with condition

查看:157
本文介绍了R有条件的累加和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(对于那些熟悉MCMC的人,我正在尝试编写Metropolis-Hastings算法(作为其步骤).

(For those of you that are familiar with MCMC I am trying to write (a step of) the Metropolis-Hastings algorithm).

我正在尝试对初始值为0.5的小随机值向量进行累加和.但是,如果任何时候的累积总和小于0或大于1,我都需要复制以前的值并继续累积总和而不求和这些值,这会打破这种情况.

I am trying to do a cumulative sum of a vector of small random values with a starting value of 0.5. However, if the cumulative sum at any point gets under 0 or over 1 I need to copy the previous value and continue on the cumulative sum, without summing the values, which would break this condition.

注意:我需要向量化的解决方案(没有循环或索引)以达到优化目的或快速实现.仅使用基本R函数的加分.

Note: I need a vectorized solution (no loops or indices) for optimization purposes or something fast. Bonus points for using only base R functions.

示例:

set.seed(1)  
temp=c(0.5,runif(20,-0.3,0.3))
cumsum(temp)

 [1] 0.5000000 0.3593052 0.2825795 0.3262916 0.5712162 0.3922254 0.6312592
 [8] 0.8980644 0.9945430 1.0720115 0.8090832 0.6326680 0.4386020 0.5508157
[15] 0.4812780 0.6431828 0.6418024 0.7723735 1.0675171 0.9955382 1.1620054

但是我需要的是

 [1] 0.5000000 0.3593052 0.2825795 0.3262916 0.5712162 0.3922254 0.6312592
 [8] 0.8980644 0.9945430 0.9945430 0.7316148 0.5551995 0.3611336 0.4733473
[15] 0.4038095 0.5657144 0.5643339 0.6949050 0.9900487 0.9180698 0.9180698

使用for循环,我们可以这样做

Using a for loop we could do this with

for (i in 2:21) {
    temp[i]=temp[i-1]+temp[i]
    if(temp[i]<0 | temp[i]>1) {
        temp[i]=temp[i-1]
    }
}

推荐答案

更快的C ++版本:

library(Rcpp)
Cpp_boundedCumsum <- cppFunction('NumericVector boundedCumsum(NumericVector x){
  int n = x.size();
  NumericVector out(n);
  double tmp;
  out[0] = x[0];
  for(int i = 1; i < n; ++i){
     tmp = out[i-1] + x[i];
     if(tmp < 0.0 || tmp > 1.0) 
        out[i] = out[i-1];
     else 
        out[i] = tmp;
  }
  return out;
}')

与R版本的比较:

R_boundedCumsum <- function(x){ 
    for (i in 2:length(x)){
        x[i] <- x[i-1]+x[i]
        if(x[i]<0 || x[i]>1) 
            x[i] <- x[i-1]
    }
    x
}

x <- runif(1000)
all.equal(R_boundedCumsum(x), Cpp_boundedCumsum(x))
[1] TRUE

library(microbenchmark)
microbenchmark(R_boundedCumsum(x), Cpp_boundedCumsum(x))
Unit: microseconds
                 expr      min        lq       mean   median       uq      max neval
   R_boundedCumsum(x) 2062.629 2262.2225 2460.65661 2319.358 2562.256 4112.540   100
 Cpp_boundedCumsum(x)    3.636    4.3475    7.06454    5.792    9.127   25.703   100

这篇关于R有条件的累加和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆