【问题标题】:Simulation of random variables using acceptance-rejection method使用接受-拒绝方法模拟随机变量
【发布时间】:2019-04-19 17:55:36
【问题描述】:

我有以下算法

步骤 1. 用 qj=P(Y=j) 模拟 Y 的值

步骤 2. 生成统一变量

步骤 3。如果 U

以及具体的例子:

X=1,2,3,4 与 P1=.2,P2=.15,P3=.25,P4=.4

为 X 生成值

  1. 让Y~UD(1,4)

  2. c=.4/.25

这是我在 R 中实现此算法的方法:

f<-function(){
n<-4

probY<-c(.25,.25,.25,.25)
probX<-c(2,.15,.25,.4)

X<-rep(0,4)

U<-runif(n)

c<-.4/.25

for(j in 1:4)
{
if(U[j]<= probX[j]/(c*probY[j]))
   X<-j

}


return(X)

}

输出是4 我不认为是正确的。我不确定我是否应该写Y&lt;-runif(n,1,4) 或这行probY&lt;-c(.25,.25,.25,.25)。还有这一行“否则返回第 1 步”。循环中缺少,但总是相同的 .25

有人可以帮忙吗?

【问题讨论】:

    标签: r algorithm simulation


    【解决方案1】:

    我认为这里的问题是与算法的工作原理有点混淆。

    为了从您的分布中生成单个值(X = 1,2,3,4 其中 P(X = 1) = .2, P(X = 2) = .15 , P(X = 3) = .25, P(X = 4) = .4),我们需要遵循算法的步骤。假设我们选择c = .4/.25

    1.从Y~UD(1,4)生成y

    2.从U~U(0,1)生成u

    3.检查是否u≤f(y)/cg(y)。如果它,定义x = y,你就完成了!如果不是,请返回第 1 步。

    在您提供的代码中,您从未真正生成 y 变量。这是一个应该为您工作的功能!希望我的 cmets 解释得足够清楚!

    accRej <- function(){
    
      #The probabilities for generating a r.v. from X
      probX <- c(.2,.15,.25,.4)
    
      #The Value for c
      c <- .4/.25
    
      #x is a placeholder for our final value of x 
      #and i is a counter variable which will allow us to stop the loop when we complete step 3
      x <- numeric(1)
      i <- 1
    
      #Now, start the loop!
      while(i <= 1){
        #Step 1, get y
        y <- sample(1:4,1)
        #Step 2, get u
        u <- runif(1)
        #Step 3, check to see if the inequality holds
        #If it does, assign y to x and add 1 to i (making it 2) to stop the while loop
        #If not, do nothing and try again!
        if(u <= probX[y]/c*.25){
          x[i] <- y
          i <- i+1
        }
      }
      #Return our value of x
      return(x)
    }
    

    注意这段代码中probX[i] 等于我们算法中的 f(y),并且由于 Y~UD(1,4),.25 = g(y ) 总是。希望这会有所帮助!

    另外,这里是使用此方法生成n 随机变量的代码。基本上和上面一样,只是有一个选项可以把 1 改成n

    accRej <- function(n){
    
      #The probabilities for generating a r.v. from X
      probX <- c(.2,.15,.25,.4)
    
      #The Value for c
      c <- .4/.25
    
      #x is a placeholder for our final value of x 
      #and i is a counter variable which will allow us to stop the loop when we complete step 3
      x <- numeric(n)
      i <- 1
    
      #Now, start the loop!
      while(i <= n){
        #Step 1, get y
        y <- sample(1:4,1)
        #Step 2, get u
        u <- runif(1)
        #Step 3, check to see if the inequality holds
        #If it does, assign y to x and add 1 to i
        #If not, do nothing and try again!
        if(u <= probX[y]/c*.25){
          x[i] <- y
          i <- i+1
        }
      }
      #Return our value of x
      return(x)
    }
    

    【讨论】:

    • 非常感谢 BurlyPotatoMan
    • 非常有帮助和详细的答案:)
    • @Isa 很高兴为您提供帮助! :)
    • BurlyPotatoMan,如果我想知道直方图的样子,需要哪些命令行?
    • @Isa 只是为了确保我理解,您想查看此方法生成的 n 个变量的直方图吗?还是别的什么?
    猜你喜欢
    • 2019-09-20
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多