R,while (TRUE) 是如何工作的? [英] R, How does while (TRUE) work?

查看:18
本文介绍了R,while (TRUE) 是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写一个如下方法的函数:

I have to write a function of the following method :

拒收方式(统一信封):

假设 fx 仅在 [a, b] 上为非零,并且 fx ≤ k.

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

  1. 生成与 X 无关的 X ∼ U(a, b) 和 Y ∼ U(0, k)(所以 P =(X, Y ) 均匀分布在矩形 [a, b] × [0, 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]).

如果 Y

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

我不明白为什么 while (TRUE) 中的这个 TRUE 是?

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

如果 (y < fx(x)) 不为真,则该方法建议再次重复循环以再次生成统一数.(y while (FALSE)?

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)?

我将在哪个基础上再次进入 while 循环?也就是说,我习惯了这个

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
   }

这里我在写条件 (a<7) 之前定义了 a .

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

但是在 while (TRUE) 中,哪个语句是正确的?

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

另外:

你可以运行代码

  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),"
")
          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)

推荐答案

这是一个无限循环.只要条件计算结果为 TRUE,表达式就会执行,它总是会这样做.然而,在表达式中有一个 return,当调用它时(例如,如果 y ),会跳出函数并因此停止循环.

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 设置为 1.
  2. 测试while 循环的条件.因为它是 TRUE,所以它的表达式被评估.
  3. 测试 if 结构的条件.由于它是 FALSEelse 表达式被计算并打印 i.
  4. i 加 1.
  5. 重复第 3 步和第 4 步.
  6. i 达到 4 时,if 构造的条件是 TRUEreturn("stopped") 被评估.这将停止整个函数并返回值已停止".
  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".

这篇关于R,while (TRUE) 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