http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875
MATLAB Central Newsreader  Simulation of 5 Random Variables with Sum Constraint
Feed for thread: Simulation of 5 Random Variables with Sum Constraint
enus
©19942015 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

Wed, 27 Mar 2013 20:50:11 +0000
Simulation of 5 Random Variables with Sum Constraint
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875#900998
Siddhartha
Let's say I have 5 random variables that are discrete. Each one of them can possess a low, base and high value somewhere between 15 and 35. I know their sum can never exceed 98. What's the best way to simulate this? Is the rejection method good? The sudden drop in the pdf of the sum bothers me, not sure if that's right, but it maybe.<br>
<br>
<br>
n = 5;<br>
a(n,3) = 0;<br>
for i = 1:n<br>
a(i,1:3) = [100/(1.5*n)+(1)^fix(rand)*rand*(100/(2*n)) 100/(1.1*n)+(1)^fix(rand)*rand*(100/(2*n)) 100/(0.8*n)+(1)^fix(rand)*rand*(100/(2*n))];<br>
end<br>
<br>
<br>
d = lhsdesign(n,10000);<br>
<br>
e = 0;<br>
b(1,10000)=0;<br>
for i = 1:n<br>
b(d(i,:)<0.3) = a(i,1);<br>
b(d(i,:)>0.7) = a(i,3);<br>
b(b==0) = a(i,2);<br>
e = e + b;<br>
end<br>
<br>
f = e(e<98);<br>
[~,cole] = size(unique(e));<br>
[~,colf] = size(unique(f));<br>
<br>
subplot(2,2,1), hist(e,cole);<br>
subplot(2,2,2), hist(e(e<98),colf);<br>
subplot(2,2,3), ecdf(e);<br>
subplot(2,2,4), ecdf(e(e<98));

Wed, 27 Mar 2013 21:22:41 +0000
Re: Simulation of 5 Random Variables with Sum Constraint
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875#901002
Steven_Lord
<br>
<br>
"Siddhartha " <siddhsam@hotmail.com> wrote in message <br>
news:kivm23$ip1$1@newscl01ah.mathworks.com...<br>
> Let's say I have 5 random variables that are discrete. Each one of them <br>
> can possess a low, base and high value somewhere between 15 and 35.<br>
<br>
So each variable can take on one of three values, and the three values each <br>
variable can take can be different for each variable?<br>
<br>
> I know their sum can never exceed 98. What's the best way to simulate <br>
> this? Is the rejection method good? The sudden drop in the pdf of the sum <br>
> bothers me, not sure if that's right, but it maybe.<br>
<br>
If you have such a small number of different possible combinations, generate <br>
the complete list of possible combinations and use RANDI (with replacement) <br>
or RANDPERM (without replacement) to select from the list.<br>
<br>
*snip*<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Thu, 28 Mar 2013 01:13:16 +0000
Re: Simulation of 5 Random Variables with Sum Constraint
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875#901017
Siddhartha
Thank you! This is awesome.<br>
<br>
<br>
But I also foresee that this problem will extend for me to 20 variables.<br>
<br>
Also, we may eventually get enough information to model the random numbers as continuous. <br>
<br>
Is there a scalable way to do this?

Thu, 28 Mar 2013 14:50:24 +0000
Re: Simulation of 5 Random Variables with Sum Constraint
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875#901063
Steven_Lord
<br>
<br>
"Siddhartha " <siddhsam@hotmail.com> wrote in message <br>
news:kj05fc$1q1$1@newscl01ah.mathworks.com...<br>
> Thank you! This is awesome.<br>
><br>
><br>
> But I also foresee that this problem will extend for me to 20 variables.<br>
<br>
I would process the variables in decreasing order of maximum value, and stop <br>
processing when you know that none of the possible combinations of values <br>
for the remaining variables can satisfy your sum constraint. That should <br>
allow you to cut out significant chunks of the combination space.<br>
<br>
> Also, we may eventually get enough information to model the random numbers <br>
> as continuous.<br>
<br>
That's different; I believe there are some submissions on the File Exchange <br>
for this case. Take a look at RANDFIXEDSUM.<br>
<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/9700randomvectorswithfixedsum">http://www.mathworks.com/matlabcentral/fileexchange/9700randomvectorswithfixedsum</a><br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Thu, 28 Mar 2013 15:04:18 +0000
Re: Simulation of 5 Random Variables with Sum Constraint
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875#901066
Bruno Luong
"Steven_Lord" <slord@mathworks.com> wrote in message .<br>
> <br>
> That's different; I believe there are some submissions on the File Exchange <br>
> for this case. Take a look at RANDFIXEDSUM.<br>
> <br>
> <a href="http://www.mathworks.com/matlabcentral/fileexchange/9700randomvectorswithfixedsum">http://www.mathworks.com/matlabcentral/fileexchange/9700randomvectorswithfixedsum</a><br>
<br>
When adding the bound constraints, there is some work to be done before apply this FEX. Please see this thread:<br>
www.mathworks.com/matlabcentral/newsreader/view_thread/324503<br>
<br>
There, Roger also has derived a special formula for uniform random sample in a ndimensional simplex.<br>
<br>
Bruno

