Niels Heijenk asked . 2022-04-23

How to draw 2 random number simultaneously which are smaller than 1 together?

I am using a formula that has 3 different parameters: alpha, beta and 1-alpha-beta. These 3 parts have to have the same average so also the same probability to be high or low.
 
I want to draw the alpha and beta simultaneously but together they have to be smaller than 1. It should also be possible that for instance alpha = 0.1 and beta is 0.8.
I already tried to first draw alpha and afterwards draw beta with restriction 1-alpha-beta but this resulted into an alpha that is higher than beta in most cases.
 
I also tried to just draw 2 random numbers and make a if-loop with the restriction that both should be smaller than 1 but also this resulted into not evenly distributed results since the third part, 1-alpha-beta, was on average smaller than alpha and beta.

random , AI, Data Science, and Statistics , Statistics and Machine Learning Toolbox , Probabilit

Expert Answer

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

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.