http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290
MATLAB Central Newsreader  how to generate random variable with constraint?
Feed for thread: how to generate random variable with constraint?
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 20 Jul 2010 01:53:03 +0000
how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#763896
jay
I need to generate 5 random variables between [0,1], let's say a, b, c, d, e. The constraint is that a<b<c<d<e. How to make this happen? Please advise. thanks

Tue, 20 Jul 2010 02:05:06 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#763899
someone
"jay " <ssjzdl@gmail.com> wrote in message <i22vhv$frn$1@fred.mathworks.com>...<br>
> I need to generate 5 random variables between [0,1], let's say a, b, c, d, e. The constraint is that a<b<c<d<e. How to make this happen? Please advise. thanks<br>
<br>
doc rand<br>
doc sort<br>
<br>
It seems to me that the only way 5 random numbers wouldn't <br>
satisfy that constraint is if 2 or more of them are equal. <br>
A very unlikely event.

Tue, 20 Jul 2010 04:00:45 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#763915
Walter Roberson
someone wrote:<br>
> "jay " <ssjzdl@gmail.com> wrote in message <br>
> <i22vhv$frn$1@fred.mathworks.com>...<br>
>> I need to generate 5 random variables between [0,1], let's say a, b, <br>
>> c, d, e. The constraint is that a<b<c<d<e. How to make this happen? <br>
>> Please advise. thanks<br>
> <br>
> doc rand<br>
> doc sort<br>
> <br>
> It seems to me that the only way 5 random numbers wouldn't satisfy that <br>
> constraint is if 2 or more of them are equal. A very unlikely event.<br>
<br>
Roger Stafford has discussed some limitations on generating random <br>
variables with constraints  if it is done incorrectly, the values tend <br>
to cluster towards the middle instead of uniformly distributed. I think <br>
sorting was okay, but I'd have to find the previous threads to be sure.

Tue, 20 Jul 2010 07:24:04 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#763938
Roger Stafford
Walter Roberson <roberson@hushmail.com> wrote in message <Ob91o.39684$YX3.15357@newsfe18.iad>...<br>
> Roger Stafford has discussed some limitations on generating random <br>
> variables with constraints  if it is done incorrectly, the values tend <br>
> to cluster towards the middle instead of uniformly distributed. I think <br>
> sorting was okay, but I'd have to find the previous threads to be sure.<br>
         <br>
I believe that "someone"'s indicated sort solution is perfectly valid, Walter. I'll admit that I paused a moment before coming to that conclusion, having the problem of random variables with a fixed sum in mind. However, I am convinced that both the solution to that problem and the sort method in this current problem do abide by the same basic principle.<br>
<br>
That principle is that when generating a combination of random variables with some constraint to be placed on them as they are generated, it should be done as though the same random variables with the same statistical relationships were to be performed without constraints except that combinations not satisfying the constraint are always rejected. A simple enough principle.<br>
<br>
In the current problem, doing a sort(rand(5,1)) and then dealing them out in ascending order to a, b, c, d, and e is the perfect equivalent of (though more efficient than) rejecting all values of rand(5,1) which are not already in sorted order. That is because, if the five variables of rand(5,1) were separately rearranged in sorted order in each of the 5! = 120 simplexes of the fivedimensional [0,1]^5 cube and reassigned to a, b, c, d, and e, these would each then have identical statistical distributions throughout that simplex as far as these latter variables are concerned. The 119 that are rejected, if rearranged, are statistically equivalent to the one that is accepted.<br>
<br>
I won't attempt to explain in detail what is involved in the fixed sum random variable generation except to say that one first imagines a small tolerance within which the sum is allowed to vary. One can envision an distribution within these tight limits which would simulate the rejection procedure without doing any rejecting. Finally if this tolerance is allowed to approach zero, the required distribution can be shown to also approach a limit, and it is this limit that can be used in generating such random variables. In that sense this fixed sum generation also adheres to the above principle.<br>
<br>
Roger Stafford

Tue, 20 Jul 2010 11:17:20 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#763973
John D'Errico
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i23iuk$lng$1@fred.mathworks.com>...<br>
> Walter Roberson <roberson@hushmail.com> wrote in message <Ob91o.39684$YX3.15357@newsfe18.iad>...<br>
> > Roger Stafford has discussed some limitations on generating random <br>
> > variables with constraints  if it is done incorrectly, the values tend <br>
> > to cluster towards the middle instead of uniformly distributed. I think <br>
> > sorting was okay, but I'd have to find the previous threads to be sure.<br>
>          <br>
> I believe that "someone"'s indicated sort solution is perfectly valid, Walter. I'll admit that I paused a moment before coming to that conclusion, having the problem of random variables with a fixed sum in mind.<br>
<br>
I'd agree with Roger here, although I too had to think<br>
about it to be confident. The constraint here is not a<br>
difficult one to satisfy, and the sort is an adequate<br>
solution.<br>
<br>
You can view this problem differently from finding a<br>
list of 5 values that are sorted in increasing order.<br>
Instead, you can view it as finding a SINGLE point,<br>
the coordinates of which represent a point that lives<br>
in a specific 5dimensional simplex. In the end though,<br>
the sort is valid.<br>
<br>
John

