Generate random number from custom PDF and CDF

I basically have the following CDF and PDF respectivley:
CDF
PDF
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.)

 Accepted Answer

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 Comments

could you please breifly describe how you derived the following expressions:
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));
Thank you
I use the inverse transform sampling method and pencil and paper (mostly)/
could you please send me details of derivation as I trield to invert the CDF and can not acheive a closed expresion.
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).

Sign in to comment.

More Answers (1)

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

Community Treasure Hunt

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

Start Hunting!