0

Experiment:'X is a binomial of five trials and p = 0.5, where X is the number of heads. Ysample is a binomial of X trials and p=0.5, where Ysample is the number of heads.' T is the number of times the experiment is repeated until the value of Ysample>=3. This is the code:

while (Ysample[i]<3){
length(X)=n
    length(Y)=n
    X=c(1:n)
    Y=c(1:n)
    i=1
    X[i]=sum(rbinom(5,1,0.5))
    Ysample[i]=sum(rbinom(X[i],1,0.5))
    i=i+1
    }
    T=n

I am unsure why it is not working and why no matter how many times I run it I always get T=10000?

2
  • Please do the favor of adding an appropriate language tab at the bottom of your posts, e.g. c, c++, php, ... It's a waste time for people to have to open this up to see what language you are writing in. Thanks. Commented Mar 7, 2014 at 0:37
  • 1
    The poster did add a language tab; r. Although it should have been capitalized... Commented Mar 7, 2014 at 0:38

2 Answers 2

1

Does this what you want?

T <- 0
Ysample <- 0

while( Ysample < 3 )
{
    X <- sum( rbinom( 5, 1, 0.5 ) )
    Ysample <- sum( rbinom( X, 1, 0.5 ) )
    T <- T + 1
}

cat( "T =  ", T, "\n\n" )
Sign up to request clarification or add additional context in comments.

Comments

1

Check what you are setting n to. T is simply set to n at the end of the loop. It isn't computed or anything.

Also, you may want to consider using higher order functions; while R can handle loops, it does much better without them, and it's more idiomatic R to use higher order functions and the default "vectorizing" behavior of operations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.