Tue, 20 Jul 2010 15:20:08 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764062
Matt J
"jay " <ssjzdl@gmail.com> wrote in message <i22vhv$frn$1@fred.mathworks.com>...<br>
> I need to generate 5 random variables between [0,1], let's say a, b, c, d, e. The constraint is that a<b<c<d<e. How to make this happen? Please advise. thanks<br>
==========<br>
<br>
A=cumsum(rand(1,5))/5;

Tue, 20 Jul 2010 15:23:20 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764064
Walter Roberson
Matt J wrote:<br>
> "jay " <ssjzdl@gmail.com> wrote in message <br>
> <i22vhv$frn$1@fred.mathworks.com>...<br>
>> I need to generate 5 random variables between [0,1], let's say a, b, <br>
>> c, d, e. The constraint is that a<b<c<d<e. How to make this happen? <br>
>> Please advise. thanks<br>
> ==========<br>
> <br>
> A=cumsum(rand(1,5))/5;<br>
<br>
I'm relatively sure that Roger showed in an earlier thread that that <br>
approach produces biased results.

Tue, 20 Jul 2010 15:35:19 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764069
Matt J
Walter Roberson <roberson@hushmail.com> wrote in message <Jbj1o.16614$Bh2.16201@newsfe04.iad>...<br>
<br>
> > <br>
> > A=cumsum(rand(1,5))/5;<br>
> <br>
> I'm relatively sure that Roger showed in an earlier thread that that <br>
> approach produces biased results.<br>
================<br>
<br>
Did the OP require that there not be bias?

Tue, 20 Jul 2010 15:58:07 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764085
Matt J
Walter Roberson <roberson@hushmail.com> wrote in message <Jbj1o.16614$Bh2.16201@newsfe04.iad>...<br>
<br>
> > <br>
> > A=cumsum(rand(1,5))/5;<br>
> <br>
> I'm relatively sure that Roger showed in an earlier thread that that <br>
> approach produces biased results.<br>
=======<br>
<br>
Another possibility. Still biased?<br>
<br>
A=cumsum(rand(1,6));<br>
A=A/sum(A);<br>
A(end)=[];

Tue, 20 Jul 2010 16:00:55 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764087
Walter Roberson
Matt J wrote:<br>
> Walter Roberson <roberson@hushmail.com> wrote in message <br>
> <Jbj1o.16614$Bh2.16201@newsfe04.iad>...<br>
> <br>
>> > > A=cumsum(rand(1,5))/5;<br>
>><br>
>> I'm relatively sure that Roger showed in an earlier thread that that <br>
>> approach produces biased results.<br>
> ================<br>
> <br>
> Did the OP require that there not be bias?<br>
<br>
You are correct, the OP placed no such restriction in the question, <br>
including not requiring that the random numbers be drawn from a uniform <br>
random distribution.<br>
<br>
In terms of what the OP demonstrably asked for, the following would also <br>
be valid:<br>
<br>
((0:4) + rand(1,5)) ./ 5<br>
<br>
There was, however, some ambiguity in the OP's phrasing.

Tue, 20 Jul 2010 16:17:04 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764095
Matt J
Walter Roberson <roberson@hushmail.com> wrote in message <YKj1o.92592$Lj2.82402@newsfe05.iad>...<br>
<br>
> You are correct, the OP placed no such restriction in the question, <br>
> including not requiring that the random numbers be drawn from a uniform <br>
> random distribution.<br>
===========<br>
<br>
Assuming he was, though, I'm still wondering if the following would do it. For larger numbers of variables, it would be good to have a way of doing this without using <br>
sort().<br>
<br>
A=cumsum(rand(1,6)); A=A/sum(A); A(end)=[];

