如何使用自定义概率分布随机选择 [英] How to pick a random choice using a custom probability distribution

查看:66
本文介绍了如何使用自定义概率分布随机选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份来自美国人口普查网站的美国姓名及其各自姓名的列表.我想使用给定的概率从这个列表中生成一个随机名称.数据在这里:美国人口普查数据

I have a list of US names and their respective names from the US census website. I would like to generate a random name from this list using the given probability. The data is here: US Census data

我见过像 轮盘选择这样的算法,这些算法很容易实现,但我想知道是否有任何方法可以在 O(1) 中生成随机名称.对于 直方图数据,这更容易,因为您可以创建生日的整数哈希,但我想这样做是为了连续分布.

I have seen algorithms like the roulette wheel selection algorithm that are easy to implement, but I wanted to know if there was any way to generate random names in O(1). For histogram data this is easier, as you could create a hash of integers to birthdays, but I would like to do this for a continuous distribution.

如果这是不可能的,是否有任何 Python 模块接受概率分布并根据这些分布生成随机值?

If this is not possible, are there any python modules that take in probability distributions and generate random values based on those distributions?

推荐答案

有一个 O(1)-time 方法参见 详细描述了 Vose 的别名"方法.不幸的是,它的初始化成本很高.有关更简单方法的比较时间,请参阅 Eli Bendersky 的博客文章.可以在 在 Python 问题跟踪器中找到更多时间.

There is an O(1)-time method See this detailed description of Vose's "alias" method. Unfortunately, it suffers from high initialization cost. For comparative timings of simpler methods, see Eli Bendersky's blog post. More timings can be found in this from the Python issue tracker.

这篇关于如何使用自定义概率分布随机选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