如何从多项分布中采样的算法

发布于 2025-01-09 12:21:10 字数 1964 浏览 4 评论 0原文

如果给定一个多项分布

p=[0.2,0.4,0.1,0.3]

,并且必须多次从该分布中采样并返回结果,我该如何编写算法呢?

例如 - 如果我有一个公平的骰子,我想滚动它 20 次并获得它落在哪一侧的总次数,

[4, 1, 7, 5, 2, 1]

这应该是结果(随机) ->它在 1 上降落了 4 次,在 2 上降落了一次,以此类推。 Numpy 中有一个函数可以做到这一点 在 numpy 中我们可以使用

numpy.random.multinomial()
>>> np.random.multinomial(20, [1/6.]*6, size=1)
array([[4, 1, 7, 5, 2, 1]]) # random

我想了解如何编写算法来执行此操作 我在 python 中尝试过这种方法 ->

import numpy as np
import random
probs = [0.2, 0.4, 0.1, 0.3]

def sample(count:int)->list:
    output = [0,0,0,0]
    for i in range(count):
        num = random.random()
        if(0 < num <= 0.15):
            output[2]+=1
        elif(0.15 < num <= 0.25):
            output[0]+=1
        elif(0.25 < num <= 0.35):
            output[3]+=1
        elif(0.35 < num <= 0.45):
            output[1]+=1
        
    return output

final_output = sample(10)
print(final_output)

np.random.multinomial(10, probs, size=1)

但我认为这不是最佳方法,也许我缺乏概率方面的一些概念?

CPython 中 Numpy 编写的实际代码: 链接到包含 numpy.random 代码的 Numpy 文件.multinomial() 从第 4176 行开始编写

可能重复: 如何从多项分布中采样?

https://numpy.org/doc/stable/参考/随机/生成/numpy.random.multinomial.html

使用 rmultinom() 从 R 中的多项分布生成随机数功能

If we are given a multinomial distribution

p=[0.2,0.4,0.1,0.3]

and we have to sample from this distribution over a number of times and return the result, how do I write the algorithm for this?

Eg - if I have a fair die and I want to roll it 20 time and get the total number of times that it landed on which side,

[4, 1, 7, 5, 2, 1]

this should be the result(randomized) -> It landed 4 times on 1, once on 2, etc.
There is a function to do this in Numpy
in numpy we can use

numpy.random.multinomial()
>>> np.random.multinomial(20, [1/6.]*6, size=1)
array([[4, 1, 7, 5, 2, 1]]) # random

I want to understand how the algorithm is written for performing this action
I've tried this approach in python ->

import numpy as np
import random
probs = [0.2, 0.4, 0.1, 0.3]

def sample(count:int)->list:
    output = [0,0,0,0]
    for i in range(count):
        num = random.random()
        if(0 < num <= 0.15):
            output[2]+=1
        elif(0.15 < num <= 0.25):
            output[0]+=1
        elif(0.25 < num <= 0.35):
            output[3]+=1
        elif(0.35 < num <= 0.45):
            output[1]+=1
        
    return output

final_output = sample(10)
print(final_output)

np.random.multinomial(10, probs, size=1)

But I don't think this is the optimal way, maybe I'm lacking some concepts in Probability?

The actual Code written in Numpy in CPython:
Link to the Numpy file where the code for numpy.random.multinomial() is written starting from line 4176

Possible Duplicate:
How to sample from a multinomial distribution?

References:
https://numpy.org/doc/stable/reference/random/generated/numpy.random.multinomial.html

Random number generation from multinomial distribution in R using rmultinom() function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2025-01-16 12:21:10

如果您关心多次从此分布中采样,那么值得查看别名方法 - https://en.wikipedia.org/wiki/Alias_method#:~:text=In%20computing%2C%20the%20alias%20method,任意%20probability%20distribution%20pi

在初始计算 $O(K\log K)$ 后,您可以在 $O(1)$ 时间内进行采样,其中 $K$ 是分布支持度的大小。

If you care about sampling from this distribution multiple times, then it is worth looking at the Aliasing method - https://en.wikipedia.org/wiki/Alias_method#:~:text=In%20computing%2C%20the%20alias%20method,arbitrary%20probability%20distribution%20pi.

You can sample in $O(1)$ time after an initial computation of $O(K\log K)$ where $K$ is the size of the support of the distribution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文