
Generate random number from custom PDF and CDF
27 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tsahar Segundp
am 17 Nov. 2020
Kommentiert: Bruno Luong
am 24 Jan. 2021
I basically have the following CDF and PDF respectivley:


Where the range is from 0 to a as mentioned in the CDF line. I would like to generate random numbers on matlab based on these equations. Has anyone done this before? Anyone has experience on this? I've tried looking on the forum but
Thanks in advance!!
(For those of you interested, it is the distribution of a 1-D Random Waypoint Mobility model proposed by Bettstetter et al.)
0 Kommentare
Akzeptierte Antwort
Bruno Luong
am 17 Nov. 2020
Bearbeitet: Bruno Luong
am 17 Nov. 2020
This is an exact generation, since the cdf is invertible by close formula:
(EDIT1: simplify the code)
a = 4;
n = 1e6;
q = 1/8-rand(1,n)/4;
d = sqrt(1/64-q.^2);
z = complex(q,d).^(1/3);
xrand = a*(1/2-real(z)+imag(z)*sqrt(3));
histogram(xrand,100,'Normalization','pdf');
hold on
x = linspace(0,a);
pdffun = @(x) -6/a^3*x.^2+6/a^2*x;
plot(x, pdffun(x), 'r')

4 Kommentare
Mohammed Aloqlah
am 24 Jan. 2021
could you please send me details of derivation as I trield to invert the CDF and can not acheive a closed expresion.
Bruno Luong
am 24 Jan. 2021
Sorry I didn't keep my note that I derive the formula since two moth ago. Too lazy to compute again.
The idea is you write down the cdf for a==1 with is thirs order polynomial.
To fintd the quantile function (invert the cdf) you make the variable change of y=(x-0.5).
Weitere Antworten (1)
Ameer Hamza
am 17 Nov. 2020
Bearbeitet: Ameer Hamza
am 17 Nov. 2020
MATLAB does not support arbitrary CDF functions, but you can approximate it using non-parametric distributions: https://www.mathworks.com/help/stats/nonparametric-and-empirical-probability-distributions.html. If you have statistics and ML toolbox, you can create a PiecewiseLinear distribution or an empirical distribution. The following shows an example
a = 4;
x = 0:0.01:a;
Fx = -2/a^3*x.^3+3/a^2*x.^2;
fx = -6/a^3*x.^2+6/a^2*x;
F_dist = makedist('PiecewiseLinear', 'x', x, 'Fx', Fx);
rand_nums = random(F_dist, 1, 1000000);
histogram(rand_nums, 'Normalization', 'pdf')
hold on
plot(x, fx, 'r', 'LineWidth', 2)
Comparison of generated and the theoretical PDF.

If you don't have the toolbox, you can use following FEX packages
0 Kommentare
Siehe auch
Kategorien
Mehr zu Random Number Generation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!