Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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
1  
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

2 Answers 2

up vote 5 down vote accepted
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 *)
share|improve this answer
    
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? :-)

share|improve this answer
    
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
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.