Borrar filtros
Borrar filtros

How to generate numbers from probability mass function?

28 visualizaciones (últimos 30 días)
Clarisha Nijman
Clarisha Nijman el 9 de Oct. de 2018
Respondida: PARTHEEBAN R el 22 de Mayo de 2021
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

Respuesta aceptada

Torsten
Torsten el 9 de Oct. de 2018
Editada: Torsten el 9 de Oct. de 2018
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;
  3 comentarios
Torsten
Torsten el 10 de Oct. de 2018
For an explanation, see
https://stats.stackexchange.com/questions/26858/how-to-generate-numbers-based-on-an-arbitrary-discrete-distribution

Iniciar sesión para comentar.

Más respuestas (3)

Bruno Luong
Bruno Luong el 9 de Oct. de 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
  1 comentario
Clarisha Nijman
Clarisha Nijman el 19 de Oct. de 2018
This answer works for me the best. I need this to do random column sampling (sampling some columns of a very big matrix A)

Iniciar sesión para comentar.


Jeff Miller
Jeff Miller el 20 de Oct. de 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
  3 comentarios
Jeff Miller
Jeff Miller el 20 de Oct. de 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
Clarisha Nijman el 20 de Oct. de 2018
Ok, tnx Jeff, I'll check it!

Iniciar sesión para comentar.


PARTHEEBAN R
PARTHEEBAN R el 22 de Mayo de 2021
A random variable X has cdf F(x) = { 0 , if x < − 1 a(1 + x ) , if − 1 < < 1 1 , if x ≥ 1 . Find (1) the value of a, (2) P(X > 1/4 ) and P ( − 0 . 5 ≤ X ≤ 0 ) .

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by