2

I have a support (supp_epsilon) and a probability mass function (pr_mass_epsilon) in Matlab, constructed as follows.

supp_epsilon=[0.005 0.01 0.015 0.02]; 

suppsize_epsilon=size(supp_epsilon,2);

pr_mass_epsilon=zeros(suppsize_epsilon,1);

alpha=1;
beta=4;

for j=1:suppsize_epsilon
    pr_mass_epsilon(j)=betacdf(supp_epsilon(j),alpha,beta)/sum(betacdf(supp_epsilon,alpha,beta));
end

Note that the components of pr_mass_epsilon sum up to 1. Now, I want to draw n random numbers from pr_mass_epsilon. How can I do this? I would like a code that works for any suppsize_epsilon.

In other words: I want to randomly draw elements from supp_epsilon, each element with a probability given by pr_mass_epsilon.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
TEX
  • 2,249
  • 20
  • 43

1 Answers1

5

Using the Statistics Toolbox

The randsample function can do that directly:

result = randsample(supp_epsilon, n, true, pr_mass_epsilon);

Without using toolboxes

Manual approach:

  1. Generate n samples of a uniform random variable in the interval (0,1).
  2. Compare each sample with the distribution function (cumulative sum of mass function).
  3. See in which interval of the distribution function each uniform sample lies.
  4. Index into the array of possible values

result = supp_epsilon(sum(rand(1,n)>cumsum(pr_mass_epsilon(:)), 1)+1);

For your example, with n=1e6 either of the two approaches gives a histogram similar to this:

histogram(result, 'normalization', 'probability')

enter image description here

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 1
    Both the answers here (with & without toolboxes) are superior to [this question](https://stackoverflow.com/q/33015000/8239061) and [this one](https://stackoverflow.com/q/13914066/8239061) both in simplicity and accessibility. – SecretAgentMan Oct 29 '19 at 16:13
  • @SecretAgentMan Good find. Do you think this should be marked as a duplicate of the second one you linked? The only difference is the population of values – Luis Mendo Oct 29 '19 at 16:51
  • I flagged both of those as duplicates of this one (this really should be the target) as they both deal with generating discrete random variables from a probability mass function. So no, my opinion is they should be dupes of this one. – SecretAgentMan Oct 29 '19 at 17:13
  • Thanks, I have posted a related question for vectors now https://stackoverflow.com/questions/58631801/vector-version-of-randsample-in-matlab – TEX Oct 30 '19 at 18:37