Function that returns integer with specific distri

2019-09-04 06:19发布

I am looking for a distribution or rather a function that returns an integer in a specific range with decreasing probability the higher the number.

Lets say the range is from 1 to 5.

85% of the time the function should return 1
8% of the time the function should return 2
4% of the time the function should return 3
2% of the time the function should return 4
1% of the time the function should return 5

Additionally it would be great if the probabilities are according to a set distribution say normal distributed or exponentially distributed.

What would a function like this look like?

3条回答
可以哭但决不认输i
2楼-- · 2019-09-04 06:36

Try:

nums = 1:5
prob = c(85,8,4,2,1)
xx = list()
for(i in 1:5) xx[[length(xx)+1]] = rep(nums[i], prob[i])
xx = unlist(xx)
xx

sample(xx,1)
[1] 1

sample(xx,1) will return values by the given distribution. For more samples at a time:

sample(xx, 25)
 [1] 1 1 1 1 1 1 1 1 1 1 1 3 1 2 1 1 1 5 1 1 1 1 1 3 1

You can check the distribution by:

table(sample(xx, 100))

 1  2  3  4  5 
85  8  4  2  1 
> 
> 
table(sample(xx, 100, replace=T))

 1  2  3  4  5 
82  8  6  2  2 
查看更多
你好瞎i
3楼-- · 2019-09-04 06:40

Use

sample.int(n, size = 1, prob = p) 

where for the probabilities you could use something like

p <- exp(-(1:n))

or make use of the standard normal distribution

p <- dnorm(1:n)

Edit For your specific example use

n <- 5
p <- c(0.85, 0.08, 0.04, 0.02, 0.01)
查看更多
一夜七次
4楼-- · 2019-09-04 06:44

Not very efficient, and assumes that you can make sure that the cumsum adds up to 1.

reqProb = c(0.85,0.08,0.04,0.02,0.01)
nRandom = 100
# unlist(lapply(runif(nRandom,0,1),function(x) min(which(x<cumsum(reqProb)))))
unlist(lapply(runif(nRandom,0,1),function(x) which(x<cumsum(reqProb))[1]))
查看更多
登录 后发表回答