来自R中的自定义分发的样本 [英] Sample from custom distribution in R
问题描述
我已经在R中实现了负二项式分布的替代参数化,例如(另请参见这里):
I have implemented an alternate parameterization of the negative binomial distribution in R, like so (also see here):
nb = function(n, l, a){
first = choose((n + a - 1), a-1)
second = (l/(l+a))^n
third = (a/(l+a))^a
return(first*second*third)
}
其中n是计数,lambda是均值,a是超分散项.
Where n is the count, lambda is the mean, and a is the overdispersion term.
我想从此分布中抽取随机样本,以验证我对负二项式混合模型的实现,但不确定如何执行此操作.此功能的CDF定义不容易,因此我考虑了按
I would like to draw random samples from this distribution in order to validate my implementation of a negative binomial mixture model, but am not sure how to go about doing this. The CDF of this function isn't easily defined, so I considered trying rejection sampling as discussed here, but that didn't work either (and I'm not sure why- the article says to first draw from a uniform distribution between 0 and 1, but I want my NB distribution to model integer counts...I'm not sure if I understand this approach fully.)
感谢您的帮助.
推荐答案
您似乎可以:
1)在0和1之间绘制一个统一的随机数.
1) Draw a uniform random number between zero and one.
2)在数值上积分概率密度函数(这实际上只是一个和,因为分布是离散的且下界为零).
2) Numerically integrate the probability density function (this is really just a sum, since the distribution is discrete and lower-bounded at zero).
3)积分中的任何一个值都会使CDF超过随机数,这就是您的随机抽奖.
3) Whichever value in your integration takes the cdf past your random number, that's your random draw.
因此,一起执行以下操作:
So all together, do something like the following:
r <- runif(1,0,1)
cdf <- 0
i <- -1
while(cdf < r){
i <- i+1
p <- PMF(i)
cdf <- cdf + p
}
其中PMF(i)是i计数上的概率质量,由分布参数指定.当while循环结束时的i值就是您的样本.
Where PMF(i) is the probability mass over a count of i, as specified by the parameters of the distribution. The value of i when this while-loop finishes is your sample.
这篇关于来自R中的自定义分发的样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!