Tue, 20 Jul 2010 16:24:10 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764098
Walter Roberson
Matt J wrote:<br>
> Walter Roberson <roberson@hushmail.com> wrote in message <br>
> <YKj1o.92592$Lj2.82402@newsfe05.iad>...<br>
> <br>
>> You are correct, the OP placed no such restriction in the question, <br>
>> including not requiring that the random numbers be drawn from a <br>
>> uniform random distribution.<br>
> ===========<br>
> <br>
> Assuming he was, though, I'm still wondering if the following would do <br>
> it. For larger numbers of variables, it would be good to have a way of <br>
> doing this without using sort().<br>
> <br>
> A=cumsum(rand(1,6)); A=A/sum(A); A(end)=[];<br>
<br>
I would tend to doubt that that would work to generate welldistributed <br>
points on the simplex. The fundamental problem with using the sum <br>
approach is that even though any one A(K) value is independent, as you <br>
add them together, the sum approaches the normal distribution, as per <br>
the Central Limit Theorem, and so the generated A vectors would tend to <br>
cluster towards the centroid of the simplex. I don't see at the moment <br>
how generating an extra value and discarding would resolve that problem.

Tue, 20 Jul 2010 16:37:04 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764099
someone
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i24i60$gfp$1@fred.mathworks.com>...<br>
> Walter Roberson <roberson@hushmail.com> wrote in message <YKj1o.92592$Lj2.82402@newsfe05.iad>...<br>
> <br>
> > You are correct, the OP placed no such restriction in the question, <br>
> > including not requiring that the random numbers be drawn from a uniform <br>
> > random distribution.<br>
> ===========<br>
> <br>
> Assuming he was, though, I'm still wondering if the following would do it. For larger numbers of variables, it would be good to have a way of doing this without using <br>
> sort().<br>
> <br>
> A=cumsum(rand(1,6)); A=A/sum(A); A(end)=[]; <br>
<br>
Wow, I have to admit that I didn't put a lot of thought into my inital solution.<br>
I simply reasoned that the constraint that a<b<c<d<e was really no constraint at all.<br>
Using sort was (in my mind) just a way of "relabeling" the a, b c, d, & e variables.<br>
The only "gotcha" would be if rand returned an equality (whiched seemed like <br>
a pretty unlikely event with an "easy" fix). Did I miss something?

Tue, 20 Jul 2010 16:59:04 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764105
Matt J
Walter Roberson <roberson@hushmail.com> wrote in message <K4k1o.93283$Lj2.50698@newsfe05.iad>...<br>
<br>
> I would tend to doubt that that would work to generate welldistributed <br>
> points on the simplex. The fundamental problem with using the sum <br>
> approach is that even though any one A(K) value is independent, as you <br>
> add them together, the sum approaches the normal distribution, as per <br>
> the Central Limit Theorem, and so the generated A vectors would tend to <br>
> cluster towards the centroid of the simplex. I don't see at the moment <br>
> how generating an extra value and discarding would resolve that problem.<br>
==================<br>
<br>
I'm not really seeing that argument. We have A/sum(A).<br>
The central limit theorem says that A(end) > (randn+.5)*sqrt(N)<br>
The law of large numbers says that sum(A)> 0.5*N<br>
<br>
This means that the A(end)/sum(A)>0 as N>inf, as we would expect it to.

Tue, 20 Jul 2010 17:10:20 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764109
John D'Errico
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i24i60$gfp$1@fred.mathworks.com>...<br>
> Walter Roberson <roberson@hushmail.com> wrote in message <YKj1o.92592$Lj2.82402@newsfe05.iad>...<br>
> <br>
> > You are correct, the OP placed no such restriction in the question, <br>
> > including not requiring that the random numbers be drawn from a uniform <br>
> > random distribution.<br>
> ===========<br>
> <br>
> Assuming he was, though, I'm still wondering if the following would do it. For larger numbers of variables, it would be good to have a way of doing this without using <br>
> sort().<br>
> <br>
> A=cumsum(rand(1,6)); A=A/sum(A); A(end)=[]; <br>
<br>
This is indeed massively biased! To convince yourself<br>
that it does not produce a random sampling, or even<br>
the correct sampling of the required domain, try it in<br>
2 dimensions!<br>
<br>
n = 10000;<br>
A = cumsum(rand(n,3),2);<br>
A = bsxfun(@rdivide,A,sum(A,2));<br>
A(:,3) = [];<br>
plot(A(:,1),A(:,2),'.')<br>
<br>
The domain of interest here SHOULD be a triangle,<br>
but not the one shown. Instead, try this:<br>
<br>
B = sort(rand(n,2),2);<br>
plot(B(:,1),B(:,2),'.')<br>
<br>
I don't even see the sort as more complex, nor does<br>
MATLAB. Try this:<br>
<br>
n = 1000000;<br>
tic<br>
A = cumsum(rand(n,6),2);<br>
A = bsxfun(@rdivide,A,sum(A,2));<br>
A(:,6) = [];<br>
toc<br>
Elapsed time is 0.219178 seconds.<br>
<br>
tic<br>
B = sort(rand(n,5),2);<br>
toc<br>
Elapsed time is 0.163978 seconds.<br>
<br>
See that the sort took LESS time than the cumsum,<br>
and the sort is verifiably correct.<br>
<br>
John

