Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, How does while (TRUE) work?

I have to write a function of the following method :

Rejection method (uniform envelope):

Suppose that fx is non-zero only on [a, b], and fx ≤ k.

  1. Generate X ∼ U(a, b) and Y ∼ U(0, k) independent of X (so P = (X, Y ) is uniformly distributed over the rectangle [a, b] × [0, k]).

  2. If Y < fx(x) then return X, otherwise go back to step 1.

    rejectionK <- function(fx, a, b, K) {
        # simulates from the pdf fx using the rejection algorithm
        # assumes fx is 0 outside [a, b] and bounded by K
        # note that we exit the infinite loop using the return statement
    
        while (TRUE) {
          x <- runif(1, a, b)
          y <- runif(1, 0, K)
          if (y < fx(x)) return(x)
       }
    }
    

I have not understood why is this TRUE in while (TRUE) ?

if (y < fx(x)) is not true then the method suggests to repeat the loop again to generate the uniform number again. (y < fx(x)) is not true=FALSE. So why will not the condition be while (FALSE)?

Again in which basis will i get enter into the while loop ? That is, i am accustomed with this

   a=5 
   while(a<7){
      a=a+1
   }

here i define a before writing the condition (a<7) .

But in while (TRUE) , which statement is true ?

Additionally:

you can run the codes

  rejectionK <- function(fx, a, b, K) {
        # simulates from the pdf fx using the rejection algorithm
        # assumes fx is 0 outside [a, b] and bounded by K
        # note that we exit the infinite loop using the return statement

        while (TRUE) {
          x <- runif(1, a, b)
          y <- runif(1, 0, K)
          cat("y=",y,"fx=",fx(x),"",y < fx(x),"\n")
          if (y < fx(x)) return(x)
       }
    }

  fx<-function(x){
     # triangular density
     if ((0<x) && (x<1)) {
       return(x)
     } else if ((1<x) && (x<2)) {
       return(2-x)
     } else {
       return(0)
     }
 }

 set.seed(123)
 rejectionK(fx, 0, 2, 1)
like image 951
user 31466 Avatar asked Sep 22 '13 09:09

user 31466


2 Answers

It's an infinite loop. The expression is executed as long as the condition evaluates to TRUE, which it will always do. However, in the expression there is a return, which when called (e.g., if y < fx(x)), breaks out of the function and thus stops the loop.

Here is a simpler example:

fun <- function(n) {
  i <- 1
  while (TRUE) {
    if (i>n) return("stopped") else print(i)
    i <- i+1
  }
}

fun(3)
#[1] 1
#[1] 2
#[1] 3
#[1] "stopped"

What happens when this function is called?

  1. i is set to 1.
  2. The condition of the while loop is tested. Because it is TRUE, it's expression is evaluated.
  3. The condition of the if construct is tested. Since it is FALSE the else expression is evaluated and i is printed.
  4. i is increased by 1.
  5. Steps 3 and 4 are repeated.
  6. When i reaches the value of 4, the condition of the if construct is TRUE and return("stopped") is evaluated. This stops the whole function and returns the value "stopped".
like image 72
Roland Avatar answered Oct 11 '22 04:10

Roland


Inside while loop, if we have return statement with true or false.. it will work accordingly..

Example: To Check a list is circular or not..

here the loop is infinite, because while (true) is true always, but we can break sometimes by using the return statement,.

while(true)
{
if(!faster || !faster->next)
return false;
else
if(faster==slower || faster->next=slower)
{
printf("the List is Circular\n");
break;
}
else
{
slower = slower->next;
faster = faster->next->next;
}
like image 33
Venkatakrishna Kalepalli Avatar answered Oct 11 '22 03:10

Venkatakrishna Kalepalli