R: censored cumsum (censored random walk) -
i able censor generation of random walk data walk never drops below target value (generally 0). following code accomplishes want except rather have function works similar cumsum can use crunch through millions of rows of such values such cumsum(x,min=0)
:
x <- rnorm(1000) y <- rep(0,length(x)) for(i in 2:length(x)) y[i] <- max(x[i]+y[i-1], 0) plot(y, type='l')
why not make own function (copied code, added initialization of y[1]
make similar cumsum
behavior):
cumsum0<-function(x,min=0){ y<-rep(0,length(x)) y[1]<-max(x[1],0) (i in 2:length(x)) y[i] <- max(x[i]+y[i-1], min) return(y) } x<-rnorm(1000) plot(cumsum0(x),type="l")
Comments
Post a Comment