如何生成表示离散均匀分布之和的数字 [英] How to generate a number representing the sum of a discrete uniform distribution

查看:20
本文介绍了如何生成表示离散均匀分布之和的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第 1 步:

假设我想生成取值 -1 或 1 的离散均匀随机数.换句话说,我想生成具有以下分布的数字:

Let's say that I want to generate discrete uniform random numbers taking the value -1 or 1. So in other words I want to generate numbers having the following distribution:

P(X = -1) = 0.5
P(X =  1) = 0.5

要生成包含 100 个这些数字的数组,我可以编写以下代码:

To generate an array of 100 of those numbers I can write this code:

n   = 100
DV  = [-1,1];          % Discrete value
RI  = unidrnd(2,n,1);  % Random uniform index
DUD = DV(RI);          % Discrete uniform distribution

我的 DUD 数组如下所示:[-1,1,1,1,-1,-1,1,-1,...]

My DUD array looks like: [-1,1,1,1,-1,-1,1,-1,...]

第 2 步:

现在我想生成 10 个等于 sum(DUD) 的数字,因此 10 个数字的分布对应于遵循离散均匀分布的 100 个数字之和.

Now I would like to generate 10 numbers equal to sum(DUD), so 10 numbers having a distribution corresponding to the sum of 100 numbers following a discrete uniform distribution.

当然可以:

for ii = 1:10
    n   = 100;
    DV  = [-1,1];          % Discrete value
    RI  = unidrnd(2,n,1);  % Random index
    DUD = DV(RI);          % Discrete uniform distribution
    SDUD(ii) = sum(DUD);
end

SDUD =

   2   2  -6  -2  -4   2   4   4   0   2 

是否有数学/matlab 技巧可以做到这一点? 不使用 for 循环.

Is there a mathematical/matlab trick to do that ? without using a for loop.

SDUD 的直方图(具有 10000 个值和 n=100)如下所示:

The histogram of SDUD (with 10000 values and n=100) looks like this:

奖励:

如果可以修改原始离散值,那就太好了.因此,离散值可以代替 [-1,1],例如 [0,1,2],每个出现 p = 1/number_of_discrete_value,因此在本例中为 1/3.

It will be great if the original discrete values could be modified. So instead of [-1,1] the discrete value could be, for example, [0,1,2], each with an occurence p = 1/number_of_discrete_value, so 1/3 in this example.

推荐答案

对于两个值

这本质上是一个二项式分布(参见 Matlab 的 binornd),仅缩放和移位,因为基础值由 DV 给出 而不是 01:

For two values

That's essentially a binomial distribution (see Matlab's binornd), only scaled and shifted because the underlying values are given by DV instead of being 0 and 1:

n = 100;
DV = [-1 1];
p = .5; % probability of DV(2)
M = 10;
SDUD = (DV(2)-DV(1))*binornd(n, p, M, 1)+DV(1)*n;

对于任意数量的值

您拥有的是 多项分布(参见 Matlab 的 mnrnd):

For an arbitrary number of values

What you have is a multinomial distribution (see Matlab's mnrnd):

n = 100;
DV = [-2 -1 0 1 2];
p = [.1 .2 .3 .3 .1]; % probability of each value. Sum 1, same size as DV
M = 10;
SDUD = sum(bsxfun(@times, DV, mnrnd(n, p, M)), 2);

这篇关于如何生成表示离散均匀分布之和的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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