简体   繁体   English

具有距离条件的随机采样

[英]Random Sampling with Distance Condition

I want to sample two integers x and y at random from an interval [1,N] such that |xy|我想从区间 [1,N] 中随机抽取两个整数 x 和 y,使得 |xy| >= D, for some D < N. The code below (written in R) is what I have been using but it is terribly inefficient. >= D,对于某些 D < N。下面的代码(用 R 编写)是我一直在使用的代码,但效率非常低。 Are there better methods for this sort of sampling?这种抽样有更好的方法吗? Thanks in adv.感谢广告。

N <- 100; D <- 10;

i <- sample(1:N, 2)

while ( abs( i[1] - i[2] ) < D ){
   i <- sort(sample(1:N, 2))
}

I guess the key is to realize that y is dependent on x (or the other way around).我想关键是要意识到 y 依赖于 x (或相反)。 Here's the algorithm that should work in at most three steps:这是最多应该在三个步骤中工作的算法:

1. sample x from [1:N]
2. sample y from [1:(x-D)] if (x-D) >= 1
   sample y from [x + D:N] if (x+D) <= N
3. If both conditions for y are met, choose one of the generated y  uniform at random

The idea is that once x was sampled, y needs to be in the range [1:(xD)] or [x+D:N] in order to satisfy |xy|这个想法是,一旦 x 被采样,y 需要在 [1:(xD)] 或 [x+D:N] 范围内才能满足 |xy| >= D. >= D。

Examples:例子:

N=100; N=100; D=10 D=10

a) x is close to N

1. x is sampled from 1:N as 95
2. to satisfy |x-y| >= D, y can be at most 85, so the range to sample y is [1:85]

b) x is close to 1

1. x is sampled from 1:N as 9
2. y must be at least 19, so the range to sample y is [19:N]

c) x is close to 50

1. x is sampled from 1:N as 45
2. y must be either at most 35, or at least 55, so the ranges to sample from are [1:35] and [55:N]

I would approach this by first randomly sampling a difference between the numbers is greater than or equal to D .我会通过首先随机抽样数字之间的差异大于或等于D In other words, we want to sample numbers between D and N-1 with replacement.换句话说,我们想用替换对DN-1之间的数字进行抽样。

difference <- sample(D:(N-1), 20, replace = TRUE)

Now all we need to do is select our lower number by selecting a number between 1 and N - difference .现在我们需要做的就是通过选择一个介于1N - difference之间的数字来选择较低的数字。 We can do this using vapply .我们可以使用vapply来做到这vapply

lowerval <- vapply(N - difference, sample, numeric(1), 1)

Finally we get the upper value by adding the difference to the lower value.最后,我们通过将差值与下限值相加得到上限值。

upperval <- lowerval + difference

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM