随机元素的统计/加权/概率选择
我正在创建一组项目,并计算每个项目在样本中出现的次数。后来我希望随机选择一个项目,但我希望选择任何特定项目的机会等于出现次数与所有项目的所有出现次数之和相比。
我相信我已经找到了一个很好的解决方案,但我感兴趣的是这个概念的标准术语是什么以及实现它的标准方法是什么。
I am creating a set of items and with each I am counting its number of occurrences in a sample. Later I wish to choose an item at random but I want the chance of choosing any particular item to be equal to the number of occurrences as compared to the total of all occurrences of all items.
I believe I have found a nice solution but I'm interested what the standard term for this concept is and what the standard methods of achieving it are.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这本身没有名称,但它是根据粒子过滤期间的证据更新您的信念的重要一步,这可能就是您正在寻找的术语。
从 0 到 n-1 中选择一个随机数 (r)(n 是所有项目出现的总次数)。然后迭代每个项目并从 r 中减去出现的次数。当您的值低于零时,选择最后一项。请注意,将相同的项目分组在同一位置并不重要。您可能会重复,但这仍然有效。
或者,如果您的出现次数单独存储在数组中(而不是直方图),则只需从数组中选择一个随机索引即可。
This doesn't have a name on it's own, but it's an important step in updating your beliefs based on evidence during PARTICLE FILTERING which is probably the term you're looking for.
Choose a random number (r) from 0 to n-1 (n is the total number of occurrences of all items). Then iterate over each item and subtract the number of occurrences from r. When you get below zero, select the last item. Note that it's not important to group the same item in the same place. You may have repeats and this will still work.
Alternatively, if your occurrences are stored individually in an array (rather than a histogram), simply select a random index from the array.