Tue, 20 Jul 2010 17:53:04 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764123
Roger Stafford
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i24i60$gfp$1@fred.mathworks.com>...<br>
> Assuming he was, though, I'm still wondering if the following would do it. For larger numbers of variables, it would be good to have a way of doing this without using <br>
> sort().<br>
> <br>
> A=cumsum(rand(1,6)); A=A/sum(A); A(end)=[]; <br>
        <br>
Matt, I'm in agreement with John and Walter on this. Your two solutions are not good ones to give as an answer even if Jay did not give specific details as to the desired distribution.<br>
<br>
For example in your second method with five variables it is impossible for the fifth one to exceed 1/2 even though doing so would easily be compatible with Jay's constraint.<br>
<br>
However, just as important, the distribution within the domain which is used is exceedingly nonuniform. If you were to show Jay a plot for the two variable case, I am sure he would not be happy with it. The area used is actually less than half of that which would be allowed by the constraint, with everything having to lie below the line 3*a+2*b = 1 and the density drops off to zero at the [0,0] corner.<br>
<br>
Roger Stafford

Tue, 20 Jul 2010 18:06:04 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764130
Matt J
"John D'Errico" <woodchips@rochester.rr.com> wrote in message <i24l9s$auq$1@fred.mathworks.com>...<br>
<br>
> I don't even see the sort as more complex, nor does<br>
> MATLAB. Try this:<br>
===========<br>
<br>
That's only because you're working in 2 dimensions. In higher dimensions, complexity theory of sorting vs. summation gaurantees that sorting will do worse. Try this modification: <br>
<br>
<br>
n = 100;<br>
data=rand(n,60000);<br>
tic<br>
A = cumsum(data,2);<br>
A = bsxfun(@rdivide,A,sum(A,2));<br>
A(:,end) = [];<br>
toc<br>
%Elapsed time is 0.141225 seconds.<br>
<br>
<br>
data=rand(n,59999);<br>
tic<br>
B = sort(data,2);<br>
toc<br>
%Elapsed time is 0.493895 seconds.

Tue, 20 Jul 2010 18:23:20 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764137
Matt J
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i24nq0$oat$1@fred.mathworks.com>...<br>
<br>
>         <br>
> Matt, I'm in agreement with John and Walter on this. Your two solutions are not good ones to give as an answer even if Jay did not give specific details as to the desired distribution.<br>
> <br>
> For example in your second method with five variables it is impossible for the fifth one to exceed 1/2 even though doing so would easily be compatible with Jay's constraint.<br>
=========<br>
<br>
Roger You're right. I had a mistake. What I really meant to give was this:<br>
<br>
A=cumsum(rand(1,6)); A=A/A(end); A(end)=[];<br>
<br>
I reran John's 2D test on this and find that it covers the correct triangular area, though slightly less uniformly than the sorting method.<br>
<br>
Again, though, for me, this was all just an exercise in seeing if we could get something nearly as good using cheaper summations instead of sorting.

Tue, 20 Jul 2010 18:29:21 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764138
Roger Stafford
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i24oic$env$1@fred.mathworks.com>...<br>
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <i24l9s$auq$1@fred.mathworks.com>...<br>
> <br>
> > I don't even see the sort as more complex, nor does<br>
> > MATLAB. Try this:<br>
> ===========<br>
> <br>
> That's only because you're working in 2 dimensions. In higher dimensions, complexity theory of sorting vs. summation gaurantees that sorting will do worse. Try this modification: <br>
> <br>
> <br>
> n = 100;<br>
> data=rand(n,60000);<br>
> tic<br>
> A = cumsum(data,2);<br>
> A = bsxfun(@rdivide,A,sum(A,2));<br>
> A(:,end) = [];<br>
> toc<br>
> %Elapsed time is 0.141225 seconds.<br>
> <br>
> <br>
> data=rand(n,59999);<br>
> tic<br>
> B = sort(data,2);<br>
> toc<br>
> %Elapsed time is 0.493895 seconds.<br>
        <br>
Matt, you shouldn't give people solutions that are distinctly inferior just because their code would run faster. That is putting too much emphasis on speed. Quality counts too. Both John and I have already shown you how far off the mark the plot of the twovariable case would be for your solution. It doesn't get any better as the number of variables increases where the speed difference would become significant.<br>
<br>
Roger Stafford

