6
$\begingroup$

Bug introduced in 12.1 and fixed in 12.3


Consider the following simple example for demonstration:

Probability[
    result=="Win"\[Conditioned]weather=="Fair",
    {weather,result}\[Distributed]CategoricalDistribution[
        {{"Rain","Fair"},{"Win","Lose"}},
        {{0.4,0.4},{0.12,0.08}}
    ]
]

Is there anything wrong with my syntax? Is it a bug?

$\endgroup$
5
  • $\begingroup$ Did you try using NProbability[] instead? $\endgroup$ Feb 18, 2021 at 11:21
  • $\begingroup$ @J.M. Yes, NProbability is also not working. I think WRI forgot to implement conditional probability for CategoricalDistribution. Should I add the tag bug on it? $\endgroup$
    – user13892
    Feb 18, 2021 at 17:30
  • $\begingroup$ That definitely sounds reportable to Support. $\endgroup$ Feb 18, 2021 at 17:31
  • $\begingroup$ @J.M. have you updated to 12.3? Can you check if it is working in 12.3? $\endgroup$
    – user13892
    Jun 27, 2021 at 6:07
  • 3
    $\begingroup$ I can access 12.3 in the cloud, and it seems both Probability[] and NProbability[] can handle it now. $\endgroup$ Jun 27, 2021 at 6:13

2 Answers 2

8
$\begingroup$

I think your syntax is just fine. Probability just doesn't know how to do it.

The brute force way, of course, is the following:

cd = CategoricalDistribution[{{"Rain", "Fair"}, {"Win", "Lose"}}, {{0.4, 0.4}, {0.12, 0.08}}];
Probability[result == "Win" && weather == "Fair", {weather, result} \[Distributed] cd]/
  Probability[weather == "Fair", {weather, result} \[Distributed] cd]
(* 0.6 *)
$\endgroup$
1
  • $\begingroup$ Have you updated to 12.3? Can you check if it is working in 12.3? Have they added the definitions of conditional probability in Probability and NProbability? $\endgroup$
    – user13892
    Jun 27, 2021 at 6:09
7
$\begingroup$
cd = CategoricalDistribution[{{"Rain", "Fair"}, {"Win", "Lose"}}, 
  {{0.4, 0.4}, {0.12, 0.08}}];

Update: Again using simple queries we can get two tables of conditional probabilities as Dataset objects

probtable = Information[cd, "ProbabilityTable"];

condProbResultGivenWeather = probtable[All, Normalize[#, Total] &];

condProbWeatherGivenResult = probtable[Transpose][All, Normalize[#, Total] &];

Row[{condProbResultGivenWeather, condProbWeatherGivenResult}, Spacer[10]] 

enter image description here

condProbResultGivenWeather["Fair", "Win"] 
0.6
condProbResultGivenWeather[All, "Win"] 

enter image description here

condProbResultGivenWeather["Fair"] 

enter image description here

condProbWeatherGivenResult["Win", "Fair"]
0.230769   
condProbWeatherGivenResult[All, "Fair"]

enter image description here

condProbWeatherGivenResult["Win"]

enter image description here

Information + "ProbabilityTable"

Using Information we get the joint probability table as a Dataset object. We then can get the conditional probabilities using simple queries:

probtable = Information[cd, "ProbabilityTable"]

enter image description here

Conditional probability of "Win" given "Fair":

probtable["Fair", #Win / Total @ # &]
0.6 

Conditional probability of "Fair" given "Win":

probtable[#Fair / Total @ # &, "Win"]
0.230769  

PDF + MarginalDistribution

We can also use the functions PDF and MarginalDistribution to get the same result:

PDF[cd, {"Fair", "Win"}] / PDF[MarginalDistribution[cd, 1], "Fair"] 
0.6
 PDF[cd, {"Fair", "Win"}] / PDF[MarginalDistribution[cd, 2], "Win"]     
0.230769
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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