Thursday, April 28, 2011

Matlab Simulation General Discrete Distribution

1. randp - probably the best
http://www.mathworks.com/matlabcentral/fileexchange/8891
  RANDP - pick random values with relative probability
 
      R = RANDP(PROB,..) returns integers in the range from 1 to
      NUMEL(PROB) with a relative probability, so that the value X is
      present approximately (PROB(X)./sum(PROB)) times in the matrix R.

2. there is this on the FEX - easier to use not as efficient
actually I often use this one for ease.
http://www.mathworks.com/matlabcentral/fileexchange/21912-sampling-from-a-discrete-distribution

sampling from a discrete distribution

The use of this function is easy:
x = discretesample(p, n).
You just input the probability mass, and tell the function how many sample you want to sample, then it returns the samples in form of a 1 x n vector.

3. what about this entry.
[~,x] = histc(rand(1,n),[0;cumsum(p(:))/sum(p)]);