[R] Finding Stopping time
Gustaf Rydevik
gustaf.rydevik at gmail.com
Thu Nov 27 16:37:04 CET 2008
Hi Debanjan,
It would be more likely that you get a response if your question was more clear.
Your code is very difficult to read, and it doesn't help that you
don't provide any context, or comment your code with "### This is
calculating the average" kind of statements.
What are you trying to do?
Anyhow, after quite a bit of effort trying to understand what you've
done, I found your (simple!)
mistake:
Since you are resetting the "k" counter after your first try, you need
to change your "k" constant in that big quantity you're calculating to
(k-N[j-1]), like this:
T[k] <-
(((k-N[j-1])/2)*log(theta1/theta2))+(((theta2-theta1)/(2*theta1*theta2))*smm[k])-((k-N[j-1])*(theta2-theta1)/2)
As an aside, try not to use variables defined outside a function in
the function code (in this case your "x"). It makes the code more
difficult to follow, and far more likely to break.
Regards,
Gustaf
On Wed, Nov 26, 2008 at 4:04 PM, Debanjan Bhattacharjee
<debanjan.bhattacharjee at gmail.com> wrote:
> Can any one help me to solve problem in my code? I am actually trying to
> find the stopping index N.
> So first I generate random numbers from normals. There is no problem in
> finding the first stopping index.
> Now I want to find the second stopping index using obeservation starting
> from the one after the first stopping index.
> E.g. If my first stopping index was 5. I want to set 6th observation from
> the generated normal variables as the first random
> number, and I stop at second stopping index.
>
> This is my code,
>
>
> alpha <- 0.05
> beta <- 0.07
> a <- log((1-beta)/alpha)
> b <- log(beta/(1-alpha))
> theta1 <- 2
> theta2 <- 3
>
> cumsm<-function(n)
> {y<-NULL
> for(i in 1:n)
> {y[i]=x[i]^2}
> s=sum(y)
> return(s)
> }
> psum <- function(p,q)
> {z <- NULL
> for(l in p:q)
> { z[l-p+1] <- x[l]^2}
> ps <- sum(z)
> return(ps)
> }
> smm <- NULL
> sm <- NULL
> N <- NULL
> Nout <- NULL
> T <- NULL
> k<-0
> x <- rnorm(100,theta1,theta1)
> for(i in 1:length(x))
> {
> sm[i] <- psum(1,i)
> T[i] <-
> ((i/2)*log(theta1/theta2))+(((theta2-theta1)/(2*theta1*theta2))*sm[i])-(i*(theta2-theta1)/2)
> if (T[i]<=b | T[i]>=a){N[1]<-i
> break}
>
> }
> for(j in 2:200)
> {
> for(k in (N[j-1]+1):length(x))
> { smm[k] <- psum((N[j-1]+1),k)
> T[k] <-
> ((k/2)*log(theta1/theta2))+(((theta2-theta1)/(2*theta1*theta2))*smm[k])-(k*(theta2-theta1)/2)
> if (T[k]<=b | T[k]>=a){N[j]<-k
> break}
> }
> }
>
> But I cannot get the stopping index after the first one.
>
> Tanks
> --
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik
More information about the R-help
mailing list