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

查看:86
本文介绍了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< fx(x)然后返回X,否则返回步骤1.

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 <fx(x))不是真= FALSE.那么为什么条件不是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)之前先定义了一个.

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

推荐答案

这是一个无限循环.只要条件的计算结果始终为TRUE,该表达式便会执行.但是,在表达式中有一个return,当调用该return时(例如,如果y < fx(x)),它将退出该函数并因此停止循环.

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构造的条件.由于它是FALSE,因此将评估else表达式并打印i.
  4. i增加1.
  5. 重复步骤3和4.
  6. i达到4的值时,if构造的条件为TRUE,并且对return("stopped")进行求值.这将停止整个功能并返回值"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天全站免登陆