Tue, 20 Jul 2010 18:40:34 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764145
John D'Errico
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i24oic$env$1@fred.mathworks.com>...<br>
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <i24l9s$auq$1@fred.mathworks.com>...<br>
> <br>
> > I don't even see the sort as more complex, nor does<br>
> > MATLAB. Try this:<br>
> ===========<br>
> <br>
> That's only because you're working in 2 dimensions. In higher dimensions, complexity theory of sorting vs. summation gaurantees that sorting will do worse. Try this modification: <br>
> <br>
<br>
No, if you bothered to look at my test, it was done<br>
in 5 dimensions. Yes, if you solve a very different<br>
problem from that which was asked about, the time<br>
will be different.<br>
<br>
John

Tue, 20 Jul 2010 18:50:20 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764151
Matt J
"John D'Errico" <woodchips@rochester.rr.com> wrote in message <i24qj2$pi6$1@fred.mathworks.com>...<br>
<br>
> <br>
> No, if you bothered to look at my test, it was done<br>
> in 5 dimensions. Yes, if you solve a very different<br>
> problem from that which was asked about, the time<br>
> will be different.<br>
========<br>
<br>
Then we have no disagreement. <br>
<br>
In case my earlier posts weren't clear, I'm no longer all that concerned with the specific case raised by the OP (no offense, Jay). I'm more interested in how we might do this more cheaply if we wanted to do it in higher dimensions (and why wouldn't we?).<br>
<br>
Since the OP already has been given at least one solution that will work, I think it's not unfair to allow the thread to stray to related tangents...

Tue, 20 Jul 2010 19:06:19 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764157
Roger Stafford
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i24pio$k5f$1@fred.mathworks.com>...<br>
> Roger You're right. I had a mistake. What I really meant to give was this:<br>
> <br>
> A=cumsum(rand(1,6)); A=A/A(end); A(end)=[];<br>
> <br>
> I reran John's 2D test on this and find that it covers the correct triangular area, though slightly less uniformly than the sorting method.<br>
> <br>
> Again, though, for me, this was all just an exercise in seeing if we could get something nearly as good using cheaper summations instead of sorting.<br>
          <br>
Matt I checked out the twodimensional plot for your revised code:<br>
<br>
A=cumsum(rand(1,6)); A=A/A(end); A(end)=[];<br>
<br>
It does cover the correct triangle. However it is grossly inaccurate to say that it is only "slightly less uniform than the sorting method." The probability area density actually drops down to zero at each corner of that triangle, whereas in a good solution it ought to be a uniform plateau throughout the entire triangle's area. This disparity would continue to worsen as the number of variables increases, though unfortunately it is difficult to illustrate this fact with plots.<br>
<br>
Roger Stafford

