Search code examples
matlabsimulationprobabilitydistributiondistributed-computing

How to simulate the Hypergeometric distribution in Matlab


i want to simulate in Matlab program the Hypergeometric distribution with probability mass function and parameters as described here : https://en.wikipedia.org/wiki/Hypergeometric_distribution how can i code that while producing random numbers from uniform distribution.


Solution

  • This is easily done with the randperm function, which generates a sample without replacement.

    Let the distribution parameters be defined as follows:

    N = 10; % population size
    K = 3;  % number of success states in the population
    n = 5;  % number of draws
    

    Then the variable k obtained as

    k = sum(randperm(N,n)<=K);
    

    has a hypergeometric distribution with parameters N,K,n.


    If you really need to use a uniform random number generator (rand function):

    [~, x] = sort(rand(1,N));
    x = x(1:n); % this gives the same result as randperm(N,n)
    k = sum(x<=K);