问题
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.
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]).
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?
i
is set to 1.- The condition of the
while
loop is tested. Because it isTRUE
, it's expression is evaluated. - The condition of the
if
construct is tested. Since it isFALSE
theelse
expression is evaluated andi
is printed. i
is increased by 1.- Steps 3 and 4 are repeated.
- When
i
reaches the value of 4, the condition of theif
construct isTRUE
andreturn("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