# Calculating the conditional probability of discrete samples

I have two lists:

a = {0, 1, 1, 1, 0, 1}
b = {1, 0, 1, 1, 0, 0}


I'd like to calculate the conditional probability of a given b; for example, $p(a=1 | b=0)$.

Is there a built in function to do this? The Probability function seems to only work with symbolic arguments?

-
Where is the randomness ? –  A.G. May 31 '14 at 2:01
a and b are the result of many (in this case 6) random simulations. I guess I am not trying to calculate the true conditional probability, but rather estimate it through many realizations of this simulation. –  kamula May 31 '14 at 2:04
Oh, so you want to select all entries of $a$ corresponding to zero entries for $b$ (here : {1,0,1}) and then evaluate the proportion of 1's (here : 2/3), right ? –  A.G. May 31 '14 at 2:09
Yes, that is correct. It looks like I needed to use EmpiricalDistribution as rasher showed below. –  kamula May 31 '14 at 2:11

ClearAll[a, b, dista, aa, bb]

a = {0, 1, 1, 1, 0, 1}
b = {1, 0, 1, 1, 0, 0}

dista = EmpiricalDistribution[Transpose[{a, b}]]

Probability[aa == 1 \[Conditioned] bb == 0, {aa, bb} \[Distributed] dista]

(* 2/3 *)

-
Awesome. Thank you! –  kamula May 31 '14 at 2:09

Following A.G.'s restating of your problem you could also do this:

Mean @ Pick[a, b, 0]

2/3


Rather more clean, is it not? :-)

-
I think you need something like Count[#, 1]/Length[#] &@Pick[a, b, 0] for Version7:) -- +1 –  kglr May 31 '14 at 11:08
@kguler Mean works in version 7, and I think it is equivalent to you code for binary (zero or one) lists; am I missing something? –  Mr.Wizard May 31 '14 at 11:11
I meant for general (non-binary) lists ... and also for other conditional probabilites (e.g Prob(a==0 given b==0)) for binary lists. –  kglr May 31 '14 at 11:22
@kguler Good point. I would still try to use use numeric methods, e.g. 1 - Mean @ . . .. –  Mr.Wizard May 31 '14 at 18:09
I'm giving you a +1 for being a wise-guy, I'm sure I don't have to tell you that clean it may be, but having to create a distinct numeric method for each possible probability query (much less extending them to non-binary data) would make for a bit of a mess ;-) –  ciao May 31 '14 at 22:53