我想在指数分发folows的背景下产生的对数正态分布随机数:对数正态分布PDF生成蟒蛇零
我有100个整数(说地方),从1到25这整数从我自己生成的指数分布。
在这个地方,我想分配N项。但是这些项目有他们自己的对数正态分布,有一些模式(1到25之间)和标准偏差(从1到7)。我的代码是这样的:
我有地方叫variable_vec
,我知道N.叫N
,我知道叫pref_value
模式,我知道标准差称为power_of_preference
的阵列。
首先,我将计算shape
和scale
参数pref_value
和power_of_preference
。比我的进展如下:
unique_localities = np.unique(np.array(vec_of_variable)) #all values of localities
res1 = [0 for i in range(len(unique_localities))]
res = [0 for i in range(len(vec_of_variable))] #this will be desired output
for i in range(len(res1)):
res1[i] = stats.lognorm.pdf(unique_localities[i], shape, 0, scale) #pdfs of values of localities
res1 = np.array([x/min(res1) for x in res1]) #here is the problem, min(res1) could be zero, see text
res1 = np.round(res1)
res1 = np.cumsum(res1)
item = 0
while item < N:
r = random.uniform(0, max(res1))
site_pdf_value_vec = [x for x in res1 if x >= r]
site_pdf_value = min(site_pdf_value_vec) #this is value of locality where Ill place one item
代码继续,但关键的部分在这里。简而言之,地点的lognorm pdf值是'我的物品放在该地区'的'概率'。这就是为什么我需要pdf值。
PS:这种方法是由我的主管批准的,所以我不想改变它。
问题是,有时会发生那min(res1) = 0
。比病除零,并res1
成为无穷的数组。 0到25之间的对数正态分布x
绝不为零,但可能非常接近。我遇到的问题是,这些pdf值中的一个太接近于零,所以python会将其round
归零。
我的问题是,如何避免在我的代码中得到零在res1
?我的想法是用python中的最小正浮点数替换零点,但我不知道这个值。还是有另一种更优雅的解决方案?
Thx寻求帮助。
PS:有人可以关于不采取res1
反向值的问题,问题步骤看起来超级流。但它是控制,这些值的最小值不是零。换句话说,每个地方都必须有一些“间隔”“概率”,如果一个pdf为零,它的概率不是区间而是一个数。
我试过这个解决方案,但问题有时还会出现。但是,谢谢。你还有其他建议吗? – Bobesh
你的意思是日志是-inf?在这种情况下,您应该将概率视为实际为零,并且如果您的算法将您除以零,则这是算法问题,而不是代码。 – jakevdp