Answers:
如果我们从开始,则求和望远镜,为(修改后的)CDF 给出。反过来,并照顾特殊情况,给出以下算法(恐怕编码为,但是您可以将其作为Python实现的伪代码):1 − 1 / k k = 1R
rsoliton <- function(n.values, n=2) {
x <- runif(n.values) # Uniform values in [0,1)
i <- ceiling(1/x) # Modified soliton distribution
i[i > n] <- 1 # Convert extreme values to 1
i
}
作为其使用(和测试)的示例,让我们为绘制值: N = 10
n.trials <- 10^5
i <- rsoliton(n.trials, n=10)
freq <- table(i) / n.trials # Tabulate frequencies
plot(freq, type="h", lwd=6)
from __future__ import print_function, division
import random
from math import ceil
def soliton(N, seed):
prng = random.Random()
prng.seed(seed)
while 1:
x = random.random() # Uniform values in [0, 1)
i = int(ceil(1/x)) # Modified soliton distribution
yield i if i <= N else 1 # Correct extreme values to 1
if __name__ == '__main__':
N = 10
T = 10 ** 5 # Number of trials
s = soliton(N, s = soliton(N, random.randint(0, 2 ** 32 - 1)) # soliton generator
f = [0]*N # frequency counter
for j in range(T):
i = next(s)
f[i-1] += 1
print("k\tFreq.\tExpected Prob\tObserved Prob\n");
print("{:d}\t{:d}\t{:f}\t{:f}".format(1, f[0], 1/N, f[0]/T))
for k in range(2, N+1):
print("{:d}\t{:d}\t{:f}\t{:f}".format(k, f[k-1], 1/(k*(k-1)), f[k-1]/T))
k Freq. Expected Prob Observed Prob
1 9965 0.100000 0.099650
2 49901 0.500000 0.499010
3 16709 0.166667 0.167090
4 8382 0.083333 0.083820
5 4971 0.050000 0.049710
6 3354 0.033333 0.033540
7 2462 0.023810 0.024620
8 1755 0.017857 0.017550
9 1363 0.013889 0.013630
10 1138 0.011111 0.011380
该代码应在Python 2或3中工作。