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:
Addition Rule, Union, AND for probability calculations

Subject: Addition Rule, Union, AND for probability calculations

From: Andrew O'Connor

Date: 7 Aug, 2012 03:20:35

Message: 1 of 5

Hello all,

I'm calculating the union of independent probability values, but wanted to know if there was a built in function to assist with this. The rule I'm calculating is:

Pr(AUB) = Pr(A) + P(B) - P(A)P(B)
For 4 terms it gets a little bigger:
Pr(AUBUCUD) = Pr(A) + P(B) P(C) + P(D)
                       - P(A)P(B) - P(A)P(C) - P(A)P(D) - P(B)P(C) - P(B)P(D) - P(C)P(D)
                       + P(A)P(B)P(C) + P(A)P(B)P(D) + P(A)P(C)P(D) + P(B)P(C)P(D)
                       - P(A)P(B)P(C)P(D)

If not I'll build one and post into file exchange, but I'd be surprised if something fundamental like this isn't included somewhere..

Thanks

Andrew

Subject: Addition Rule, Union, AND for probability calculations

From: R

Date: 7 Aug, 2012 06:15:18

Message: 2 of 5

"Andrew O'Connor" <AndrewNOConnor@gmail.com> wrote in message <jvq1i3$rau$1@newscl01ah.mathworks.com>...
> Hello all,
>
> I'm calculating the union of independent probability values, but wanted to know if there was a built in function to assist with this. The rule I'm calculating is:
>
> Pr(AUB) = Pr(A) + P(B) - P(A)P(B)
> For 4 terms it gets a little bigger:
> Pr(AUBUCUD) = Pr(A) + P(B) P(C) + P(D)
> - P(A)P(B) - P(A)P(C) - P(A)P(D) - P(B)P(C) - P(B)P(D) - P(C)P(D)
> + P(A)P(B)P(C) + P(A)P(B)P(D) + P(A)P(C)P(D) + P(B)P(C)P(D)
> - P(A)P(B)P(C)P(D)
>
> If not I'll build one and post into file exchange, but I'd be surprised if something fundamental like this isn't included somewhere..
>
> Thanks
>
> Andrew

function prob = probUnion(probvec)
  if (length(probvec) == 1)
    prob = probvec;
  else
    a = probvec(1);
    b = probUnion(probvec(2:end));
    prob = a + b - a*b;
  end
end

syms a b c d
probUnion([a b c d])

Subject: Addition Rule, Union, AND for probability calculations

From: Bruno Luong

Date: 7 Aug, 2012 07:23:16

Message: 3 of 5

P = rand(1,4); % Vector Pr(A)... Pr(D)
PUnion = sum(arrayfun(@(k) (-1)^(k-1)*sum(prod(nchoosek(P,k),2)),1:length(P)))

% Bruno

Subject: Addition Rule, Union, AND for probability calculations

From: Bruno Luong

Date: 7 Aug, 2012 08:42:14

Message: 4 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jvqfp3$fi9$1@newscl01ah.mathworks.com>...
> P = rand(1,4); % Vector Pr(A)... Pr(D)
> PUnion = sum(arrayfun(@(k) (-1)^(k-1)*sum(prod(nchoosek(P,k),2)),1:length(P)))
>
> % Bruno

Doh

There is a much simpler formula, since the complement of A, ..., D are independent as well, and
union(A,B,C,D) = Complement(Intersection(Ac,Bc,Cc,Dc) (Ac is complement of A, etc)

So
PUnion = 1-prod(1-P)

% Bruno

Subject: Addition Rule, Union, AND for probability calculations

From: Andrew O'Connor

Date: 7 Aug, 2012 10:56:16

Message: 5 of 5

R, Bruno, great efforts. Your posts look like lean mean answers to a cody question.

My effort was:

function aans = sumind(daValues)
%Create combinations
n = length(daValues);
aans = 0;
%Loop through combinations and add/subtract from ans
for i = 1:n
    combval = combnk(daValues,i);
    combval = prod(combval,2);
    combval = sum(combval);
    aans = -1*aans + combval;
end
%If answer was negative (odd loop) then correct
if aans < 0, aans = aans*-1; end

There was a massive difference in speed between the functions. In fact because mine used combnk it actually couldn't find the combinations for large vectors.

A = rand(1,20);

sumind(A)
Elapsed time is 10.035609 seconds.

probUnion(A)
Elapsed time is 0.004224 seconds. (Slick)

sum(arrayfun(@(k) (-1)^(k-1)*sum(prod(nchoosek(A,k),2)),1:length(A)))
Elapsed time is 33.895413 seconds.

1-prod(1-A)
Elapsed time is 0.003240 seconds. (Very Slick)

Thanks again!

Andrew

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