Generate pair of random numbers with respect to a sum constraint?
3 views (last 30 days)
Show older comments
Michael Ziedalski
on 21 Feb 2018
Commented: César
on 26 Mar 2019
Hello, all. So I am still gaining experience with Matlab and am currently trying to generate sequences of two numbers, x and y, such that their sum is <= 1. The naive way I immediately thought to do this would be by 1) generating x within the range [0, .5], and 2) keep generating y until it is < x, which would guarantee my condition, but introduce some significant statistical bias.
Is there some standard, statistically robust way to do this, guys? I would be very grateful for any of your input on this matter.
0 Comments
Accepted Answer
Roger Stafford
on 22 Feb 2018
Edited: Roger Stafford
on 22 Feb 2018
(Corrected) Assuming you restrict x and y to non-negative values, the set of x and y values for which x+y<=1 would be a triangular area in the xy plane. You can obtain an area-wise uniform distribution of x and y with the following.
For generating a single pair:
x = 1-sqrt(rand);
y = (1-x)*rand;
To get row vectors with n elements each:
x = 1-sqrt(rand(1,n));
y = (1-x).*rand(1,n);
0 Comments
More Answers (1)
Jeff Miller
on 21 Feb 2018
It isn't entirely clear what joint distribution you want for (x,y), but here is one possibility:
x = rand; % uniform 0 to 1
y = (1-x)*rand; % uniform 0 to 1-x
4 Comments
César
on 26 Mar 2019
Dear @Roger Stafford,
Could you please give also some explanations of how do y = (1-x).*rand(N,1) give the correcrt distribution please?
See Also
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!