How to generate a number representing the sum of a discrete uniform distribution

前端 未结 2 446
感动是毒
感动是毒 2021-01-21 03:10

Step 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

2条回答
  •  醉梦人生
    2021-01-21 03:32

    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;
    

    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);
    

提交回复
热议问题