R, How does while (TRUE) work?

北战南征 提交于 2019-12-09 12:31:35

问题


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)

回答1:


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".



回答2:


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;
}


来源:https://stackoverflow.com/questions/18942299/r-how-does-while-true-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!