Hi everyone,

you might find my question elementary but I am a beginner and unfortunately I can't fix the problem. 

So, I simulate this following algorithm and some values of c are NA. Therefore, I should add these following two if-statements but I don't know how I should do it in a for-loop. 

Thank in advance if someone helps me!

The conditions: 

If there is no input greater or equal to d, then ALG = b*T
If  max(s +  b*(0:T)) < b*T , then OPT = b*T


The algorithm:

a <- 0.0008
b <- 0.0001 
T <- 100 
t <- 0:T 
alpha <- 1 

d<- sqrt(a * b) * T - b * t

N <- 100
c <- rep(0, N)
for (j in 1:N) {

e <- rnorm(T, mean = 0, sd = 0.001) 
s<- c( runif(1,0, a*T), rep(0, T-1) ) 
minx <- 0 
for(i in 2:T) { 
    x <- alpha * s[i-1] + e[i] 
maxx <- a*(T-i) 
if(x < minx) { 
 s[i] <- minx 
} else { 
if(x > maxx) { 
 s[i] <- maxx 
} else s[i] <- x 
} 
} 

p<- which(s >= d)[1]
ALG<- s[p] + b*(p-1)
OPT <- max(s +  b*(0:T))

c[j] <-  OPT / ALG

}

head(c)
mean(c)
plot(c)



 		 	   		  
	[[alternative HTML version deleted]]

