[英]Generate n samples, Rejection sampling in R
Im working with rejection sampling with a truncated normal distribution, see r code below. 我正在处理截断正态分布的拒绝采样,请参见下面的r代码。 How can I make the sampling stop at a specific n?
如何使采样在特定的n处停止? for example 1000 observations.
例如1000个观察值。 Ie I want to stop the sampling when the number of accepted samples has reached n (1000).
即我要在接受的样本数达到n(1000)时停止采样。
Any suggestions? 有什么建议么? Any help is greatly appreciated :)
任何帮助是极大的赞赏 :)
#Truncated normal curve
curve(dnorm(x, mean=2, sd=2)/(1-pnorm(1, mean=2, sd=2)),1,9)
#create a data.frame with 100000 random values between 1 and 9
sampled <- data.frame(proposal = runif(100000,1,9))
sampled$targetDensity <- dnorm(sampled$proposal, mean=2, sd=2)/(1-pnorm(1, mean=2, sd=2))
#accept proportional to the targetDensity
maxDens = max(sampled$targetDensity, na.rm = T)
sampled$accepted = ifelse(runif(100000,0,1) < sampled$targetDensity / maxDens, TRUE, FALSE)
hist(sampled$proposal[sampled$accepted], freq = F, col = "grey", breaks = 100, xlim = c(1,9), ylim = c(0,0.35),main="Random draws from skewed normal, truncated at 1")
curve(dnorm(x, mean=2, sd=2)/(1-pnorm(1, mean=2, sd=2)),1,9, add =TRUE, col = "red", xlim = c(1,9), ylim = c(0,0.35))
X <- sampled$proposal[sampled$accepted]
How can I set the length of X to a specific number when I sample? 采样时如何将X的长度设置为特定数字?
After sleeping on it, if you're determined to use rejection sampling and only doing it until 1,000 have passed, I don't think there's a better option than just using a while loop. 睡一觉之后,如果您确定要使用拒绝采样并仅在经过1000次之前进行采样,我认为没有比使用while循环更好的选择了。 This is significantly less efficient than
效率远不如
sampled$accepted = ifelse(runif(100000,0,1) < sampled$targetDensity / maxDens, TRUE, FALSE)
X <- sampled$proposal[sampled$accepted][1:1000]
The time taken for the above code is 0.0624001s
. 上面的代码
0.0624001s
的时间为0.0624001s
。 The time taken for the code below is 0.780005s
. 下面的代码花费的时间为
0.780005s
。 I include it because it is the answer to the specific question you've asked, but the approach is inefficient. 我将其包括在内是因为它是您所提出的特定问题的答案,但是这种方法效率低下。 If there's another option I'd use that.
如果还有其他选择,我会使用它。
#Number of samples
N_Target <- 1000
N_Accepted <- 0
#Loop until condition is met
i = 1
sampled$accepted = FALSE
while( N_Accepted < N_Target ){
sampled$accepted[i] = ifelse(runif(1,0,1) < sampled$targetDensity[i] / maxDens, TRUE, FALSE)
N_Accepted = ifelse( sampled$accepted[i], N_Accepted + 1 , N_Accepted )
i = i + 1
if( i > nrow( sampled ) ) break
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.