骨灰盒从drawing [英] Numpy drawing from urn

查看:106
本文介绍了骨灰盒从drawing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在numpy中运行一个相对简单的随机绘制,但是我找不到表达它的好方法. 我认为最好的方法是将其描述为从replacement中提取而无需更换.我有一个带有k种颜色的an,以及每种颜色的n_k个球.我想画m个球,知道我每种颜色有多少个球.

I want to run a relatively simple random draw in numpy, but I can't find a good way to express it. I think the best way is to describe it as drawing from an urn without replacement. I have an urn with k colors, and n_k balls of every color. I want to draw m balls, and know how many balls of every color I have.

我目前的尝试

np.bincount(np.random.permutation(np.repeat(np.arange(k), n_k))[:m], minlength=k)

在这里,n_k是一个长度为k的数组,其中包含球的数量.

here, n_k is an array of length k with the counts of the balls.

这似乎等同于 np.bincount(np.random.choice(k, m, n_k / n_k.sum(), minlength=k)

这更好一些,但仍然不是很好.

which is a bit better, but still not great.

推荐答案

您想要的是

What you want is an implementation of the multivariate hypergeometric distribution. I don't know of one in numpy or scipy, but it might already exist out there somewhere.

您可以通过重复调用 .这是否会比您的实现更有效,取决于存在多少种颜色以及每种颜色有多少个球.

You can implement it using repeated calls to numpy.random.hypergeometric. Whether that will be more efficient than your implementation depends on how many colors there are and how many balls of each color.

例如,这是一个脚本,该脚本从包含三种颜色(红色,绿色和蓝色)的中打印绘图结果:

For example, here's a script that prints the result of drawing from an urn containing three colors (red, green and blue):

from __future__ import print_function

import numpy as np


nred = 12
ngreen = 4
nblue = 18

m = 15

red = np.random.hypergeometric(nred, ngreen + nblue, m)
green = np.random.hypergeometric(ngreen, nblue, m - red)
blue = m - (red + green)

print("red:   %2i" % red)
print("green: %2i" % green)
print("blue:  %2i" % blue)

示例输出:

red:    6
green:  1
blue:   8

以下功能可以概括为:选择给定包含每个颜色编号的数组colorsm球:

The following function generalizes that to choosing m balls given an array colors holding the number of each color:

def sample(m, colors):
    """
    Parameters
    ----------
    m : number balls to draw from the urn
    colors : one-dimensional array of number balls of each color in the urn

    Returns
    -------
    One-dimensional array with the same length as `colors` containing the
    number of balls of each color in a random sample.
    """

    remaining = np.cumsum(colors[::-1])[::-1]
    result = np.zeros(len(colors), dtype=np.int)
    for i in range(len(colors)-1):
        if m < 1:
            break
        result[i] = np.random.hypergeometric(colors[i], remaining[i+1], m)
        m -= result[i]
    result[-1] = m
    return result

例如,

>>> sample(10, [2, 4, 8, 16])
array([2, 3, 1, 4])

这篇关于骨灰盒从drawing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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