Tue, 20 Jul 2010 19:09:06 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764159
Matt J
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i24pu1$d60$1@fred.mathworks.com>...<br>
<br>
> Matt, you shouldn't give people solutions that are distinctly inferior just because their code would run faster. <br>
================<br>
<br>
Roger, see my revision in Message #18. It seems to be a better contender. <br>
<br>
In any case, yes, I would hate for Jay to walk away without knowing the limitations of the solutions we propose, but I'm still feeling my through it myself. <br>
<br>
Even if I haven't figured out exactly how, it seems distinctly intuitive that we should be able to derive this with cumsum because the jumps between a<b<c<d,etc... <br>
form a positivevalued Markov process <br>
(like cumsum(rand(1,N)). So you would think it possible to derive the solution from this.<br>
<br>
Should I be brainstorming out loud on the NG? Debatable, but I've seen lots of people here do it...

Tue, 20 Jul 2010 19:40:22 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764171
Matt J
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i24s3b$58o$1@fred.mathworks.com>...<br>
<br>
> It does cover the correct triangle. However it is grossly inaccurate to say that it is only "slightly less uniform than the sorting method." <br>
==============<br>
<br>
I was going by an eyeball assessment of John's plots. Those plots don't give a full picture of the distribution, but only saltandpepper sampling patterns (which were slightly more salty than peppery for the cumsum method).<br>
<br>
<br>
The probability area density actually drops down to zero at each corner of that triangle, whereas in a good solution it ought to be a uniform plateau throughout the entire triangle's area. This disparity would continue to worsen as the number of variables increases, though unfortunately it is difficult to illustrate this fact with plots.<br>
===========<br>
<br>
So you're saying it's more like a Gaussian distribution over the triangle? That's strange. However, surely this is a much more reasonable contender than my earlier version, considering (a) that Jay never said whether he was interested in a uniform or a Gaussian distribution and (b) that it's more efficient to generate.<br>
<br>

Tue, 20 Jul 2010 23:49:05 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764238
Roger Stafford
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i24u36$g1a$1@fred.mathworks.com>...<br>
> ............<br>
> So you're saying it's more like a Gaussian distribution over the triangle? That's strange. However, surely this is a much more reasonable contender than my earlier version, considering (a) that Jay never said whether he was interested in a uniform or a Gaussian distribution and (b) that it's more efficient to generate.<br>
        <br>
Yes I agree your revised version is a more reasonable contender than the previous one, Matt. I used your earlier code blindly but should have realized that you surely meant something else.<br>
<br>
This later version is rather similar in a sense to the solutions that have been given often in this group for the problem of n random variables with a predetermined sum which I mentioned earlier, where n rand's are taken and then they are each divided by their sum times the desired sum value. Both techniques tend to concentrate values in the center regions at the expense of the outer regions  that is disproportionately to the ndimensonal volumes of those regions. And yes for large n they begin to approach gaussian distributions (the central limit theorem at work again.)<br>
<br>
For that reason such methods don't satisfy the principle I mentioned earlier of generating the variables in such a manner that they are equivalent, statistically speaking, to a process that generates the variables without constraints but then rejects all that don't satisfy the constraints.<br>
<br>
Roger Stafford

Wed, 21 Jul 2010 10:02:10 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764313
Matt J
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i25clh$jhi$1@fred.mathworks.com>...<br>
<br>
> This later version is rather similar in a sense to the solutions that have been given often in this group for the problem of n random variables with a predetermined sum which I mentioned earlier, where n rand's are taken and then they are each divided by their sum times the desired sum value. Both techniques tend to concentrate values in the center regions at the expense of the outer regions  that is disproportionately to the ndimensonal volumes of those regions. And yes for large n they begin to approach gaussian distributions (the central limit theorem at work again.)<br>
=======<br>
<br>
As I was saying to Walter, I really don't follow that reasoning. If we're now talking about<br>
<br>
Z=rand(1,N); <br>
X=Z/sum(Z);<br>
<br>
it is quite clear that each X(i)>0 as N>inf. Also, X does not result from the summation of i.i.d random vectors for any N, so I don't see how the Central Limit Theorem would imply that the elements X(i) tend to be jointly Gaussian as N>inf.

Wed, 21 Jul 2010 19:47:04 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764501
Roger Stafford
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i26gj2$s3o$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i25clh$jhi$1@fred.mathworks.com>...<br>
> <br>
> > This later version is rather similar in a sense to the solutions that have been given often in this group for the problem of n random variables with a predetermined sum which I mentioned earlier, where n rand's are taken and then they are each divided by their sum times the desired sum value. Both techniques tend to concentrate values in the center regions at the expense of the outer regions  that is disproportionately to the ndimensonal volumes of those regions. And yes for large n they begin to approach gaussian distributions (the central limit theorem at work again.)<br>
> =======<br>
> <br>
> As I was saying to Walter, I really don't follow that reasoning. If we're now talking about<br>
> <br>
> Z=rand(1,N); <br>
> X=Z/sum(Z);<br>
> <br>
> it is quite clear that each X(i)>0 as N>inf. Also, X does not result from the summation of i.i.d random vectors for any N, so I don't see how the Central Limit Theorem would imply that the elements X(i) tend to be jointly Gaussian as N>inf.<br>
          <br>
As I understand it, Matt, the argument would go something like this. In your notation, for each individual Z(i), when it is divided by sum(Z) for N > 1, that affects the distribution of the resulting X(i). It no longer possesses its original uniform distribution on [0,1] (or whatever distribution it might have had.) Theoretically it can still range from 0 to 1 but statistically it is crowded more and more closely in towards 0 for increasing N. Its theoretical mean and variance can be calculated as a function of N. If we were to translate and rescale it so as to have mean zero and variance one, its distribution would begin to resemble more and more the bellshaped curve of a standard normal distribution as N increases  and yes stretching out towards infinity in both plus and minus directions. And this is actually independent of whatever distribution the original Z's possessed, <br>
assuming they were independent. So apparently says the mysterious central limit theorem in one of its numerous manifestations (please don't ask me which one.) You will note that this is not in contradiction with the fact that the mean value of X(i) must itself approach zero as N approaches infinity. It just says that if you continue to shift and rescale it so as to match standard normal in mean and variance for each N, then the rest of the distribution curve will also approach normality. The CLT is a remarkable theorem. <br>
<br>
Roger Stafford

Thu, 22 Jul 2010 10:44:04 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764633
Paulo
"jay " <ssjzdl@gmail.com> wrote in message <i22vhv$frn$1@fred.mathworks.com>...<br>
> I need to generate 5 random variables between [0,1], let's say a, b, c, d, e. The constraint is that a<b<c<d<e. How to make this happen? Please advise. thanks<br>
<br>
<br>
while(1) %infinite loop<br>
a=rand();b=rand();c=rand();d=rand();e=rand(); %put random values on variables<br>
if((a<b) && (b<c) && (c<d) && (d<e)) , break ,end %test if constraint is true<br>
%if constraint is true than stop the loop<br>
end<br>
fprintf('a=%d b=%d c=%d d=%d',a,b,c,d); % just to show the values

Thu, 22 Jul 2010 13:27:07 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764677
John D'Errico
"Paulo " <paulojmdsilva@gmail.com> wrote in message <i297dj$bd7$1@fred.mathworks.com>...<br>
> "jay " <ssjzdl@gmail.com> wrote in message <i22vhv$frn$1@fred.mathworks.com>...<br>
> > I need to generate 5 random variables between [0,1], let's say a, b, c, d, e. The constraint is that a<b<c<d<e. How to make this happen? Please advise. thanks<br>
> <br>
> <br>
> while(1) %infinite loop<br>
> a=rand();b=rand();c=rand();d=rand();e=rand(); %put random values on variables<br>
> if((a<b) && (b<c) && (c<d) && (d<e)) , break ,end %test if constraint is true<br>
> %if constraint is true than stop the loop<br>
> end<br>
> fprintf('a=%d b=%d c=%d d=%d',a,b,c,d); % just to show the values<br>
<br>
This is a rejection method, quite an inefficient way to<br>
do the same thing as a sort on this problem. Since<br>
there are 5! = 24 ways to generate a set of 5 numbers,<br>
only ONE of which is sorted, this dumps its results into<br>
the bit bucket nearly 96% of the time.<br>
<br>
23/24<br>
ans =<br>
0.958333333333333<br>
<br>
It makes far more sense to generate 5 numbers (as a<br>
vector, in ONE operation) and then use sort on them.<br>
<br>
sort(rand(1,5))<br>
<br>
The statistics of this result are the same in the end, yet<br>
it wastes far less cpu time to do the operation.<br>
<br>
John

Thu, 22 Jul 2010 14:43:20 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764723
Paulo
"John D'Errico" <woodchips@rochester.rr.com> wrote in message <i29gvb$cs2$1@fred.mathworks.com>...<br>
> "Paulo " <paulojmdsilva@gmail.com> wrote in message <i297dj$bd7$1@fred.mathworks.com>...<br>
> > "jay " <ssjzdl@gmail.com> wrote in message <i22vhv$frn$1@fred.mathworks.com>...<br>
> > > I need to generate 5 random variables between [0,1], let's say a, b, c, d, e. The constraint is that a<b<c<d<e. How to make this happen? Please advise. thanks<br>
> > <br>
> > <br>
> > while(1) %infinite loop<br>
> > a=rand();b=rand();c=rand();d=rand();e=rand(); %put random values on variables<br>
> > if((a<b) && (b<c) && (c<d) && (d<e)) , break ,end %test if constraint is true<br>
> > %if constraint is true than stop the loop<br>
> > end<br>
> > fprintf('a=%d b=%d c=%d d=%d',a,b,c,d); % just to show the values<br>
> <br>
> This is a rejection method, quite an inefficient way to<br>
> do the same thing as a sort on this problem. Since<br>
> there are 5! = 24 ways to generate a set of 5 numbers,<br>
> only ONE of which is sorted, this dumps its results into<br>
> the bit bucket nearly 96% of the time.<br>
> <br>
> 23/24<br>
> ans =<br>
> 0.958333333333333<br>
> <br>
> It makes far more sense to generate 5 numbers (as a<br>
> vector, in ONE operation) and then use sort on them.<br>
> <br>
> sort(rand(1,5))<br>
> <br>
> The statistics of this result are the same in the end, yet<br>
> it wastes far less cpu time to do the operation.<br>
> <br>
> John<br>
<br>
That's a good point but unless he wants to create those variables many many times it doesn't matter much, also there's no problem with variables when they got the same value, my code is simple and doesn't require any fancy manipulations or functions.

Thu, 22 Jul 2010 16:26:04 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764762
someone
"Paulo " <paulojmdsilva@gmail.com> wrote in message <i29le8$2pa$1@fred.mathworks.com>...<br>
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <i29gvb$cs2$1@fred.mathworks.com>...<br>
> > "Paulo " <paulojmdsilva@gmail.com> wrote in message <i297dj$bd7$1@fred.mathworks.com>...<br>
> > > "jay " <ssjzdl@gmail.com> wrote in message <i22vhv$frn$1@fred.mathworks.com>...<br>
> > > > I need to generate 5 random variables between [0,1], let's say a, b, c, d, e. The constraint is that a<b<c<d<e. How to make this happen? Please advise. thanks<br>
> > > <br>
> > > <br>
> > > while(1) %infinite loop<br>
> > > a=rand();b=rand();c=rand();d=rand();e=rand(); %put random values on variables<br>
> > > if((a<b) && (b<c) && (c<d) && (d<e)) , break ,end %test if constraint is true<br>
> > > %if constraint is true than stop the loop<br>
> > > end<br>
> > > fprintf('a=%d b=%d c=%d d=%d',a,b,c,d); % just to show the values<br>
> > <br>
> > This is a rejection method, quite an inefficient way to<br>
> > do the same thing as a sort on this problem. Since<br>
> > there are 5! = 24 ways to generate a set of 5 numbers,<br>
> > only ONE of which is sorted, this dumps its results into<br>
> > the bit bucket nearly 96% of the time.<br>
> > <br>
> > 23/24<br>
> > ans =<br>
> > 0.958333333333333<br>
> > <br>
> > It makes far more sense to generate 5 numbers (as a<br>
> > vector, in ONE operation) and then use sort on them.<br>
> > <br>
> > sort(rand(1,5))<br>
> > <br>
> > The statistics of this result are the same in the end, yet<br>
> > it wastes far less cpu time to do the operation.<br>
> > <br>
> > John<br>
> <br>
> That's a good point but unless he wants to create those variables many many times it doesn't matter much,<br>
<br>
This is a little out of my expertise, but it COULD matter. I believe your code is an example of "indefinite postponement". It is possible you might go through your while loop an infinite number of times before you finally find a solution that breaks out of the loop. <br>
<br>
When John said (correct me if I'm wrong) "96% of the time" I believe that is an average number. There is no guarantee.<br>
<br>
> also there's no problem with variables when they got the same value, my code is simple and doesn't require any fancy manipulations or functions.<br>
<br>
Do you really think the while loop is simplier than the one liner solution?

Thu, 22 Jul 2010 18:43:22 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#764808
Walter Roberson
someone wrote:<br>
> "Paulo " <paulojmdsilva@gmail.com> wrote in message <br>
> <i29le8$2pa$1@fred.mathworks.com>...<br>
<br>
>> > > > > > > while(1) %infinite loop<br>
>> > > a=rand();b=rand();c=rand();d=rand();e=rand(); %put random values <br>
>> on variables<br>
>> > > if((a<b) && (b<c) && (c<d) && (d<e)) , break ,end %test if <br>
>> constraint is true<br>
>> > > %if constraint is true than stop the loop<br>
>> > > end<br>
>> > > fprintf('a=%d b=%d c=%d d=%d',a,b,c,d); % just to show the values<br>
<br>
> I believe <br>
> your code is an example of "indefinite postponement". It is possible <br>
> you might go through your while loop an infinite number of times before <br>
> you finally find a solution that breaks out of the loop.<br>
> When John said (correct me if I'm wrong) "96% of the time" I believe <br>
> that is an average number. There is no guarantee.<br>
<br>
This is a situation of a geometric distribution with probability of success p <br>
= 1/24 . The "expected value" of such a distribution is success on the 1/p'th <br>
trial (that is, the 24th trial in this case), but Yes, it does have an <br>
infinite tail. An infinite tail will occur for all rejection methods whose <br>
trials are independent and of finite nonunitary probability.<br>
<br>
The sort method would succeed in any situation in which no two random values <br>
were the same. The probability of identical random values within N trials <br>
depends substantially upon the details of the psuedorandom number generator. <br>
The old linear congruential generator could not produce duplicate numbers in <br>
less than 2^32 trials. I do not know how to analyze the subtractwithborrow <br>
generator. The Mersenne Twister generator has been analyzed to be <br>
statistically independent up to 633 dimensions (I think the number is); I <br>
calculate that the probability of failure for it would be<br>
99035202923775336283058995157/42535295785889145473997720539375861762<br>
which is about 2.3E9, leading to an average number of trials of 1 + 2.3E9

Wed, 27 Mar 2013 21:09:05 +0000
Re: how to generate random variable with constraint?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287290#901001
Siddhartha
I have a similar question. <br>
<br>
I have five random variables. They are discrete and can take a 'low', 'base' or 'high' value between 15 and 35. The only condition is that their sum never exceeds 98. It is one thing to scale the sum to fit the given range, but how do I simulate the individual variables for this?