Asked by Clarisha Nijman
on 9 Oct 2018

Hallo,

Given a probability mass function defined as P(X=3)=0.2, P(X=7)=0.3 and P(X=10)=0.5, I want to generate randomly 30 numbers (values for X) with this probability mass function as base. But I really have no idea how and where to start.

Can somebody help me?

Thank you in advance

Answer by Torsten
on 9 Oct 2018

Edited by Torsten
on 9 Oct 2018

Accepted Answer

n = 30;

X = zeros(n,1);

x = rand(n,1);

X(x <= 0.5) = 10;

X(x > 0.5 & x <= 0.8) = 7;

X(x > 0.8) = 3;

Clarisha Nijman
on 9 Oct 2018

Thanks a lot Torsten,

I do not fully understand the code, But it works!!!!

kind regards

Torsten
on 10 Oct 2018

For an explanation, see

https://stats.stackexchange.com/questions/26858/how-to-generate-numbers-based-on-an-arbitrary-discrete-distribution

Clarisha Nijman
on 19 Oct 2018

tnx u!

Sign in to comment.

Answer by Bruno Luong
on 9 Oct 2018

A more generic method:

p = [0.2 0.3 0.5];

v = [3 7 10];

n = 10000;

c = cumsum([0,p(:).']);

c = c/c(end); % make sur the cumulative is 1

[~,i] = histc(rand(1,n),c);

r = v(i); % map to v values

Clarisha Nijman
on 19 Oct 2018

Sign in to comment.

Answer by Jeff Miller
on 20 Oct 2018

With Cupid you could write:

v = [3 7 10]; % the values p = [0.2 0.3 0.5]; % their probabilities rv = List(v,p); % a random variable with those values & probabilities n = 10000; randoms = rv.Random(n,1); % generate n random values of the random variable

Clarisha Nijman
on 20 Oct 2018

Hi Jeff, your suggestion give me three problems

1.Shouldn't the 3rd line be: rv=[v;p]? It gives an error.

2.The last line does not work and I have no idea how to replace it.

3.Where does the code take the cumulative distribution into account? Should simulation not be based on flipping a coin, choosing at random a prob and then determine based on the cumulative distribution what should be drawn?

Please let me know, thank you in advance

kind regards,

Jeff Miller
on 20 Oct 2018

Did you download the Cupid files (see the link in my answer)? These define the List class (which handles the cumulative distribution behind the scene). Do the other Cupid demos run correctly?

Well, Cupid may be overkill for your problem, but it does have a lot of flexibility.

Clarisha Nijman
on 20 Oct 2018

Ok, tnx Jeff, I'll check it!

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.