2017-01-23 99 views
1

我想從區間[1,N]隨機抽樣兩個整數x和y,使得| x-y | > = D,對於某些D < N.下面的代碼(用R編寫)是我一直在使用的,但效率非常低。這種抽樣有更好的方法嗎?謝謝。隨機抽樣與距離條件

N <- 100; D <- 10; 

i <- sample(1:N, 2) 

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

根本看起來效率不高 - 你爲什麼這麼說?對於10的距離和值1到100,大多數時候你只需要調用'sample'一次 – rawr

+0

我認爲(效率)取決於具體的用例,但最重要的是,這個算法從「對於任何樣本調用(1:N,2),沿着1 - [(Nx)/ N +(x-0)/ N + 2D/N]對於[1:N]和D miraculixx

回答

1

我想關鍵是要認識到y是依賴於x(或其他方式)。下面是應該至多三個步驟的工作中的算法:

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 

的想法是,一旦X取樣,Y必須是在範圍[1:(XD)]或[X + d:N]以滿足| xy | > = D。

示例:

N = 100; 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] 
0

我將由第一處理這一隨機採樣數之間的差大於或等於D。換句話說,我們想要用DN-1之間的數字進行取樣。

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

現在我們需要做的就是通過選擇1N - difference之間的數字。我們選擇較小的數字。我們可以使用vapply來做到這一點。

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

最後,我們通過將差值加到較低的值來得到較高的值。

upperval <- lowerval + difference 
+0

需要注意的一點是,使用這種算法的分佈將與使用OP算法的分佈不同。因此,根據隨機抽樣所需的任何其他要求,這可能或可能不是他們想要的。 – Dason