# Why is the conditional probability not working for CategoricalDistribution?

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?

• Did you try using NProbability[] instead? Commented Feb 18, 2021 at 11:21
• @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? Commented Feb 18, 2021 at 17:30
• That definitely sounds reportable to Support. Commented Feb 18, 2021 at 17:31
• @J.M. have you updated to 12.3? Can you check if it is working in 12.3? Commented Jun 27, 2021 at 6:07
• I can access 12.3 in the cloud, and it seems both Probability[] and NProbability[] can handle it now. Commented Jun 27, 2021 at 6:13

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 *)

• 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? Commented Jun 27, 2021 at 6:09
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]]


condProbResultGivenWeather["Fair", "Win"]

0.6

condProbResultGivenWeather[All, "Win"]


condProbResultGivenWeather["Fair"]


condProbWeatherGivenResult["Win", "Fair"]

0.230769

condProbWeatherGivenResult[All, "Fair"]


condProbWeatherGivenResult["Win"]


### 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"]


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