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

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

问题描述

第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 

是否有数学/矩阵技巧可以做到这一点?而无需使用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:

奖金:

如果可以修改原始离散值,那就太好了.因此,离散值可以是[0,1,2],而不是[-1,1],每个都出现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的

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的

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天全站免登陆