Hello, "gncl dzgn",
your problem has a flavor of homework which is usually not delt with on
this list. However, a few comments:
0. The description of your problem is rather vague, in particular, the
meaning of "input" in the description of your "conditions"
is unclear! (By
the way, your main problem is probably not the inclusion of the
conditions; see 2.)
Note: "PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented,
minimal, self-contained, reproducible code."
^^^^^^^
1. Do not use variable names like t, T, c, s because they already exist as
R-objects.
2. Have you noticed the warnings "In s >= d : longer object length is
not
a multiple of shorter object length" and "In s + b * (0:T) : longer
object
length is not a multiple of shorter object length"? Apparently the
involved objects (s, d, b and T) do not "fit together". Hint: Compare
the
lenght of s with the lengths of d and 0:T!
3. Do not run the code all at once, but either without the for-loops line
by line with j and i set to approriate values (like i = 1 and j = 2), or
with the for-loops, but with N and T very small, e. g. N = 1 and T = 2 to
start with. Alternatively, you could also take a look at the function
browser() (and include it in the bodies of your for-loops).
4. Another hint: Your if-else-statement
if(x < minx) {
s[i] <- minx
} else {
if(x > maxx) {
s[i] <- maxx
} else s[i] <- x
}
appears be simplifiable to
s[i] <- min( max( x, minx), maxx)
Hth -- Gerrit
> 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)
>