使用接受-拒绝方法模拟随机变量
Simulation of random variables using acceptance-rejection method
我有如下算法
第一步,用qj=P(Y=j)模拟Y的值
步骤 2. 生成统一变量
第3步。如果U<=Pj/(c*qj)则X=j并停止。否则返回第 1 步。
具体例子:
X=1,2,3,4 与 P1=.2,P2=.15,P3=.25,P4=.4
为 X 生成值
设Y~UD(1,4)
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<-runif(n,1,4)
或这一行 probY<-c(.25,.25,.25,.25)
。此行 'Otherwise come back to step 1.' 在循环中也丢失了,尽管总是相同的 .25
有人可以帮忙吗?
我认为这里的问题是对算法的工作方式有点混淆。
为了从您的分布(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.Generate y 来自 Y ~ UD(1,4).
2.Generate u 从 U ~ U(0,1).
3.Check看是否u≤f(y)/cg(y)。如果它 是 ,定义 x = y 就大功告成了!如果不是,则返回步骤 1。
在您提供的代码中,您实际上从未生成 y 变量。这是一个应该为您工作的功能!希望我的评论能很好地解释它!
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)
}
我有如下算法
第一步,用qj=P(Y=j)模拟Y的值
步骤 2. 生成统一变量
第3步。如果U<=Pj/(c*qj)则X=j并停止。否则返回第 1 步。
具体例子:
X=1,2,3,4 与 P1=.2,P2=.15,P3=.25,P4=.4
为 X 生成值
设Y~UD(1,4)
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<-runif(n,1,4)
或这一行 probY<-c(.25,.25,.25,.25)
。此行 'Otherwise come back to step 1.' 在循环中也丢失了,尽管总是相同的 .25
有人可以帮忙吗?
我认为这里的问题是对算法的工作方式有点混淆。
为了从您的分布(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.Generate y 来自 Y ~ UD(1,4).
2.Generate u 从 U ~ U(0,1).
3.Check看是否u≤f(y)/cg(y)。如果它 是 ,定义 x = y 就大功告成了!如果不是,则返回步骤 1。
在您提供的代码中,您实际上从未生成 y 变量。这是一个应该为您工作的功能!希望我的评论能很好地解释它!
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)
}