from even distribution on
. Namely:
where
- taking the whole part of the argument.
face loss probability
. This is a natural limitation.
. I will try to answer the question: how to simulate a pseudo-random sequence with such a distribution?#Naive approach len<-10 ps<-seq(len,2, by=-1) ps<- 1/ps^2 ps<-ps/sum(ps) ss<-cumsum(ps) gen_naiv <- function() { alpha<-runif(1) return (min(which(alpha<ss))) } #sample val<-c() len<-10000 for(i in 1:len) { val<-append(val, gen_naiv()) } vals<-factor(val) plot(vals) 
, you can write equality
. It remains to famous
and
find specific i. If you simply go through all i starting from 1 and ending with K, then on average you need
comparison operations. In the worst case, which, by the way, meets with probability
, we will need K comparisons, for an example from the graph above, it appears with a probability of 0.45473749. On the average, 7.5 comparisons and generation of one random variable are required. The number of operations makes you sad especially if you want to simulate a large number of throws with the wrong bone.
on M equal parts. We introduce an additional array r of length M. In the element
the array will be stored i, such that
: #Preprocessing ss<-cumsum(ps) ss<-append(ss, 0, after=0) M<-length(ss) rs<-c() for(k in 0:(M-1)) rs<-append(rs, min(which(ss>=k/M))) #chen's method finite discrete distribution generator gen_dfd <- function() { M<-10 alpha<-runif(1) idx<-rs[floor(alpha*M)+1] return (idx-1+min(which(alpha<ss[idx:(M+1)]))-1) } #sample code val<-c() len<-10000 for(i in 1:len) { val<-append(val, gen_dfd()) } vals<-factor(val) plot(vals)
) is relatively rare, M can be chosen arbitrarily large.Source: https://habr.com/ru/post/157689/
All Articles