Prashant Kumar answered . 2024-12-21 03:20:21
You appear to want to generate a pair of random numbers such that they are uniformly distributed, live in the interval [0,1], AND they have a sum that is less than 1. (See below, because you actually appear to have a third constraint.)
The clue is to think about what this means about where the numbers must live. They must live in the triangle in the (x,y) plane with vertices {[0,0], [0,1], [1,0]}. Thus...
fill([0 0 1],[0 1 0],'r')
axis equal
So ANY point inside that triangle corresponds to an (x,y) pair of numbers that satisfies your requirement. A uniform sampling means that any location in that triangle is equally likely to arise.
The simplest way to do so is by a simple rejection scheme. (There are other ways to generate this sampling, but be careful. because there are many ways to do it wrongly too. If you wanted, I could show you how to do the sampling in other ways, but this answer is already long enough.) Just generate more points in the unit square than you need. Then discard any point that lies above the diagonal. Those that remain satisfy your goal. These rejection methods are often a good choice, because they result in a uniform sampling. And here we will reject roughly 50% of the points generated. So if you want 1000 points, then generate a little more than twice as many as you need.
However, as I re-read your question, I see that you really have a third variable.
You really want to generate THREE variables, call them {a,b,c}, such that a+b+c == 1.
The goal is not just that you want a sampling of a and b, such that (a+b)<1, but also that (1-a-b) has the same distribution as the others, and is also uniform.
The solution is then simple.
Generate THREE variables that lie in the unit cube, such that their sum is 1.
abc = randfixedsum(3,10,1,0,1);
sum(abc,1)
ans =
1 1 1 1 1 1 1 1 1 1
a = abc(1,:);
b = abc(2,:);
It will always be true that a+b<=1. The variable c=1-a-b will have the same distribution as a and b. And the three variables together will be uniformly distributed.
abc = randfixedsum(3,10000,1,0,1);
plot3(abc(1,:),abc(2,:),abc(3,:),'.')
grid on
box on
axis equal
The uniform distribution I refer to is the one that matters. See that the three variables {a,b,c} live in a triangular region in a plane in the three dimensional space, and that region is uniformly populated.
Be carful though, because don't expect that a itself will be uniformly distributed. That cannot be, because of your other requirements. In fact, each of the variables a, b, and c=(1-a+b) will follow a triangular distribution.
abc = randfixedsum(3,10000000,1,0,1);
hist(abc(1,:),1000)
So this is a histogram of a alone.
But now, let us return to the rejection scheme I suggested initially. Does it truly fail?
ab = rand(1e6,2); ab(sum(ab,2) > 1,:) = []; size(ab) ans = 500444 2
So we rejected roughly 50% of the samples, as expected
subplot(3,1,1)
hist(ab(:,1),1000)
subplot(3,1,2)
hist(ab(:,2),1000)
subplot(3,1,3)
hist(1-sum(ab,2),1000)
They all follow the same marginal triangular distribution, as we would want to see.
All three variables have the same mean, and standard deviations.
mean([ab,1-sum(ab,2)],1)
ans =
0.33328 0.3334 0.33332
std([ab,1-sum(ab,2)])
ans =
0.23575 0.23549 0.23556
So in reality, both randfixedsum and the simple rejection scheme work fine here.
The problem is you need to understand that the marginal distributions of a multidimensional random variable, when drawn under constraints, need not have the same distribution as a univariate random variable, drawn with no constraints at all.
So your problem was one of your expectations not meeting reality. In the case of random variables drawn under constraints, that is a common problem.
Not satisfied with the answer ?? ASK NOW