I have a discete probability distribution (histogram) and want to invoke rand to produce n random samples that follow the distribution specified by the histogram -how?

7 views (last 30 days)
I have a distribution specified by a 1d histogram, say [1 , 10; 2 , 50; 3 , 10; 4 , 30]
now I want to invoke rand to produce n discrete random samples (of value 1,2,3, or 4), such that the output distribution of the random samples matches closely the distribution specified by the histogram.
E.g. in this example for n=10 I would expect the output vector to have one 1, five 2s, one 3, and three 4s or a result that would be very close to it. How can I ensure that through rand()?

Answers (3)

Bjorn Gustavsson
Bjorn Gustavsson on 9 Feb 2017
0, always start by looking for a solution at the file exchange. 1, I did a quick search and perhaps these submissions solves your problem:
HTH

Roger Stafford
Roger Stafford on 9 Feb 2017
Edited: Roger Stafford on 9 Feb 2017
p = sum(bsxfun(@le,[0,0.1,0.6,0.7],rand(n,1)),2);
(Note: Since this is a probability distribution, you cannot always expect “ the output vector to have one 1, five 2s, one 3, and three 4s or a result that would be very close to it. ” There is always a finite probability that the result would be otherwise.)

Image Analyst
Image Analyst on 10 Feb 2017
You need "inverse transform sampling" : https://en.wikipedia.org/wiki/Inverse_transform_sampling Basically compute the CDF of the distribution, and use it to get samples.
I attach an m-file where I get samples drawn from a Rayleigh distribution.
You might also look at RANDRAW which does these distributions: Alpha, Anglit, Antilognormal, Arcsin, Bernoulli, Bessel, Beta, Binomial, Bradford, Burr, Cauchy, Chi, Chi-Square (Non-Central), Chi-Square (Central), Cobb-Douglas, Cosine, Double-Exponential, Erlang, Exponential, Extreme-Value, F (Central), F (Non-Central), Fisher-Tippett, Fisk, Frechet, Furry, Gamma, Generalized Inverse Gaussian, Generalized Hyperbolic, Geometric, Gompertz, Gumbel, Half-Cosine, Hyperbolic Secant, Hypergeometric, Inverse Gaussian, Laplace, Logistic, Lognormal, Lomax, Lorentz, Maxwell, Nakagami, Negative Binomial, Normal, Normal-Inverse-Gaussian (NIG), Pareto, Pareto2, Pascal, Planck, Poisson, Quadratic, Rademacher, Rayleigh, Rice, Semicircle, Skellam, Student's-t, Triangular, Truncated Normal, Tukey-Lambda, U-shape, Uniform (continuous), Von Mises, Wald, Weibull, Wigner Semicircle, Yule, Zeta, Zipf

Community Treasure Hunt

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

Start Hunting!