2014-05-05 38 views
0

序列我要生成的序列:生成由式为R

X_n= |X_{n-1} - epsilon_n|, 

其中epsilon_n具有指数分布。

例如

epsilon <- rexp(100, rate = 0.3) 
+0

的可能重复的[生成由式为R的序列(http://stackoverflow.com/questions/23477782/generate-a-sequence-by-formula-with-r) – joran

+0

该线程似乎有已被删除,@joran。 – gung

回答

1
## n is length of the sequence, X0 is initial value, 
## default exponential rate is 0.3 
xSeq <- function(n,X0,rate=0.3){ 
    vOut <- rep(0,n) 
    vOut[1] <- X0 
    eSeq <- rexp(n-1,rate) 
    for(i in 2:n){ 
    vOut[i] <- abs(vOut[i-1]-eSeq[i-1]) 
    vOut 
    } 
    return(vOut) 
} 
4

使用Reduce

X0 <- 10 
set.seed(42) 
epsilon<-rexp(100, rate = 0.3) 
eps <- c(X0, epsilon) 

X <- Reduce(function(x, y) abs(x-y), eps, accumulate = TRUE) 

plot(X) 

enter image description here

+0

这很有趣!感谢您的'减少'功能! – Marta