Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Simulation of 5 Random Variables with Sum Constraint

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Siddhartha

Date: 27 Mar, 2013 20:50:11

Message: 1 of 9

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.


n = 5;
a(n,3) = 0;
for i = 1:n
    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))];
end


d = lhsdesign(n,10000);

e = 0;
b(1,10000)=0;
for i = 1:n
    b(d(i,:)<0.3) = a(i,1);
    b(d(i,:)>0.7) = a(i,3);
    b(b==0) = a(i,2);
    e = e + b;
end

f = e(e<98);
[~,cole] = size(unique(e));
[~,colf] = size(unique(f));

subplot(2,2,1), hist(e,cole);
subplot(2,2,2), hist(e(e<98),colf);
subplot(2,2,3), ecdf(e);
subplot(2,2,4), ecdf(e(e<98));

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Steven_Lord

Date: 27 Mar, 2013 21:22:41

Message: 2 of 9



"Siddhartha " <siddhsam@hotmail.com> wrote in message
news:kivm23$ip1$1@newscl01ah.mathworks.com...
> 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.

So each variable can take on one of three values, and the three values each
variable can take can be different for each variable?

> 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.

If you have such a small number of different possible combinations, generate
the complete list of possible combinations and use RANDI (with replacement)
or RANDPERM (without replacement) to select from the list.

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Siddhartha

Date: 28 Mar, 2013 01:13:16

Message: 3 of 9

Thank you! This is awesome.


But I also foresee that this problem will extend for me to 20 variables.

Also, we may eventually get enough information to model the random numbers as continuous.

Is there a scalable way to do this?

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Steven_Lord

Date: 28 Mar, 2013 14:50:24

Message: 4 of 9



"Siddhartha " <siddhsam@hotmail.com> wrote in message
news:kj05fc$1q1$1@newscl01ah.mathworks.com...
> Thank you! This is awesome.
>
>
> But I also foresee that this problem will extend for me to 20 variables.

I would process the variables in decreasing order of maximum value, and stop
processing when you know that none of the possible combinations of values
for the remaining variables can satisfy your sum constraint. That should
allow you to cut out significant chunks of the combination space.

> Also, we may eventually get enough information to model the random numbers
> as continuous.

That's different; I believe there are some submissions on the File Exchange
for this case. Take a look at RANDFIXEDSUM.

http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Bruno Luong

Date: 28 Mar, 2013 15:04:18

Message: 5 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message .
>
> That's different; I believe there are some submissions on the File Exchange
> for this case. Take a look at RANDFIXEDSUM.
>
> http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum

When adding the bound constraints, there is some work to be done before apply this FEX. Please see this thread:
www.mathworks.com/matlabcentral/newsreader/view_thread/324503

There, Roger also has derived a special formula for uniform random sample in a n-dimensional simplex.

Bruno

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Siddhartha

Date: 28 Mar, 2013 20:08:06

Message: 6 of 9

> When adding the bound constraints, there is some work to be done before apply this FEX. Please see this thread:
> www.mathworks.com/matlabcentral/newsreader/view_thread/324503
>
> There, Roger also has derived a special formula for uniform random sample in a n-dimensional simplex.

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.


Let me redefine the problem better.

a1 = [10 20 35];
a2 = [13 19 35];
a3 = [12 22 35];
a4 = [15 20 35];
a5 = [11 19 35];

%Above are detailed the discrete possibilities for each of the 5 variables.
%There is a 30% chance of low and high, and a 40% chance of base.

Now, these random variables are basically percentages. So their sum can never exceed 100.
But from some data we know that they can actually never exceed 98.

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.

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Bruno Luong

Date: 28 Mar, 2013 21:34:18

Message: 7 of 9

"Siddhartha " <siddhsam@hotmail.com> wrote in message
>
> 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.

If it's your impression then you only read superficially the thread in the link I have provided earlier.

>
>
> Let me redefine the problem better.
>
> a1 = [10 20 35];
> a2 = [13 19 35];
> a3 = [12 22 35];
> a4 = [15 20 35];
> a5 = [11 19 35];
>
> %Above are detailed the discrete possibilities for each of the 5 variables.
> %There is a 30% chance of low and high, and a 40% chance of base.
>
> Now, these random variables are basically percentages. So their sum can never exceed 100.
> But from some data we know that they can actually never exceed 98.

Steve already told that with 5 variables you can generate the whole set (cardinal <= 243) then randomly pick the one that verify the constraint.

If you have more variables (e.g., > 10), rejection method may be the way to go.

>
> 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.

Again Steve told you the discrete and continuous cases are two different problems, involving different maths. Please just pick you choice.

Bruno

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Siddhartha

Date: 1 Apr, 2013 23:00:23

Message: 8 of 9

You are right Bruno.

I implemented Steve's idea in the following manner.

n = 5;
a = [];
% for i = 1:n
% 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))]}];
% end

a = {[10 17 34], [12 15 25], [8 14, 40], [12 19, 35], [12 19, 32]};

b = allcomb(a{:});

c = sum(b,2);

d = b(c<98,:);
dsum = sum(d,2);
[dsize, ~] = size(d);

e(dsize, n) = 0;

for i = 1:n
    e(d(:,i) == a{i}(1),i) = 3;
    e(d(:,i) == a{i}(2),i) = 4;
    e(d(:,i) == a{i}(3),i) = 3;
end

f = prod(e,2);
g = cumsum(f);

h = g/g(end);

k = rand(10000,1);

l = arrayfun(@(x)(find(h>x,1)),k);


subplot(3,2,1), hist(d(l,1));
subplot(3,2,2), hist(d(l,2));
subplot(3,2,3), hist(d(l,3));
subplot(3,2,4), hist(d(l,4));
subplot(3,2,5), hist(d(l,5));
subplot(3,2,6), hist(dsum(l));



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.

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?

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Bruno Luong

Date: 3 Apr, 2013 07:05:13

Message: 9 of 9

"Siddhartha " <siddhsam@hotmail.com> wrote in message <kjd3i7$qls$1@newscl01ah.mathworks.com>...
>
> but what if the inputs were scaled beta distributions between 10 and 30? How would we implement the solution then?

I can't see direct method for beta distribution, may be the only choice is the rejection method.

Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us