Thu, 28 Mar 2013 20:08:06 +0000
Re: Simulation of 5 Random Variables with Sum Constraint
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875#901091
Siddhartha
> When adding the bound constraints, there is some work to be done before apply this FEX. Please see this thread:<br>
> www.mathworks.com/matlabcentral/newsreader/view_thread/324503<br>
> <br>
> There, Roger also has derived a special formula for uniform random sample in a ndimensional simplex.<br>
<br>
Thanks for the info Bruno, but the simplex generation seems to strike me as a little odd, especially as seeing that there is no fixed sum constraint.<br>
<br>
<br>
Let me redefine the problem better.<br>
<br>
a1 = [10 20 35];<br>
a2 = [13 19 35];<br>
a3 = [12 22 35];<br>
a4 = [15 20 35];<br>
a5 = [11 19 35];<br>
<br>
%Above are detailed the discrete possibilities for each of the 5 variables.<br>
%There is a 30% chance of low and high, and a 40% chance of base.<br>
<br>
Now, these random variables are basically percentages. So their sum can never exceed 100.<br>
But from some data we know that they can actually never exceed 98. <br>
<br>
I think, maybe, one can use randfixedsum for each value from min(a1) + min(a2) + min(a3) + min(a4) + min(a5) until 98, in a for loop, but again  that seems inefficient and slow, and I'm having difficulty applying it to the discrete distribution above.

Thu, 28 Mar 2013 21:34:18 +0000
Re: Simulation of 5 Random Variables with Sum Constraint
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875#901096
Bruno Luong
"Siddhartha " <siddhsam@hotmail.com> wrote in message <br>
> <br>
> Thanks for the info Bruno, but the simplex generation seems to strike me as a little odd, especially as seeing that there is no fixed sum constraint.<br>
<br>
If it's your impression then you only read superficially the thread in the link I have provided earlier.<br>
<br>
> <br>
> <br>
> Let me redefine the problem better.<br>
> <br>
> a1 = [10 20 35];<br>
> a2 = [13 19 35];<br>
> a3 = [12 22 35];<br>
> a4 = [15 20 35];<br>
> a5 = [11 19 35];<br>
> <br>
> %Above are detailed the discrete possibilities for each of the 5 variables.<br>
> %There is a 30% chance of low and high, and a 40% chance of base.<br>
> <br>
> Now, these random variables are basically percentages. So their sum can never exceed 100.<br>
> But from some data we know that they can actually never exceed 98. <br>
<br>
Steve already told that with 5 variables you can generate the whole set (cardinal <= 243) then randomly pick the one that verify the constraint. <br>
<br>
If you have more variables (e.g., > 10), rejection method may be the way to go.<br>
<br>
> <br>
> I think, maybe, one can use randfixedsum for each value from min(a1) + min(a2) + min(a3) + min(a4) + min(a5) until 98, in a for loop, but again  that seems inefficient and slow, and I'm having difficulty applying it to the discrete distribution above.<br>
<br>
Again Steve told you the discrete and continuous cases are two different problems, involving different maths. Please just pick you choice.<br>
<br>
Bruno

Mon, 01 Apr 2013 23:00:23 +0000
Re: Simulation of 5 Random Variables with Sum Constraint
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875#901323
Siddhartha
You are right Bruno.<br>
<br>
I implemented Steve's idea in the following manner.<br>
<br>
n = 5;<br>
a = [];<br>
% for i = 1:n<br>
% a = [a,{[100/(1.8*n)+(1)*(1)^fix(rand)*rand*(100/(2*n)) 100/(1.4*n)+(1)^fix(rand)*rand*(100/(2*n)) 100/(0.9*n)+(1)^fix(rand)*rand*(100/(2*n))]}];<br>
% end<br>
<br>
a = {[10 17 34], [12 15 25], [8 14, 40], [12 19, 35], [12 19, 32]}; <br>
<br>
b = allcomb(a{:});<br>
<br>
c = sum(b,2);<br>
<br>
d = b(c<98,:);<br>
dsum = sum(d,2);<br>
[dsize, ~] = size(d);<br>
<br>
e(dsize, n) = 0;<br>
<br>
for i = 1:n<br>
e(d(:,i) == a{i}(1),i) = 3;<br>
e(d(:,i) == a{i}(2),i) = 4;<br>
e(d(:,i) == a{i}(3),i) = 3;<br>
end<br>
<br>
f = prod(e,2);<br>
g = cumsum(f);<br>
<br>
h = g/g(end);<br>
<br>
k = rand(10000,1);<br>
<br>
l = arrayfun(@(x)(find(h>x,1)),k);<br>
<br>
<br>
subplot(3,2,1), hist(d(l,1));<br>
subplot(3,2,2), hist(d(l,2));<br>
subplot(3,2,3), hist(d(l,3));<br>
subplot(3,2,4), hist(d(l,4));<br>
subplot(3,2,5), hist(d(l,5));<br>
subplot(3,2,6), hist(dsum(l));<br>
<br>
<br>
<br>
As you can see, in the subplots, the probability distribution of the inputs have changed, which is in accordance with what Roger Stafford wrote on the thread you provided as reference.<br>
<br>
This was possible by using ALLCOMB, but what if the inputs were scaled beta distributions between 10 and 30? How would we implement the solution then?

Wed, 03 Apr 2013 07:05:13 +0000
Re: Simulation of 5 Random Variables with Sum Constraint
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327875#901391
Bruno Luong
"Siddhartha " <siddhsam@hotmail.com> wrote in message <kjd3i7$qls$1@newscl01ah.mathworks.com>...<br>
> <br>
> but what if the inputs were scaled beta distributions between 10 and 30? How would we implement the solution then?<br>
<br>
I can't see direct method for beta distribution, may be the only choice is the rejection method.<br>
<br>
Bruno