Generate beta random number without statistics toolbox
Afficher commentaires plus anciens
Hello everyone,
I am currently working on a project involving the PDF of beta distribution, and generation of random numbers with according distribution. My script is supposed to be converted into an executable so I am limited in use of toolboxes and some commands.
Does someone know how to generate such numbers (a few millions of them, so manually is excluded) without the Statistics toolbox?
Many thanks!
Réponse acceptée
Plus de réponses (1)
dpb
le 18 Juin 2019
Given Ui are uniform random variates
Y1=U1/α1 and Y2=U1/β1,
such that Y1+Y2<=1,
then
X=Y1/(Y1+Y2)
follows the beta distribution with parameters α and β
5 commentaires
Rémi BERNARD
le 18 Juin 2019
dpb
le 18 Juin 2019
I no longer recall where I first came across this, either, John.
John D'Errico
le 18 Juin 2019
Modifié(e) : John D'Errico
le 18 Juin 2019
ARGH. For some reason, my lengthy explanation of where it fails got deleted. Somehow, I got logged off in the middle of writing it, and then my comment just disappeared.
But there is a clear error in your claim.
Y1=U1/α1 and Y2=U1/β1,
such that Y1+Y2<=1,
You use U1 for BOTH Y1 and Y2. That would clearly resut in Y1/(Y1+Y2) a constant value, independent of U1. So you must have intended to write:
Y1=U1/α1 and Y2=U2/β1,
such that Y1+Y2<=1,
No problem there. But you don't tell us the required bounds on U1 and U2. Are they [0,1] both? I'll assume that at first.
First, I note that when alpha=beta=1, it seems true. A simple test would confirm that to be reasonable.
X = rand(1e6,1);
Y = rand(1e6,1);
k = X + Y <= 1;
Z = X(k)./(X(k) + Y(k));
histogram(Z,100,'norm','pdf')

Ok, but then what happens if alpha=beta, when they are both greater than 2?
Z = (U1/alpha) / (U1/alpha + U2/beta)
Now assume that alpha = beta. Then the ratio reduces to
Z = U1 / U1 + U2)
This tends to imply that the beta distribution does not change shape when alpha==beta.
fplot(@(x) betapdf(x,3,3),[0,1])
>> hold on
>> fplot(@(x) betapdf(x,2,2),[0,1])
>> fplot(@(x) betapdf(x,1,1),[0,1])
>> fplot(@(x) betapdf(x,1/2,1/2),[0,1])
>> fplot(@(x) betapdf(x,10,10),[0,1])

These are all beta distributions with alpha==beta. Clearly they are not the same. And as long as both parameters are always greater than 2 and eual, then the resulting ditribution will seem to vary, but still be symmetric. And the sum Y1+Y2 will ALWAYS be less than 1.
So the idea that U1 and U2 live in the unit square [0,1]X[0,1] must have been wrong.
Instead, perhaps the idea is that U1 and U2 live in a triangular region in the plane, and are uniform on that triangle. So might we have U1 and U2 live in the triangle bounded by vertices {[0,0], [alpha,0], [0,beta]} ?
So if U1 and U2 are uniformly distributed in that planar region, then it would be true that
U1/alpha + U2/beta <= 1
will always hold true. However, now if alpha = beta, then the ratio reduces to one that is NOT a function of alpha or beta. The point is, then we would have a case where the resulting distribution will not change as we vary alpha and beta.
Finally, I think where you got that result comes from something that DOES look like what you claim.
In the wiki page for the beta distribution,
I find the claim that when X and Y are gamma distributed, such that
X ~ gamma(alpha,theta)
Y ~ gamma(beta,theta)
Then the ratio
Z = X/(X+Y)
follows a beta distribution, beta(alpha,beta).
Let me test this for a simple example. Here, alpha = 2, beta = 3.
X = gamrnd(2,1,1,1e6);
Y = gamrnd(3,1,1,1e6);
Z = X./(X+Y);
histogram(Z,100,'norm','pdf');
hold on
H = fplot(@(x) betapdf(x,2,3),[0,1]);
H.Color = 'r';

As you see, the histogram closely follows the predicted beta distribution. I might conjecture that this is what you remember learning in the past.
Rémi BERNARD
le 19 Juin 2019
John D'Errico
le 19 Juin 2019
As I said, what dpb told you to do is not technically correct. In fact, it does not generate the desired distribution, as I proved that it cannot. But you will need to make that decision for yourself.
Catégories
En savoir plus sur Multivariate t Distribution dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
